$treeview $search $mathjax
Eigen  3.2.3
$projectbrief
$projectbrief
$searchbox

Ref.h

00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2012 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_REF_H
00011 #define EIGEN_REF_H
00012 
00013 namespace Eigen { 
00014 
00015 template<typename Derived> class RefBase;
00016 template<typename PlainObjectType, int Options = 0,
00017          typename StrideType = typename internal::conditional<PlainObjectType::IsVectorAtCompileTime,InnerStride<1>,OuterStride<> >::type > class Ref;
00018 
00088 namespace internal {
00089 
00090 template<typename _PlainObjectType, int _Options, typename _StrideType>
00091 struct traits<Ref<_PlainObjectType, _Options, _StrideType> >
00092   : public traits<Map<_PlainObjectType, _Options, _StrideType> >
00093 {
00094   typedef _PlainObjectType PlainObjectType;
00095   typedef _StrideType StrideType;
00096   enum {
00097     Options = _Options,
00098     Flags = traits<Map<_PlainObjectType, _Options, _StrideType> >::Flags | NestByRefBit
00099   };
00100 
00101   template<typename Derived> struct match {
00102     enum {
00103       HasDirectAccess = internal::has_direct_access<Derived>::ret,
00104       StorageOrderMatch = PlainObjectType::IsVectorAtCompileTime || Derived::IsVectorAtCompileTime || ((PlainObjectType::Flags&RowMajorBit)==(Derived::Flags&RowMajorBit)),
00105       InnerStrideMatch = int(StrideType::InnerStrideAtCompileTime)==int(Dynamic)
00106                       || int(StrideType::InnerStrideAtCompileTime)==int(Derived::InnerStrideAtCompileTime)
00107                       || (int(StrideType::InnerStrideAtCompileTime)==0 && int(Derived::InnerStrideAtCompileTime)==1),
00108       OuterStrideMatch = Derived::IsVectorAtCompileTime
00109                       || int(StrideType::OuterStrideAtCompileTime)==int(Dynamic) || int(StrideType::OuterStrideAtCompileTime)==int(Derived::OuterStrideAtCompileTime),
00110       AlignmentMatch = (_Options!=Aligned) || ((PlainObjectType::Flags&AlignedBit)==0) || ((traits<Derived>::Flags&AlignedBit)==AlignedBit),
00111       MatchAtCompileTime = HasDirectAccess && StorageOrderMatch && InnerStrideMatch && OuterStrideMatch && AlignmentMatch
00112     };
00113     typedef typename internal::conditional<MatchAtCompileTime,internal::true_type,internal::false_type>::type type;
00114   };
00115   
00116 };
00117 
00118 template<typename Derived>
00119 struct traits<RefBase<Derived> > : public traits<Derived> {};
00120 
00121 }
00122 
00123 template<typename Derived> class RefBase
00124  : public MapBase<Derived>
00125 {
00126   typedef typename internal::traits<Derived>::PlainObjectType PlainObjectType;
00127   typedef typename internal::traits<Derived>::StrideType StrideType;
00128 
00129 public:
00130 
00131   typedef MapBase<Derived> Base;
00132   EIGEN_DENSE_PUBLIC_INTERFACE(RefBase)
00133 
00134   inline Index innerStride() const
00135   {
00136     return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
00137   }
00138 
00139   inline Index outerStride() const
00140   {
00141     return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
00142          : IsVectorAtCompileTime ? this->size()
00143          : int(Flags)&RowMajorBit ? this->cols()
00144          : this->rows();
00145   }
00146 
00147   RefBase()
00148     : Base(0,RowsAtCompileTime==Dynamic?0:RowsAtCompileTime,ColsAtCompileTime==Dynamic?0:ColsAtCompileTime),
00149       // Stride<> does not allow default ctor for Dynamic strides, so let' initialize it with dummy values:
00150       m_stride(StrideType::OuterStrideAtCompileTime==Dynamic?0:StrideType::OuterStrideAtCompileTime,
00151                StrideType::InnerStrideAtCompileTime==Dynamic?0:StrideType::InnerStrideAtCompileTime)
00152   {}
00153   
00154   EIGEN_INHERIT_ASSIGNMENT_OPERATORS(RefBase)
00155 
00156 protected:
00157 
00158   typedef Stride<StrideType::OuterStrideAtCompileTime,StrideType::InnerStrideAtCompileTime> StrideBase;
00159 
00160   template<typename Expression>
00161   void construct(Expression& expr)
00162   {
00163     if(PlainObjectType::RowsAtCompileTime==1)
00164     {
00165       eigen_assert(expr.rows()==1 || expr.cols()==1);
00166       ::new (static_cast<Base*>(this)) Base(expr.data(), 1, expr.size());
00167     }
00168     else if(PlainObjectType::ColsAtCompileTime==1)
00169     {
00170       eigen_assert(expr.rows()==1 || expr.cols()==1);
00171       ::new (static_cast<Base*>(this)) Base(expr.data(), expr.size(), 1);
00172     }
00173     else
00174       ::new (static_cast<Base*>(this)) Base(expr.data(), expr.rows(), expr.cols());
00175     
00176     if(Expression::IsVectorAtCompileTime && (!PlainObjectType::IsVectorAtCompileTime) && ((Expression::Flags&RowMajorBit)!=(PlainObjectType::Flags&RowMajorBit)))
00177       ::new (&m_stride) StrideBase(expr.innerStride(), StrideType::InnerStrideAtCompileTime==0?0:1);
00178     else
00179       ::new (&m_stride) StrideBase(StrideType::OuterStrideAtCompileTime==0?0:expr.outerStride(),
00180                                    StrideType::InnerStrideAtCompileTime==0?0:expr.innerStride());    
00181   }
00182 
00183   StrideBase m_stride;
00184 };
00185 
00186 
00187 template<typename PlainObjectType, int Options, typename StrideType> class Ref
00188   : public RefBase<Ref<PlainObjectType, Options, StrideType> >
00189 {
00190     typedef internal::traits<Ref> Traits;
00191     template<typename Derived>
00192     inline Ref(const PlainObjectBase<Derived>& expr);
00193   public:
00194 
00195     typedef RefBase<Ref> Base;
00196     EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
00197 
00198 
00199     #ifndef EIGEN_PARSED_BY_DOXYGEN
00200     template<typename Derived>
00201     inline Ref(PlainObjectBase<Derived>& expr)
00202     {
00203       EIGEN_STATIC_ASSERT(static_cast<bool>(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
00204       Base::construct(expr.derived());
00205     }
00206     template<typename Derived>
00207     inline Ref(const DenseBase<Derived>& expr)
00208     #else
00209     template<typename Derived>
00210     inline Ref(DenseBase<Derived>& expr)
00211     #endif
00212     {
00213       EIGEN_STATIC_ASSERT(static_cast<bool>(internal::is_lvalue<Derived>::value), THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
00214       EIGEN_STATIC_ASSERT(static_cast<bool>(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
00215       enum { THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY = Derived::ThisConstantIsPrivateInPlainObjectBase};
00216       Base::construct(expr.const_cast_derived());
00217     }
00218 
00219     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Ref)
00220 
00221 };
00222 
00223 // this is the const ref version
00224 template<typename TPlainObjectType, int Options, typename StrideType> class Ref<const TPlainObjectType, Options, StrideType>
00225   : public RefBase<Ref<const TPlainObjectType, Options, StrideType> >
00226 {
00227     typedef internal::traits<Ref> Traits;
00228   public:
00229 
00230     typedef RefBase<Ref> Base;
00231     EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
00232 
00233     template<typename Derived>
00234     inline Ref(const DenseBase<Derived>& expr)
00235     {
00236 //      std::cout << match_helper<Derived>::HasDirectAccess << "," << match_helper<Derived>::OuterStrideMatch << "," << match_helper<Derived>::InnerStrideMatch << "\n";
00237 //      std::cout << int(StrideType::OuterStrideAtCompileTime) << " - " << int(Derived::OuterStrideAtCompileTime) << "\n";
00238 //      std::cout << int(StrideType::InnerStrideAtCompileTime) << " - " << int(Derived::InnerStrideAtCompileTime) << "\n";
00239       construct(expr.derived(), typename Traits::template match<Derived>::type());
00240     }
00241 
00242   protected:
00243 
00244     template<typename Expression>
00245     void construct(const Expression& expr,internal::true_type)
00246     {
00247       Base::construct(expr);
00248     }
00249 
00250     template<typename Expression>
00251     void construct(const Expression& expr, internal::false_type)
00252     {
00253       m_object.lazyAssign(expr);
00254       Base::construct(m_object);
00255     }
00256 
00257   protected:
00258     TPlainObjectType m_object;
00259 };
00260 
00261 } // end namespace Eigen
00262 
00263 #endif // EIGEN_REF_H