home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / type_traits.h < prev   
C/C++ Source or Header  |  2002-02-02  |  14KB  |  418 lines

  1. /*
  2.  *
  3.  * Copyright (c) 1996,1997
  4.  * Silicon Graphics Computer Systems, Inc.
  5.  *
  6.  * Copyright (c) 1997
  7.  * Moscow Center for SPARC Technology
  8.  *
  9.  * Copyright (c) 1999 
  10.  * Boris Fomitchev
  11.  *
  12.  * This material is provided "as is", with absolutely no warranty expressed
  13.  * or implied. Any use is at your own risk.
  14.  *
  15.  * Permission to use or copy this software for any purpose is hereby granted 
  16.  * without fee, provided the above notices are retained on all copies.
  17.  * Permission to modify the code and to distribute modified code is granted,
  18.  * provided the above notices are retained, and a notice that the code was
  19.  * modified is included with the above copyright notice.
  20.  *
  21.  */
  22.  
  23. #ifndef _STLP_TYPE_TRAITS_H
  24. #define _STLP_TYPE_TRAITS_H
  25.  
  26. /*
  27. This header file provides a framework for allowing compile time dispatch
  28. based on type attributes. This is useful when writing template code.
  29. For example, when making a copy of an array of an unknown type, it helps
  30. to know if the type has a trivial copy constructor or not, to help decide
  31. if a memcpy can be used.
  32.  
  33. The class template __type_traits provides a series of typedefs each of
  34. which is either __true_type or __false_type. The argument to
  35. __type_traits can be any type. The typedefs within this template will
  36. attain their correct values by one of these means:
  37.     1. The general instantiation contain conservative values which work
  38.        for all types.
  39.     2. Specializations may be declared to make distinctions between types.
  40.     3. Some compilers (such as the Silicon Graphics N32 and N64 compilers)
  41.        will automatically provide the appropriate specializations for all
  42.        types.
  43.  
  44. EXAMPLE:
  45.  
  46. //Copy an array of elements which have non-trivial copy constructors
  47. template <class T> void copy(T* source, T* destination, int n, __false_type);
  48. //Copy an array of elements which have trivial copy constructors. Use memcpy.
  49. template <class T> void copy(T* source, T* destination, int n, __true_type);
  50.  
  51. //Copy an array of any type by using the most efficient copy mechanism
  52. template <class T> inline void copy(T* source,T* destination,int n) {
  53.    copy(source, destination, n,
  54.         typename __type_traits<T>::has_trivial_copy_constructor());
  55. }
  56. */
  57.  
  58. #ifdef __WATCOMC__
  59. # include <cwchar>
  60. #endif
  61.  
  62. _STLP_BEGIN_NAMESPACE
  63.  
  64. struct __true_type {};
  65. struct __false_type {};
  66.  
  67.  
  68. template <int _Is> struct __bool2type {
  69.   typedef __false_type _Ret; 
  70. };
  71.  
  72. _STLP_TEMPLATE_NULL
  73. struct __bool2type<1> { typedef __true_type _Ret; };
  74.  
  75. _STLP_TEMPLATE_NULL
  76. struct __bool2type<0> { typedef __false_type _Ret; };
  77.  
  78. // logical end of 3 predicated
  79. template <class _P1, class _P2, class _P3>
  80. struct _Land3 {
  81.   typedef __false_type _Ret;
  82. };
  83.  
  84. _STLP_TEMPLATE_NULL
  85. struct _Land3<__true_type, __true_type, __true_type> {
  86.   typedef __true_type _Ret;
  87. };
  88.  
  89.  
  90. // Forward declarations.
  91. template <class _Tp> struct __type_traits; 
  92. template <int _IsPOD> struct __type_traits_aux {
  93.    typedef __false_type    has_trivial_default_constructor;
  94.    typedef __false_type    has_trivial_copy_constructor;
  95.    typedef __false_type    has_trivial_assignment_operator;
  96.    typedef __false_type    has_trivial_destructor;
  97.    typedef __false_type    is_POD_type;
  98. };
  99.  
  100. _STLP_TEMPLATE_NULL
  101. struct __type_traits_aux<0> {
  102.    typedef __false_type    has_trivial_default_constructor;
  103.    typedef __false_type    has_trivial_copy_constructor;
  104.    typedef __false_type    has_trivial_assignment_operator;
  105.    typedef __false_type    has_trivial_destructor;
  106.    typedef __false_type    is_POD_type;
  107. };
  108.  
  109. _STLP_TEMPLATE_NULL
  110. struct __type_traits_aux<1> { 
  111.    typedef __true_type    has_trivial_default_constructor;
  112.    typedef __true_type    has_trivial_copy_constructor;
  113.    typedef __true_type    has_trivial_assignment_operator;
  114.    typedef __true_type    has_trivial_destructor;
  115.    typedef __true_type    is_POD_type;
  116. };
  117.  
  118. # ifdef _STLP_SIMULATE_PARTIAL_SPEC_FOR_TYPE_TRAITS
  119.  
  120. // Boris : simulation technique is used here according to Adobe Open Source License Version 1.0.
  121. // Copyright 2000 Adobe Systems Incorporated and others. All rights reserved.
  122. // Authors: Mat Marcus and Jesse Jones
  123. // The original version of this source code may be found at
  124. // http://opensource.adobe.com.
  125.  
  126. struct _PointerShim {
  127.   // Since the compiler only allows at most one non-trivial
  128.   // implicit conversion we can make use of a shim class to
  129.   // be sure that IsPtr below doesn't accept classes with
  130.   // implicit pointer conversion operators
  131.   _PointerShim(const volatile void*); // no implementation
  132. };
  133.  
  134. // These are the discriminating functions
  135.  
  136. char _STLP_CALL _IsP(bool, _PointerShim); // no implementation is required
  137. char* _STLP_CALL _IsP(bool, ...);          // no implementation is required
  138.  
  139. template <class _Tp>
  140. char _STLP_CALL _IsSameFun(bool, _Tp*, _Tp*); // no implementation is required
  141. char* _STLP_CALL _IsSameFun(bool, ...);          // no implementation is required
  142.  
  143. template <class _Tp1, class _Tp2>
  144. struct _IsSame {
  145.   static _Tp1& __null_rep1();
  146.   static _Tp2& __null_rep2();
  147.   enum { _Ret = (sizeof(_IsSameFun(false,__null_rep1(),__null_rep2())) == sizeof(char)) };
  148. };
  149.  
  150. template <class _Tp>
  151. struct _IsPtr {
  152.   
  153.   // This template meta function takes a type T
  154.   // and returns true exactly when T is a pointer.
  155.   // One can imagine meta-functions discriminating on
  156.   // other criteria.
  157.   static _Tp& __null_rep();
  158.   enum { _Ret = (sizeof(_IsP(false,__null_rep())) == sizeof(char)) };
  159.  
  160. };
  161.  
  162. template <class _Tp>
  163. struct _IsPtrType {
  164.   enum { _Is =  _IsPtr<_Tp>::_Ret } ;
  165.   typedef __bool2type< _Is > _BT;
  166.   typedef typename _BT::_Ret _Type;
  167.   static _Type _Ret() { return _Type(); }
  168. };
  169.  
  170. template <class _Tp1, class _Tp2>
  171. struct _BothPtrType {
  172.   typedef __bool2type< _IsPtr<_Tp1>::_Ret> _B1;
  173.   typedef __bool2type< _IsPtr<_Tp2>::_Ret> _B2;
  174.   typedef typename _B1::_Ret _Type1;
  175.   typedef typename _B2::_Ret _Type2;
  176.   typedef typename _Land3<_Type1, _Type2, __true_type>::_Ret _Type;
  177.   static _Type _Ret() { return _Type(); }
  178. };
  179.  
  180. // we make general case dependant on the fact the type is actually a pointer.
  181.  
  182. template <class _Tp>
  183. struct __type_traits : __type_traits_aux<_IsPtr<_Tp>::_Ret> {};
  184.  
  185. # else
  186.  
  187. template <class _Tp>
  188. struct __type_traits { 
  189.    typedef __true_type     this_dummy_member_must_be_first;
  190.                    /* Do not remove this member. It informs a compiler which
  191.                       automatically specializes __type_traits that this
  192.                       __type_traits template is special. It just makes sure that
  193.                       things work if an implementation is using a template
  194.                       called __type_traits for something unrelated. */
  195.  
  196.    /* The following restrictions should be observed for the sake of
  197.       compilers which automatically produce type specific specializations 
  198.       of this class:
  199.           - You may reorder the members below if you wish
  200.           - You may remove any of the members below if you wish
  201.           - You must not rename members without making the corresponding
  202.             name change in the compiler
  203.           - Members you add will be treated like regular members unless
  204.             you add the appropriate support in the compiler. */
  205.    typedef __false_type    has_trivial_default_constructor;
  206.    typedef __false_type    has_trivial_copy_constructor;
  207.    typedef __false_type    has_trivial_assignment_operator;
  208.    typedef __false_type    has_trivial_destructor;
  209.    typedef __false_type    is_POD_type;
  210. };
  211.  
  212.  
  213. template <class _Tp>  struct _IsPtr { enum { _Ret = 0 }; };
  214. template <class _Tp>  struct _IsPtrType { 
  215.   static __false_type _Ret() { return __false_type();} 
  216. };
  217. template <class _Tp1, class _Tp2>  struct _BothPtrType { 
  218.   static __false_type _Ret() { return __false_type();} 
  219. };
  220.  
  221. template <class _Tp1, class _Tp2>
  222. struct _IsSame { enum { _Ret = 0 }; };
  223.  
  224. // template <class _Tp1, class _Tp2>
  225. // struct _IsSameType {   static __false_type _Ret() { return __false_type(); }  };
  226.  
  227. #  ifdef _STLP_CLASS_PARTIAL_SPECIALIZATION
  228. template <class _Tp>  struct _IsPtr<_Tp*> { enum { _Ret = 1 }; };
  229. template <class _Tp>  struct _IsPtrType<_Tp*> { 
  230.   static __true_type _Ret() { return __true_type();} 
  231. };
  232. template <class _Tp1, class _Tp2>  struct _BothPtrType<_Tp1*, _Tp2*> { 
  233.   static __true_type _Ret() { return __true_type();} 
  234. };
  235. template <class _Tp>
  236. struct _IsSame<_Tp, _Tp> { enum { _Ret = 1 }; };
  237. #  endif
  238.  
  239. # endif /* _STLP_SIMULATE_PARTIAL_SPEC_FOR_TYPE_TRAITS */
  240.  
  241. // Provide some specializations.  This is harmless for compilers that
  242. //  have built-in __types_traits support, and essential for compilers
  243. //  that don't.
  244. #ifndef _STLP_NO_BOOL
  245. _STLP_TEMPLATE_NULL struct __type_traits<bool> : __type_traits_aux<1> {};
  246. #endif /* _STLP_NO_BOOL */
  247. _STLP_TEMPLATE_NULL struct __type_traits<char> : __type_traits_aux<1> {};
  248. #ifndef _STLP_NO_SIGNED_BUILTINS
  249. _STLP_TEMPLATE_NULL struct __type_traits<signed char> : __type_traits_aux<1> {};
  250. # endif
  251. _STLP_TEMPLATE_NULL struct __type_traits<unsigned char> : __type_traits_aux<1> {};
  252. #if defined ( _STLP_HAS_WCHAR_T ) && ! defined (_STLP_WCHAR_T_IS_USHORT)
  253. _STLP_TEMPLATE_NULL struct __type_traits<wchar_t> : __type_traits_aux<1> {};
  254. #endif /* _STLP_HAS_WCHAR_T */
  255.  
  256. _STLP_TEMPLATE_NULL struct __type_traits<short> : __type_traits_aux<1> {};
  257. _STLP_TEMPLATE_NULL struct __type_traits<unsigned short> : __type_traits_aux<1> {};
  258. _STLP_TEMPLATE_NULL struct __type_traits<int> : __type_traits_aux<1> {};
  259. _STLP_TEMPLATE_NULL struct __type_traits<unsigned int> : __type_traits_aux<1> {};
  260. _STLP_TEMPLATE_NULL struct __type_traits<long> : __type_traits_aux<1> {};
  261. _STLP_TEMPLATE_NULL struct __type_traits<unsigned long> : __type_traits_aux<1> {};
  262.  
  263. #ifdef _STLP_LONG_LONG
  264. _STLP_TEMPLATE_NULL struct __type_traits<_STLP_LONG_LONG> : __type_traits_aux<1> {};
  265. _STLP_TEMPLATE_NULL struct __type_traits<unsigned _STLP_LONG_LONG> : __type_traits_aux<1> {};
  266. #endif /* _STLP_LONG_LONG */
  267.  
  268. _STLP_TEMPLATE_NULL struct __type_traits<float> : __type_traits_aux<1> {};
  269. _STLP_TEMPLATE_NULL struct __type_traits<double> : __type_traits_aux<1> {};
  270.  
  271. # if !defined ( _STLP_NO_LONG_DOUBLE )
  272. _STLP_TEMPLATE_NULL struct __type_traits<long double> : __type_traits_aux<1> {};
  273. # endif
  274.  
  275. #ifdef _STLP_CLASS_PARTIAL_SPECIALIZATION
  276. template <class _Tp> struct __type_traits<_Tp*> : __type_traits_aux<1> {};
  277. #endif
  278.  
  279. // The following could be written in terms of numeric_limits.  
  280. // We're doing it separately to reduce the number of dependencies.
  281.  
  282. template <class _Tp> struct _Is_integer {
  283.   typedef __false_type _Integral;
  284. };
  285.  
  286. #ifndef _STLP_NO_BOOL
  287.  
  288. _STLP_TEMPLATE_NULL struct _Is_integer<bool> {
  289.   typedef __true_type _Integral;
  290. };
  291.  
  292. #endif /* _STLP_NO_BOOL */
  293.  
  294. _STLP_TEMPLATE_NULL struct _Is_integer<char> {
  295.   typedef __true_type _Integral;
  296. };
  297.  
  298. #ifndef _STLP_NO_SIGNED_BUILTINS
  299.  
  300. _STLP_TEMPLATE_NULL struct _Is_integer<signed char> {
  301.   typedef __true_type _Integral;
  302. };
  303. #endif
  304.  
  305. _STLP_TEMPLATE_NULL struct _Is_integer<unsigned char> {
  306.   typedef __true_type _Integral;
  307. };
  308.  
  309. #if defined ( _STLP_HAS_WCHAR_T ) && ! defined (_STLP_WCHAR_T_IS_USHORT)
  310.  
  311. _STLP_TEMPLATE_NULL struct _Is_integer<wchar_t> {
  312.   typedef __true_type _Integral;
  313. };
  314.  
  315. #endif /* _STLP_HAS_WCHAR_T */
  316.  
  317. _STLP_TEMPLATE_NULL struct _Is_integer<short> {
  318.   typedef __true_type _Integral;
  319. };
  320.  
  321. _STLP_TEMPLATE_NULL struct _Is_integer<unsigned short> {
  322.   typedef __true_type _Integral;
  323. };
  324.  
  325. _STLP_TEMPLATE_NULL struct _Is_integer<int> {
  326.   typedef __true_type _Integral;
  327. };
  328.  
  329. _STLP_TEMPLATE_NULL struct _Is_integer<unsigned int> {
  330.   typedef __true_type _Integral;
  331. };
  332.  
  333. _STLP_TEMPLATE_NULL struct _Is_integer<long> {
  334.   typedef __true_type _Integral;
  335. };
  336.  
  337. _STLP_TEMPLATE_NULL struct _Is_integer<unsigned long> {
  338.   typedef __true_type _Integral;
  339. };
  340.  
  341. #ifdef _STLP_LONG_LONG
  342.  
  343. _STLP_TEMPLATE_NULL struct _Is_integer<_STLP_LONG_LONG> {
  344.   typedef __true_type _Integral;
  345. };
  346.  
  347. _STLP_TEMPLATE_NULL struct _Is_integer<unsigned _STLP_LONG_LONG> {
  348.   typedef __true_type _Integral;
  349. };
  350.  
  351. #endif /* _STLP_LONG_LONG */
  352.  
  353. template <class _Tp1, class _Tp2>
  354. struct _OKToMemCpy {
  355.   enum { _Same = _IsSame<_Tp1,_Tp2>::_Ret } ;
  356.   typedef typename __type_traits<_Tp1>::has_trivial_assignment_operator _Tr1;
  357.   typedef typename __type_traits<_Tp2>::has_trivial_assignment_operator _Tr2;
  358.   typedef typename __bool2type< _Same >::_Ret _Tr3;
  359.   typedef typename _Land3<_Tr1, _Tr2, _Tr3>::_Ret _Type;
  360.   static _Type _Ret() { return _Type(); }
  361. };
  362.  
  363. template <class _Tp1, class _Tp2>
  364. inline _OKToMemCpy<_Tp1, _Tp2> _IsOKToMemCpy(_Tp1*, _Tp2*)  {
  365.   return _OKToMemCpy<_Tp1, _Tp2>();
  366. }
  367.  
  368. template <class _Tp> 
  369. struct _IsPOD {
  370.   typedef typename __type_traits<_Tp>::is_POD_type _Type;
  371.   static _Type _Ret() { return _Type(); }
  372. };
  373.  
  374. template <class _Tp> 
  375. inline _IsPOD<_Tp>  _Is_POD (_Tp*) { return _IsPOD<_Tp>(); } 
  376.  
  377. #  ifdef _STLP_CLASS_PARTIAL_SPECIALIZATION
  378. #   if defined (__BORLANDC__) || defined (__SUNPRO_CC) || ( defined (__MWERKS__) && (__MWERKS__ <= 0x2303)) || ( defined (__sgi) && defined (_COMPILER_VERSION))
  379. #   define _IS_POD_ITER(_It, _Tp) __type_traits< typename iterator_traits< _Tp >::value_type >::is_POD_type()
  380. #   else
  381. #   define _IS_POD_ITER(_It, _Tp) typename __type_traits< typename iterator_traits< _Tp >::value_type >::is_POD_type()
  382. #   endif
  383. #  else
  384. #   define _IS_POD_ITER(_It, _Tp) _Is_POD( _STLP_VALUE_TYPE( _It, _Tp ) )._Ret()
  385. #  endif
  386.  
  387. # ifdef _STLP_DEFAULT_CONSTRUCTOR_BUG
  388. // Those adaptors are here to fix common compiler bug regarding builtins:
  389. // expressions like int k = int() should initialize k to 0
  390. template <class _Tp>
  391. inline _Tp __default_constructed_aux(_Tp*, const __false_type&) {
  392.   return _Tp();
  393. }
  394. template <class _Tp>
  395. inline _Tp __default_constructed_aux(_Tp*, const __true_type&) {
  396.   return _Tp(0);
  397. }
  398.  
  399. template <class _Tp>
  400. inline _Tp __default_constructed(_Tp* __p) {
  401.   typedef typename _Is_integer<_Tp>::_Integral _Is_Integral;
  402.   return __default_constructed_aux(__p, _Is_Integral());
  403. }
  404.  
  405. #  define _STLP_DEFAULT_CONSTRUCTED(_TTp) __default_constructed((_TTp*)0)
  406. # else
  407. #  define _STLP_DEFAULT_CONSTRUCTED(_TTp) _TTp()
  408. # endif
  409.  
  410. _STLP_END_NAMESPACE
  411.  
  412. #endif /* __TYPE_TRAITS_H */
  413.  
  414. // Local Variables:
  415. // mode:C++
  416. // End:
  417.  
  418.