Phoenix
Object-oriented orthogonally persistent operating system
|
00001 /* 00002 * /phoenix/include/triton/utils.h 00003 * 00004 * This file is a part of Phoenix operating system. 00005 * Copyright (c) 2011-2012, Artyom Lebedev <artyom.lebedev@gmail.com> 00006 * All rights reserved. 00007 * See COPYING file for copyright details. 00008 */ 00009 00014 #ifndef UTILS_H_ 00015 #define UTILS_H_ 00016 00018 namespace triton { 00019 00020 /* ************************************************************************** */ 00021 00022 namespace triton_internal { 00023 00024 template <typename T> 00025 struct remove_const_impl { 00026 typedef T type; 00027 }; 00028 00029 template <typename T> 00030 struct remove_const_impl<const T> { 00031 typedef T type; 00032 }; 00033 00034 template <typename T> 00035 struct remove_volatile_impl { 00036 typedef T type; 00037 }; 00038 00039 template <typename T> 00040 struct remove_volatile_impl<volatile T> { 00041 typedef T type; 00042 }; 00043 00044 template <typename T> 00045 struct remove_ptr_impl { 00046 typedef T type; 00047 }; 00048 00049 template <typename T> 00050 struct remove_ptr_impl<T *> { 00051 typedef T type; 00052 }; 00053 00054 } /* namespace triton_internal */ 00055 00057 template <typename T> 00058 using remove_const = typename triton_internal::remove_const_impl<T>::type; 00059 00061 template <typename T> 00062 using remove_volatile = typename triton_internal::remove_volatile_impl<T>::type; 00063 00065 template <typename T> 00066 using remove_cv = typename triton_internal::remove_volatile_impl< 00067 typename triton_internal::remove_const_impl<T>::type>::type; 00068 00070 template <typename T> 00071 using remove_ptr = typename triton_internal::remove_ptr_impl<T>::type; 00072 00073 /* ************************************************************************** */ 00074 00075 namespace triton_internal { 00076 00077 /* Conditional template instantiation helpers. Idea borrowed from Boost library. */ 00078 00079 template <bool cond, class T = void> 00080 struct enable_if_impl { 00081 typedef T type; 00082 }; 00083 00084 template <class T> 00085 struct enable_if_impl<false, T> {}; 00086 00087 template <bool cond, class T = void> 00088 struct disable_if_impl { 00089 typedef T type; 00090 }; 00091 00092 template <class T> 00093 struct disable_if_impl<true, T> {}; 00094 00095 } /* namespace triton_internal */ 00096 00114 template <bool cond, class T = void> 00115 using enable_if = typename triton_internal::enable_if_impl<cond, T>::type; 00116 00120 template <bool cond, class T = void> 00121 using disable_if = typename triton_internal::enable_if_impl<cond, T>::type; 00122 00123 /* ************************************************************************** */ 00124 00126 template <typename T> 00127 using add_const = const T; 00128 00130 template <typename T> 00131 using add_reference = T &; 00132 00134 template <typename T> 00135 using add_const_reference = const T &; 00136 00137 /* ************************************************************************** */ 00138 /* Integral constant expressions manipulations. */ 00139 00140 namespace triton_internal { 00141 00142 template <bool... values> 00143 struct ice_or_impl; 00144 00145 template <> 00146 struct ice_or_impl<> 00147 { 00148 static const bool value = false; 00149 }; 00150 00151 template <bool... values> 00152 struct ice_or_impl<true, values...> 00153 { 00154 static const bool value = true; 00155 }; 00156 00157 template <bool... values> 00158 struct ice_or_impl<false, values...> 00159 { 00160 static const bool value = ice_or_impl<values...>::value; 00161 }; 00162 00163 template <bool... values> 00164 struct ice_and_impl; 00165 00166 template <> 00167 struct ice_and_impl<> 00168 { 00169 static const bool value = true; 00170 }; 00171 00172 template <bool... values> 00173 struct ice_and_impl<false, values...> 00174 { 00175 static const bool value = false; 00176 }; 00177 00178 template <bool... values> 00179 struct ice_and_impl<true, values...> 00180 { 00181 static const bool value = ice_and_impl<values...>::value; 00182 }; 00183 00184 } /* namespace triton_internal */ 00185 00194 template <bool... values> 00195 constexpr bool 00196 ice_or() 00197 { 00198 return triton_internal::ice_or_impl<values...>::value; 00199 } 00200 00209 template <bool... values> 00210 constexpr bool 00211 ice_and() 00212 { 00213 return triton_internal::ice_and_impl<values...>::value; 00214 } 00215 00224 template <bool value> 00225 constexpr bool 00226 ice_not() 00227 { 00228 return true; 00229 } 00230 00231 template <> 00232 constexpr bool 00233 ice_not<true>() 00234 { 00235 return false; 00236 } 00237 00238 /* ************************************************************************** */ 00239 00240 namespace triton_internal { 00241 00242 template <typename T> 00243 constexpr bool 00244 is_integral_impl() 00245 { 00246 return false; 00247 } 00248 00249 #define TRITON_IS_INTEGRAL_IMPL(__type) \ 00250 template <> \ 00251 constexpr bool \ 00252 is_integral_impl<__type>() \ 00253 { \ 00254 return true; \ 00255 } 00256 00257 TRITON_IS_INTEGRAL_IMPL(char) 00258 TRITON_IS_INTEGRAL_IMPL(unsigned char) 00259 TRITON_IS_INTEGRAL_IMPL(wchar_t) 00260 TRITON_IS_INTEGRAL_IMPL(int) 00261 TRITON_IS_INTEGRAL_IMPL(unsigned) 00262 TRITON_IS_INTEGRAL_IMPL(long) 00263 TRITON_IS_INTEGRAL_IMPL(unsigned long) 00264 TRITON_IS_INTEGRAL_IMPL(long long) 00265 TRITON_IS_INTEGRAL_IMPL(unsigned long long) 00266 00267 template <typename T> 00268 constexpr bool 00269 is_float_impl() 00270 { 00271 return false; 00272 } 00273 00274 #define TRITON_IS_FLOAT_IMPL(__type) \ 00275 template <> \ 00276 constexpr bool \ 00277 is_float_impl<__type>() \ 00278 { \ 00279 return true; \ 00280 } 00281 00282 TRITON_IS_FLOAT_IMPL(float) 00283 TRITON_IS_FLOAT_IMPL(double) 00284 TRITON_IS_FLOAT_IMPL(long double) 00285 00286 } /* namespace triton_internal */ 00287 00289 template <typename T> 00290 constexpr bool 00291 is_integral() 00292 { 00293 return triton_internal::is_integral_impl<remove_cv<T>>(); 00294 } 00295 00297 template <typename T> 00298 constexpr bool 00299 is_float() 00300 { 00301 return triton_internal::is_float_impl<remove_cv<T>>(); 00302 } 00303 00305 template <typename T> 00306 constexpr bool 00307 is_numeric() 00308 { 00309 return triton_internal::is_integral_impl<remove_cv<T>>() || 00310 triton_internal::is_float_impl<remove_cv<T>>(); 00311 } 00312 00313 /* ************************************************************************** */ 00314 00315 } /* namespace triton */ 00316 00317 #endif /* UTILS_H_ */