aboutsummaryrefslogtreecommitdiff
path: root/stlport/type_traits
blob: fb9004d5cf9d8b0781cb2fd7db14d0cc384bd8f3 (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
// -*- C++ -*- Time-stamp: <08/07/20 19:10:15 ptr>

/*
 * Copyright (c) 2007, 2008
 * Petr Ovtchenkov
 *
 * Licensed under the Academic Free License version 3.0
 *
 * Derived from original <misc/type_traits.h> of 'complement' project
 * [http://complement.sourceforge.net]
 * to make it close to JTC1/SC22/WG21 C++ 0x working draft
 * [http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2008/n2521.pdf]
 */

#ifndef __STLP_TYPE_TRAITS
#define __STLP_TYPE_TRAITS

#ifndef _STLP_OUTERMOST_HEADER_ID
#  define _STLP_OUTERMOST_HEADER_ID 0x3
#  include <stl/_prolog.h>
#endif

// libstdc++ v3, timestamp 20050519 (3.4.4) has __type_traits,
// libstdc++ v3, timestamp 20060306 (3.4.6) has __type_traits,
// while libstdc++ v3, 20050921 (4.0.2) not; use libstdc++ instead
// # if defined(STLPORT) || (defined(__GNUC__) && (__GNUC__ < 4)) /* !defined(__GLIBCXX__) || (defined(__GNUC__) && (__GNUC__ < 4)) */

_STLP_BEGIN_NAMESPACE

_STLP_BEGIN_TR1_NAMESPACE

namespace detail {

struct __select_types
{
    typedef char __t1;
    struct __t2
    {
        char __two[2];
    };
};

template <class _Tp>
struct __instance :
    public __select_types
{
  private:
    template <class _Up>
    static __t1 __test(_Up(*)[1]);

    template <class>
    static __t2 __test(...);
    
  public:
#ifdef _STLP_STATIC_CONST_INIT_BUG
    static const bool __value;
#else
    static const bool __value = sizeof(__test<_Tp>(0)) == sizeof(__select_types::__t1);
#endif

};

#ifdef _STLP_STATIC_CONST_INIT_BUG
template <class _Tp>
const bool __instance<_Tp>::__value = sizeof(__instance<_Tp>::__test<_Tp>(0)) == sizeof(__select_types::__t1);
#endif

template <class T>
struct __uoc_aux : // union or class
    public __select_types
{
  private:
    template <class _Up>
    static __t1 __test( int _Up::* );

    template <class>
    static __t2 __test(...);
    
  public:
#ifdef __FIT_NO_INLINE_TEMPLATE_STATIC_INITIALISATION
    static const bool __value;
#else
    static const bool __value = sizeof(__test<T>(0)) == sizeof(__select_types::__t1);
#endif
};

#ifdef __FIT_NO_INLINE_TEMPLATE_STATIC_INITIALISATION
template <class T>
const bool __uoc_aux<T>::__value = sizeof(__uoc_aux<T>::__test<T>(0)) == sizeof(__select_types::__t1);
#endif

template <class T>
class __empty
{ };

template <class T, bool B>
class __inheritance_aux
{};

template <class T>
class __inheritance_aux<T,true> :
    public T
{
  public:
    virtual ~__inheritance_aux()
      { }
};

#if 0
template <class T, bool B>
struct __virtual_aux
{
  public:
#ifdef _STLP_STATIC_CONST_INIT_BUG
    static const bool __value;
#else
    static const bool __value = B ? (sizeof(__inheritance_aux<T,B>) == sizeof(T)) : false;
#endif
};

#ifdef _STLP_STATIC_CONST_INIT_BUG
template <class T, bool B>
const bool __virtual_aux<T,B>::__value = B ? (sizeof(__inheritance_aux<T,B>) == sizeof(T)) : false;
#endif
#endif

} // namespace detail

template <class _Tp, _Tp __v>
struct integral_constant
{
    static const _Tp                    value = __v;
    // enum { value = __v }; ?

    typedef _Tp                         value_type;
    typedef integral_constant<_Tp, __v> type;
};

typedef integral_constant<bool, true>   true_type;
typedef integral_constant<bool, false>  false_type;

namespace detail {

template <typename _Tp>
struct __is_union_or_class :
    public integral_constant<bool, __uoc_aux<_Tp>::__value>
{ };

#if 0
template<typename _Tp>
struct __is_vtbl : // has virtual table?
    public integral_constant<bool, __virtual_aux<_Tp,__is_union_or_class<_Tp>::value >::__value>
{ };
#endif

template <typename _Tp>
struct __is_vtbl : // has virtual table?
    public integral_constant<bool, __is_union_or_class<_Tp>::value ? (sizeof(__inheritance_aux<_Tp,__is_union_or_class<_Tp>::value>) == sizeof(_Tp)) : false >
{ };

} // namespace detail

#define  __SPEC_(C,T,B)               \
template <>                           \
struct C<T> :                         \
    public integral_constant<bool, B> \
{ }

#define __CV_SPEC(C,T,B) \
__SPEC_(C,T,B);            \
__SPEC_(C,const T,B);      \
__SPEC_(C,volatile T,B);   \
__SPEC_(C,const volatile T,B)

#define  __SPEC_1(C,T,B)              \
template <class _Tp>                  \
struct C<T> :                         \
    public integral_constant<bool, B> \
{ }

#define __CV_SPEC_1(C,T,B) \
__SPEC_1(C,T,B);            \
__SPEC_1(C,T const,B);      \
__SPEC_1(C,T volatile,B);   \
__SPEC_1(C,T const volatile,B)

#define  __SPEC_2(C,T,B)              \
template <class _Tp1, class _Tp2>     \
struct C<T> :                         \
    public integral_constant<bool, B> \
{ }

#define __CV_SPEC_2(C,T,B) \
__SPEC_2(C,T,B);            \
__SPEC_2(C,T const,B);      \
__SPEC_2(C,T volatile,B);   \
__SPEC_2(C,T const volatile,B)

// [4.5.1] primary type categories:

template <class _Tp>
struct is_void :
    public false_type
{ };

template <>
struct is_void<void> :
    public true_type
{ };

template <class _Tp>
struct is_integral :
    public false_type
{ };

__CV_SPEC(is_integral,bool,true);
__CV_SPEC(is_integral,char,true);
__CV_SPEC(is_integral,signed char,true);
__CV_SPEC(is_integral,unsigned char,true);
__CV_SPEC(is_integral,wchar_t,true);
__CV_SPEC(is_integral,short,true);
__CV_SPEC(is_integral,unsigned short,true);
__CV_SPEC(is_integral,int,true);
__CV_SPEC(is_integral,unsigned int,true);
__CV_SPEC(is_integral,long,true);
__CV_SPEC(is_integral,unsigned long,true);
__CV_SPEC(is_integral,long long,true);
__CV_SPEC(is_integral,unsigned long long,true);

template <class _Tp>
struct is_floating_point :
    public false_type
{ };

__CV_SPEC(is_floating_point,float,true);
__CV_SPEC(is_floating_point,double,true);
__CV_SPEC(is_floating_point,long double,true);

template <class _Tp>
struct is_array :
    public false_type
{ };

template <class _Tp, std::size_t _Sz>
struct is_array<_Tp[_Sz]> :
    public true_type
{ };

template <class _Tp>
struct is_array<_Tp[]> :
    public true_type
{ };

template <class _Tp>
struct is_pointer :
    public false_type
{ };

__CV_SPEC_1(is_pointer,_Tp *,true);

template <class _Tp>
struct is_lvalue_reference :
    public false_type
{ };

template <class _Tp>
struct is_lvalue_reference<_Tp&> :
    public true_type
{ };

template <class _Tp>
struct is_rvalue_reference :
    public false_type
{ };

// template <class _Tp>
// struct is_rvalue_reference<_Tp&&> :
//     public true_type
// { };

template <class _Tp>
struct is_reference :
    public false_type
{ };

template <class _Tp>
struct is_reference<_Tp&> :
    public true_type
{ };

template <class _Tp>
struct is_function :
    public integral_constant<bool, !(detail::__instance<_Tp>::__value
                                     || detail::__is_union_or_class<_Tp>::value
                                     || is_reference<_Tp>::value
                                     || is_void<_Tp>::value)>
{ };

template <class _Tp>
struct is_member_object_pointer :
    public false_type
{ };

// _SPEC_FULL2(is_member_object_pointer, _Tp1 _Tp2::*,!is_function<_Tp1>::value);

template <class _Tp1, class _Tp2>
struct is_member_object_pointer<_Tp1 _Tp2::*> :
    public integral_constant<bool, !is_function<_Tp1>::value>
{ };

template <class _Tp1, class _Tp2>
struct is_member_object_pointer<_Tp1 _Tp2::* const> :
    public integral_constant<bool, !is_function<_Tp1>::value>
{ };

template <class _Tp1, class _Tp2>
struct is_member_object_pointer<_Tp1 _Tp2::* volatile> :
    public integral_constant<bool, !is_function<_Tp1>::value>
{ };

template <class _Tp1, class _Tp2>
struct is_member_object_pointer<_Tp1 _Tp2::* const volatile> :
    public integral_constant<bool, !is_function<_Tp1>::value>
{ };

template <class _Tp>
struct is_member_function_pointer :
    public false_type
{ };

// _SPEC_FULL2(is_member_function_pointer,_Tp1 _Tp2::*,is_function<_Tp1>::value);

template <class _Tp1, class _Tp2>
struct is_member_function_pointer<_Tp1 _Tp2::*> :                         
    public integral_constant<bool, is_function<_Tp1>::value> 
{ };

template <class _Tp1, class _Tp2>
struct is_member_function_pointer<_Tp1 _Tp2::* const> :
    public integral_constant<bool, is_function<_Tp1>::value>
{ };

template <class _Tp1, class _Tp2>
struct is_member_function_pointer<_Tp1 _Tp2::* volatile> :
    public integral_constant<bool, is_function<_Tp1>::value>
{ };

template <class _Tp1, class _Tp2>
struct is_member_function_pointer<_Tp1 _Tp2::* const volatile> :
    public integral_constant<bool, is_function<_Tp1>::value>
{ };

template <class _Tp>
struct is_member_pointer :
    public integral_constant<bool, (is_member_object_pointer<_Tp>::value || is_member_function_pointer<_Tp>::value)>
{ };

// 4.5.2 composite type categories

template <class _Tp>
struct is_arithmetic :
    public integral_constant<bool, (is_integral<_Tp>::value || is_floating_point<_Tp>::value)>
{ };

template <class _Tp>
struct is_fundamental :
    public integral_constant<bool, (is_arithmetic<_Tp>::value || is_void<_Tp>::value)>
{ };

// [4.5.1] primary type categories (continued):

template <class _Tp>
struct is_enum :
    public integral_constant<bool, !(is_fundamental<_Tp>::value
                                     || is_array<_Tp>::value
                                     || is_pointer<_Tp>::value
                                     || is_reference<_Tp>::value
                                     || is_member_pointer<_Tp>::value
                                     || is_function<_Tp>::value
                                     || detail::__is_union_or_class<_Tp>::value) >
{ };

template <class T>
struct is_union
{ };

template <class T>
struct is_class
{ };

// is_function (above)

// 4.5.2 composite type categories (continued)

// is_arithmetic (above)
// is_fundamental (above)

template <class _Tp>
struct is_object :
    public integral_constant<bool, (is_arithmetic<_Tp>::value ||
                                    is_array<_Tp>::value ||
                                    is_pointer<_Tp>::value ||
                                    is_member_pointer<_Tp>::value ||
                                    detail::__is_union_or_class<_Tp>::value)>
{ };

template <class _Tp>
struct is_scalar :
    public integral_constant<bool, (is_arithmetic<_Tp>::value
                                    || is_enum<_Tp>::value
                                    || is_pointer<_Tp>::value
                                    || is_member_pointer<_Tp>::value)>
{ };

template <class _Tp>
struct is_compound :
    public integral_constant<bool, !is_fundamental<_Tp>::value>
{ };

// is_member_pointer

// 4.5.3 type properties:

template <class _Tp>
struct is_const :
    public false_type
{ };

template <class _Tp>
struct is_const<_Tp const> :
    public true_type
{ };

template <class _Tp>
struct is_volatile :
    public false_type
{ };

template <class _Tp>
struct is_volatile<_Tp volatile> :
    public true_type
{ };


// 4.7.3 array modifications:

template <class _Tp>
struct remove_extent
{
    typedef _Tp type;
};

template <class _Tp, std::size_t _Sz>
struct remove_extent<_Tp[_Sz]>
{
    typedef _Tp type;
};

template <class _Tp>
struct remove_extent<_Tp[]>
{
    typedef _Tp type;
};

template <class _Tp>
struct remove_all_extents
{
    typedef _Tp type;
};

template <class _Tp, std::size_t _Size>
struct remove_all_extents<_Tp[_Size]>
{
    typedef typename remove_all_extents<_Tp>::type type;
};

template<typename _Tp>
struct remove_all_extents<_Tp[]>
{
    typedef typename remove_all_extents<_Tp>::type type;
};

// 4.5.3 type properties (continued):

template <class _Tp>
struct is_trivial :
    public integral_constant<bool, (is_void<_Tp>::value
                                    || is_scalar<typename remove_all_extents<_Tp>::type>::value)>
{ };

template <class _Tp>
struct is_standard_layout :
    public integral_constant<bool, (is_void<_Tp>::value
                                    || is_scalar<typename remove_all_extents<_Tp>::type>::value)>
{ };

template <class _Tp>
struct is_pod :
    public integral_constant<bool, (is_void<_Tp>::value
                                    || is_scalar<typename remove_all_extents<_Tp>::type>::value)>
{ };

template<typename _Tp>
struct is_empty
    : public integral_constant<bool, (detail::__is_union_or_class<_Tp>::value
                                      && (sizeof(detail::__empty<_Tp>) == sizeof(_Tp)))>
{ };

// is_polimorphic
// is_abstract

template <class _Tp>
struct has_trivial_constructor :
    public integral_constant<bool, is_pod<_Tp>::value>
{ };

template <class _Tp>
struct has_trivial_copy :
    public integral_constant<bool, is_pod<_Tp>::value>
{ };

template <class _Tp>
struct has_trivial_assign :
    public integral_constant<bool, is_pod<_Tp>::value>
{ };

template <class _Tp>
struct has_trivial_destructor :
    public integral_constant<bool, is_pod<_Tp>::value>
{ };

template <class _Tp>
struct has_nothrow_constructor :
    public integral_constant<bool, is_pod<_Tp>::value>
{ };

template <class _Tp>
struct has_nothrow_copy :
    public integral_constant<bool, is_pod<_Tp>::value>
{ };

template <class _Tp>
struct has_nothrow_assign :
    public integral_constant<bool, is_pod<_Tp>::value>
{ };

template <class _Tp>
struct has_virtual_destructor :
    public false_type
{ };

template <class _Tp>
struct is_signed :
    public false_type
{ };

__CV_SPEC(is_signed,signed char,true);
__CV_SPEC(is_signed,short,true);
__CV_SPEC(is_signed,int,true);
__CV_SPEC(is_signed,long,true);
__CV_SPEC(is_signed,long long,true);

template <class _Tp>
struct is_unsigned :
    public false_type
{ };

__CV_SPEC(is_unsigned,unsigned char,true);
__CV_SPEC(is_unsigned,unsigned short,true);
__CV_SPEC(is_unsigned,unsigned int,true);
__CV_SPEC(is_unsigned,unsigned long,true);
__CV_SPEC(is_unsigned,unsigned long long,true);

// alignment_of
// rank
// extent

// 4.6 type relations:

template <class _Tp1, class _Tp2>
struct is_same :
    public false_type
{ };

template <class _Tp>
struct is_same<_Tp, _Tp> :
    public true_type
{ };

// is_base_of
// is_convertible

// 4.7.1 const-volatile modifications

template <class _Tp>
struct remove_const
{
    typedef _Tp type;
};

template <class _Tp>
struct remove_const<_Tp const>
{
    typedef _Tp type;
};
  
template <class _Tp>
struct remove_volatile
{
    typedef _Tp type;
};

template <class _Tp>
struct remove_volatile<_Tp volatile>
{
    typedef _Tp type;
};

template <class _Tp>
struct remove_cv
{
    typedef typename remove_const<typename remove_volatile<_Tp>::type>::type type;
};
  
template <class _Tp>
struct add_const
{
    typedef _Tp const type;
};
   
template <class _Tp>
struct add_volatile
{
    typedef _Tp volatile type;
};
  
template <class _Tp>
struct add_cv
{
    typedef typename add_const<typename add_volatile<_Tp>::type>::type type;
};

// 4.7.2 reference modifications:

template <class _Tp>
struct remove_reference
{
    typedef _Tp type;
};

template <class _Tp>
struct remove_reference<_Tp&>
{
    typedef _Tp type;
};

#if __cplusplus >= 201103L
// This shouldn't be in tr1 but was added here anyways since the existing remove_reference is in
// tr1.
template <class _Tp>
struct remove_reference<_Tp&&>
{
    typedef _Tp type;
};
#endif

template <class _Tp>
struct add_reference
{
    typedef _Tp& type;
};

template <class _Tp>
struct add_reference<_Tp&>
{
    typedef _Tp& type;
};

// 4.7.3 array modifications (see above)

// 4.7.4 pointer modifications:

template <class _Tp>
struct remove_pointer
{
    typedef _Tp type;
};

template <class _Tp>
struct remove_pointer<_Tp *>
{
    typedef _Tp type;
};

template <class _Tp>
struct remove_pointer<_Tp * const>
{
    typedef _Tp type;
};

template <class _Tp>
struct remove_pointer<_Tp * volatile>
{
    typedef _Tp type;
};

template <class _Tp>
struct remove_pointer<_Tp * const volatile>
{
    typedef _Tp type;
};

template <class _Tp>
struct add_pointer
{
    typedef typename remove_reference<_Tp>::type * type;
};

// 20.5.7 other transformations:

// template <std::size_t Len, std::size_t Align> struct aligned_storage;
// template <std::size_t Len, class... Types> struct aligned_union;

namespace detail {

template <bool,class _U>
struct _decay_aux2
{
    typedef typename remove_cv<_U>::type type;
};

template <class _U>
struct _decay_aux2<true,_U>
{
    typedef typename add_pointer<_U>::type type;
};

template <bool, class _U>
struct _decay_aux1
{
    typedef typename _decay_aux2<is_function<_U>::value,_U>::type type; 
};

template <class _U>
struct _decay_aux1<true,_U>
{
    typedef typename remove_extent<_U>::type* type;
};

} // namespace detail

template <class _Tp>
class decay
{
  private:
    typedef typename remove_reference<_Tp>::type _U;

  public:
    typedef typename detail::_decay_aux1<is_array<_U>::value,_U>::type type;
};

template <bool, class _Tp = void>
struct enable_if
{
};

template <class _Tp>
struct enable_if<true,_Tp>
{
    typedef _Tp type;
};

template <bool, class _Tp1, class _Tp2>
struct conditional
{
    typedef _Tp2 type;
};

template <class _Tp1, class _Tp2>
struct conditional<true,_Tp1,_Tp2>
{
    typedef _Tp1 type;
};

// template <class... _Tp> struct common_type;

#undef __CV_SPEC
#undef __SPEC_
#undef __CV_SPEC_1
#undef __SPEC_1
#undef __CV_SPEC_2
#undef __SPEC_2

_STLP_END_NAMESPACE // tr1

_STLP_END_NAMESPACE

// # else // __GLIBCXX__ && (__GNUC__ >= 4) && !STLPORT
// #  include <tr1/type_traits>
// # endif

#if (_STLP_OUTERMOST_HEADER_ID == 0x3)
#  include <stl/_epilog.h>
#  undef _STLP_OUTERMOST_HEADER_ID
#endif

#endif // __STLP_TYPE_TRAITS