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

VecExprMeta.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_VEC_EXPR_META_H
00007 #define _GMTL_VEC_EXPR_META_H
00008 
00009 #include <gmtl/Util/Meta.h>
00010 #include <gmtl/VecOpsMeta.h>
00011 #include <gmtl/VecBase.h>
00012 
00015 namespace gmtl
00016 {
00017 namespace meta
00018 {
00021 
00022 
00023 // ------------ Expression templates primitives ----------- //
00024 // Expression templates for vectors
00025 //
00026 // NOTE: These could probably be optimized more in the future
00027 
00028 // Concepts:
00029 //
00030 // VectorExpression:
00031 //    types:
00032 //    interface:
00033 //       TYPE eval(unsigned i):  Returns evaluation of expression at elt i
00034 
00035 
00037 template <typename T>
00038 struct ScalarArg
00039 {
00040    typedef T DataType;
00041 
00042    const T mScalar;
00043 
00044    inline ScalarArg(const T scalar) : mScalar(scalar) {}
00045    inline T operator[](const unsigned) const
00046    { return mScalar; }
00047 };
00048 
00049 template <typename T>
00050 inline ScalarArg<T> makeScalarArg(T val)
00051 { return ScalarArg<T>(val); }
00052 
00053 // --- TRAITS --- //
00058 template<typename T>
00059 struct ExprTraits
00060 {
00061    typedef const T ExprRef;     // Refer using a constant reference
00062 };
00063 
00064 template<typename T, unsigned SIZE>
00065 struct ExprTraits< VecBase<T, SIZE, ScalarArg<T> > >
00066 {
00067    typedef const VecBase<T,SIZE,ScalarArg<T> > ExprRef;
00068 };
00069 
00070 template<typename T, unsigned SIZE>
00071 struct ExprTraits< VecBase<T, SIZE, DefaultVecTag> >
00072 {
00073    typedef const VecBase<T,SIZE,DefaultVecTag>& ExprRef;
00074 };
00075 
00076 
00077 // -- Expressions -- //
00082 template <typename EXP1_T, typename EXP2_T, typename OP>
00083 struct VecBinaryExpr
00084 {
00085    typedef typename EXP1_T::DataType DataType;
00086 
00087    typename ExprTraits<EXP1_T>::ExprRef Exp1;
00088    typename ExprTraits<EXP2_T>::ExprRef Exp2;
00089 
00090    inline VecBinaryExpr(const EXP1_T& e1, const EXP2_T& e2) : Exp1(e1), Exp2(e2) {;}
00091    inline DataType operator[](const unsigned i) const
00092    { return OP::eval(Exp1[i], Exp2[i]); }
00093 };
00094 
00098 template <typename EXP1_T, typename OP>
00099 struct VecUnaryExpr
00100 {
00101    typedef typename EXP1_T::DataType DataType;
00102 
00103    typename ExprTraits<EXP1_T>::ExprRef Exp1;
00104 
00105    inline VecUnaryExpr(const EXP1_T& e1) : Exp1(e1) {;}
00106    inline DataType operator[](const unsigned i) const
00107    { return OP::eval(Exp1[i]); }
00108 };
00109 
00110 // --- Operations --- //
00111 /* Binary operations to add two vector expressions. */
00112 struct VecPlusBinary
00113 {
00114    template <typename T>
00115    static inline T eval(const T a1, const T a2)
00116    { return a1+a2; }
00117 };
00118 
00119 /* Binary operations to subtract two vector expressions. */
00120 struct VecMinusBinary
00121 {
00122    template <typename T>
00123    static inline T eval(const T a1, const T a2)
00124    { return a1-a2; }
00125 };
00126 
00127 struct VecMultBinary
00128 {
00129    template<typename T>
00130    static inline T eval(const T a1, const T a2)
00131    { return a1 * a2; }
00132 };
00133 
00134 struct VecDivBinary
00135 {
00136    template<typename T>
00137    static inline T eval(const T a1, const T a2)
00138    { return a1/a2; }
00139 };
00140 
00142 struct VecNegUnary
00143 {
00144    template <typename T>
00145    static inline T eval(const T a1)
00146    { return -a1; }
00147 };
00148 
00149 
00150 /*
00151 template<typename T, unsigned SIZE, typename R1, typename R2>
00152 inline VecBase<T,SIZE, VecBinaryExpr<VecBase<T,SIZE,R1>, VecBase<T,SIZE,R2>, VecPlusBinary> >
00153 sum(const VecBase<T,SIZE,R1>& v1, const VecBase<T,SIZE,R2>& v2)
00154 {
00155    return VecBase<T,SIZE,
00156                VecBinaryExpr<VecBase<T,SIZE,R1>,
00157                              VecBase<T,SIZE,R2>,
00158                              VecPlusBinary> >( VecBinaryExpr<VecBase<T,SIZE,R1>,
00159                                                              VecBase<T,SIZE,R2>,
00160                                                              VecPlusBinary>(v1,v2) );
00161 }
00162 
00163 
00164 template<typename T, unsigned SIZE, typename R1>
00165 inline VecBase<T,SIZE, VecBinaryExpr<VecBase<T,SIZE,R1>, VecBase<T,SIZE,ScalarArg<T> >, VecPlusBinary> >
00166 sum(const VecBase<T,SIZE,R1>& v1, const T& arg)
00167 {
00168    return VecBase<T,SIZE,
00169                VecBinaryExpr<VecBase<T,SIZE,R1>,
00170                              VecBase<T,SIZE,ScalarArg<T> >,
00171                              VecPlusBinary> >( VecBinaryExpr<VecBase<T,SIZE,R1>,
00172                                                              VecBase<T,SIZE,ScalarArg<T> >,
00173                                                              VecPlusBinary>(v1,ScalarArg<T>(arg)) );
00174 }
00175 */
00176 
00178 
00179 
00180 } // namespace meta
00181 } // end namespace
00182 
00183 
00184 #endif

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