ReturnByValue.h
Go to the documentation of this file.
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 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
20 template<typename Derived>
21 struct traits<ReturnByValue<Derived> >
22  : public traits<typename traits<Derived>::ReturnType>
23 {
24  enum {
25  // We're disabling the DirectAccess because e.g. the constructor of
26  // the Block-with-DirectAccess expression requires to have a coeffRef method.
27  // Also, we don't want to have to implement the stride stuff.
28  Flags = (traits<typename traits<Derived>::ReturnType>::Flags
30  };
31 };
32 
33 /* The ReturnByValue object doesn't even have a coeff() method.
34  * So the only way that nesting it in an expression can work, is by evaluating it into a plain matrix.
35  * So internal::nested always gives the plain return matrix type.
36  *
37  * FIXME: I don't understand why we need this specialization: isn't this taken care of by the EvalBeforeNestingBit ??
38  * Answer: EvalBeforeNestingBit should be deprecated since we have the evaluators
39  */
40 template<typename Derived,int n,typename PlainObject>
41 struct nested_eval<ReturnByValue<Derived>, n, PlainObject>
42 {
43  typedef typename traits<Derived>::ReturnType type;
44 };
45 
46 } // end namespace internal
47 
52 template<typename Derived> class ReturnByValue
53  : public internal::dense_xpr_base< ReturnByValue<Derived> >::type, internal::no_assignment_operator
54 {
55  public:
56  typedef typename internal::traits<Derived>::ReturnType ReturnType;
57 
58  typedef typename internal::dense_xpr_base<ReturnByValue>::type Base;
60 
61  template<typename Dest>
63  inline void evalTo(Dest& dst) const
64  { static_cast<const Derived*>(this)->evalTo(dst); }
66  inline Index rows() const EIGEN_NOEXCEPT { return static_cast<const Derived*>(this)->rows(); }
68  inline Index cols() const EIGEN_NOEXCEPT { return static_cast<const Derived*>(this)->cols(); }
69 
70 #ifndef EIGEN_PARSED_BY_DOXYGEN
71 #define Unusable YOU_ARE_TRYING_TO_ACCESS_A_SINGLE_COEFFICIENT_IN_A_SPECIAL_EXPRESSION_WHERE_THAT_IS_NOT_ALLOWED_BECAUSE_THAT_WOULD_BE_INEFFICIENT
72  class Unusable{
73  Unusable(const Unusable&) {}
74  Unusable& operator=(const Unusable&) {return *this;}
75  };
76  const Unusable& coeff(Index) const { return *reinterpret_cast<const Unusable*>(this); }
77  const Unusable& coeff(Index,Index) const { return *reinterpret_cast<const Unusable*>(this); }
78  Unusable& coeffRef(Index) { return *reinterpret_cast<Unusable*>(this); }
79  Unusable& coeffRef(Index,Index) { return *reinterpret_cast<Unusable*>(this); }
80 #undef Unusable
81 #endif
82 };
83 
84 template<typename Derived>
85 template<typename OtherDerived>
87 {
88  other.evalTo(derived());
89  return derived();
90 }
91 
92 namespace internal {
93 
94 // Expression is evaluated in a temporary; default implementation of Assignment is bypassed so that
95 // when a ReturnByValue expression is assigned, the evaluator is not constructed.
96 // TODO: Finalize port to new regime; ReturnByValue should not exist in the expression world
97 
98 template<typename Derived>
99 struct evaluator<ReturnByValue<Derived> >
100  : public evaluator<typename internal::traits<Derived>::ReturnType>
101 {
102  typedef ReturnByValue<Derived> XprType;
103  typedef typename internal::traits<Derived>::ReturnType PlainObject;
104  typedef evaluator<PlainObject> Base;
105 
106  EIGEN_DEVICE_FUNC explicit evaluator(const XprType& xpr)
107  : m_result(xpr.rows(), xpr.cols())
108  {
109  internal::construct_at<Base>(this, m_result);
110  xpr.evalTo(m_result);
111  }
112 
113 protected:
114  PlainObject m_result;
115 };
116 
117 } // end namespace internal
118 
119 } // end namespace Eigen
120 
121 #endif // EIGEN_RETURNBYVALUE_H
int n
#define EIGEN_NOEXCEPT
Definition: Macros.h:1260
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1168
Derived & operator=(const DenseBase< OtherDerived > &other)
Definition: Assign.h:41
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: ReturnByValue.h:66
internal::dense_xpr_base< ReturnByValue >::type Base
Definition: ReturnByValue.h:58
internal::traits< Derived >::ReturnType ReturnType
Definition: ReturnByValue.h:56
void evalTo(Dest &dst) const
Definition: ReturnByValue.h:63
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: ReturnByValue.h:68
const unsigned int EvalBeforeNestingBit
Definition: Constants.h:72
const unsigned int DirectAccessBit
Definition: Constants.h:157
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82