Eigne Matrix Class Library
Dependents: MPC_current_control HydraulicControlBoard_SW AHRS Test_ekf ... more
src/Core/util/Meta.h@1:3b8049da21b8, 2019-09-24 (annotated)
- Committer:
- jsoh91
- Date:
- Tue Sep 24 00:18:23 2019 +0000
- Revision:
- 1:3b8049da21b8
- Parent:
- 0:13a5d365ba16
ignore and revise some of error parts
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ykuroda | 0:13a5d365ba16 | 1 | // This file is part of Eigen, a lightweight C++ template library |
ykuroda | 0:13a5d365ba16 | 2 | // for linear algebra. |
ykuroda | 0:13a5d365ba16 | 3 | // |
ykuroda | 0:13a5d365ba16 | 4 | // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr> |
ykuroda | 0:13a5d365ba16 | 5 | // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com> |
ykuroda | 0:13a5d365ba16 | 6 | // |
ykuroda | 0:13a5d365ba16 | 7 | // This Source Code Form is subject to the terms of the Mozilla |
ykuroda | 0:13a5d365ba16 | 8 | // Public License v. 2.0. If a copy of the MPL was not distributed |
ykuroda | 0:13a5d365ba16 | 9 | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
ykuroda | 0:13a5d365ba16 | 10 | |
ykuroda | 0:13a5d365ba16 | 11 | #ifndef EIGEN_META_H |
ykuroda | 0:13a5d365ba16 | 12 | #define EIGEN_META_H |
ykuroda | 0:13a5d365ba16 | 13 | |
ykuroda | 0:13a5d365ba16 | 14 | namespace Eigen { |
ykuroda | 0:13a5d365ba16 | 15 | |
ykuroda | 0:13a5d365ba16 | 16 | namespace internal { |
ykuroda | 0:13a5d365ba16 | 17 | |
ykuroda | 0:13a5d365ba16 | 18 | /** \internal |
ykuroda | 0:13a5d365ba16 | 19 | * \file Meta.h |
ykuroda | 0:13a5d365ba16 | 20 | * This file contains generic metaprogramming classes which are not specifically related to Eigen. |
ykuroda | 0:13a5d365ba16 | 21 | * \note In case you wonder, yes we're aware that Boost already provides all these features, |
ykuroda | 0:13a5d365ba16 | 22 | * we however don't want to add a dependency to Boost. |
ykuroda | 0:13a5d365ba16 | 23 | */ |
ykuroda | 0:13a5d365ba16 | 24 | |
ykuroda | 0:13a5d365ba16 | 25 | struct true_type { enum { value = 1 }; }; |
ykuroda | 0:13a5d365ba16 | 26 | struct false_type { enum { value = 0 }; }; |
ykuroda | 0:13a5d365ba16 | 27 | |
ykuroda | 0:13a5d365ba16 | 28 | template<bool Condition, typename Then, typename Else> |
ykuroda | 0:13a5d365ba16 | 29 | struct conditional { typedef Then type; }; |
ykuroda | 0:13a5d365ba16 | 30 | |
ykuroda | 0:13a5d365ba16 | 31 | template<typename Then, typename Else> |
ykuroda | 0:13a5d365ba16 | 32 | struct conditional <false, Then, Else> { typedef Else type; }; |
ykuroda | 0:13a5d365ba16 | 33 | |
ykuroda | 0:13a5d365ba16 | 34 | template<typename T, typename U> struct is_same { enum { value = 0 }; }; |
ykuroda | 0:13a5d365ba16 | 35 | template<typename T> struct is_same<T,T> { enum { value = 1 }; }; |
ykuroda | 0:13a5d365ba16 | 36 | |
ykuroda | 0:13a5d365ba16 | 37 | template<typename T> struct remove_reference { typedef T type; }; |
ykuroda | 0:13a5d365ba16 | 38 | template<typename T> struct remove_reference<T&> { typedef T type; }; |
ykuroda | 0:13a5d365ba16 | 39 | |
ykuroda | 0:13a5d365ba16 | 40 | template<typename T> struct remove_pointer { typedef T type; }; |
ykuroda | 0:13a5d365ba16 | 41 | template<typename T> struct remove_pointer<T*> { typedef T type; }; |
ykuroda | 0:13a5d365ba16 | 42 | template<typename T> struct remove_pointer<T*const> { typedef T type; }; |
ykuroda | 0:13a5d365ba16 | 43 | |
ykuroda | 0:13a5d365ba16 | 44 | template <class T> struct remove_const { typedef T type; }; |
ykuroda | 0:13a5d365ba16 | 45 | template <class T> struct remove_const<const T> { typedef T type; }; |
ykuroda | 0:13a5d365ba16 | 46 | template <class T> struct remove_const<const T[]> { typedef T type[]; }; |
ykuroda | 0:13a5d365ba16 | 47 | template <class T, unsigned int Size> struct remove_const<const T[Size]> { typedef T type[Size]; }; |
ykuroda | 0:13a5d365ba16 | 48 | |
ykuroda | 0:13a5d365ba16 | 49 | template<typename T> struct remove_all { typedef T type; }; |
ykuroda | 0:13a5d365ba16 | 50 | template<typename T> struct remove_all<const T> { typedef typename remove_all<T>::type type; }; |
ykuroda | 0:13a5d365ba16 | 51 | template<typename T> struct remove_all<T const&> { typedef typename remove_all<T>::type type; }; |
ykuroda | 0:13a5d365ba16 | 52 | template<typename T> struct remove_all<T&> { typedef typename remove_all<T>::type type; }; |
ykuroda | 0:13a5d365ba16 | 53 | template<typename T> struct remove_all<T const*> { typedef typename remove_all<T>::type type; }; |
ykuroda | 0:13a5d365ba16 | 54 | template<typename T> struct remove_all<T*> { typedef typename remove_all<T>::type type; }; |
ykuroda | 0:13a5d365ba16 | 55 | |
ykuroda | 0:13a5d365ba16 | 56 | template<typename T> struct is_arithmetic { enum { value = false }; }; |
ykuroda | 0:13a5d365ba16 | 57 | template<> struct is_arithmetic<float> { enum { value = true }; }; |
ykuroda | 0:13a5d365ba16 | 58 | template<> struct is_arithmetic<double> { enum { value = true }; }; |
ykuroda | 0:13a5d365ba16 | 59 | template<> struct is_arithmetic<long double> { enum { value = true }; }; |
ykuroda | 0:13a5d365ba16 | 60 | template<> struct is_arithmetic<bool> { enum { value = true }; }; |
ykuroda | 0:13a5d365ba16 | 61 | template<> struct is_arithmetic<char> { enum { value = true }; }; |
ykuroda | 0:13a5d365ba16 | 62 | template<> struct is_arithmetic<signed char> { enum { value = true }; }; |
ykuroda | 0:13a5d365ba16 | 63 | template<> struct is_arithmetic<unsigned char> { enum { value = true }; }; |
ykuroda | 0:13a5d365ba16 | 64 | template<> struct is_arithmetic<signed short> { enum { value = true }; }; |
ykuroda | 0:13a5d365ba16 | 65 | template<> struct is_arithmetic<unsigned short>{ enum { value = true }; }; |
ykuroda | 0:13a5d365ba16 | 66 | template<> struct is_arithmetic<signed int> { enum { value = true }; }; |
ykuroda | 0:13a5d365ba16 | 67 | template<> struct is_arithmetic<unsigned int> { enum { value = true }; }; |
ykuroda | 0:13a5d365ba16 | 68 | template<> struct is_arithmetic<signed long> { enum { value = true }; }; |
ykuroda | 0:13a5d365ba16 | 69 | template<> struct is_arithmetic<unsigned long> { enum { value = true }; }; |
ykuroda | 0:13a5d365ba16 | 70 | |
ykuroda | 0:13a5d365ba16 | 71 | template <typename T> struct add_const { typedef const T type; }; |
ykuroda | 0:13a5d365ba16 | 72 | template <typename T> struct add_const<T&> { typedef T& type; }; |
ykuroda | 0:13a5d365ba16 | 73 | |
ykuroda | 0:13a5d365ba16 | 74 | template <typename T> struct is_const { enum { value = 0 }; }; |
ykuroda | 0:13a5d365ba16 | 75 | template <typename T> struct is_const<T const> { enum { value = 1 }; }; |
ykuroda | 0:13a5d365ba16 | 76 | |
ykuroda | 0:13a5d365ba16 | 77 | template<typename T> struct add_const_on_value_type { typedef const T type; }; |
ykuroda | 0:13a5d365ba16 | 78 | template<typename T> struct add_const_on_value_type<T&> { typedef T const& type; }; |
ykuroda | 0:13a5d365ba16 | 79 | template<typename T> struct add_const_on_value_type<T*> { typedef T const* type; }; |
ykuroda | 0:13a5d365ba16 | 80 | template<typename T> struct add_const_on_value_type<T* const> { typedef T const* const type; }; |
ykuroda | 0:13a5d365ba16 | 81 | template<typename T> struct add_const_on_value_type<T const* const> { typedef T const* const type; }; |
ykuroda | 0:13a5d365ba16 | 82 | |
ykuroda | 0:13a5d365ba16 | 83 | /** \internal Allows to enable/disable an overload |
ykuroda | 0:13a5d365ba16 | 84 | * according to a compile time condition. |
ykuroda | 0:13a5d365ba16 | 85 | */ |
ykuroda | 0:13a5d365ba16 | 86 | template<bool Condition, typename T> struct enable_if; |
ykuroda | 0:13a5d365ba16 | 87 | |
ykuroda | 0:13a5d365ba16 | 88 | template<typename T> struct enable_if<true,T> |
ykuroda | 0:13a5d365ba16 | 89 | { typedef T type; }; |
ykuroda | 0:13a5d365ba16 | 90 | |
ykuroda | 0:13a5d365ba16 | 91 | |
ykuroda | 0:13a5d365ba16 | 92 | |
ykuroda | 0:13a5d365ba16 | 93 | /** \internal |
ykuroda | 0:13a5d365ba16 | 94 | * A base class do disable default copy ctor and copy assignement operator. |
ykuroda | 0:13a5d365ba16 | 95 | */ |
ykuroda | 0:13a5d365ba16 | 96 | class noncopyable |
ykuroda | 0:13a5d365ba16 | 97 | { |
ykuroda | 0:13a5d365ba16 | 98 | noncopyable(const noncopyable&); |
ykuroda | 0:13a5d365ba16 | 99 | const noncopyable& operator=(const noncopyable&); |
ykuroda | 0:13a5d365ba16 | 100 | protected: |
ykuroda | 0:13a5d365ba16 | 101 | noncopyable() {} |
ykuroda | 0:13a5d365ba16 | 102 | ~noncopyable() {} |
ykuroda | 0:13a5d365ba16 | 103 | }; |
ykuroda | 0:13a5d365ba16 | 104 | |
ykuroda | 0:13a5d365ba16 | 105 | |
ykuroda | 0:13a5d365ba16 | 106 | /** \internal |
ykuroda | 0:13a5d365ba16 | 107 | * Convenient struct to get the result type of a unary or binary functor. |
ykuroda | 0:13a5d365ba16 | 108 | * |
ykuroda | 0:13a5d365ba16 | 109 | * It supports both the current STL mechanism (using the result_type member) as well as |
ykuroda | 0:13a5d365ba16 | 110 | * upcoming next STL generation (using a templated result member). |
ykuroda | 0:13a5d365ba16 | 111 | * If none of these members is provided, then the type of the first argument is returned. FIXME, that behavior is a pretty bad hack. |
ykuroda | 0:13a5d365ba16 | 112 | */ |
ykuroda | 0:13a5d365ba16 | 113 | template<typename T> struct result_of {}; |
ykuroda | 0:13a5d365ba16 | 114 | |
ykuroda | 0:13a5d365ba16 | 115 | struct has_none {int a[1];}; |
ykuroda | 0:13a5d365ba16 | 116 | struct has_std_result_type {int a[2];}; |
ykuroda | 0:13a5d365ba16 | 117 | struct has_tr1_result {int a[3];}; |
ykuroda | 0:13a5d365ba16 | 118 | |
ykuroda | 0:13a5d365ba16 | 119 | template<typename Func, typename ArgType, int SizeOf=sizeof(has_none)> |
ykuroda | 0:13a5d365ba16 | 120 | struct unary_result_of_select {typedef ArgType type;}; |
ykuroda | 0:13a5d365ba16 | 121 | |
ykuroda | 0:13a5d365ba16 | 122 | template<typename Func, typename ArgType> |
ykuroda | 0:13a5d365ba16 | 123 | struct unary_result_of_select<Func, ArgType, sizeof(has_std_result_type)> {typedef typename Func::result_type type;}; |
ykuroda | 0:13a5d365ba16 | 124 | |
ykuroda | 0:13a5d365ba16 | 125 | template<typename Func, typename ArgType> |
ykuroda | 0:13a5d365ba16 | 126 | struct unary_result_of_select<Func, ArgType, sizeof(has_tr1_result)> {typedef typename Func::template result<Func(ArgType)>::type type;}; |
ykuroda | 0:13a5d365ba16 | 127 | |
ykuroda | 0:13a5d365ba16 | 128 | template<typename Func, typename ArgType> |
ykuroda | 0:13a5d365ba16 | 129 | struct result_of<Func(ArgType)> { |
ykuroda | 0:13a5d365ba16 | 130 | template<typename T> |
ykuroda | 0:13a5d365ba16 | 131 | static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0); |
ykuroda | 0:13a5d365ba16 | 132 | template<typename T> |
ykuroda | 0:13a5d365ba16 | 133 | static has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType)>::type const * = 0); |
ykuroda | 0:13a5d365ba16 | 134 | static has_none testFunctor(...); |
ykuroda | 0:13a5d365ba16 | 135 | |
ykuroda | 0:13a5d365ba16 | 136 | // note that the following indirection is needed for gcc-3.3 |
ykuroda | 0:13a5d365ba16 | 137 | enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))}; |
ykuroda | 0:13a5d365ba16 | 138 | typedef typename unary_result_of_select<Func, ArgType, FunctorType>::type type; |
ykuroda | 0:13a5d365ba16 | 139 | }; |
ykuroda | 0:13a5d365ba16 | 140 | |
ykuroda | 0:13a5d365ba16 | 141 | template<typename Func, typename ArgType0, typename ArgType1, int SizeOf=sizeof(has_none)> |
ykuroda | 0:13a5d365ba16 | 142 | struct binary_result_of_select {typedef ArgType0 type;}; |
ykuroda | 0:13a5d365ba16 | 143 | |
ykuroda | 0:13a5d365ba16 | 144 | template<typename Func, typename ArgType0, typename ArgType1> |
ykuroda | 0:13a5d365ba16 | 145 | struct binary_result_of_select<Func, ArgType0, ArgType1, sizeof(has_std_result_type)> |
ykuroda | 0:13a5d365ba16 | 146 | {typedef typename Func::result_type type;}; |
ykuroda | 0:13a5d365ba16 | 147 | |
ykuroda | 0:13a5d365ba16 | 148 | template<typename Func, typename ArgType0, typename ArgType1> |
ykuroda | 0:13a5d365ba16 | 149 | struct binary_result_of_select<Func, ArgType0, ArgType1, sizeof(has_tr1_result)> |
ykuroda | 0:13a5d365ba16 | 150 | {typedef typename Func::template result<Func(ArgType0,ArgType1)>::type type;}; |
ykuroda | 0:13a5d365ba16 | 151 | |
ykuroda | 0:13a5d365ba16 | 152 | template<typename Func, typename ArgType0, typename ArgType1> |
ykuroda | 0:13a5d365ba16 | 153 | struct result_of<Func(ArgType0,ArgType1)> { |
ykuroda | 0:13a5d365ba16 | 154 | template<typename T> |
ykuroda | 0:13a5d365ba16 | 155 | static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0); |
ykuroda | 0:13a5d365ba16 | 156 | template<typename T> |
ykuroda | 0:13a5d365ba16 | 157 | static has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType0,ArgType1)>::type const * = 0); |
ykuroda | 0:13a5d365ba16 | 158 | static has_none testFunctor(...); |
ykuroda | 0:13a5d365ba16 | 159 | |
ykuroda | 0:13a5d365ba16 | 160 | // note that the following indirection is needed for gcc-3.3 |
ykuroda | 0:13a5d365ba16 | 161 | enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))}; |
ykuroda | 0:13a5d365ba16 | 162 | typedef typename binary_result_of_select<Func, ArgType0, ArgType1, FunctorType>::type type; |
ykuroda | 0:13a5d365ba16 | 163 | }; |
ykuroda | 0:13a5d365ba16 | 164 | |
ykuroda | 0:13a5d365ba16 | 165 | /** \internal In short, it computes int(sqrt(\a Y)) with \a Y an integer. |
ykuroda | 0:13a5d365ba16 | 166 | * Usage example: \code meta_sqrt<1023>::ret \endcode |
ykuroda | 0:13a5d365ba16 | 167 | */ |
ykuroda | 0:13a5d365ba16 | 168 | template<int Y, |
ykuroda | 0:13a5d365ba16 | 169 | int InfX = 0, |
ykuroda | 0:13a5d365ba16 | 170 | int SupX = ((Y==1) ? 1 : Y/2), |
ykuroda | 0:13a5d365ba16 | 171 | bool Done = ((SupX-InfX)<=1 ? true : ((SupX*SupX <= Y) && ((SupX+1)*(SupX+1) > Y))) > |
ykuroda | 0:13a5d365ba16 | 172 | // use ?: instead of || just to shut up a stupid gcc 4.3 warning |
ykuroda | 0:13a5d365ba16 | 173 | class meta_sqrt |
ykuroda | 0:13a5d365ba16 | 174 | { |
ykuroda | 0:13a5d365ba16 | 175 | enum { |
ykuroda | 0:13a5d365ba16 | 176 | MidX = (InfX+SupX)/2, |
ykuroda | 0:13a5d365ba16 | 177 | TakeInf = MidX*MidX > Y ? 1 : 0, |
ykuroda | 0:13a5d365ba16 | 178 | NewInf = int(TakeInf) ? InfX : int(MidX), |
ykuroda | 0:13a5d365ba16 | 179 | NewSup = int(TakeInf) ? int(MidX) : SupX |
ykuroda | 0:13a5d365ba16 | 180 | }; |
ykuroda | 0:13a5d365ba16 | 181 | public: |
ykuroda | 0:13a5d365ba16 | 182 | enum { ret = meta_sqrt<Y,NewInf,NewSup>::ret }; |
ykuroda | 0:13a5d365ba16 | 183 | }; |
ykuroda | 0:13a5d365ba16 | 184 | |
ykuroda | 0:13a5d365ba16 | 185 | template<int Y, int InfX, int SupX> |
ykuroda | 0:13a5d365ba16 | 186 | class meta_sqrt<Y, InfX, SupX, true> { public: enum { ret = (SupX*SupX <= Y) ? SupX : InfX }; }; |
ykuroda | 0:13a5d365ba16 | 187 | |
ykuroda | 0:13a5d365ba16 | 188 | /** \internal determines whether the product of two numeric types is allowed and what the return type is */ |
ykuroda | 0:13a5d365ba16 | 189 | template<typename T, typename U> struct scalar_product_traits |
ykuroda | 0:13a5d365ba16 | 190 | { |
ykuroda | 0:13a5d365ba16 | 191 | enum { Defined = 0 }; |
ykuroda | 0:13a5d365ba16 | 192 | }; |
ykuroda | 0:13a5d365ba16 | 193 | |
ykuroda | 0:13a5d365ba16 | 194 | template<typename T> struct scalar_product_traits<T,T> |
ykuroda | 0:13a5d365ba16 | 195 | { |
ykuroda | 0:13a5d365ba16 | 196 | enum { |
ykuroda | 0:13a5d365ba16 | 197 | // Cost = NumTraits<T>::MulCost, |
ykuroda | 0:13a5d365ba16 | 198 | Defined = 1 |
ykuroda | 0:13a5d365ba16 | 199 | }; |
ykuroda | 0:13a5d365ba16 | 200 | typedef T ReturnType; |
ykuroda | 0:13a5d365ba16 | 201 | }; |
ykuroda | 0:13a5d365ba16 | 202 | |
ykuroda | 0:13a5d365ba16 | 203 | template<typename T> struct scalar_product_traits<T,std::complex<T> > |
ykuroda | 0:13a5d365ba16 | 204 | { |
ykuroda | 0:13a5d365ba16 | 205 | enum { |
ykuroda | 0:13a5d365ba16 | 206 | // Cost = 2*NumTraits<T>::MulCost, |
ykuroda | 0:13a5d365ba16 | 207 | Defined = 1 |
ykuroda | 0:13a5d365ba16 | 208 | }; |
ykuroda | 0:13a5d365ba16 | 209 | typedef std::complex<T> ReturnType; |
ykuroda | 0:13a5d365ba16 | 210 | }; |
ykuroda | 0:13a5d365ba16 | 211 | |
ykuroda | 0:13a5d365ba16 | 212 | template<typename T> struct scalar_product_traits<std::complex<T>, T> |
ykuroda | 0:13a5d365ba16 | 213 | { |
ykuroda | 0:13a5d365ba16 | 214 | enum { |
ykuroda | 0:13a5d365ba16 | 215 | // Cost = 2*NumTraits<T>::MulCost, |
ykuroda | 0:13a5d365ba16 | 216 | Defined = 1 |
ykuroda | 0:13a5d365ba16 | 217 | }; |
ykuroda | 0:13a5d365ba16 | 218 | typedef std::complex<T> ReturnType; |
ykuroda | 0:13a5d365ba16 | 219 | }; |
ykuroda | 0:13a5d365ba16 | 220 | |
ykuroda | 0:13a5d365ba16 | 221 | // FIXME quick workaround around current limitation of result_of |
ykuroda | 0:13a5d365ba16 | 222 | // template<typename Scalar, typename ArgType0, typename ArgType1> |
ykuroda | 0:13a5d365ba16 | 223 | // struct result_of<scalar_product_op<Scalar>(ArgType0,ArgType1)> { |
ykuroda | 0:13a5d365ba16 | 224 | // typedef typename scalar_product_traits<typename remove_all<ArgType0>::type, typename remove_all<ArgType1>::type>::ReturnType type; |
ykuroda | 0:13a5d365ba16 | 225 | // }; |
ykuroda | 0:13a5d365ba16 | 226 | |
ykuroda | 0:13a5d365ba16 | 227 | template<typename T> struct is_diagonal |
ykuroda | 0:13a5d365ba16 | 228 | { enum { ret = false }; }; |
ykuroda | 0:13a5d365ba16 | 229 | |
ykuroda | 0:13a5d365ba16 | 230 | template<typename T> struct is_diagonal<DiagonalBase<T> > |
ykuroda | 0:13a5d365ba16 | 231 | { enum { ret = true }; }; |
ykuroda | 0:13a5d365ba16 | 232 | |
ykuroda | 0:13a5d365ba16 | 233 | template<typename T> struct is_diagonal<DiagonalWrapper<T> > |
ykuroda | 0:13a5d365ba16 | 234 | { enum { ret = true }; }; |
ykuroda | 0:13a5d365ba16 | 235 | |
ykuroda | 0:13a5d365ba16 | 236 | template<typename T, int S> struct is_diagonal<DiagonalMatrix<T,S> > |
ykuroda | 0:13a5d365ba16 | 237 | { enum { ret = true }; }; |
ykuroda | 0:13a5d365ba16 | 238 | |
ykuroda | 0:13a5d365ba16 | 239 | } // end namespace internal |
ykuroda | 0:13a5d365ba16 | 240 | |
ykuroda | 0:13a5d365ba16 | 241 | } // end namespace Eigen |
ykuroda | 0:13a5d365ba16 | 242 | |
ykuroda | 0:13a5d365ba16 | 243 | #endif // EIGEN_META_H |