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