type_manips.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. *
  3. * Copyright (c) 2003
  4. * Francois Dumont
  5. *
  6. * This material is provided "as is", with absolutely no warranty expressed
  7. * or implied. Any use is at your own risk.
  8. *
  9. * Permission to use or copy this software for any purpose is hereby granted
  10. * without fee, provided the above notices are retained on all copies.
  11. * Permission to modify the code and to distribute modified code is granted,
  12. * provided the above notices are retained, and a notice that the code was
  13. * modified is included with the above copyright notice.
  14. *
  15. */
  16. #ifndef _STLP_TYPE_MANIPS_H
  17. #define _STLP_TYPE_MANIPS_H
  18. _STLP_BEGIN_NAMESPACE
  19. struct __true_type {};
  20. struct __false_type {};
  21. #if defined (_STLP_USE_NAMESPACES) && !defined (_STLP_DONT_USE_PRIV_NAMESPACE)
  22. _STLP_MOVE_TO_PRIV_NAMESPACE
  23. using _STLP_STD::__true_type;
  24. using _STLP_STD::__false_type;
  25. _STLP_MOVE_TO_STD_NAMESPACE
  26. #endif
  27. //bool to type
  28. template <int _Is>
  29. struct __bool2type
  30. { typedef __true_type _Ret; };
  31. _STLP_TEMPLATE_NULL
  32. struct __bool2type<1> { typedef __true_type _Ret; };
  33. _STLP_TEMPLATE_NULL
  34. struct __bool2type<0> { typedef __false_type _Ret; };
  35. //type to bool
  36. template <class __bool_type>
  37. struct __type2bool { enum {_Ret = 1}; };
  38. _STLP_TEMPLATE_NULL
  39. struct __type2bool<__true_type> { enum {_Ret = 1}; };
  40. _STLP_TEMPLATE_NULL
  41. struct __type2bool<__false_type> { enum {_Ret = 0}; };
  42. //Negation
  43. template <class _BoolType>
  44. struct _Not { typedef __false_type _Ret; };
  45. _STLP_TEMPLATE_NULL
  46. struct _Not<__false_type> { typedef __true_type _Ret; };
  47. // logical and of 2 predicated
  48. template <class _P1, class _P2>
  49. struct _Land2 { typedef __false_type _Ret; };
  50. _STLP_TEMPLATE_NULL
  51. struct _Land2<__true_type, __true_type> { typedef __true_type _Ret; };
  52. // logical and of 3 predicated
  53. template <class _P1, class _P2, class _P3>
  54. struct _Land3 { typedef __false_type _Ret; };
  55. _STLP_TEMPLATE_NULL
  56. struct _Land3<__true_type, __true_type, __true_type> { typedef __true_type _Ret; };
  57. //logical or of 2 predicated
  58. template <class _P1, class _P2>
  59. struct _Lor2 { typedef __true_type _Ret; };
  60. _STLP_TEMPLATE_NULL
  61. struct _Lor2<__false_type, __false_type> { typedef __false_type _Ret; };
  62. // logical or of 3 predicated
  63. template <class _P1, class _P2, class _P3>
  64. struct _Lor3 { typedef __true_type _Ret; };
  65. _STLP_TEMPLATE_NULL
  66. struct _Lor3<__false_type, __false_type, __false_type> { typedef __false_type _Ret; };
  67. ////////////////////////////////////////////////////////////////////////////////
  68. // class template __select
  69. // Selects one of two types based upon a boolean constant
  70. // Invocation: __select<_Cond, T, U>::Result
  71. // where:
  72. // flag is a compile-time boolean constant
  73. // T and U are types
  74. // Result evaluates to T if flag is true, and to U otherwise.
  75. ////////////////////////////////////////////////////////////////////////////////
  76. // BEWARE: If the compiler do not support partial template specialization or nested template
  77. //classes the default behavior of the __select is to consider the condition as false and so return
  78. //the second template type!!
  79. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  80. # if defined (__BORLANDC__)
  81. template <class _CondT, class _Tp1, class _Tp2>
  82. struct __selectT { typedef _Tp1 _Ret; };
  83. template <class _Tp1, class _Tp2>
  84. struct __selectT<__false_type, _Tp1, _Tp2> { typedef _Tp2 _Ret; };
  85. # endif
  86. # if !defined (__BORLANDC__) || (__BORLANDC__ >= 0x590)
  87. template <bool _Cond, class _Tp1, class _Tp2>
  88. struct __select { typedef _Tp1 _Ret; };
  89. template <class _Tp1, class _Tp2>
  90. struct __select<false, _Tp1, _Tp2> { typedef _Tp2 _Ret; };
  91. # else
  92. template <bool _Cond, class _Tp1, class _Tp2>
  93. struct __select
  94. { typedef __selectT<typename __bool2type<_Cond>::_Ret, _Tp1, _Tp2>::_Ret _Ret; };
  95. # endif
  96. #else
  97. # if defined (_STLP_MEMBER_TEMPLATE_CLASSES)
  98. template <int _Cond>
  99. struct __select_aux {
  100. template <class _Tp1, class _Tp2>
  101. struct _In {
  102. typedef _Tp1 _Ret;
  103. };
  104. };
  105. _STLP_TEMPLATE_NULL
  106. struct __select_aux<0> {
  107. template <class _Tp1, class _Tp2>
  108. struct _In {
  109. typedef _Tp2 _Ret;
  110. };
  111. };
  112. template <int _Cond, class _Tp1, class _Tp2>
  113. struct __select {
  114. typedef typename __select_aux<_Cond>::_STLP_TEMPLATE _In<_Tp1, _Tp2>::_Ret _Ret;
  115. };
  116. # else /* _STLP_MEMBER_TEMPLATE_CLASSES */
  117. //default behavior
  118. template <int _Cond, class _Tp1, class _Tp2>
  119. struct __select {
  120. typedef _Tp2 _Ret;
  121. };
  122. # endif /* _STLP_MEMBER_TEMPLATE_CLASSES */
  123. #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
  124. /* Rather than introducing a new macro for the following constrution we use
  125. * an existing one (_STLP_DONT_SIMULATE_PARTIAL_SPEC_FOR_TYPE_TRAITS) that
  126. * is used for a similar feature.
  127. */
  128. #if !defined (_STLP_DONT_SIMULATE_PARTIAL_SPEC_FOR_TYPE_TRAITS) && \
  129. (!defined (__GNUC__) || (__GNUC__ > 2))
  130. // Helper struct that will forbid volatile qualified types:
  131. # if !defined (__BORLANDC__)
  132. struct _NoVolatilePointerShim { _NoVolatilePointerShim(const void*); };
  133. template <class _Tp>
  134. char _STLP_CALL _IsCopyableFun(bool, _NoVolatilePointerShim, _Tp const*, _Tp*); // no implementation is required
  135. char* _STLP_CALL _IsCopyableFun(bool, ...); // no implementation is required
  136. template <class _Src, class _Dst>
  137. struct _Copyable {
  138. static _Src* __null_src();
  139. static _Dst* __null_dst();
  140. enum { _Ret = (sizeof(_IsCopyableFun(false, __null_src(), __null_src(), __null_dst())) == sizeof(char)) };
  141. typedef typename __bool2type<_Ret>::_Ret _RetT;
  142. };
  143. # else
  144. template <class _Tp1, class _Tp2> struct _AreSameTypes;
  145. template <class _Tp> struct _IsUnQual;
  146. template <class _Src, class _Dst>
  147. struct _Copyable {
  148. typedef typename _AreSameTypes<_Src, _Dst>::_Ret _Tr1;
  149. typedef typename _IsUnQual<_Dst>::_Ret _Tr2;
  150. typedef typename _Land2<_Tr1, _Tr2>::_Ret _RetT;
  151. enum { _Ret = __type2bool<_RetT>::_Ret };
  152. };
  153. # endif
  154. #else
  155. template <class _Src, class _Dst>
  156. struct _Copyable {
  157. enum { _Ret = 0 };
  158. typedef __false_type _RetT;
  159. };
  160. #endif
  161. /*
  162. * The following struct will tell you if 2 types are the same and if copying memory
  163. * from the _Src type to the _Dst type is right considering qualifiers. If _Src and
  164. * _Dst types are the same unqualified types _Ret will be false if:
  165. * - any of the type has the volatile qualifier
  166. * - _Dst is const qualified
  167. */
  168. template <class _Src, class _Dst>
  169. struct _AreCopyable {
  170. enum { _Same = _Copyable<_Src, _Dst>::_Ret };
  171. typedef typename _Copyable<_Src, _Dst>::_RetT _Ret;
  172. };
  173. template <class _Tp1, class _Tp2>
  174. struct _AreSameTypes {
  175. enum { _Same = 0 };
  176. typedef __false_type _Ret;
  177. };
  178. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  179. template <class _Tp>
  180. struct _AreSameTypes<_Tp, _Tp> {
  181. enum { _Same = 1 };
  182. typedef __true_type _Ret;
  183. };
  184. #endif
  185. #if !defined (_STLP_DONT_SIMULATE_PARTIAL_SPEC_FOR_TYPE_TRAITS)
  186. template <class _Src, class _Dst>
  187. struct _ConversionHelper {
  188. static char _Test(bool, _Dst);
  189. static char* _Test(bool, ...);
  190. static _Src _MakeSource();
  191. };
  192. template <class _Src, class _Dst>
  193. struct _IsConvertible {
  194. typedef _ConversionHelper<_Src*, const volatile _Dst*> _H;
  195. enum { value = (sizeof(char) == sizeof(_H::_Test(false, _H::_MakeSource()))) };
  196. typedef typename __bool2type<value>::_Ret _Ret;
  197. };
  198. # if defined (__BORLANDC__)
  199. # if (__BORLANDC__ < 0x590)
  200. template<class _Tp>
  201. struct _UnConstPtr { typedef _Tp _Type; };
  202. template<class _Tp>
  203. struct _UnConstPtr<_Tp*> { typedef _Tp _Type; };
  204. template<class _Tp>
  205. struct _UnConstPtr<const _Tp*> { typedef _Tp _Type; };
  206. # endif
  207. # if !defined (_STLP_QUALIFIED_SPECIALIZATION_BUG)
  208. template <class _Tp>
  209. struct _IsConst { typedef __false_type _Ret; };
  210. # else
  211. template <class _Tp>
  212. struct _IsConst { typedef _AreSameTypes<_Tp, const _Tp>::_Ret _Ret; };
  213. # endif
  214. # if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && !defined (_STLP_QUALIFIED_SPECIALIZATION_BUG)
  215. template <class _Tp>
  216. struct _IsConst <const _Tp> { typedef __true_type _Ret; };
  217. # endif
  218. # if (__BORLANDC__ < 0x590)
  219. template<class _Tp>
  220. struct _IsConst<_Tp*> { typedef _AreSameTypes<_Tp*, const _Tp*>::_Ret _Ret; };
  221. # endif
  222. template <class _Tp>
  223. struct _IsVolatile { typedef _AreSameTypes<_Tp, volatile _Tp>::_Ret _Ret; };
  224. template<class _Tp>
  225. struct _IsUnQual {
  226. typedef _IsConst<_Tp>::_Ret _Tr1;
  227. typedef _IsVolatile<_Tp>::_Ret _Tr2;
  228. typedef _Not<_Tr1>::_Ret _NotCon;
  229. typedef _Not<_Tr2>::_Ret _NotVol;
  230. typedef _Land2<_NotCon, _NotVol>::_Ret _Ret;
  231. };
  232. # if !defined (_STLP_QUALIFIED_SPECIALIZATION_BUG)
  233. template <class _Tp> struct _UnQual { typedef _Tp _Type; };
  234. template <class _Tp> struct _UnQual<const _Tp> { typedef _Tp _Type; };
  235. template <class _Tp> struct _UnQual<volatile _Tp> { typedef _Tp _Type; };
  236. template <class _Tp> struct _UnQual<const volatile _Tp> { typedef _Tp _Type; };
  237. # endif
  238. # endif
  239. /* This struct is intended to say if a pointer can be convertible to an other
  240. * taking into account cv qualifications. It shouldn't be instanciated with
  241. * something else than pointer type as it uses pass by value parameter that
  242. * results in compilation error when parameter type has a special memory
  243. * alignment
  244. */
  245. template <class _Src, class _Dst>
  246. struct _IsCVConvertible {
  247. # if !defined (__BORLANDC__) || (__BORLANDC__ >= 0x590)
  248. typedef _ConversionHelper<_Src, _Dst> _H;
  249. enum { value = (sizeof(char) == sizeof(_H::_Test(false, _H::_MakeSource()))) };
  250. # else
  251. enum { _Is1 = __type2bool<_IsConst<_Src>::_Ret>::_Ret };
  252. enum { _Is2 = _IsConvertible<_UnConstPtr<_Src>::_Type, _UnConstPtr<_Dst>::_Type>::value };
  253. enum { value = _Is1 ? 0 : _Is2 };
  254. # endif
  255. typedef typename __bool2type<value>::_Ret _Ret;
  256. };
  257. #else
  258. template <class _Src, class _Dst>
  259. struct _IsConvertible {
  260. enum { value = 0 };
  261. typedef __false_type _Ret;
  262. };
  263. template <class _Src, class _Dst>
  264. struct _IsCVConvertible {
  265. enum { value = 0 };
  266. typedef __false_type _Ret;
  267. };
  268. #endif
  269. _STLP_END_NAMESPACE
  270. #endif /* _STLP_TYPE_MANIPS_H */