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 // Eigen is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 3 of the License, or (at your option) any later version.
11 //
12 // Alternatively, you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of
15 // the License, or (at your option) any later version.
16 //
17 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License and a copy of the GNU General Public License along with
24 // Eigen. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef EIGEN_RETURNBYVALUE_H
27 #define EIGEN_RETURNBYVALUE_H
28 
29 namespace Eigen {
30 
36 namespace internal {
37 
38 template<typename Derived>
39 struct traits<ReturnByValue<Derived> >
40  : public traits<typename traits<Derived>::ReturnType>
41 {
42  enum {
43  // We're disabling the DirectAccess because e.g. the constructor of
44  // the Block-with-DirectAccess expression requires to have a coeffRef method.
45  // Also, we don't want to have to implement the stride stuff.
48  };
49 };
50 
51 /* The ReturnByValue object doesn't even have a coeff() method.
52  * So the only way that nesting it in an expression can work, is by evaluating it into a plain matrix.
53  * So internal::nested always gives the plain return matrix type.
54  *
55  * FIXME: I don't understand why we need this specialization: isn't this taken care of by the EvalBeforeNestingBit ??
56  */
57 template<typename Derived,int n,typename PlainObject>
58 struct nested<ReturnByValue<Derived>, n, PlainObject>
59 {
60  typedef typename traits<Derived>::ReturnType type;
61 };
62 
63 } // end namespace internal
64 
65 template<typename Derived> class ReturnByValue
66  : public internal::dense_xpr_base< ReturnByValue<Derived> >::type
67 {
68  public:
69  typedef typename internal::traits<Derived>::ReturnType ReturnType;
70 
71  typedef typename internal::dense_xpr_base<ReturnByValue>::type Base;
73 
74  template<typename Dest>
75  inline void evalTo(Dest& dst) const
76  { static_cast<const Derived*>(this)->evalTo(dst); }
77  inline Index rows() const { return static_cast<const Derived*>(this)->rows(); }
78  inline Index cols() const { return static_cast<const Derived*>(this)->cols(); }
79 
80 #ifndef EIGEN_PARSED_BY_DOXYGEN
81 #define Unusable YOU_ARE_TRYING_TO_ACCESS_A_SINGLE_COEFFICIENT_IN_A_SPECIAL_EXPRESSION_WHERE_THAT_IS_NOT_ALLOWED_BECAUSE_THAT_WOULD_BE_INEFFICIENT
82  class Unusable{
83  Unusable(const Unusable&) {}
84  Unusable& operator=(const Unusable&) {return *this;}
85  };
86  const Unusable& coeff(Index) const { return *reinterpret_cast<const Unusable*>(this); }
87  const Unusable& coeff(Index,Index) const { return *reinterpret_cast<const Unusable*>(this); }
88  Unusable& coeffRef(Index) { return *reinterpret_cast<Unusable*>(this); }
89  Unusable& coeffRef(Index,Index) { return *reinterpret_cast<Unusable*>(this); }
90 #endif
91 };
92 
93 template<typename Derived>
94 template<typename OtherDerived>
96 {
97  other.evalTo(derived());
98  return derived();
99 }
100 
101 } // end namespace Eigen
102 
103 #endif // EIGEN_RETURNBYVALUE_H