Changeset 70:8d36d23fc17e in rrlib_util


Ignore:
Timestamp:
24.10.2012 03:10:55 (7 years ago)
Author:
Tobias Föhst <foehst@…>
Branch:
default
Children:
71:341c8d5984bb, 77:57c756db0b8d
Phase:
public
Message:

Reworked type lists

Files:
2 added
14 edited

Legend:

Unmodified
Added
Removed
  • tTypeList.h

    r43 r70  
    4444#define __rrlib__util__type_list__include_guard__ 
    4545 
     46namespace rrlib 
     47{ 
     48namespace util 
     49{ 
     50template <typename ... TTypes> 
     51struct tTypeList; 
     52} 
     53} 
     54 
    4655#include "rrlib/util/type_list/definitions.h" 
    4756 
     
    5059#include "rrlib/util/type_list/tFind.h" 
    5160#include "rrlib/util/type_list/tAppend.h" 
     61#include "rrlib/util/type_list/tAppendList.h" 
    5262#include "rrlib/util/type_list/tRemove.h" 
    5363#include "rrlib/util/type_list/tRemoveAll.h" 
     
    5767#include "rrlib/util/type_list/tMostDerived.h" 
    5868#include "rrlib/util/type_list/tDerivedToFront.h" 
     69 
     70#include "rrlib/util/type_list/tTypeListBase.h" 
    5971 
    6072#undef __rrlib__util__type_list__include_guard__ 
     
    8395 * 
    8496 */ 
    85 namespace 
     97template <typename THead, typename ... TTail> 
     98struct tTypeList<THead, TTail...> : public type_list::tTypeListBase<tTypeList<THead, TTail...>> 
    8699{ 
    87  
    88 template <typename THead, typename TTail> 
    89 class tTypeListBase 
    90 { 
    91   typedef tTypeList<THead, TTail> tList; 
    92  
    93 public: 
    94  
    95100  typedef THead tHead; 
    96   typedef TTail tTail; 
    97  
    98   static const size_t cSIZE = type_list::tSizeOf<tList>::cVALUE; 
    99  
    100   template <size_t Tindex> 
    101   struct tAt 
    102   { 
    103     typedef typename type_list::tAt<tList, Tindex>::tResult tResult; 
    104   }; 
    105  
    106   template <typename TType> 
    107   struct tIndexOf 
    108   { 
    109     static const size_t cVALUE = type_list::tFind<tList, TType>::cINDEX; 
    110   }; 
    111  
    112   template <typename TType> 
    113   struct tAppend 
    114   { 
    115     typedef typename type_list::tAppend<tList, TType>::tResult tResult; 
    116   }; 
    117  
    118   template <typename TType> 
    119   struct tRemove 
    120   { 
    121     typedef typename type_list::tRemove<tList, TType>::tResult tResult; 
    122   }; 
    123  
    124   template <typename TType> 
    125   struct tRemoveAll 
    126   { 
    127     typedef typename type_list::tRemoveAll<tList, TType>::tResult tResult; 
    128   }; 
    129  
    130   struct tUnique 
    131   { 
    132     typedef typename type_list::tUnique<tList>::tResult tResult; 
    133   }; 
    134  
    135   template <typename TOldType, typename TNewType> 
    136   struct tReplace 
    137   { 
    138     typedef typename type_list::tReplace<tList, TOldType, TNewType>::tResult tResult; 
    139   }; 
    140  
    141   template <typename TOldType, typename TNewType> 
    142   struct tReplaceAll 
    143   { 
    144     typedef typename type_list::tReplaceAll<tList, TOldType, TNewType>::tResult tResult; 
    145   }; 
    146  
    147   template <typename TBase> 
    148   struct tMostDerived 
    149   { 
    150     typedef typename type_list::tMostDerived<tList, TBase>::tResult tResult; 
    151   }; 
    152  
    153   struct tDerivedToFront 
    154   { 
    155     typedef typename type_list::tDerivedToFront<tList>::tResult tResult; 
    156   }; 
    157  
     101  typedef tTypeList<TTail...> tTail; 
    158102}; 
    159103 
    160 } 
    161  
    162 template <typename THead> 
    163 struct tTypeList<THead> : public tTypeListBase<THead, type_list::tEmptyList> 
    164 {}; 
    165  
    166 template <typename THead, typename ... TTail> 
    167 struct tTypeList<THead, TTail...> : public tTypeListBase<THead, tTypeList<TTail...>> 
    168 {}; 
    169  
    170 template <typename THead, typename ... TTail> 
    171 struct tTypeList<THead, tTypeList<TTail...>> : public tTypeListBase<THead, tTypeList<TTail...>> 
     104template <> 
     105struct tTypeList<> : public type_list::tTypeListBase<tTypeList<>> 
    172106{}; 
    173107 
  • test/test_type_list.cpp

    r34 r70  
    192192  PrintList<type_list::tAppend<tTypeList<>, A>::tResult>(); 
    193193  std::cout << "<!>  + <AB!> = "; 
    194   PrintList<type_list::tAppend<tTypeList<>, tTypeList<A, B>>::tResult>(); 
     194  PrintList<type_list::tAppendList<tTypeList<>, tTypeList<A, B>>::tResult>(); 
    195195  std::cout << "<A!> + B     = "; 
    196196  PrintList<tTypeList<A>::tAppend<B>::tResult>(); 
    197197  std::cout << "<A!> + <BC!> = "; 
    198   PrintList<tTypeList<A>::tAppend<tTypeList<B, C>>::tResult>(); 
     198  PrintList<tTypeList<A>::tAppendList<tTypeList<B, C>>::tResult>(); 
    199199 
    200200  std::cout << "Testing tRemove C" << std::endl; 
  • type_list/definitions.h

    r34 r70  
    5858namespace util 
    5959{ 
     60namespace type_list 
     61{ 
    6062 
    6163//---------------------------------------------------------------------- 
    6264// Forward declarations / typedefs / enums 
    6365//---------------------------------------------------------------------- 
    64 template <typename ... TTypes> struct tTypeList; 
    65  
    66 namespace type_list 
    67 { 
    6866 
    6967typedef tTypeList<> tEmptyList; 
    7068 
    7169const size_t cNOT_IN_LIST = static_cast<size_t>(-1); 
    72  
    73 } 
    7470 
    7571//---------------------------------------------------------------------- 
     
    7874} 
    7975} 
     76} 
    8077 
    8178#endif 
  • type_list/tAppend.h

    r34 r70  
    6464// Forward declarations / typedefs / enums 
    6565//---------------------------------------------------------------------- 
    66 template <typename TTypeList, typename TType> class tAppend; 
    6766 
    6867//---------------------------------------------------------------------- 
     
    7372 * 
    7473 */ 
    75 template <typename TType> 
    76 struct tAppend<tEmptyList, TType> 
     74template <typename TList, typename T> 
     75struct tAppend; 
     76 
     77template <typename ... TListTypes, typename T> 
     78struct tAppend<tTypeList<TListTypes...>, T> 
    7779{ 
    78   typedef tTypeList<TType> tResult; 
    79 }; 
    80  
    81 template <typename THead, typename ... TTail> 
    82 struct tAppend<tEmptyList, tTypeList<THead, TTail...>> 
    83 { 
    84   typedef tTypeList<THead, TTail...> tResult; 
    85 }; 
    86  
    87 template <typename TType, typename THead, typename ... TTail> 
    88 struct tAppend<tTypeList<THead, TTail...>, TType> 
    89 { 
    90   typedef tTypeList<THead, typename tAppend<typename tTypeList<THead, TTail...>::tTail, TType>::tResult> tResult; 
     80  typedef tTypeList<TListTypes..., T> tResult; 
    9181}; 
    9282 
  • type_list/tAt.h

    r34 r70  
    6464// Forward declarations / typedefs / enums 
    6565//---------------------------------------------------------------------- 
    66 template <typename TList, size_t Tindex> class tAt; 
    6766 
    6867//---------------------------------------------------------------------- 
     
    7372 * 
    7473 */ 
    75 template <typename THead, typename ... TTail> 
    76 struct tAt<tTypeList<THead, TTail...>, 0> 
     74template <typename TList, size_t Tindex> 
     75struct tAt 
    7776{ 
    78   typedef THead tResult; 
     77  typedef typename tAt < typename TList::tTail, Tindex - 1 >::tResult tResult; 
    7978}; 
    8079 
    81 template <size_t Tindex, typename THead, typename ... TTail> 
    82 struct tAt<tTypeList<THead, TTail...>, Tindex> 
     80template <typename TList> 
     81struct tAt<TList, 0> 
    8382{ 
    84   typedef typename tAt < typename tTypeList<THead, TTail...>::tTail, Tindex - 1 >::tResult tResult; 
     83  typedef typename TList::tHead tResult; 
    8584}; 
    8685 
  • type_list/tDerivedToFront.h

    r34 r70  
    6464// Forward declarations / typedefs / enums 
    6565//---------------------------------------------------------------------- 
    66 template <typename TTypeList> class tDerivedToFront; 
    6766 
    6867//---------------------------------------------------------------------- 
     
    7372 * 
    7473 */ 
     74template <typename TList> 
     75class tDerivedToFront 
     76{ 
     77  typedef typename tMostDerived<typename TList::tTail, typename TList::tHead>::tResult tMostDerivedFromHeadInTail; 
     78  typedef typename tReplace<typename TList::tTail, tMostDerivedFromHeadInTail, typename TList::tHead>::tResult tProcessedTail; 
     79  typedef typename tDerivedToFront<tProcessedTail>::tResult tSortedTail; 
     80public: 
     81  typedef typename tAppendList<tTypeList<tMostDerivedFromHeadInTail>, tSortedTail>::tResult tResult; 
     82}; 
     83 
    7584template <> 
    7685struct tDerivedToFront<tEmptyList> 
    7786{ 
    7887  typedef tEmptyList tResult; 
    79 }; 
    80  
    81 template <typename THead, typename ... TTail> 
    82 class tDerivedToFront<tTypeList<THead, TTail...>> 
    83 { 
    84   typedef typename tMostDerived<typename tTypeList<THead, TTail...>::tTail, THead>::tResult tTheMostDerived; 
    85   typedef typename tReplace<typename tTypeList<THead, TTail...>::tTail, tTheMostDerived, THead>::tResult tTail; 
    86  
    87 public: 
    88  
    89   typedef tTypeList<tTheMostDerived, typename tDerivedToFront<tTail>::tResult> tResult; 
    9088}; 
    9189 
  • type_list/tFind.h

    r34 r70  
    6464// Forward declarations / typedefs / enums 
    6565//---------------------------------------------------------------------- 
    66 template <typename TList, typename TType> class tFind; 
    6766 
    6867//---------------------------------------------------------------------- 
     
    7372 * 
    7473 */ 
    75 template <typename TType> 
    76 struct tFind<tEmptyList, TType> 
     74template <typename TList, typename T> 
     75class tFind 
     76{ 
     77  static const size_t cINDEX_IN_TAIL = tFind<typename TList::tTail, T>::cINDEX; 
     78public: 
     79  static const size_t cINDEX = cINDEX_IN_TAIL == cNOT_IN_LIST ? cNOT_IN_LIST : cINDEX_IN_TAIL + 1; 
     80}; 
     81 
     82template <typename T> 
     83struct tFind<tEmptyList, T> 
    7784{ 
    7885  static const size_t cINDEX = cNOT_IN_LIST; 
    7986}; 
    8087 
    81 template <typename TType, typename ... TTail> 
    82 struct tFind<tTypeList<TType, TTail...>, TType> 
     88template <typename TList> 
     89struct tFind<TList, typename TList::tHead> 
    8390{ 
    8491  static const size_t cINDEX = 0; 
    85 }; 
    86  
    87 template <typename TType, typename THead, typename ... TTail> 
    88 class tFind<tTypeList<THead, TTail...>, TType> 
    89 { 
    90   static const size_t temp = tFind<typename tTypeList<THead, TTail...>::tTail, TType>::cINDEX; 
    91  
    92 public: 
    93  
    94   static const size_t cINDEX = temp == cNOT_IN_LIST ? cNOT_IN_LIST : temp + 1; 
    95  
    9692}; 
    9793 
  • type_list/tMostDerived.h

    r34 r70  
    4242// External includes (system with <>, local with "") 
    4343//---------------------------------------------------------------------- 
    44 #include <boost/detail/select_type.hpp> 
    45 #include <boost/type_traits/is_base_and_derived.hpp> 
     44#include <type_traits> 
    4645 
    4746//---------------------------------------------------------------------- 
     
    6665// Forward declarations / typedefs / enums 
    6766//---------------------------------------------------------------------- 
    68 template <typename TList, typename TBase> class tMostDerived; 
    6967 
    7068//---------------------------------------------------------------------- 
     
    7573 * 
    7674 */ 
     75namespace 
     76{ 
     77 
     78template <bool, typename TThen, typename TElse> 
     79struct tIfThenElse 
     80{ 
     81  typedef TThen tResult; 
     82}; 
     83 
     84template <typename TThen, typename TElse> 
     85struct tIfThenElse<false, TThen, TElse> 
     86{ 
     87  typedef TElse tResult; 
     88}; 
     89 
     90} 
     91 
     92template <typename TList, typename TBase> 
     93class tMostDerived 
     94{ 
     95  typedef typename tMostDerived<typename TList::tTail, TBase>::tResult tCandidate; 
     96  static const bool cHEAD_DERIVED_FROM_CANDIDATE = std::is_base_of<tCandidate, typename TList::tHead>::value; 
     97public: 
     98  typedef typename tIfThenElse<cHEAD_DERIVED_FROM_CANDIDATE, typename TList::tHead, tCandidate>::tResult tResult; 
     99}; 
     100 
    77101template <typename TBase> 
    78102struct tMostDerived<tEmptyList, TBase> 
    79103{ 
    80104  typedef TBase tResult; 
    81 }; 
    82  
    83 template <typename TBase, typename THead, typename ... TTail> 
    84 class tMostDerived<tTypeList<THead, TTail...>, TBase> 
    85 { 
    86   typedef typename tMostDerived<typename tTypeList<THead, TTail...>::tTail, TBase>::tResult tCandidate; 
    87  
    88 public: 
    89  
    90   typedef typename boost::detail::if_true<boost::is_base_and_derived<tCandidate, THead>::value>::template then<THead, tCandidate>::type tResult; 
    91105}; 
    92106 
  • type_list/tRemove.h

    r34 r70  
    6464// Forward declarations / typedefs / enums 
    6565//---------------------------------------------------------------------- 
    66 template <typename TList, typename TType> class tRemove; 
    6766 
    6867//---------------------------------------------------------------------- 
     
    7372 * 
    7473 */ 
    75 template <typename TType> 
    76 struct tRemove<tEmptyList, TType> 
     74template <typename TList, typename T> 
     75class tRemove 
     76{ 
     77  typedef typename tRemove<typename TList::tTail, T>::tResult tProcessedTail; 
     78public: 
     79  typedef typename tAppendList<tTypeList<typename TList::tHead>, tProcessedTail>::tResult tResult; 
     80}; 
     81 
     82template <typename T> 
     83struct tRemove<tEmptyList, T> 
    7784{ 
    7885  typedef tEmptyList tResult; 
    7986}; 
    8087 
    81 template <typename TType, typename ... TTail> 
    82 struct tRemove<tTypeList<TType, TTail...>, TType> 
     88template <typename TList> 
     89struct tRemove<TList, typename TList::tHead> 
    8390{ 
    84   typedef typename tTypeList<TType, TTail...>::tTail tResult; 
    85 }; 
    86  
    87 template <typename TType, typename THead, typename ... TTail> 
    88 struct tRemove<tTypeList<THead, TTail...>, TType> 
    89 { 
    90   typedef tTypeList<THead, typename tRemove<typename tTypeList<THead, TTail...>::tTail, TType>::tResult> tResult; 
     91  typedef typename TList::tTail tResult; 
    9192}; 
    9293 
  • type_list/tRemoveAll.h

    r34 r70  
    6464// Forward declarations / typedefs / enums 
    6565//---------------------------------------------------------------------- 
    66 template <typename TList, typename TType> class tRemoveAll; 
    6766 
    6867//---------------------------------------------------------------------- 
     
    7372 * 
    7473 */ 
    75 template <typename TType> 
    76 struct tRemoveAll<tEmptyList, TType> 
     74template <typename TList, typename T> 
     75class tRemoveAll 
     76{ 
     77  typedef typename tRemoveAll<typename TList::tTail, T>::tResult tTailWithoutT; 
     78public: 
     79  typedef typename tAppendList<tTypeList<typename TList::tHead>, tTailWithoutT>::tResult tResult; 
     80}; 
     81 
     82template <typename T> 
     83struct tRemoveAll<tEmptyList, T> 
    7784{ 
    7885  typedef tEmptyList tResult; 
    7986}; 
    8087 
    81 template <typename TType, typename ... TTail> 
    82 struct tRemoveAll<tTypeList<TType, TTail...>, TType> 
     88template <typename TList> 
     89struct tRemoveAll<TList, typename TList::tHead> 
    8390{ 
    84   typedef typename tRemoveAll<typename tTypeList<TType, TTail...>::tTail, TType>::tResult tResult; 
    85 }; 
    86  
    87 template <typename TType, typename THead, typename ... TTail> 
    88 struct tRemoveAll<tTypeList<THead, TTail...>, TType> 
    89 { 
    90   typedef tTypeList<THead, typename tRemoveAll<typename tTypeList<THead, TTail...>::tTail, TType>::tResult> tResult; 
     91  typedef typename tRemoveAll<typename TList::tTail, typename TList::tHead>::tResult tResult; 
    9192}; 
    9293 
  • type_list/tReplace.h

    r34 r70  
    6464// Forward declarations / typedefs / enums 
    6565//---------------------------------------------------------------------- 
    66 template <typename TList, typename TOldType, typename TNewType> class tReplace; 
    6766 
    6867//---------------------------------------------------------------------- 
     
    7372 * 
    7473 */ 
    75 template <typename TOldType, typename TNewType> 
    76 struct tReplace<tEmptyList, TOldType, TNewType> 
     74template <typename TList, typename TOld, typename TNew> 
     75class tReplace 
     76{ 
     77  typedef typename tReplace<typename TList::tTail, TOld, TNew>::tResult tProcessedTail; 
     78public: 
     79  typedef typename tAppendList<tTypeList<typename TList::tHead>, tProcessedTail>::tResult tResult; 
     80}; 
     81 
     82template <typename TOld, typename TNew> 
     83struct tReplace<tEmptyList, TOld, TNew> 
    7784{ 
    7885  typedef tEmptyList tResult; 
    7986}; 
    8087 
    81 template <typename TOldType, typename TNewType, typename ... TTail> 
    82 struct tReplace<tTypeList<TOldType, TTail...>, TOldType, TNewType> 
     88template <typename TList, typename TNew> 
     89struct tReplace<TList, typename TList::tHead, TNew> 
    8390{ 
    84   typedef tTypeList<TNewType, TTail...> tResult; 
    85 }; 
    86  
    87 template <typename TOldType, typename TNewType, typename THead, typename ... TTail> 
    88 struct tReplace<tTypeList<THead, TTail...>, TOldType, TNewType> 
    89 { 
    90   typedef tTypeList<THead, typename tReplace<typename tTypeList<THead, TTail...>::tTail, TOldType, TNewType>::tResult> tResult; 
     91  typedef typename tAppendList<tTypeList<TNew>, typename TList::tTail>::tResult tResult; 
    9192}; 
    9293 
  • type_list/tReplaceAll.h

    r34 r70  
    6464// Forward declarations / typedefs / enums 
    6565//---------------------------------------------------------------------- 
    66 template <typename TList, typename TOldType, typename TNewType> class tReplaceAll; 
    6766 
    6867//---------------------------------------------------------------------- 
     
    7372 * 
    7473 */ 
    75 template <typename TOldType, typename TNewType> 
    76 struct tReplaceAll<tEmptyList, TOldType, TNewType> 
     74template <typename TList, typename TOld, typename TNew> 
     75class tReplaceAll 
     76{ 
     77  typedef typename tReplaceAll<typename TList::tTail, TOld, TNew>::tResult tProcessedTail; 
     78public: 
     79  typedef typename tAppendList<tTypeList<typename TList::tHead>, tProcessedTail>::tResult tResult; 
     80}; 
     81 
     82template <typename TOld, typename TNew> 
     83struct tReplaceAll<tEmptyList, TOld, TNew> 
    7784{ 
    7885  typedef tEmptyList tResult; 
    7986}; 
    8087 
    81 template <typename TOldType, typename TNewType, typename ... TTail> 
    82 struct tReplaceAll<tTypeList<TOldType, TTail...>, TOldType, TNewType> 
     88template <typename TList, typename TNew> 
     89class tReplaceAll<TList, typename TList::tHead, TNew> 
    8390{ 
    84   typedef tTypeList<TNewType, typename tReplaceAll<typename tTypeList<TOldType, TTail...>::tTail, TOldType, TNewType>::tResult> tResult; 
    85 }; 
    86  
    87 template <typename TOldType, typename TNewType, typename THead, typename ... TTail> 
    88 struct tReplaceAll<tTypeList<THead, TTail...>, TOldType, TNewType> 
    89 { 
    90   typedef tTypeList<THead, typename tReplaceAll<typename tTypeList<THead, TTail...>::tTail, TOldType, TNewType>::tResult> tResult; 
     91  typedef typename tReplaceAll<typename TList::tTail, typename TList::tHead, TNew>::tResult tProcessedTail; 
     92public: 
     93  typedef typename tAppendList<tTypeList<TNew>, tProcessedTail>::tResult tResult; 
    9194}; 
    9295 
  • type_list/tSizeOf.h

    r34 r70  
    6464// Forward declarations / typedefs / enums 
    6565//---------------------------------------------------------------------- 
    66 template <typename TList> class tSizeOf; 
    6766 
    6867//---------------------------------------------------------------------- 
     
    7372 * 
    7473 */ 
     74template <typename TList> 
     75struct tSizeOf 
     76{ 
     77  static const size_t cVALUE = tSizeOf<typename TList::tTail>::cVALUE + 1; 
     78}; 
     79 
    7580template <> 
    7681struct tSizeOf<tEmptyList> 
    7782{ 
    78   enum { cVALUE = 0 }; 
     83  static const size_t cVALUE = 0; 
    7984}; 
    80  
    81 template <typename THead, typename ... TTail> 
    82 struct tSizeOf<tTypeList<THead, TTail...>> 
    83 { 
    84   enum { cVALUE = tSizeOf<typename tTypeList<THead, TTail...>::tTail>::cVALUE + 1 }; 
    85 }; 
    86  
    8785 
    8886//---------------------------------------------------------------------- 
  • type_list/tUnique.h

    r34 r70  
    6464// Forward declarations / typedefs / enums 
    6565//---------------------------------------------------------------------- 
    66 template <typename TList> class tUnique; 
    6766 
    6867//---------------------------------------------------------------------- 
     
    7372 * 
    7473 */ 
     74template <typename TList> 
     75class tUnique 
     76{ 
     77  typedef typename tRemoveAll<typename TList::tTail, typename TList::tHead>::tResult tTailWithoutHead; 
     78  typedef typename tUnique<tTailWithoutHead>::tResult tUniqueTailWithoutHead; 
     79public: 
     80  typedef typename tAppendList<tTypeList<typename TList::tHead>, tUniqueTailWithoutHead>::tResult tResult; 
     81}; 
     82 
    7583template <> 
    7684struct tUnique<tEmptyList> 
    7785{ 
    7886  typedef tEmptyList tResult; 
    79 }; 
    80  
    81 template <typename THead> 
    82 struct tUnique<tTypeList<THead>> 
    83 { 
    84   typedef tTypeList<THead> tResult; 
    85 }; 
    86  
    87 template <typename THead, typename ... TTail> 
    88 class tUnique<tTypeList<THead, TTail...>> 
    89 { 
    90   typedef typename tUnique<typename tTypeList<THead, TTail...>::tTail>::tResult tTail; 
    91   typedef typename tRemove<tTail, THead>::tResult tTailWithoutHead; 
    92  
    93 public: 
    94  
    95   typedef tTypeList<THead, tTailWithoutHead> tResult; 
    96  
    9787}; 
    9888 
Note: See TracChangeset for help on using the changeset viewer.