Logo Search packages:      
Sourcecode: eigen3 version File versions  Download package

Meta.h

// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
//
// Eigen is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// Alternatively, you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// Eigen. If not, see <http://www.gnu.org/licenses/>.

#ifndef EIGEN_META_H
#define EIGEN_META_H

namespace internal {

/** \internal
  * \file Meta.h
  * This file contains generic metaprogramming classes which are not specifically related to Eigen.
  * \note In case you wonder, yes we're aware that Boost already provides all these features,
  * we however don't want to add a dependency to Boost.
  */

00038 struct true_type {  enum { value = 1 }; };
00039 struct false_type { enum { value = 0 }; };

template<bool Condition, typename Then, typename Else>
00042 struct conditional { typedef Then type; };

template<typename Then, typename Else>
00045 struct conditional <false, Then, Else> { typedef Else type; };

00047 template<typename T, typename U> struct is_same { enum { value = 0 }; };
00048 template<typename T> struct is_same<T,T> { enum { value = 1 }; };

00050 template<typename T> struct remove_reference { typedef T type; };
00051 template<typename T> struct remove_reference<T&> { typedef T type; };

00053 template<typename T> struct remove_pointer { typedef T type; };
00054 template<typename T> struct remove_pointer<T*> { typedef T type; };
00055 template<typename T> struct remove_pointer<T*const> { typedef T type; };

00057 template <class T> struct remove_const { typedef T type; };
00058 template <class T> struct remove_const<const T> { typedef T type; };
00059 template <class T> struct remove_const<const T[]> { typedef T type[]; };
00060 template <class T, unsigned int Size> struct remove_const<const T[Size]> { typedef T type[Size]; };

00062 template<typename T> struct remove_all { typedef T type; };
00063 template<typename T> struct remove_all<const T>   { typedef typename remove_all<T>::type type; };
00064 template<typename T> struct remove_all<T const&>  { typedef typename remove_all<T>::type type; };
00065 template<typename T> struct remove_all<T&>        { typedef typename remove_all<T>::type type; };
00066 template<typename T> struct remove_all<T const*>  { typedef typename remove_all<T>::type type; };
00067 template<typename T> struct remove_all<T*>        { typedef typename remove_all<T>::type type; };

00069 template<typename T> struct is_arithmetic      { enum { value = false }; };
00070 template<> struct is_arithmetic<float>         { enum { value = true }; };
00071 template<> struct is_arithmetic<double>        { enum { value = true }; };
00072 template<> struct is_arithmetic<long double>   { enum { value = true }; };
00073 template<> struct is_arithmetic<bool>          { enum { value = true }; };
00074 template<> struct is_arithmetic<char>          { enum { value = true }; };
00075 template<> struct is_arithmetic<signed char>   { enum { value = true }; };
00076 template<> struct is_arithmetic<unsigned char> { enum { value = true }; };
00077 template<> struct is_arithmetic<signed short>  { enum { value = true }; };
00078 template<> struct is_arithmetic<unsigned short>{ enum { value = true }; };
00079 template<> struct is_arithmetic<signed int>    { enum { value = true }; };
00080 template<> struct is_arithmetic<unsigned int>  { enum { value = true }; };
00081 template<> struct is_arithmetic<signed long>   { enum { value = true }; };
00082 template<> struct is_arithmetic<unsigned long> { enum { value = true }; };
00083 template<> struct is_arithmetic<signed long long>   { enum { value = true }; };
00084 template<> struct is_arithmetic<unsigned long long> { enum { value = true }; };

00086 template <typename T> struct add_const { typedef const T type; };
00087 template <typename T> struct add_const<T&> { typedef T& type; };

00089 template <typename T> struct is_const { enum { value = 0 }; };
00090 template <typename T> struct is_const<T const> { enum { value = 1 }; };

00092 template<typename T> struct add_const_on_value_type            { typedef const T type;  };
00093 template<typename T> struct add_const_on_value_type<T&>        { typedef T const& type; };
00094 template<typename T> struct add_const_on_value_type<T*>        { typedef T const* type; };
00095 template<typename T> struct add_const_on_value_type<T* const>  { typedef T const* const type; };
00096 template<typename T> struct add_const_on_value_type<T const* const>  { typedef T const* const type; };

/** \internal Allows to enable/disable an overload
  * according to a compile time condition.
  */
template<bool Condition, typename T> struct enable_if;

00103 template<typename T> struct enable_if<true,T>
{ typedef T type; };

/** \internal
  * Convenient struct to get the result type of a unary or binary functor.
  *
  * It supports both the current STL mechanism (using the result_type member) as well as
  * upcoming next STL generation (using a templated result member).
  * If none of these members is provided, then the type of the first argument is returned. FIXME, that behavior is a pretty bad hack.
  */
00113 template<typename T> struct result_of {};

00115 struct has_none {int a[1];};
00116 struct has_std_result_type {int a[2];};
00117 struct has_tr1_result {int a[3];};

template<typename Func, typename ArgType, int SizeOf=sizeof(has_none)>
00120 struct unary_result_of_select {typedef ArgType type;};

template<typename Func, typename ArgType>
00123 struct unary_result_of_select<Func, ArgType, sizeof(has_std_result_type)> {typedef typename Func::result_type type;};

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;};

