• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

VecBase.h

Go to the documentation of this file.
00001 // GMTL is (C) Copyright 2001-2010 by Allen Bierbaum
00002 // Distributed under the GNU Lesser General Public License 2.1 with an
00003 // addendum covering inlined code. (See accompanying files LICENSE and
00004 // LICENSE.addendum or http://www.gnu.org/copyleft/lesser.txt)
00005 
00006 #ifndef _GMTL_VECBASE_H_
00007 #define _GMTL_VECBASE_H_
00008 
00009 #include <gmtl/Defines.h>
00010 #include <gmtl/Util/Assert.h>
00011 #include <gmtl/Util/StaticAssert.h>
00012 #include <gmtl/Util/Meta.h>
00013 #include <gmtl/Config.h>
00014 #include <gmtl/Helpers.h>
00015 
00016 
00017 namespace gmtl
00018 {
00019 
00020 #ifndef GMTL_NO_METAPROG
00021 namespace meta
00022 {
00023    struct DefaultVecTag
00024    {};
00025 }
00026 #endif
00027 
00028 
00038 #ifndef GMTL_NO_METAPROG
00039 template<class DATA_TYPE, unsigned SIZE, typename REP=meta::DefaultVecTag>
00040 class VecBase
00041 {
00042 protected:
00043    const REP  expRep;      // The expression rep
00044 
00045 public:
00047    typedef DATA_TYPE DataType;
00048 
00050    enum Params { Size = SIZE };
00051 
00052 public:
00053    VecBase()
00054    {;}
00055 
00056    VecBase(const REP& rep)
00057       : expRep(rep)
00058    {;}
00059 
00061    /*
00062    operator VecBase<DATA_TYPE,SIZE,meta::DefaultVecTag>()
00063    {
00064       return VecBase<DATA_TYPE,SIZE,meta::DefaultVecTag>(*this);
00065    }
00066    */
00067 
00069    inline DATA_TYPE operator [](const unsigned i)
00070    {
00071       gmtlASSERT(i < SIZE);
00072       return expRep[i];
00073    }
00074    inline const DATA_TYPE  operator [](const unsigned i) const
00075    {
00076       gmtlASSERT(i < SIZE);
00077       return expRep[i];
00078    }
00079 };
00080 #endif
00081 
00082 
00090 template<class DATA_TYPE, unsigned SIZE>
00091 #ifdef GMTL_NO_METAPROG
00092 class VecBase
00093 #else
00094 class VecBase<DATA_TYPE,SIZE,meta::DefaultVecTag>
00095 #endif
00096 {
00097 public:
00099    typedef DATA_TYPE DataType;
00100 
00101 #ifdef GMTL_NO_METAPROG
00102    typedef VecBase<DATA_TYPE, SIZE> VecType;
00103 #else
00104    typedef VecBase<DATA_TYPE, SIZE, meta::DefaultVecTag> VecType;
00105 #endif
00106 
00108    enum Params { Size = SIZE };
00109 
00110 public:
00117    VecBase()
00118    {
00119 #ifdef GMTL_COUNT_CONSTRUCT_CALLS
00120       gmtl::helpers::VecCtrCounterInstance()->inc();
00121 #endif
00122    }
00123 
00129    VecBase(const VecBase<DATA_TYPE, SIZE>& rVec)
00130    {
00131 #ifdef GMTL_COUNT_CONSTRUCT_CALLS
00132       gmtl::helpers::VecCtrCounterInstance()->inc();
00133 #endif
00134 #ifdef GMTL_NO_METAPROG
00135       for(unsigned i=0;i<SIZE;++i)
00136          mData[i] = rVec.mData[i];
00137 #else
00138       gmtl::meta::AssignVecUnrolled<SIZE-1, VecBase<DATA_TYPE,SIZE> >::func(*this, rVec);
00139 #endif
00140    }
00141 
00142 #ifndef GMTL_NO_METAPROG
00143    template<typename REP2>
00144    VecBase(const VecBase<DATA_TYPE, SIZE, REP2>& rVec)
00145    {
00146 #ifdef GMTL_COUNT_CONSTRUCT_CALLS
00147       gmtl::helpers::VecCtrCounterInstance()->inc();
00148 #endif
00149       for(unsigned i=0;i<SIZE;++i)
00150       {  mData[i] = rVec[i]; }
00151    }
00152 #endif
00153 
00155 
00158    VecBase(const DATA_TYPE& val0,const DATA_TYPE& val1)
00159    {
00160 #ifdef GMTL_COUNT_CONSTRUCT_CALLS
00161       gmtl::helpers::VecCtrCounterInstance()->inc();
00162 #endif
00163       GMTL_STATIC_ASSERT( SIZE == 2, Invalid_constructor_of_size_2_used);
00164       mData[0] = val0; mData[1] = val1;
00165    }
00166    VecBase(const DATA_TYPE& val0,const DATA_TYPE& val1,const DATA_TYPE& val2)
00167    {
00168 #ifdef GMTL_COUNT_CONSTRUCT_CALLS
00169       gmtl::helpers::VecCtrCounterInstance()->inc();
00170 #endif
00171       GMTL_STATIC_ASSERT( SIZE == 3, Invalid_constructor_of_size_3_used );
00172       mData[0] = val0;  mData[1] = val1;  mData[2] = val2;
00173    }
00174    VecBase(const DATA_TYPE& val0,const DATA_TYPE& val1,const DATA_TYPE& val2,const DATA_TYPE& val3)
00175    {
00176 #ifdef GMTL_COUNT_CONSTRUCT_CALLS
00177       gmtl::helpers::VecCtrCounterInstance()->inc();
00178 #endif
00179       // @todo need compile time assert
00180       GMTL_STATIC_ASSERT( SIZE == 4, Invalid_constructor_of_size_4_used);
00181       mData[0] = val0;  mData[1] = val1;  mData[2] = val2;  mData[3] = val3;
00182    }
00184 
00191    inline void set(const DATA_TYPE* dataPtr)
00192    {
00193 #ifdef GMTL_NO_METAPROG
00194       for ( unsigned int i = 0; i < SIZE; ++i )
00195       {
00196          mData[i] = dataPtr[i];
00197       }
00198 #else
00199       gmtl::meta::AssignArrayUnrolled<SIZE-1, DATA_TYPE>::func(&(mData[0]),
00200                                                                dataPtr);
00201 #endif
00202    }
00203 
00205 
00208    inline void set(const DATA_TYPE& val0)
00209    { mData[0] = val0; }
00210 
00211    inline void set(const DATA_TYPE& val0,const DATA_TYPE& val1)
00212    {
00213       GMTL_STATIC_ASSERT( SIZE >= 2, Set_out_of_valid_range);
00214       mData[0] = val0; mData[1] = val1;
00215    }
00216    inline void set(const DATA_TYPE& val0,const DATA_TYPE& val1,const DATA_TYPE& val2)
00217    {
00218       GMTL_STATIC_ASSERT( SIZE >= 3, Set_out_of_valid_range);
00219       mData[0] = val0;  mData[1] = val1;  mData[2] = val2;
00220    }
00221    inline void set(const DATA_TYPE& val0,const DATA_TYPE& val1,const DATA_TYPE& val2,const DATA_TYPE& val3)
00222    {
00223       GMTL_STATIC_ASSERT( SIZE >= 4, Set_out_of_valid_range);
00224       mData[0] = val0;  mData[1] = val1;  mData[2] = val2;  mData[3] = val3;
00225    }
00227 
00229 
00237    inline DATA_TYPE& operator [](const unsigned i)
00238    {
00239       gmtlASSERT(i < SIZE);
00240       return mData[i];
00241    }
00242    inline const DATA_TYPE&  operator [](const unsigned i) const
00243    {
00244       gmtlASSERT(i < SIZE);
00245       return mData[i];
00246    }
00248 
00250 #ifdef GMTL_NO_METAPROG
00251    inline VecType& operator=(const VecBase<DATA_TYPE,SIZE>& rhs)
00252    {
00253       for(unsigned i=0;i<SIZE;++i)
00254       {
00255          mData[i] = rhs[i];
00256       }
00257 
00258       return *this;
00259    }
00260 #else
00261    template<typename REP2>
00262    inline VecType& operator=(const VecBase<DATA_TYPE,SIZE,REP2>& rhs)
00263    {
00264       for(unsigned i=0;i<SIZE;++i)
00265       {
00266          mData[i] = rhs[i];
00267       }
00268 
00269       //gmtl::meta::AssignVecUnrolled<SIZE-1, VecBase<DATA_TYPE,SIZE> >::func(*this, rVec);
00270       return *this;
00271    }
00272 #endif
00273 
00274    /*
00275     Assign from another of same type.
00276    inline VecType& operator=(const VecType&  rhs)
00277    {
00278       for(unsigned i=0;i<SIZE;++i)
00279       {
00280          mData[i] = rhs[i];
00281       }
00282       return *this;
00283    }
00284 */
00285 
00287 
00292    DATA_TYPE* getData()
00293    { return mData; }
00294    const DATA_TYPE* getData() const
00295    { return mData; }
00297 
00298 public:
00300    DATA_TYPE mData[SIZE];
00301 };
00302 
00303 
00304 }
00305 
00306 #endif

Generated on Sun Sep 19 2010 14:35:14 for GenericMathTemplateLibrary by  doxygen 1.7.1