Eigne Matrix Class Library
Dependents: Eigen_test Odometry_test AttitudeEstimation_usingTicker MPU9250_Quaternion_Binary_Serial ... more
Meta.h
00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. 00003 // 00004 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr> 00005 // Copyright (C) 2006-2008 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_META_H 00012 #define EIGEN_META_H 00013 00014 namespace Eigen { 00015 00016 namespace internal { 00017 00018 /** \internal 00019 * \file Meta.h 00020 * This file contains generic metaprogramming classes which are not specifically related to Eigen. 00021 * \note In case you wonder, yes we're aware that Boost already provides all these features, 00022 * we however don't want to add a dependency to Boost. 00023 */ 00024 00025 struct true_type { enum { value = 1 }; }; 00026 struct false_type { enum { value = 0 }; }; 00027 00028 template<bool Condition, typename Then, typename Else> 00029 struct conditional { typedef Then type; }; 00030 00031 template<typename Then, typename Else> 00032 struct conditional <false, Then, Else> { typedef Else type; }; 00033 00034 template<typename T, typename U> struct is_same { enum { value = 0 }; }; 00035 template<typename T> struct is_same<T,T> { enum { value = 1 }; }; 00036 00037 template<typename T> struct remove_reference { typedef T type; }; 00038 template<typename T> struct remove_reference<T&> { typedef T type; }; 00039 00040 template<typename T> struct remove_pointer { typedef T type; }; 00041 template<typename T> struct remove_pointer<T*> { typedef T type; }; 00042 template<typename T> struct remove_pointer<T*const> { typedef T type; }; 00043 00044 template <class T> struct remove_const { typedef T type; }; 00045 template <class T> struct remove_const<const T> { typedef T type; }; 00046 template <class T> struct remove_const<const T[]> { typedef T type[]; }; 00047 template <class T, unsigned int Size> struct remove_const<const T[Size]> { typedef T type[Size]; }; 00048 00049 template<typename T> struct remove_all { typedef T type; }; 00050 template<typename T> struct remove_all<const T> { typedef typename remove_all<T>::type type; }; 00051 template<typename T> struct remove_all<T const&> { typedef typename remove_all<T>::type type; }; 00052 template<typename T> struct remove_all<T&> { typedef typename remove_all<T>::type type; }; 00053 template<typename T> struct remove_all<T const*> { typedef typename remove_all<T>::type type; }; 00054 template<typename T> struct remove_all<T*> { typedef typename remove_all<T>::type type; }; 00055 00056 template<typename T> struct is_arithmetic { enum { value = false }; }; 00057 template<> struct is_arithmetic<float> { enum { value = true }; }; 00058 template<> struct is_arithmetic<double> { enum { value = true }; }; 00059 template<> struct is_arithmetic<long double> { enum { value = true }; }; 00060 template<> struct is_arithmetic<bool> { enum { value = true }; }; 00061 template<> struct is_arithmetic<char> { enum { value = true }; }; 00062 template<> struct is_arithmetic<signed char> { enum { value = true }; }; 00063 template<> struct is_arithmetic<unsigned char> { enum { value = true }; }; 00064 template<> struct is_arithmetic<signed short> { enum { value = true }; }; 00065 template<> struct is_arithmetic<unsigned short>{ enum { value = true }; }; 00066 template<> struct is_arithmetic<signed int> { enum { value = true }; }; 00067 template<> struct is_arithmetic<unsigned int> { enum { value = true }; }; 00068 template<> struct is_arithmetic<signed long> { enum { value = true }; }; 00069 template<> struct is_arithmetic<unsigned long> { enum { value = true }; }; 00070 00071 template <typename T> struct add_const { typedef const T type; }; 00072 template <typename T> struct add_const<T&> { typedef T& type; }; 00073 00074 template <typename T> struct is_const { enum { value = 0 }; }; 00075 template <typename T> struct is_const<T const> { enum { value = 1 }; }; 00076 00077 template<typename T> struct add_const_on_value_type { typedef const T type; }; 00078 template<typename T> struct add_const_on_value_type<T&> { typedef T const& type; }; 00079 template<typename T> struct add_const_on_value_type<T*> { typedef T const* type; }; 00080 template<typename T> struct add_const_on_value_type<T* const> { typedef T const* const type; }; 00081 template<typename T> struct add_const_on_value_type<T const* const> { typedef T const* const type; }; 00082 00083 /** \internal Allows to enable/disable an overload 00084 * according to a compile time condition. 00085 */ 00086 template<bool Condition, typename T> struct enable_if; 00087 00088 template<typename T> struct enable_if<true,T> 00089 { typedef T type; }; 00090 00091 00092 00093 /** \internal 00094 * A base class do disable default copy ctor and copy assignement operator. 00095 */ 00096 class noncopyable 00097 { 00098 noncopyable(const noncopyable&); 00099 const noncopyable& operator=(const noncopyable&); 00100 protected: 00101 noncopyable() {} 00102 ~noncopyable() {} 00103 }; 00104 00105 00106 /** \internal 00107 * Convenient struct to get the result type of a unary or binary functor. 00108 * 00109 * It supports both the current STL mechanism (using the result_type member) as well as 00110 * upcoming next STL generation (using a templated result member). 00111 * If none of these members is provided, then the type of the first argument is returned. FIXME, that behavior is a pretty bad hack. 00112 */ 00113 template<typename T> struct result_of {}; 00114 00115 struct has_none {int a[1];}; 00116 struct has_std_result_type {int a[2];}; 00117 struct has_tr1_result {int a[3];}; 00118 00119 template<typename Func, typename ArgType, int SizeOf=sizeof(has_none)> 00120 struct unary_result_of_select {typedef ArgType type;}; 00121 00122 template<typename Func, typename ArgType> 00123 struct unary_result_of_select<Func, ArgType, sizeof(has_std_result_type)> {typedef typename Func::result_type type;}; 00124 00125 template<typename Func, typename ArgType> 00126 struct unary_result_of_select<Func, ArgType, sizeof(has_tr1_result)> {typedef typename Func::template result<Func(ArgType)>::type type;}; 00127 00128 template<typename Func, typename ArgType> 00129 struct result_of<Func(ArgType)> { 00130 template<typename T> 00131 static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0); 00132 template<typename T> 00133 static has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType)>::type const * = 0); 00134 static has_none testFunctor(...); 00135 00136 // note that the following indirection is needed for gcc-3.3 00137 enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))}; 00138 typedef typename unary_result_of_select<Func, ArgType, FunctorType>::type type; 00139 }; 00140 00141 template<typename Func, typename ArgType0, typename ArgType1, int SizeOf=sizeof(has_none)> 00142 struct binary_result_of_select {typedef ArgType0 type;}; 00143 00144 template<typename Func, typename ArgType0, typename ArgType1> 00145 struct binary_result_of_select<Func, ArgType0, ArgType1, sizeof(has_std_result_type)> 00146 {typedef typename Func::result_type type;}; 00147 00148 template<typename Func, typename ArgType0, typename ArgType1> 00149 struct binary_result_of_select<Func, ArgType0, ArgType1, sizeof(has_tr1_result)> 00150 {typedef typename Func::template result<Func(ArgType0,ArgType1)>::type type;}; 00151 00152 template<typename Func, typename ArgType0, typename ArgType1> 00153 struct result_of<Func(ArgType0,ArgType1)> { 00154 template<typename T> 00155 static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0); 00156 template<typename T> 00157 static has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType0,ArgType1)>::type const * = 0); 00158 static has_none testFunctor(...); 00159 00160 // note that the following indirection is needed for gcc-3.3 00161 enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))}; 00162 typedef typename binary_result_of_select<Func, ArgType0, ArgType1, FunctorType>::type type; 00163 }; 00164 00165 /** \internal In short, it computes int(sqrt(\a Y)) with \a Y an integer. 00166 * Usage example: \code meta_sqrt<1023>::ret \endcode 00167 */ 00168 template<int Y, 00169 int InfX = 0, 00170 int SupX = ((Y==1) ? 1 : Y/2), 00171 bool Done = ((SupX-InfX)<=1 ? true : ((SupX*SupX <= Y) && ((SupX+1)*(SupX+1) > Y))) > 00172 // use ?: instead of || just to shut up a stupid gcc 4.3 warning 00173 class meta_sqrt 00174 { 00175 enum { 00176 MidX = (InfX+SupX)/2, 00177 TakeInf = MidX*MidX > Y ? 1 : 0, 00178 NewInf = int(TakeInf) ? InfX : int(MidX), 00179 NewSup = int(TakeInf) ? int(MidX) : SupX 00180 }; 00181 public: 00182 enum { ret = meta_sqrt<Y,NewInf,NewSup>::ret }; 00183 }; 00184 00185 template<int Y, int InfX, int SupX> 00186 class meta_sqrt<Y, InfX, SupX, true> { public: enum { ret = (SupX*SupX <= Y) ? SupX : InfX }; }; 00187 00188 /** \internal determines whether the product of two numeric types is allowed and what the return type is */ 00189 template<typename T, typename U> struct scalar_product_traits 00190 { 00191 enum { Defined = 0 }; 00192 }; 00193 00194 template<typename T> struct scalar_product_traits<T,T> 00195 { 00196 enum { 00197 // Cost = NumTraits<T>::MulCost, 00198 Defined = 1 00199 }; 00200 typedef T ReturnType; 00201 }; 00202 00203 template<typename T> struct scalar_product_traits<T,std::complex<T> > 00204 { 00205 enum { 00206 // Cost = 2*NumTraits<T>::MulCost, 00207 Defined = 1 00208 }; 00209 typedef std::complex<T> ReturnType; 00210 }; 00211 00212 template<typename T> struct scalar_product_traits<std::complex<T>, T> 00213 { 00214 enum { 00215 // Cost = 2*NumTraits<T>::MulCost, 00216 Defined = 1 00217 }; 00218 typedef std::complex<T> ReturnType; 00219 }; 00220 00221 // FIXME quick workaround around current limitation of result_of 00222 // template<typename Scalar, typename ArgType0, typename ArgType1> 00223 // struct result_of<scalar_product_op<Scalar>(ArgType0,ArgType1)> { 00224 // typedef typename scalar_product_traits<typename remove_all<ArgType0>::type, typename remove_all<ArgType1>::type>::ReturnType type; 00225 // }; 00226 00227 template<typename T> struct is_diagonal 00228 { enum { ret = false }; }; 00229 00230 template<typename T> struct is_diagonal<DiagonalBase<T> > 00231 { enum { ret = true }; }; 00232 00233 template<typename T> struct is_diagonal<DiagonalWrapper<T> > 00234 { enum { ret = true }; }; 00235 00236 template<typename T, int S> struct is_diagonal<DiagonalMatrix<T,S> > 00237 { enum { ret = true }; }; 00238 00239 } // end namespace internal 00240 00241 } // end namespace Eigen 00242 00243 #endif // EIGEN_META_H
Generated on Tue Jul 12 2022 17:46:58 by 1.7.2