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