lean cpp library
A lean C++ library providing efficient utility classes for high-performance C++ applications.
|
00001 /*****************************************************/ 00002 /* lean built-in types (c) Tobias Zirr 2011 */ 00003 /*****************************************************/ 00004 00005 #ifndef LEAN_TYPE_INFO 00006 #define LEAN_TYPE_INFO 00007 00008 #include "lean.h" 00009 #include <typeinfo> 00010 #include "meta/strip.h" 00011 00012 namespace lean 00013 { 00014 00015 namespace types 00016 { 00017 00019 struct type_info 00020 { 00022 const std::type_info &type; 00024 const size_t size; 00025 00027 type_info(const std::type_info &type, size_t size) 00028 : type(type), 00029 size(size) { } 00030 }; 00031 00032 namespace impl 00033 { 00034 template <class Type> 00035 struct robust_sizeof { static const size_t value = sizeof(Type); }; 00036 template <> 00037 struct robust_sizeof<void> { static const size_t value = 0; }; 00038 } 00039 00041 template <class Type> 00042 inline const type_info& get_type_info() 00043 { 00044 static type_info info( 00045 typeid(Type), 00046 impl::robust_sizeof<typename lean::strip_modref<Type>::type>::value 00047 ); 00048 return info; 00049 } 00050 00051 00053 template <class Type> 00054 LEAN_INLINE bool is_type(const std::type_info &type) 00055 { 00056 return typeid(Type) == type; 00057 } 00058 00059 00061 template <class Value> 00062 LEAN_INLINE Value* to_type(const std::type_info &type, void *value) 00063 { 00064 return is_type<Value>(type) 00065 ? static_cast<Value*>(value) 00066 : nullptr; 00067 } 00068 00070 template <class Value> 00071 LEAN_INLINE const Value* to_type(const std::type_info &type, const void *value) 00072 { 00073 return to_type<Value>(type, const_cast<void*>(value)); 00074 } 00075 00077 template <class Value> 00078 LEAN_INLINE volatile Value* to_type(const std::type_info &type, volatile void *value) 00079 { 00080 return to_type<Value>(type, const_cast<void*>(value)); 00081 } 00082 00084 template <class Value> 00085 LEAN_INLINE const volatile Value* to_type(const std::type_info &type, const volatile void *value) 00086 { 00087 return to_type<Value>(type, const_cast<void*>(value)); 00088 } 00089 00090 00092 LEAN_INLINE void* get_element(size_t stride, void *value, size_t offset) 00093 { 00094 return reinterpret_cast<char*>(value) + offset * stride; 00095 } 00096 00098 LEAN_INLINE const void* get_element(size_t stride, const void *value, size_t offset) 00099 { 00100 return get_element(stride, const_cast<void*>(value), offset); 00101 } 00102 00104 LEAN_INLINE volatile void* get_element(size_t stride, volatile void *value, size_t offset) 00105 { 00106 return get_element(stride, const_cast<void*>(value), offset); 00107 } 00108 00110 LEAN_INLINE const volatile void* get_element(size_t stride, const volatile void *value, size_t offset) 00111 { 00112 return get_element(stride, const_cast<void*>(value), offset); 00113 } 00114 00115 00116 } // namespace 00117 00118 using types::type_info; 00119 using types::get_type_info; 00120 00121 using types::is_type; 00122 using types::to_type; 00123 using types::get_element; 00124 00125 } // namespace 00126 00127 #endif