template<typename Func, typename ArgType>
00129 struct result_of<Func(ArgType)> {
    template<typename T>
    static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0);
    template<typename T>
    static has_tr1_result      testFunctor(T const *, typename T::template result<T(ArgType)>::type const * = 0);
    static has_none            testFunctor(...);

    // note that the following indirection is needed for gcc-3.3
    enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))};
    typedef typename unary_result_of_select<Func, ArgType, FunctorType>::type type;
};

template<typename Func, typename ArgType0, typename ArgType1, int SizeOf=sizeof(has_none)>
00142 struct binary_result_of_select {typedef ArgType0 type;};

template<typename Func, typename ArgType0, typename ArgType1>
00145 struct binary_result_of_select<Func, ArgType0, ArgType1, sizeof(has_std_result_type)>
{typedef typename Func::result_type type;};

template<typename Func, typename ArgType0, typename ArgType1>
00149 struct binary_result_of_select<Func, ArgType0, ArgType1, sizeof(has_tr1_result)>
{typedef typename Func::template result<Func(ArgType0,ArgType1)>::type type;};

template<typename Func, typename ArgType0, typename ArgType1>
00153 struct result_of<Func(ArgType0,ArgType1)> {
    template<typename T>
    static has_std_result_type testFunctor(T const *, typename T::result_type const * = 0);
    template<typename T>
    static has_tr1_result      testFunctor(T const *, typename T::template result<T(ArgType0,ArgType1)>::type const * = 0);
    static has_none            testFunctor(...);

    // note that the following indirection is needed for gcc-3.3
    enum {FunctorType = sizeof(testFunctor(static_cast<Func*>(0)))};
    typedef typename binary_result_of_select<Func, ArgType0, ArgType1, FunctorType>::type type;
};

/** \internal In short, it computes int(sqrt(\a Y)) with \a Y an integer.
  * Usage example: \code meta_sqrt<1023>::ret \endcode
  */
template<int Y,
         int InfX = 0,
         int SupX = ((Y==1) ? 1 : Y/2),
         bool Done = ((SupX-InfX)<=1 ? true : ((SupX*SupX <= Y) && ((SupX+1)*(SupX+1) > Y))) >
                                // use ?: instead of || just to shut up a stupid gcc 4.3 warning
00173 class meta_sqrt
{
    enum {
      MidX = (InfX+SupX)/2,
      TakeInf = MidX*MidX > Y ? 1 : 0,
      NewInf = int(TakeInf) ? InfX : int(MidX),
      NewSup = int(TakeInf) ? int(MidX) : SupX
    };
  public:
    enum { ret = meta_sqrt<Y,NewInf,NewSup>::ret };
};

template<int Y, int InfX, int SupX>
00186 class meta_sqrt<Y, InfX, SupX, true> { public:  enum { ret = (SupX*SupX <= Y) ? SupX : InfX }; };

/** \internal determines whether the product of two numeric types is allowed and what the return type is */
template<typename T, typename U> struct scalar_product_traits;

00191 template<typename T> struct scalar_product_traits<T,T>
{
  //enum { Cost = NumTraits<T>::MulCost };
  typedef T ReturnType;
};

00197 template<typename T> struct scalar_product_traits<T,std::complex<T> >
{
  //enum { Cost = 2*NumTraits<T>::MulCost };
  typedef std::complex<T> ReturnType;
};

00203 template<typename T> struct scalar_product_traits<std::complex<T>, T>
{
  //enum { Cost = 2*NumTraits<T>::MulCost  };
  typedef std::complex<T> ReturnType;
};

// FIXME quick workaround around current limitation of result_of
// template<typename Scalar, typename ArgType0, typename ArgType1>
// struct result_of<scalar_product_op<Scalar>(ArgType0,ArgType1)> {
// typedef typename scalar_product_traits<typename remove_all<ArgType0>::type, typename remove_all<ArgType1>::type>::ReturnType type;
// };

00215 template<typename T> struct is_diagonal
{ enum { ret = false }; };

00218 template<typename T> struct is_diagonal<DiagonalBase<T> >
{ enum { ret = true }; };

00221 template<typename T> struct is_diagonal<DiagonalWrapper<T> >
{ enum { ret = true }; };

00224 template<typename T, int S> struct is_diagonal<DiagonalMatrix<T,S> >
{ enum { ret = true }; };

} // end namespace internal

#endif // EIGEN_META_H

Generated by  Doxygen 1.6.0   Back to index