1
// This file is part of Eigen, a lightweight C++ template library
4
// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5
// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
7
// Eigen is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 3 of the License, or (at your option) any later version.
12
// Alternatively, you can redistribute it and/or
13
// modify it under the terms of the GNU General Public License as
14
// published by the Free Software Foundation; either version 2 of
15
// the License, or (at your option) any later version.
17
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20
// GNU General Public License for more details.
22
// You should have received a copy of the GNU Lesser General Public
23
// License and a copy of the GNU General Public License along with
24
// Eigen. If not, see <http://www.gnu.org/licenses/>.
33
* This file contains generic metaprogramming classes which are not specifically related to Eigen.
34
* \note In case you wonder, yes we're aware that Boost already provides all these features,
35
* we however don't want to add a dependency to Boost.
38
struct true_type { enum { value = 1 }; };
39
struct false_type { enum { value = 0 }; };
41
template<bool Condition, typename Then, typename Else>
42
struct conditional { typedef Then type; };
44
template<typename Then, typename Else>
45
struct conditional <false, Then, Else> { typedef Else type; };
47
template<typename T, typename U> struct is_same { enum { value = 0 }; };
48
template<typename T> struct is_same<T,T> { enum { value = 1 }; };
50
template<typename T> struct remove_reference { typedef T type; };
51
template<typename T> struct remove_reference<T&> { typedef T type; };
53
template<typename T> struct remove_pointer { typedef T type; };
54
template<typename T> struct remove_pointer<T*> { typedef T type; };
55
template<typename T> struct remove_pointer<T*const> { typedef T type; };
57
template <class T> struct remove_const { typedef T type; };
58
template <class T> struct remove_const<const T> { typedef T type; };
59
template <class T> struct remove_const<const T[]> { typedef T type[]; };
60
template <class T, unsigned int Size> struct remove_const<const T[Size]> { typedef T type[Size]; };
62
template<typename T> struct remove_all { typedef T type; };
63
template<typename T> struct remove_all<const T> { typedef typename remove_all<T>::type type; };
64
template<typename T> struct remove_all<T const&> { typedef typename remove_all<T>::type type; };
65
template<typename T> struct remove_all<T&> { typedef typename remove_all<T>::type type; };
66
template<typename T> struct remove_all<T const*> { typedef typename remove_all<T>::type type; };
67
template<typename T> struct remove_all<T*> { typedef typename remove_all<T>::type type; };
69
template<typename T> struct is_arithmetic { enum { value = false }; };
70
template<> struct is_arithmetic<float> { enum { value = true }; };
71
template<> struct is_arithmetic<double> { enum { value = true }; };
72
template<> struct is_arithmetic<long double> { enum { value = true }; };
73
template<> struct is_arithmetic<bool> { enum { value = true }; };
74
template<> struct is_arithmetic<char> { enum { value = true }; };
75
template<> struct is_arithmetic<signed char> { enum { value = true }; };
76
template<> struct is_arithmetic<unsigned char> { enum { value = true }; };
77
template<> struct is_arithmetic<signed short> { enum { value = true }; };
78
template<> struct is_arithmetic<unsigned short>{ enum { value = true }; };
79
template<> struct is_arithmetic<signed int> { enum { value = true }; };
80
template<> struct is_arithmetic<unsigned int> { enum { value = true }; };
81
template<> struct is_arithmetic<signed long> { enum { value = true }; };
82
template<> struct is_arithmetic<unsigned long> { enum { value = true }; };
83
template<> struct is_arithmetic<signed long long> { enum { value = true }; };
84
template<> struct is_arithmetic<unsigned long long> { enum { value = true }; };
86
template <typename T> struct add_const { typedef const T type; };
87
template <typename T> struct add_const<T&> { typedef T& type; };
89
template <typename T> struct is_const { enum { value = 0 }; };
90
template <typename T> struct is_const<T const> { enum { value = 1 }; };
92
template<typename T> struct add_const_on_value_type { typedef const T type; };
93
template<typename T> struct add_const_on_value_type<T&> { typedef T const& type; };
94
template<typename T> struct add_const_on_value_type<T*> { typedef T const* type; };
95
template<typename T> struct add_const_on_value_type<T* const> { typedef T const* const type; };
96
template<typename T> struct add_const_on_value_type<T const* const> { typedef T const* const type; };
98
/** \internal Allows to enable/disable an overload
99
* according to a compile time condition.
101
template<bool Condition, typename T> struct enable_if;
103
template<typename T> struct enable_if<true,T>
107
* Convenient struct to get the result type of a unary or binary functor.
109
* It supports both the current STL mechanism (using the result_type member) as well as
110
* upcoming next STL generation (using a templated result member).
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.
113
template<typename T> struct result_of {};
115
struct has_none {int a[1];};
116
struct has_std_result_type {int a[2];};
117
struct has_tr1_result {int a[3];};
119
template<typename Func, typename ArgType, int SizeOf=sizeof(has_none)>
120
struct unary_result_of_select {typedef ArgType type;};
122
template<typename Func, typename ArgType>
123
struct unary_result_of_select<Func, ArgType, sizeof(has_std_result_type)> {typedef typename Func::result_type type;};
125
template<typename Func, typename ArgType>
126
struct unary_result_of_select<Func, ArgType, sizeof(has_tr1_result)> {typedef typename Func::template result<Func(ArgType)>::type type;};
128
template<typename Func, typename ArgType>
129
struct result_of<Func(ArgType)> {
131
static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0);
133
static has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType)>::type const * = 0);
134
static has_none testFunctor(...);
136
// note that the following indirection is needed for gcc-3.3
137
enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))};
138
typedef typename unary_result_of_select<Func, ArgType, FunctorType>::type type;
141
template<typename Func, typename ArgType0, typename ArgType1, int SizeOf=sizeof(has_none)>
142
struct binary_result_of_select {typedef ArgType0 type;};
144
template<typename Func, typename ArgType0, typename ArgType1>
145
struct binary_result_of_select<Func, ArgType0, ArgType1, sizeof(has_std_result_type)>
146
{typedef typename Func::result_type type;};
148
template<typename Func, typename ArgType0, typename ArgType1>
149
struct binary_result_of_select<Func, ArgType0, ArgType1, sizeof(has_tr1_result)>
150
{typedef typename Func::template result<Func(ArgType0,ArgType1)>::type type;};
152
template<typename Func, typename ArgType0, typename ArgType1>
153
struct result_of<Func(ArgType0,ArgType1)> {
155
static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0);
157
static has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType0,ArgType1)>::type const * = 0);
158
static has_none testFunctor(...);
160
// note that the following indirection is needed for gcc-3.3
161
enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))};
162
typedef typename binary_result_of_select<Func, ArgType0, ArgType1, FunctorType>::type type;
165
/** \internal In short, it computes int(sqrt(\a Y)) with \a Y an integer.
166
* Usage example: \code meta_sqrt<1023>::ret \endcode
170
int SupX = ((Y==1) ? 1 : Y/2),
171
bool Done = ((SupX-InfX)<=1 ? true : ((SupX*SupX <= Y) && ((SupX+1)*(SupX+1) > Y))) >
172
// use ?: instead of || just to shut up a stupid gcc 4.3 warning
176
MidX = (InfX+SupX)/2,
177
TakeInf = MidX*MidX > Y ? 1 : 0,
178
NewInf = int(TakeInf) ? InfX : int(MidX),
179
NewSup = int(TakeInf) ? int(MidX) : SupX
182
enum { ret = meta_sqrt<Y,NewInf,NewSup>::ret };
185
template<int Y, int InfX, int SupX>
186
class meta_sqrt<Y, InfX, SupX, true> { public: enum { ret = (SupX*SupX <= Y) ? SupX : InfX }; };
188
/** \internal determines whether the product of two numeric types is allowed and what the return type is */
189
template<typename T, typename U> struct scalar_product_traits;
191
template<typename T> struct scalar_product_traits<T,T>
193
//enum { Cost = NumTraits<T>::MulCost };
194
typedef T ReturnType;
197
template<typename T> struct scalar_product_traits<T,std::complex<T> >
199
//enum { Cost = 2*NumTraits<T>::MulCost };
200
typedef std::complex<T> ReturnType;
203
template<typename T> struct scalar_product_traits<std::complex<T>, T>
205
//enum { Cost = 2*NumTraits<T>::MulCost };
206
typedef std::complex<T> ReturnType;
209
// FIXME quick workaround around current limitation of result_of
210
// template<typename Scalar, typename ArgType0, typename ArgType1>
211
// struct result_of<scalar_product_op<Scalar>(ArgType0,ArgType1)> {
212
// typedef typename scalar_product_traits<typename remove_all<ArgType0>::type, typename remove_all<ArgType1>::type>::ReturnType type;
215
template<typename T> struct is_diagonal
216
{ enum { ret = false }; };
218
template<typename T> struct is_diagonal<DiagonalBase<T> >
219
{ enum { ret = true }; };
221
template<typename T> struct is_diagonal<DiagonalWrapper<T> >
222
{ enum { ret = true }; };
224
template<typename T, int S> struct is_diagonal<DiagonalMatrix<T,S> >
225
{ enum { ret = true }; };
227
} // end namespace internal
229
#endif // EIGEN_META_H