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