$darkmode
Eigen  5.0.1-dev
ReturnByValue.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2009-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_RETURNBYVALUE_H
12 #define EIGEN_RETURNBYVALUE_H
13 
14 // IWYU pragma: private
15 #include "./InternalHeaderCheck.h"
16 
17 namespace Eigen {
18 
19 namespace internal {
20 
21 template <typename Derived>
22 struct traits<ReturnByValue<Derived> > : public traits<typename traits<Derived>::ReturnType> {
23  enum {
24  // We're disabling the DirectAccess because e.g. the constructor of
25  // the Block-with-DirectAccess expression requires to have a coeffRef method.
26  // Also, we don't want to have to implement the stride stuff.
27  Flags = (traits<typename traits<Derived>::ReturnType>::Flags | EvalBeforeNestingBit) & ~DirectAccessBit
28  };
29 };
30 
31 /* The ReturnByValue object doesn't even have a coeff() method.
32  * So the only way that nesting it in an expression can work, is by evaluating it into a plain matrix.
33  * So internal::nested always gives the plain return matrix type.
34  *
35  * FIXME: I don't understand why we need this specialization: isn't this taken care of by the EvalBeforeNestingBit ??
36  * Answer: EvalBeforeNestingBit should be deprecated since we have the evaluators
37  */
38 template <typename Derived, int n, typename PlainObject>
39 struct nested_eval<ReturnByValue<Derived>, n, PlainObject> {
40  typedef typename traits<Derived>::ReturnType type;
41 };
42 
43 } // end namespace internal
44 
49 template <typename Derived>
50 class ReturnByValue : public internal::dense_xpr_base<ReturnByValue<Derived> >::type, internal::no_assignment_operator {
51  public:
52  typedef typename internal::traits<Derived>::ReturnType ReturnType;
53 
54  typedef typename internal::dense_xpr_base<ReturnByValue>::type Base;
55  EIGEN_DENSE_PUBLIC_INTERFACE(ReturnByValue)
56 
57  template <typename Dest>
58  EIGEN_DEVICE_FUNC inline void evalTo(Dest& dst) const {
59  static_cast<const Derived*>(this)->evalTo(dst);
60  }
61  EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return static_cast<const Derived*>(this)->rows(); }
62  EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return static_cast<const Derived*>(this)->cols(); }
63 
64 #ifndef EIGEN_PARSED_BY_DOXYGEN
65 #define Unusable \
66  YOU_ARE_TRYING_TO_ACCESS_A_SINGLE_COEFFICIENT_IN_A_SPECIAL_EXPRESSION_WHERE_THAT_IS_NOT_ALLOWED_BECAUSE_THAT_WOULD_BE_INEFFICIENT
67  class Unusable {
68  Unusable(const Unusable&) {}
69  Unusable& operator=(const Unusable&) { return *this; }
70  };
71  const Unusable& coeff(Index) const { return *reinterpret_cast<const Unusable*>(this); }
72  const Unusable& coeff(Index, Index) const { return *reinterpret_cast<const Unusable*>(this); }
73  Unusable& coeffRef(Index) { return *reinterpret_cast<Unusable*>(this); }
74  Unusable& coeffRef(Index, Index) { return *reinterpret_cast<Unusable*>(this); }
75 #undef Unusable
76 #endif
77 };
78 
79 template <typename Derived>
80 template <typename OtherDerived>
81 EIGEN_DEVICE_FUNC Derived& DenseBase<Derived>::operator=(const ReturnByValue<OtherDerived>& other) {
82  other.evalTo(derived());
83  return derived();
84 }
85 
86 namespace internal {
87 
88 // Expression is evaluated in a temporary; default implementation of Assignment is bypassed so that
89 // when a ReturnByValue expression is assigned, the evaluator is not constructed.
90 // TODO: Finalize port to new regime; ReturnByValue should not exist in the expression world
91 
92 template <typename Derived>
93 struct evaluator<ReturnByValue<Derived> > : public evaluator<typename internal::traits<Derived>::ReturnType> {
94  typedef ReturnByValue<Derived> XprType;
95  typedef typename internal::traits<Derived>::ReturnType PlainObject;
96  typedef evaluator<PlainObject> Base;
97 
98  EIGEN_DEVICE_FUNC explicit evaluator(const XprType& xpr) : m_result(xpr.rows(), xpr.cols()) {
99  internal::construct_at<Base>(this, m_result);
100  xpr.evalTo(m_result);
101  }
102 
103  protected:
104  PlainObject m_result;
105 };
106 
107 } // end namespace internal
108 
109 } // end namespace Eigen
110 
111 #endif // EIGEN_RETURNBYVALUE_H
const unsigned int DirectAccessBit
Definition: Constants.h:159
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
Derived & operator=(const DenseBase< OtherDerived > &other)
Definition: Assign.h:39
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const unsigned int EvalBeforeNestingBit
Definition: Constants.h:74