Eigen  3.4.90 (git rev a4098ac676528a83cfb73d4d26ce1b42ec05f47c)
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#include "./InternalHeaderCheck.h"
15
16namespace Eigen {
17
18namespace internal {
19
20template<typename Derived>
21struct 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 */
40template<typename Derived,int n,typename PlainObject>
41struct nested_eval<ReturnByValue<Derived>, n, PlainObject>
42{
43 typedef typename traits<Derived>::ReturnType type;
44};
45
46} // end namespace internal
47
52template<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;
59 EIGEN_DENSE_PUBLIC_INTERFACE(ReturnByValue)
60
61 template<typename Dest>
62 EIGEN_DEVICE_FUNC
63 inline void evalTo(Dest& dst) const
64 { static_cast<const Derived*>(this)->evalTo(dst); }
65 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
66 inline Index rows() const EIGEN_NOEXCEPT { return static_cast<const Derived*>(this)->rows(); }
67 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
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
84template<typename Derived>
85template<typename OtherDerived>
86EIGEN_DEVICE_FUNC Derived& DenseBase<Derived>::operator=(const ReturnByValue<OtherDerived>& other)
87{
88 other.evalTo(derived());
89 return derived();
90}
91
92namespace 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
98template<typename Derived>
99struct 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 ::new (static_cast<Base*>(this)) Base(m_result);
110 xpr.evalTo(m_result);
111 }
112
113protected:
114 PlainObject m_result;
115};
116
117} // end namespace internal
118
119} // end namespace Eigen
120
121#endif // EIGEN_RETURNBYVALUE_H
Derived & operator=(const DenseBase< OtherDerived > &other)
Definition: Assign.h:41
const unsigned int EvalBeforeNestingBit
Definition: Constants.h:72
const unsigned int DirectAccessBit
Definition: Constants.h:157
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59