Eigen  3.3.3
ReturnByValue.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 // Copyright (C) 2009-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
00006 //
00007 // This Source Code Form is subject to the terms of the Mozilla
00008 // Public License v. 2.0. If a copy of the MPL was not distributed
00009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00010 
00011 #ifndef EIGEN_RETURNBYVALUE_H
00012 #define EIGEN_RETURNBYVALUE_H
00013 
00014 namespace Eigen {
00015 
00016 namespace internal {
00017 
00018 template<typename Derived>
00019 struct traits<ReturnByValue<Derived> >
00020   : public traits<typename traits<Derived>::ReturnType>
00021 {
00022   enum {
00023     // We're disabling the DirectAccess because e.g. the constructor of
00024     // the Block-with-DirectAccess expression requires to have a coeffRef method.
00025     // Also, we don't want to have to implement the stride stuff.
00026     Flags = (traits<typename traits<Derived>::ReturnType>::Flags
00027              | EvalBeforeNestingBit) & ~DirectAccessBit
00028   };
00029 };
00030 
00031 /* The ReturnByValue object doesn't even have a coeff() method.
00032  * So the only way that nesting it in an expression can work, is by evaluating it into a plain matrix.
00033  * So internal::nested always gives the plain return matrix type.
00034  *
00035  * FIXME: I don't understand why we need this specialization: isn't this taken care of by the EvalBeforeNestingBit ??
00036  * Answer: EvalBeforeNestingBit should be deprecated since we have the evaluators
00037  */
00038 template<typename Derived,int n,typename PlainObject>
00039 struct nested_eval<ReturnByValue<Derived>, n, PlainObject>
00040 {
00041   typedef typename traits<Derived>::ReturnType type;
00042 };
00043 
00044 } // end namespace internal
00045 
00050 template<typename Derived> class ReturnByValue
00051   : public internal::dense_xpr_base< ReturnByValue<Derived> >::type, internal::no_assignment_operator
00052 {
00053   public:
00054     typedef typename internal::traits<Derived>::ReturnType ReturnType;
00055 
00056     typedef typename internal::dense_xpr_base<ReturnByValue>::type Base;
00057     EIGEN_DENSE_PUBLIC_INTERFACE(ReturnByValue)
00058 
00059     template<typename Dest>
00060     EIGEN_DEVICE_FUNC
00061     inline void evalTo(Dest& dst) const
00062     { static_cast<const Derived*>(this)->evalTo(dst); }
00063     EIGEN_DEVICE_FUNC inline Index rows() const { return static_cast<const Derived*>(this)->rows(); }
00064     EIGEN_DEVICE_FUNC inline Index cols() const { return static_cast<const Derived*>(this)->cols(); }
00065 
00066 #ifndef EIGEN_PARSED_BY_DOXYGEN
00067 #define Unusable YOU_ARE_TRYING_TO_ACCESS_A_SINGLE_COEFFICIENT_IN_A_SPECIAL_EXPRESSION_WHERE_THAT_IS_NOT_ALLOWED_BECAUSE_THAT_WOULD_BE_INEFFICIENT
00068     class Unusable{
00069       Unusable(const Unusable&) {}
00070       Unusable& operator=(const Unusable&) {return *this;}
00071     };
00072     const Unusable& coeff(Index) const { return *reinterpret_cast<const Unusable*>(this); }
00073     const Unusable& coeff(Index,Index) const { return *reinterpret_cast<const Unusable*>(this); }
00074     Unusable& coeffRef(Index) { return *reinterpret_cast<Unusable*>(this); }
00075     Unusable& coeffRef(Index,Index) { return *reinterpret_cast<Unusable*>(this); }
00076 #undef Unusable
00077 #endif
00078 };
00079 
00080 template<typename Derived>
00081 template<typename OtherDerived>
00082 Derived& DenseBase<Derived>::operator=(const ReturnByValue<OtherDerived>& other)
00083 {
00084   other.evalTo(derived());
00085   return derived();
00086 }
00087 
00088 namespace internal {
00089 
00090 // Expression is evaluated in a temporary; default implementation of Assignment is bypassed so that
00091 // when a ReturnByValue expression is assigned, the evaluator is not constructed.
00092 // TODO: Finalize port to new regime; ReturnByValue should not exist in the expression world
00093   
00094 template<typename Derived>
00095 struct evaluator<ReturnByValue<Derived> >
00096   : public evaluator<typename internal::traits<Derived>::ReturnType>
00097 {
00098   typedef ReturnByValue<Derived> XprType;
00099   typedef typename internal::traits<Derived>::ReturnType PlainObject;
00100   typedef evaluator<PlainObject> Base;
00101   
00102   EIGEN_DEVICE_FUNC explicit evaluator(const XprType& xpr)
00103     : m_result(xpr.rows(), xpr.cols())
00104   {
00105     ::new (static_cast<Base*>(this)) Base(m_result);
00106     xpr.evalTo(m_result);
00107   }
00108 
00109 protected:
00110   PlainObject m_result;
00111 };
00112 
00113 } // end namespace internal
00114 
00115 } // end namespace Eigen
00116 
00117 #endif // EIGEN_RETURNBYVALUE_H
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends