src/core/type-traits.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 1683 e4adb8cd4fc6
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
     1 #ifndef TYPE_TRAITS_H
     2 #define TYPE_TRAITS_H
     3 
     4 template <typename T>
     5 struct TypeTraits
     6 {
     7 private:
     8   struct NullType {};
     9   template <typename U> struct UnConst
    10   {
    11     typedef U Result;
    12   };
    13   template <typename U> struct UnConst<const U>
    14   {
    15     typedef U Result;
    16   };
    17   template <typename U> struct ReferenceTraits
    18   {
    19     enum {IsReference = 0};
    20     typedef U ReferencedType;
    21   };
    22   template <typename U> struct ReferenceTraits<U&>
    23   {
    24     enum {IsReference = 1};
    25     typedef U ReferencedType;
    26   };
    27   template <typename U> struct PointerTraits
    28   {
    29     enum {IsPointer = 0};
    30     typedef U PointeeType;
    31   };
    32   template <typename U> struct PointerTraits<U *>
    33   {
    34     enum {IsPointer = 1};
    35     typedef U PointeeType;
    36   };
    37   template <typename U> struct FunctionPtrTraits
    38   {
    39     enum {IsFunctionPointer = 0};
    40   };
    41   template <typename U> 
    42   struct FunctionPtrTraits <U (*) (void)>
    43   {
    44     enum {IsFunctionPointer = 1};
    45     enum {nArgs = 0};
    46     typedef U ReturnType;
    47   };
    48   template <typename U, typename V1> 
    49   struct FunctionPtrTraits <U (*) (V1)>
    50   {
    51     enum {IsFunctionPointer = 1};
    52     enum {nArgs = 1};
    53     typedef U ReturnType;
    54     typedef V1 Arg1Type;
    55   };
    56   template <typename U, typename V1, typename V2> 
    57   struct FunctionPtrTraits <U (*) (V1,V2)>
    58   {
    59     enum {IsFunctionPointer = 1};
    60     enum {nArgs = 2};
    61     typedef U ReturnType;
    62     typedef V1 Arg1Type;
    63     typedef V2 Arg2Type;
    64   };
    65   template <typename U, typename V1, typename V2,
    66             typename V3> 
    67   struct FunctionPtrTraits <U (*) (V1,V2,V3)>
    68   {
    69     enum {IsFunctionPointer = 1};
    70     enum {nArgs = 3};
    71     typedef U ReturnType;
    72     typedef V1 Arg1Type;
    73     typedef V2 Arg2Type;
    74     typedef V3 Arg3Type;
    75   };
    76   template <typename U, typename V1, typename V2,
    77             typename V3, typename V4> 
    78   struct FunctionPtrTraits <U (*) (V1,V2,V3,V4)>
    79   {
    80     enum {IsFunctionPointer = 1};
    81     enum {nArgs = 4};
    82     typedef U ReturnType;
    83     typedef V1 Arg1Type;
    84     typedef V2 Arg2Type;
    85     typedef V3 Arg3Type;
    86     typedef V4 Arg4Type;
    87   };
    88   template <typename U, typename V1, typename V2,
    89             typename V3, typename V4,
    90             typename V5> 
    91   struct FunctionPtrTraits <U (*) (V1,V2,V3,V4,V5)>
    92   {
    93     enum {IsFunctionPointer = 1};
    94     enum {nArgs = 5};
    95     typedef U ReturnType;
    96     typedef V1 Arg1Type;
    97     typedef V2 Arg2Type;
    98     typedef V3 Arg3Type;
    99     typedef V4 Arg4Type;
   100     typedef V5 Arg5Type;
   101   };
   102   template <typename U, typename V1, typename V2,
   103             typename V3, typename V4,
   104             typename V5, typename V6> 
   105   struct FunctionPtrTraits <U (*) (V1,V2,V3,V4,V5,V6)>
   106   {
   107     enum {IsFunctionPointer = 1};
   108     enum {nArgs = 6};
   109     typedef U ReturnType;
   110     typedef V1 Arg1Type;
   111     typedef V2 Arg2Type;
   112     typedef V3 Arg3Type;
   113     typedef V4 Arg4Type;
   114     typedef V5 Arg5Type;
   115     typedef V6 Arg6Type;
   116   };
   117   template <typename U> struct PtrToMemberTraits
   118   {
   119     enum {IsPointerToMember = 0};
   120   };
   121   template <typename U, typename V> 
   122   struct PtrToMemberTraits <U (V::*) (void)>
   123   {
   124     enum {IsPointerToMember = 1};
   125     enum {nArgs = 0};
   126     typedef U ReturnType;
   127   };
   128   template <typename U, typename V> 
   129   struct PtrToMemberTraits <U (V::*) (void) const>
   130   {
   131     enum {IsPointerToMember = 1};
   132     enum {nArgs = 0};
   133     typedef U ReturnType;
   134   };
   135   template <typename U, typename V,typename W1> 
   136   struct PtrToMemberTraits <U (V::*) (W1)>
   137   {
   138     enum {IsPointerToMember = 1};
   139     enum {nArgs = 1};
   140     typedef U ReturnType;
   141     typedef W1 Arg1Type;
   142   };
   143   template <typename U, typename V,typename W1> 
   144   struct PtrToMemberTraits <U (V::*) (W1) const>
   145   {
   146     enum {IsPointerToMember = 1};
   147     enum {nArgs = 1};
   148     typedef U ReturnType;
   149     typedef W1 Arg1Type;
   150   };
   151   template <typename U, typename V,typename W1, typename W2> 
   152   struct PtrToMemberTraits <U (V::*) (W1,W2)>
   153   {
   154     enum {IsPointerToMember = 1};
   155     enum {nArgs = 2};
   156     typedef U ReturnType;
   157     typedef W1 Arg1Type;
   158     typedef W2 Arg2Type;
   159   };
   160   template <typename U, typename V,typename W1, typename W2> 
   161   struct PtrToMemberTraits <U (V::*) (W1,W2) const>
   162   {
   163     enum {IsPointerToMember = 1};
   164     enum {nArgs = 2};
   165     typedef U ReturnType;
   166     typedef W1 Arg1Type;
   167     typedef W2 Arg2Type;
   168   };
   169   template <typename U, typename V,
   170             typename W1, typename W2,
   171             typename W3> 
   172   struct PtrToMemberTraits <U (V::*) (W1,W2,W3)>
   173   {
   174     enum {IsPointerToMember = 1};
   175     enum {nArgs = 3};
   176     typedef U ReturnType;
   177     typedef W1 Arg1Type;
   178     typedef W2 Arg2Type;
   179     typedef W3 Arg3Type;
   180   };
   181   template <typename U, typename V,
   182             typename W1, typename W2,
   183             typename W3> 
   184   struct PtrToMemberTraits <U (V::*) (W1,W2,W3) const>
   185   {
   186     enum {IsPointerToMember = 1};
   187     enum {nArgs = 3};
   188     typedef U ReturnType;
   189     typedef W1 Arg1Type;
   190     typedef W2 Arg2Type;
   191     typedef W3 Arg3Type;
   192   };
   193   template <typename U, typename V,
   194             typename W1, typename W2,
   195             typename W3, typename W4> 
   196   struct PtrToMemberTraits <U (V::*) (W1,W2,W3,W4)>
   197   {
   198     enum {IsPointerToMember = 1};
   199     enum {nArgs = 4};
   200     typedef U ReturnType;
   201     typedef W1 Arg1Type;
   202     typedef W2 Arg2Type;
   203     typedef W3 Arg3Type;
   204     typedef W4 Arg4Type;
   205   };
   206   template <typename U, typename V,
   207             typename W1, typename W2,
   208             typename W3, typename W4> 
   209   struct PtrToMemberTraits <U (V::*) (W1,W2,W3,W4) const>
   210   {
   211     enum {IsPointerToMember = 1};
   212     enum {nArgs = 4};
   213     typedef U ReturnType;
   214     typedef W1 Arg1Type;
   215     typedef W2 Arg2Type;
   216     typedef W3 Arg3Type;
   217     typedef W4 Arg4Type;
   218   };
   219   template <typename U, typename V,
   220             typename W1, typename W2,
   221             typename W3, typename W4,
   222             typename W5> 
   223   struct PtrToMemberTraits <U (V::*) (W1,W2,W3,W4,W5)>
   224   {
   225     enum {IsPointerToMember = 1};
   226     enum {nArgs = 5};
   227     typedef U ReturnType;
   228     typedef W1 Arg1Type;
   229     typedef W2 Arg2Type;
   230     typedef W3 Arg3Type;
   231     typedef W4 Arg4Type;
   232     typedef W5 Arg5Type;
   233   };
   234   template <typename U, typename V,
   235             typename W1, typename W2,
   236             typename W3, typename W4,
   237             typename W5> 
   238   struct PtrToMemberTraits <U (V::*) (W1,W2,W3,W4,W5) const>
   239   {
   240     enum {IsPointerToMember = 1};
   241     enum {nArgs = 5};
   242     typedef U ReturnType;
   243     typedef W1 Arg1Type;
   244     typedef W2 Arg2Type;
   245     typedef W3 Arg3Type;
   246     typedef W4 Arg4Type;
   247     typedef W5 Arg5Type;
   248   };
   249   template <typename U, typename V,
   250             typename W1, typename W2,
   251             typename W3, typename W4,
   252             typename W5, typename W6> 
   253   struct PtrToMemberTraits <U (V::*) (W1,W2,W3,W4,W5,W6)>
   254   {
   255     enum {IsPointerToMember = 1};
   256     enum {nArgs = 6};
   257     typedef U ReturnType;
   258     typedef W1 Arg1Type;
   259     typedef W2 Arg2Type;
   260     typedef W3 Arg3Type;
   261     typedef W4 Arg4Type;
   262     typedef W5 Arg5Type;
   263     typedef W6 Arg6Type;
   264   };
   265   template <typename U, typename V,
   266             typename W1, typename W2,
   267             typename W3, typename W4,
   268             typename W5, typename W6> 
   269   struct PtrToMemberTraits <U (V::*) (W1,W2,W3,W4,W5,W6) const>
   270   {
   271     enum {IsPointerToMember = 1};
   272     enum {nArgs = 6};
   273     typedef U ReturnType;
   274     typedef W1 Arg1Type;
   275     typedef W2 Arg2Type;
   276     typedef W3 Arg3Type;
   277     typedef W4 Arg4Type;
   278     typedef W5 Arg5Type;
   279     typedef W6 Arg6Type;
   280   };
   281 
   282 public:
   283   typedef typename UnConst<T>::Result NonConstType;
   284   typedef typename ReferenceTraits<T>::ReferencedType ReferencedType;
   285   typedef typename PointerTraits<T>::PointeeType PointeeType;
   286   enum {IsPointerToMember = PtrToMemberTraits<T>::IsPointerToMember};
   287   enum {IsPointer = PointerTraits<T>::IsPointer};
   288   enum {IsReference = ReferenceTraits<T>::IsReference};
   289   enum {IsFunctionPointer = FunctionPtrTraits<T>::IsFunctionPointer};
   290   typedef PtrToMemberTraits<T> PointerToMemberTraits;
   291   typedef FunctionPtrTraits<T> FunctionPointerTraits;
   292 };
   293 
   294 
   295 #endif /* TYPE_TRAITS_H */