ViennaGrid - The Vienna Grid Library  2.1.0
viennagrid/meta/utils.hpp
Go to the documentation of this file.
00001 #ifndef VIENNAGRID_META_UTILS_HPP
00002 #define VIENNAGRID_META_UTILS_HPP
00003 
00004 /* =======================================================================
00005    Copyright (c) 2011-2014, Institute for Microelectronics,
00006                             Institute for Analysis and Scientific Computing,
00007                             TU Wien.
00008 
00009                             -----------------
00010                      ViennaGrid - The Vienna Grid Library
00011                             -----------------
00012 
00013    License:      MIT (X11), see file LICENSE in the base directory
00014 ======================================================================= */
00015 
00016 #include <iterator>
00017 
00022 namespace viennagrid
00023 {
00024 
00026   class null_type {};
00027 
00028   // some special types
00030   class out_of_range {};
00032   class not_found {};
00033 
00034 
00035   // a static type pair
00037   template<typename FirstT, typename SecondT>
00038   struct static_pair
00039   {
00040     typedef FirstT   first;
00041     typedef SecondT  second;
00042   };
00043 
00044   namespace detail
00045   {
00046     // basic operations
00047     template<typename type1, typename type2>
00048     struct EQUAL
00049     {
00050       static const bool value = false;
00051     };
00052 
00053     template<typename type>
00054     struct EQUAL<type,type>
00055     {
00056       static const bool value = true;
00057     };
00058 
00059     template<typename type1, typename type2>
00060     struct NOT_EQUAL
00061     {
00062       static const bool value = !EQUAL<type1, type2>::value;
00063     };
00064 
00065     template<bool condition, typename type1, typename type2>
00066     struct IF
00067     {
00068       typedef type1 type;
00069     };
00070 
00071     template<typename type1, typename type2>
00072     struct IF<false, type1, type2>
00073     {
00074       typedef type2 type;
00075     };
00076 
00077     template<typename T>
00078     struct IDENTITY
00079     {
00080       typedef T type;
00081     };
00082 
00083 
00084     // error generator
00085     template<bool ErrorT>
00086     struct STATIC_ASSERT
00087     {
00088       typedef void type;
00089     };
00090 
00091     template<>
00092     struct STATIC_ASSERT<false>
00093     {};
00094 
00095 
00096 
00097     namespace result_of
00098     {
00099       template<typename pair>
00100       struct first
00101       {
00102           typedef not_found type;
00103       };
00104 
00105       template<typename first_, typename second_>
00106       struct first< static_pair<first_, second_> >
00107       {
00108           typedef first_ type;
00109       };
00110 
00111       template<typename pair>
00112       struct second
00113       {
00114           typedef not_found type;
00115       };
00116 
00117       template<typename first_, typename second_>
00118       struct second< static_pair<first_, second_> >
00119       {
00120           typedef second_ type;
00121       };
00122     }
00123 
00124 
00125     // a tag wrapper
00126     template<typename foo>
00127     class tag {};
00128 
00129 
00130     // a true functor
00131     struct true_predicate
00132     {
00133       template<typename type>
00134       bool operator()(const type &)
00135       {
00136           return true;
00137       }
00138     };
00139 
00140 
00141 
00142 
00143 
00144 
00145     template<typename some_type>
00146     struct remove_const
00147     {
00148       typedef some_type type;
00149     };
00150 
00151     template<typename some_type>
00152     struct remove_const<const some_type>
00153     {
00154       typedef some_type type;
00155     };
00156 
00157 
00158 
00159     // http://stackoverflow.com/questions/5423246/how-to-detect-if-a-type-is-an-iterator-or-const-iterator
00160     template<typename T>
00161     struct is_const_pointer { static const bool value = false; };
00162 
00163     template<typename T>
00164     struct is_const_pointer<const T*> { static const bool value = true; };
00165 
00166     template <typename TIterator>
00167     struct is_const_iterator
00168     {
00169       typedef typename std::iterator_traits<TIterator>::pointer pointer;
00170       static const bool value = is_const_pointer<pointer>::value;
00171     };
00172 
00173   } // namespace detail
00174 } // namespace viennagrid
00175 
00176 
00177 #endif