Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygentype_manips.h
Go to the documentation of this file.
00001 /* 00002 * 00003 * Copyright (c) 2003 00004 * Francois Dumont 00005 * 00006 * This material is provided "as is", with absolutely no warranty expressed 00007 * or implied. Any use is at your own risk. 00008 * 00009 * Permission to use or copy this software for any purpose is hereby granted 00010 * without fee, provided the above notices are retained on all copies. 00011 * Permission to modify the code and to distribute modified code is granted, 00012 * provided the above notices are retained, and a notice that the code was 00013 * modified is included with the above copyright notice. 00014 * 00015 */ 00016 00017 00018 #ifndef _STLP_TYPE_MANIPS_H 00019 #define _STLP_TYPE_MANIPS_H 00020 00021 _STLP_BEGIN_NAMESPACE 00022 00023 struct __true_type {}; 00024 struct __false_type {}; 00025 00026 #if defined (_STLP_USE_NAMESPACES) && !defined (_STLP_DONT_USE_PRIV_NAMESPACE) 00027 _STLP_MOVE_TO_PRIV_NAMESPACE 00028 using _STLP_STD::__true_type; 00029 using _STLP_STD::__false_type; 00030 _STLP_MOVE_TO_STD_NAMESPACE 00031 #endif 00032 00033 //bool to type 00034 template <int _Is> 00035 struct __bool2type 00036 { typedef __true_type _Ret; }; 00037 00038 _STLP_TEMPLATE_NULL 00039 struct __bool2type<1> { typedef __true_type _Ret; }; 00040 00041 _STLP_TEMPLATE_NULL 00042 struct __bool2type<0> { typedef __false_type _Ret; }; 00043 00044 //type to bool 00045 template <class __bool_type> 00046 struct __type2bool { enum {_Ret = 1}; }; 00047 00048 _STLP_TEMPLATE_NULL 00049 struct __type2bool<__true_type> { enum {_Ret = 1}; }; 00050 00051 _STLP_TEMPLATE_NULL 00052 struct __type2bool<__false_type> { enum {_Ret = 0}; }; 00053 00054 //Negation 00055 template <class _BoolType> 00056 struct _Not { typedef __false_type _Ret; }; 00057 00058 _STLP_TEMPLATE_NULL 00059 struct _Not<__false_type> { typedef __true_type _Ret; }; 00060 00061 // logical and of 2 predicated 00062 template <class _P1, class _P2> 00063 struct _Land2 { typedef __false_type _Ret; }; 00064 00065 _STLP_TEMPLATE_NULL 00066 struct _Land2<__true_type, __true_type> { typedef __true_type _Ret; }; 00067 00068 // logical and of 3 predicated 00069 template <class _P1, class _P2, class _P3> 00070 struct _Land3 { typedef __false_type _Ret; }; 00071 00072 _STLP_TEMPLATE_NULL 00073 struct _Land3<__true_type, __true_type, __true_type> { typedef __true_type _Ret; }; 00074 00075 //logical or of 2 predicated 00076 template <class _P1, class _P2> 00077 struct _Lor2 { typedef __true_type _Ret; }; 00078 00079 _STLP_TEMPLATE_NULL 00080 struct _Lor2<__false_type, __false_type> { typedef __false_type _Ret; }; 00081 00082 // logical or of 3 predicated 00083 template <class _P1, class _P2, class _P3> 00084 struct _Lor3 { typedef __true_type _Ret; }; 00085 00086 _STLP_TEMPLATE_NULL 00087 struct _Lor3<__false_type, __false_type, __false_type> { typedef __false_type _Ret; }; 00088 00090 // class template __select 00091 // Selects one of two types based upon a boolean constant 00092 // Invocation: __select<_Cond, T, U>::Result 00093 // where: 00094 // flag is a compile-time boolean constant 00095 // T and U are types 00096 // Result evaluates to T if flag is true, and to U otherwise. 00098 // BEWARE: If the compiler do not support partial template specialization or nested template 00099 //classes the default behavior of the __select is to consider the condition as false and so return 00100 //the second template type!! 00101 00102 #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) 00103 # if defined (__BORLANDC__) 00104 template <class _CondT, class _Tp1, class _Tp2> 00105 struct __selectT { typedef _Tp1 _Ret; }; 00106 00107 template <class _Tp1, class _Tp2> 00108 struct __selectT<__false_type, _Tp1, _Tp2> { typedef _Tp2 _Ret; }; 00109 # endif 00110 00111 # if !defined (__BORLANDC__) || (__BORLANDC__ >= 0x590) 00112 template <bool _Cond, class _Tp1, class _Tp2> 00113 struct __select { typedef _Tp1 _Ret; }; 00114 00115 template <class _Tp1, class _Tp2> 00116 struct __select<false, _Tp1, _Tp2> { typedef _Tp2 _Ret; }; 00117 # else 00118 template <bool _Cond, class _Tp1, class _Tp2> 00119 struct __select 00120 { typedef __selectT<typename __bool2type<_Cond>::_Ret, _Tp1, _Tp2>::_Ret _Ret; }; 00121 # endif 00122 00123 #else 00124 00125 # if defined (_STLP_MEMBER_TEMPLATE_CLASSES) 00126 template <int _Cond> 00127 struct __select_aux { 00128 template <class _Tp1, class _Tp2> 00129 struct _In { 00130 typedef _Tp1 _Ret; 00131 }; 00132 }; 00133 00134 _STLP_TEMPLATE_NULL 00135 struct __select_aux<0> { 00136 template <class _Tp1, class _Tp2> 00137 struct _In { 00138 typedef _Tp2 _Ret; 00139 }; 00140 }; 00141 00142 template <int _Cond, class _Tp1, class _Tp2> 00143 struct __select { 00144 typedef typename __select_aux<_Cond>::_STLP_TEMPLATE _In<_Tp1, _Tp2>::_Ret _Ret; 00145 }; 00146 # else /* _STLP_MEMBER_TEMPLATE_CLASSES */ 00147 //default behavior 00148 template <int _Cond, class _Tp1, class _Tp2> 00149 struct __select { 00150 typedef _Tp2 _Ret; 00151 }; 00152 # endif /* _STLP_MEMBER_TEMPLATE_CLASSES */ 00153 00154 #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */ 00155 00156 /* Rather than introducing a new macro for the following constrution we use 00157 * an existing one (_STLP_DONT_SIMULATE_PARTIAL_SPEC_FOR_TYPE_TRAITS) that 00158 * is used for a similar feature. 00159 */ 00160 #if !defined (_STLP_DONT_SIMULATE_PARTIAL_SPEC_FOR_TYPE_TRAITS) && \ 00161 (!defined (__GNUC__) || (__GNUC__ > 2)) 00162 // Helper struct that will forbid volatile qualified types: 00163 # if !defined (__BORLANDC__) 00164 struct _NoVolatilePointerShim { _NoVolatilePointerShim(const void*); }; 00165 template <class _Tp> 00166 char _STLP_CALL _IsCopyableFun(bool, _NoVolatilePointerShim, _Tp const*, _Tp*); // no implementation is required 00167 char* _STLP_CALL _IsCopyableFun(bool, ...); // no implementation is required 00168 00169 template <class _Src, class _Dst> 00170 struct _Copyable { 00171 static _Src* __null_src(); 00172 static _Dst* __null_dst(); 00173 enum { _Ret = (sizeof(_IsCopyableFun(false, __null_src(), __null_src(), __null_dst())) == sizeof(char)) }; 00174 typedef typename __bool2type<_Ret>::_Ret _RetT; 00175 }; 00176 # else 00177 template <class _Tp1, class _Tp2> struct _AreSameTypes; 00178 template <class _Tp> struct _IsUnQual; 00179 template <class _Src, class _Dst> 00180 struct _Copyable { 00181 typedef typename _AreSameTypes<_Src, _Dst>::_Ret _Tr1; 00182 typedef typename _IsUnQual<_Dst>::_Ret _Tr2; 00183 typedef typename _Land2<_Tr1, _Tr2>::_Ret _RetT; 00184 enum { _Ret = __type2bool<_RetT>::_Ret }; 00185 }; 00186 # endif 00187 #else 00188 template <class _Src, class _Dst> 00189 struct _Copyable { 00190 enum { _Ret = 0 }; 00191 typedef __false_type _RetT; 00192 }; 00193 #endif 00194 00195 /* 00196 * The following struct will tell you if 2 types are the same and if copying memory 00197 * from the _Src type to the _Dst type is right considering qualifiers. If _Src and 00198 * _Dst types are the same unqualified types _Ret will be false if: 00199 * - any of the type has the volatile qualifier 00200 * - _Dst is const qualified 00201 */ 00202 template <class _Src, class _Dst> 00203 struct _AreCopyable { 00204 enum { _Same = _Copyable<_Src, _Dst>::_Ret }; 00205 typedef typename _Copyable<_Src, _Dst>::_RetT _Ret; 00206 }; 00207 00208 template <class _Tp1, class _Tp2> 00209 struct _AreSameTypes { 00210 enum { _Same = 0 }; 00211 typedef __false_type _Ret; 00212 }; 00213 00214 #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) 00215 template <class _Tp> 00216 struct _AreSameTypes<_Tp, _Tp> { 00217 enum { _Same = 1 }; 00218 typedef __true_type _Ret; 00219 }; 00220 #endif 00221 00222 #if !defined (_STLP_DONT_SIMULATE_PARTIAL_SPEC_FOR_TYPE_TRAITS) 00223 template <class _Src, class _Dst> 00224 struct _ConversionHelper { 00225 static char _Test(bool, _Dst); 00226 static char* _Test(bool, ...); 00227 static _Src _MakeSource(); 00228 }; 00229 00230 template <class _Src, class _Dst> 00231 struct _IsConvertible { 00232 typedef _ConversionHelper<_Src*, const volatile _Dst*> _H; 00233 enum { value = (sizeof(char) == sizeof(_H::_Test(false, _H::_MakeSource()))) }; 00234 typedef typename __bool2type<value>::_Ret _Ret; 00235 }; 00236 00237 # if defined (__BORLANDC__) 00238 # if (__BORLANDC__ < 0x590) 00239 template<class _Tp> 00240 struct _UnConstPtr { typedef _Tp _Type; }; 00241 00242 template<class _Tp> 00243 struct _UnConstPtr<_Tp*> { typedef _Tp _Type; }; 00244 00245 template<class _Tp> 00246 struct _UnConstPtr<const _Tp*> { typedef _Tp _Type; }; 00247 # endif 00248 00249 # if !defined (_STLP_QUALIFIED_SPECIALIZATION_BUG) 00250 template <class _Tp> 00251 struct _IsConst { typedef __false_type _Ret; }; 00252 # else 00253 template <class _Tp> 00254 struct _IsConst { typedef _AreSameTypes<_Tp, const _Tp>::_Ret _Ret; }; 00255 # endif 00256 00257 # if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && !defined (_STLP_QUALIFIED_SPECIALIZATION_BUG) 00258 template <class _Tp> 00259 struct _IsConst <const _Tp> { typedef __true_type _Ret; }; 00260 # endif 00261 00262 # if (__BORLANDC__ < 0x590) 00263 template<class _Tp> 00264 struct _IsConst<_Tp*> { typedef _AreSameTypes<_Tp*, const _Tp*>::_Ret _Ret; }; 00265 # endif 00266 template <class _Tp> 00267 struct _IsVolatile { typedef _AreSameTypes<_Tp, volatile _Tp>::_Ret _Ret; }; 00268 00269 template<class _Tp> 00270 struct _IsUnQual { 00271 typedef _IsConst<_Tp>::_Ret _Tr1; 00272 typedef _IsVolatile<_Tp>::_Ret _Tr2; 00273 typedef _Not<_Tr1>::_Ret _NotCon; 00274 typedef _Not<_Tr2>::_Ret _NotVol; 00275 typedef _Land2<_NotCon, _NotVol>::_Ret _Ret; 00276 }; 00277 00278 # if !defined (_STLP_QUALIFIED_SPECIALIZATION_BUG) 00279 template <class _Tp> struct _UnQual { typedef _Tp _Type; }; 00280 template <class _Tp> struct _UnQual<const _Tp> { typedef _Tp _Type; }; 00281 template <class _Tp> struct _UnQual<volatile _Tp> { typedef _Tp _Type; }; 00282 template <class _Tp> struct _UnQual<const volatile _Tp> { typedef _Tp _Type; }; 00283 # endif 00284 # endif 00285 00286 /* This struct is intended to say if a pointer can be convertible to an other 00287 * taking into account cv qualifications. It shouldn't be instanciated with 00288 * something else than pointer type as it uses pass by value parameter that 00289 * results in compilation error when parameter type has a special memory 00290 * alignment 00291 */ 00292 template <class _Src, class _Dst> 00293 struct _IsCVConvertible { 00294 # if !defined (__BORLANDC__) || (__BORLANDC__ >= 0x590) 00295 typedef _ConversionHelper<_Src, _Dst> _H; 00296 enum { value = (sizeof(char) == sizeof(_H::_Test(false, _H::_MakeSource()))) }; 00297 # else 00298 enum { _Is1 = __type2bool<_IsConst<_Src>::_Ret>::_Ret }; 00299 enum { _Is2 = _IsConvertible<_UnConstPtr<_Src>::_Type, _UnConstPtr<_Dst>::_Type>::value }; 00300 enum { value = _Is1 ? 0 : _Is2 }; 00301 # endif 00302 typedef typename __bool2type<value>::_Ret _Ret; 00303 }; 00304 00305 #else 00306 template <class _Src, class _Dst> 00307 struct _IsConvertible { 00308 enum { value = 0 }; 00309 typedef __false_type _Ret; 00310 }; 00311 00312 template <class _Src, class _Dst> 00313 struct _IsCVConvertible { 00314 enum { value = 0 }; 00315 typedef __false_type _Ret; 00316 }; 00317 #endif 00318 00319 _STLP_END_NAMESPACE 00320 00321 #endif /* _STLP_TYPE_MANIPS_H */ Generated on Fri May 25 2012 04:28:21 for ReactOS by
1.7.6.1
|