Eigen  3.3.3
MapBase.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2007-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
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_MAPBASE_H
00012 #define EIGEN_MAPBASE_H
00013 
00014 #define EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived) \
00015       EIGEN_STATIC_ASSERT((int(internal::evaluator<Derived>::Flags) & LinearAccessBit) || Derived::IsVectorAtCompileTime, \
00016                           YOU_ARE_TRYING_TO_USE_AN_INDEX_BASED_ACCESSOR_ON_AN_EXPRESSION_THAT_DOES_NOT_SUPPORT_THAT)
00017 
00018 namespace Eigen { 
00019 
00037 template<typename Derived> class MapBase<Derived, ReadOnlyAccessors>
00038   : public internal::dense_xpr_base<Derived>::type
00039 {
00040   public:
00041 
00042     typedef typename internal::dense_xpr_base<Derived>::type Base;
00043     enum {
00044       RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
00045       ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
00046       SizeAtCompileTime = Base::SizeAtCompileTime
00047     };
00048 
00049     typedef typename internal::traits<Derived>::StorageKind StorageKind;
00050     typedef typename internal::traits<Derived>::Scalar Scalar;
00051     typedef typename internal::packet_traits<Scalar>::type PacketScalar;
00052     typedef typename NumTraits<Scalar>::Real RealScalar;
00053     typedef typename internal::conditional<
00054                          bool(internal::is_lvalue<Derived>::value),
00055                          Scalar *,
00056                          const Scalar *>::type
00057                      PointerType;
00058 
00059     using Base::derived;
00060 //    using Base::RowsAtCompileTime;
00061 //    using Base::ColsAtCompileTime;
00062 //    using Base::SizeAtCompileTime;
00063     using Base::MaxRowsAtCompileTime;
00064     using Base::MaxColsAtCompileTime;
00065     using Base::MaxSizeAtCompileTime;
00066     using Base::IsVectorAtCompileTime;
00067     using Base::Flags;
00068     using Base::IsRowMajor;
00069 
00070     using Base::rows;
00071     using Base::cols;
00072     using Base::size;
00073     using Base::coeff;
00074     using Base::coeffRef;
00075     using Base::lazyAssign;
00076     using Base::eval;
00077 
00078     using Base::innerStride;
00079     using Base::outerStride;
00080     using Base::rowStride;
00081     using Base::colStride;
00082 
00083     // bug 217 - compile error on ICC 11.1
00084     using Base::operator=;
00085 
00086     typedef typename Base::CoeffReturnType CoeffReturnType;
00087 
00089     EIGEN_DEVICE_FUNC inline Index rows() const { return m_rows.value(); }
00091     EIGEN_DEVICE_FUNC inline Index cols() const { return m_cols.value(); }
00092 
00099     EIGEN_DEVICE_FUNC inline const Scalar* data() const { return m_data; }
00100 
00102     EIGEN_DEVICE_FUNC
00103     inline const Scalar& coeff(Index rowId, Index colId) const
00104     {
00105       return m_data[colId * colStride() + rowId * rowStride()];
00106     }
00107 
00109     EIGEN_DEVICE_FUNC
00110     inline const Scalar& coeff(Index index) const
00111     {
00112       EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
00113       return m_data[index * innerStride()];
00114     }
00115 
00117     EIGEN_DEVICE_FUNC
00118     inline const Scalar& coeffRef(Index rowId, Index colId) const
00119     {
00120       return this->m_data[colId * colStride() + rowId * rowStride()];
00121     }
00122 
00124     EIGEN_DEVICE_FUNC
00125     inline const Scalar& coeffRef(Index index) const
00126     {
00127       EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
00128       return this->m_data[index * innerStride()];
00129     }
00130 
00132     template<int LoadMode>
00133     inline PacketScalar packet(Index rowId, Index colId) const
00134     {
00135       return internal::ploadt<PacketScalar, LoadMode>
00136                (m_data + (colId * colStride() + rowId * rowStride()));
00137     }
00138 
00140     template<int LoadMode>
00141     inline PacketScalar packet(Index index) const
00142     {
00143       EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
00144       return internal::ploadt<PacketScalar, LoadMode>(m_data + index * innerStride());
00145     }
00146 
00148     EIGEN_DEVICE_FUNC
00149     explicit inline MapBase(PointerType dataPtr) : m_data(dataPtr), m_rows(RowsAtCompileTime), m_cols(ColsAtCompileTime)
00150     {
00151       EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
00152       checkSanity<Derived>();
00153     }
00154 
00156     EIGEN_DEVICE_FUNC
00157     inline MapBase(PointerType dataPtr, Index vecSize)
00158             : m_data(dataPtr),
00159               m_rows(RowsAtCompileTime == Dynamic ? vecSize : Index(RowsAtCompileTime)),
00160               m_cols(ColsAtCompileTime == Dynamic ? vecSize : Index(ColsAtCompileTime))
00161     {
00162       EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00163       eigen_assert(vecSize >= 0);
00164       eigen_assert(dataPtr == 0 || SizeAtCompileTime == Dynamic || SizeAtCompileTime == vecSize);
00165       checkSanity<Derived>();
00166     }
00167 
00169     EIGEN_DEVICE_FUNC
00170     inline MapBase(PointerType dataPtr, Index rows, Index cols)
00171             : m_data(dataPtr), m_rows(rows), m_cols(cols)
00172     {
00173       eigen_assert( (dataPtr == 0)
00174               || (   rows >= 0 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
00175                   && cols >= 0 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols)));
00176       checkSanity<Derived>();
00177     }
00178 
00179     #ifdef EIGEN_MAPBASE_PLUGIN
00180     #include EIGEN_MAPBASE_PLUGIN
00181     #endif
00182 
00183   protected:
00184 
00185     template<typename T>
00186     EIGEN_DEVICE_FUNC
00187     void checkSanity(typename internal::enable_if<(internal::traits<T>::Alignment>0),void*>::type = 0) const
00188     {
00189 #if EIGEN_MAX_ALIGN_BYTES>0
00190       eigen_assert((   ((internal::UIntPtr(m_data) % internal::traits<Derived>::Alignment) == 0)
00191                     || (cols() * rows() * innerStride() * sizeof(Scalar)) < internal::traits<Derived>::Alignment ) && "data is not aligned");
00192 #endif
00193     }
00194 
00195     template<typename T>
00196     EIGEN_DEVICE_FUNC
00197     void checkSanity(typename internal::enable_if<internal::traits<T>::Alignment==0,void*>::type = 0) const
00198     {}
00199 
00200     PointerType m_data;
00201     const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
00202     const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
00203 };
00204 
00215 template<typename Derived> class MapBase<Derived, WriteAccessors>
00216   : public MapBase<Derived, ReadOnlyAccessors>
00217 {
00218     typedef MapBase<Derived, ReadOnlyAccessors> ReadOnlyMapBase;
00219   public:
00220 
00221     typedef MapBase<Derived, ReadOnlyAccessors> Base;
00222 
00223     typedef typename Base::Scalar Scalar;
00224     typedef typename Base::PacketScalar PacketScalar;
00225     typedef typename Base::StorageIndex StorageIndex;
00226     typedef typename Base::PointerType PointerType;
00227 
00228     using Base::derived;
00229     using Base::rows;
00230     using Base::cols;
00231     using Base::size;
00232     using Base::coeff;
00233     using Base::coeffRef;
00234 
00235     using Base::innerStride;
00236     using Base::outerStride;
00237     using Base::rowStride;
00238     using Base::colStride;
00239 
00240     typedef typename internal::conditional<
00241                     internal::is_lvalue<Derived>::value,
00242                     Scalar,
00243                     const Scalar
00244                   >::type ScalarWithConstIfNotLvalue;
00245 
00246     EIGEN_DEVICE_FUNC
00247     inline const Scalar* data() const { return this->m_data; }
00248     EIGEN_DEVICE_FUNC
00249     inline ScalarWithConstIfNotLvalue* data() { return this->m_data; } // no const-cast here so non-const-correct code will give a compile error
00250 
00251     EIGEN_DEVICE_FUNC
00252     inline ScalarWithConstIfNotLvalue& coeffRef(Index row, Index col)
00253     {
00254       return this->m_data[col * colStride() + row * rowStride()];
00255     }
00256 
00257     EIGEN_DEVICE_FUNC
00258     inline ScalarWithConstIfNotLvalue& coeffRef(Index index)
00259     {
00260       EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
00261       return this->m_data[index * innerStride()];
00262     }
00263 
00264     template<int StoreMode>
00265     inline void writePacket(Index row, Index col, const PacketScalar& val)
00266     {
00267       internal::pstoret<Scalar, PacketScalar, StoreMode>
00268                (this->m_data + (col * colStride() + row * rowStride()), val);
00269     }
00270 
00271     template<int StoreMode>
00272     inline void writePacket(Index index, const PacketScalar& val)
00273     {
00274       EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
00275       internal::pstoret<Scalar, PacketScalar, StoreMode>
00276                 (this->m_data + index * innerStride(), val);
00277     }
00278 
00279     EIGEN_DEVICE_FUNC explicit inline MapBase(PointerType dataPtr) : Base(dataPtr) {}
00280     EIGEN_DEVICE_FUNC inline MapBase(PointerType dataPtr, Index vecSize) : Base(dataPtr, vecSize) {}
00281     EIGEN_DEVICE_FUNC inline MapBase(PointerType dataPtr, Index rows, Index cols) : Base(dataPtr, rows, cols) {}
00282 
00283     EIGEN_DEVICE_FUNC
00284     Derived& operator=(const MapBase& other)
00285     {
00286       ReadOnlyMapBase::Base::operator=(other);
00287       return derived();
00288     }
00289 
00290     // In theory we could simply refer to Base:Base::operator=, but MSVC does not like Base::Base,
00291     // see bugs 821 and 920.
00292     using ReadOnlyMapBase::Base::operator=;
00293 };
00294 
00295 #undef EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS
00296 
00297 } // end namespace Eigen
00298 
00299 #endif // EIGEN_MAPBASE_H
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends