summaryrefslogtreecommitdiffstats
path: root/stlport/stlport/stl/type_manips.h
blob: 410b59d2569a3178dfde45aa6b9cd5c524d40124 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
 *
 * Copyright (c) 2003
 * Francois Dumont
 *
 * This material is provided "as is", with absolutely no warranty expressed
 * or implied. Any use is at your own risk.
 *
 * Permission to use or copy this software for any purpose is hereby granted
 * without fee, provided the above notices are retained on all copies.
 * Permission to modify the code and to distribute modified code is granted,
 * provided the above notices are retained, and a notice that the code was
 * modified is included with the above copyright notice.
 *
 */


#ifndef _STLP_TYPE_MANIPS_H
#define _STLP_TYPE_MANIPS_H

_STLP_BEGIN_NAMESPACE

struct __true_type {};
struct __false_type {};

#if defined (_STLP_USE_NAMESPACES) && !defined (_STLP_DONT_USE_PRIV_NAMESPACE)
_STLP_MOVE_TO_PRIV_NAMESPACE
using _STLP_STD::__true_type;
using _STLP_STD::__false_type;
_STLP_MOVE_TO_STD_NAMESPACE
#endif

//bool to type
template <int _Is>
struct __bool2type
{ typedef __true_type _Ret; };

_STLP_TEMPLATE_NULL
struct __bool2type<1> { typedef __true_type _Ret; };

_STLP_TEMPLATE_NULL
struct __bool2type<0> { typedef __false_type _Ret; };

//type to bool
template <class __bool_type>
struct __type2bool { enum {_Ret = 1}; };

_STLP_TEMPLATE_NULL
struct __type2bool<__true_type> { enum {_Ret = 1}; };

_STLP_TEMPLATE_NULL
struct __type2bool<__false_type> { enum {_Ret = 0}; };

//Negation
template <class _BoolType>
struct _Not { typedef __false_type _Ret; };

_STLP_TEMPLATE_NULL
struct _Not<__false_type> { typedef __true_type _Ret; };

// logical and of 2 predicated
template <class _P1, class _P2>
struct _Land2 { typedef __false_type _Ret; };

_STLP_TEMPLATE_NULL
struct _Land2<__true_type, __true_type> { typedef __true_type _Ret; };

// logical and of 3 predicated
template <class _P1, class _P2, class _P3>
struct _Land3 { typedef __false_type _Ret; };

_STLP_TEMPLATE_NULL
struct _Land3<__true_type, __true_type, __true_type> { typedef __true_type _Ret; };

//logical or of 2 predicated
template <class _P1, class _P2>
struct _Lor2 { typedef __true_type _Ret; };

_STLP_TEMPLATE_NULL
struct _Lor2<__false_type, __false_type> { typedef __false_type _Ret; };

// logical or of 3 predicated
template <class _P1, class _P2, class _P3>
struct _Lor3 { typedef __true_type _Ret; };

_STLP_TEMPLATE_NULL
struct _Lor3<__false_type, __false_type, __false_type> { typedef __false_type _Ret; };

////////////////////////////////////////////////////////////////////////////////
// class template __select
// Selects one of two types based upon a boolean constant
// Invocation: __select<_Cond, T, U>::Result
// where:
// flag is a compile-time boolean constant
// T and U are types
// Result evaluates to T if flag is true, and to U otherwise.
////////////////////////////////////////////////////////////////////////////////
// BEWARE: If the compiler do not support partial template specialization or nested template
//classes the default behavior of the __select is to consider the condition as false and so return
//the second template type!!

#if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
#  if defined (__BORLANDC__) 
template <class _CondT, class _Tp1, class _Tp2>
struct __selectT { typedef _Tp1 _Ret; };

template <class _Tp1, class _Tp2>
struct __selectT<__false_type, _Tp1, _Tp2> { typedef _Tp2 _Ret; };
#  endif

#  if !defined (__BORLANDC__) || (__BORLANDC__ >= 0x590)
template <bool _Cond, class _Tp1, class _Tp2>
struct __select { typedef _Tp1 _Ret; };

template <class _Tp1, class _Tp2>
struct __select<false, _Tp1, _Tp2> { typedef _Tp2 _Ret; };
#  else
template <bool _Cond, class _Tp1, class _Tp2>
struct __select 
{ typedef __selectT<typename __bool2type<_Cond>::_Ret, _Tp1, _Tp2>::_Ret _Ret; };
#  endif

#else

#  if defined (_STLP_MEMBER_TEMPLATE_CLASSES)
template <int _Cond>
struct __select_aux {
  template <class _Tp1, class _Tp2>
  struct _In {
    typedef _Tp1 _Ret;
  };
};

_STLP_TEMPLATE_NULL
struct __select_aux<0> {
  template <class _Tp1, class _Tp2>
  struct _In {
    typedef _Tp2 _Ret;
  };
};

template <int _Cond, class _Tp1, class _Tp2>
struct __select {
  typedef typename __select_aux<_Cond>::_STLP_TEMPLATE _In<_Tp1, _Tp2>::_Ret _Ret;
};
#  else /* _STLP_MEMBER_TEMPLATE_CLASSES */
//default behavior
template <int _Cond, class _Tp1, class _Tp2>
struct __select {
  typedef _Tp2 _Ret;
};
#  endif /* _STLP_MEMBER_TEMPLATE_CLASSES */

#endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */

/* Rather than introducing a new macro for the following constrution we use
 * an existing one (_STLP_DONT_SIMULATE_PARTIAL_SPEC_FOR_TYPE_TRAITS) that
 * is used for a similar feature.
 */
#if !defined (_STLP_DONT_SIMULATE_PARTIAL_SPEC_FOR_TYPE_TRAITS) && \
    (!defined (__GNUC__) || (__GNUC__ > 2))
// Helper struct that will forbid volatile qualified types:
#  if !defined (__BORLANDC__)
struct _NoVolatilePointerShim { _NoVolatilePointerShim(const void*); };
template <class _Tp>
char _STLP_CALL _IsCopyableFun(bool, _NoVolatilePointerShim, _Tp const*, _Tp*); // no implementation is required
char* _STLP_CALL _IsCopyableFun(bool, ...);       // no implementation is required

template <class _Src, class _Dst>
struct _Copyable {
  static _Src* __null_src();
  static _Dst* __null_dst();
  enum { _Ret = (sizeof(_IsCopyableFun(false, __null_src(), __null_src(), __null_dst())) == sizeof(char)) };
  typedef typename __bool2type<_Ret>::_Ret _RetT;
};
#  else
template <class _Tp1, class _Tp2> struct _AreSameTypes;
template <class _Tp> struct _IsUnQual;
template <class _Src, class _Dst>
struct _Copyable {
  typedef typename _AreSameTypes<_Src, _Dst>::_Ret _Tr1;
  typedef typename _IsUnQual<_Dst>::_Ret _Tr2;
  typedef typename _Land2<_Tr1, _Tr2>::_Ret _RetT;
  enum { _Ret = __type2bool<_RetT>::_Ret };
};
#  endif
#else
template <class _Src, class _Dst>
struct _Copyable {
  enum { _Ret = 0 };
  typedef __false_type _RetT;
};
#endif

/*
 * The following struct will tell you if 2 types are the same and if copying memory
 * from the _Src type to the _Dst type is right considering qualifiers. If _Src and
 * _Dst types are the same unqualified types _Ret will be false if:
 *  - any of the type has the volatile qualifier
 *  - _Dst is const qualified
 */
template <class _Src, class _Dst>
struct _AreCopyable {
  enum { _Same = _Copyable<_Src, _Dst>::_Ret };
  typedef typename _Copyable<_Src, _Dst>::_RetT _Ret;
};

template <class _Tp1, class _Tp2>
struct _AreSameTypes {
  enum { _Same = 0 };
  typedef __false_type _Ret;
};

#if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
template <class _Tp>
struct _AreSameTypes<_Tp, _Tp> {
  enum { _Same = 1 };
  typedef __true_type _Ret;
};
#endif

#if !defined (_STLP_DONT_SIMULATE_PARTIAL_SPEC_FOR_TYPE_TRAITS)
template <class _Src, class _Dst>
struct _ConversionHelper {
  static char _Test(bool, _Dst);
  static char* _Test(bool, ...);
  static _Src _MakeSource();
};

template <class _Src, class _Dst>
struct _IsConvertible {
  typedef _ConversionHelper<_Src*, const volatile _Dst*> _H;
  enum { value = (sizeof(char) == sizeof(_H::_Test(false, _H::_MakeSource()))) };
  typedef typename __bool2type<value>::_Ret _Ret;
};

#  if defined (__BORLANDC__)
#    if (__BORLANDC__ < 0x590)
template<class _Tp>
struct _UnConstPtr { typedef _Tp _Type; };

template<class _Tp>
struct _UnConstPtr<_Tp*> { typedef _Tp _Type; };

template<class _Tp>
struct _UnConstPtr<const _Tp*> { typedef _Tp _Type; };
#    endif

#    if !defined (_STLP_QUALIFIED_SPECIALIZATION_BUG)
template <class _Tp>
struct _IsConst { typedef __false_type _Ret; };
#    else
template <class _Tp>
struct _IsConst { typedef _AreSameTypes<_Tp, const _Tp>::_Ret _Ret; };
#    endif

#    if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && !defined (_STLP_QUALIFIED_SPECIALIZATION_BUG)
template <class _Tp>
struct _IsConst <const _Tp> { typedef __true_type _Ret; };
#    endif

#    if (__BORLANDC__ < 0x590)
template<class _Tp>
struct _IsConst<_Tp*> { typedef _AreSameTypes<_Tp*, const _Tp*>::_Ret _Ret; };
#    endif
template <class _Tp>
struct _IsVolatile { typedef _AreSameTypes<_Tp, volatile _Tp>::_Ret _Ret; };

template<class _Tp>
struct _IsUnQual {
  typedef _IsConst<_Tp>::_Ret _Tr1;
  typedef _IsVolatile<_Tp>::_Ret _Tr2;
  typedef _Not<_Tr1>::_Ret _NotCon;
  typedef _Not<_Tr2>::_Ret _NotVol;
  typedef _Land2<_NotCon, _NotVol>::_Ret _Ret;
};

#    if !defined (_STLP_QUALIFIED_SPECIALIZATION_BUG)
template <class _Tp> struct _UnQual { typedef _Tp _Type; };
template <class _Tp> struct _UnQual<const _Tp> { typedef _Tp _Type; };
template <class _Tp> struct _UnQual<volatile _Tp> { typedef _Tp _Type; };
template <class _Tp> struct _UnQual<const volatile _Tp> { typedef _Tp _Type; };
#    endif
#  endif

/* This struct is intended to say if a pointer can be convertible to an other
 * taking into account cv qualifications. It shouldn't be instanciated with
 * something else than pointer type as it uses pass by value parameter that
 * results in compilation error when parameter type has a special memory
 * alignment
 */
template <class _Src, class _Dst>
struct _IsCVConvertible {
#  if !defined (__BORLANDC__) || (__BORLANDC__ >= 0x590)
  typedef _ConversionHelper<_Src, _Dst> _H;
  enum { value = (sizeof(char) == sizeof(_H::_Test(false, _H::_MakeSource()))) };
#  else
  enum { _Is1 = __type2bool<_IsConst<_Src>::_Ret>::_Ret };
  enum { _Is2 = _IsConvertible<_UnConstPtr<_Src>::_Type, _UnConstPtr<_Dst>::_Type>::value };
  enum { value = _Is1 ? 0 : _Is2 };
#  endif
  typedef typename __bool2type<value>::_Ret _Ret;
};

#else
template <class _Src, class _Dst>
struct _IsConvertible {
  enum { value = 0 };
  typedef __false_type _Ret;
};

template <class _Src, class _Dst>
struct _IsCVConvertible {
  enum { value = 0 };
  typedef __false_type _Ret;
};
#endif

_STLP_END_NAMESPACE

#endif /* _STLP_TYPE_MANIPS_H */