28 #ifndef __RTTR_RTTR_ENABLE_H__
29 #define __RTTR_RTTR_ENABLE_H__
31 #include <type_traits>
52 struct base_class_info
54 base_class_info(type t,
void*(*rttr_cast_func)(
void*))
55 : _base_type(t), _rttr_cast_func(rttr_cast_func)
58 void* (*_rttr_cast_func)(
void*);
66 template<
typename... U>
struct type_list {};
74 class has_base_class_list_impl
76 typedef char YesType[1];
77 typedef char NoType[2];
80 static YesType& test(
typename C::base_class_list*);
83 static NoType& test(...);
86 static const bool value = (
sizeof(YesType) ==
sizeof(test<T>(0)));
92 template<
typename T,
typename =
void>
93 struct has_base_class_list : std::integral_constant<bool, false>
97 struct has_base_class_list<T, typename std::enable_if<has_base_class_list_impl<T>::value>::type > : std::integral_constant<bool, true>
100 typedef std::vector<detail::base_class_info> info_container;
105 template<
typename DerivedClass,
typename... T>
106 struct type_from_base_classes;
108 template<
typename DerivedClass>
109 struct type_from_base_classes<DerivedClass>
124 template<
typename DerivedType,
typename BaseType>
125 static void* rttr_cast_impl(
void* ptr)
127 return static_cast<void*
>(
static_cast<BaseType*
>(
static_cast<DerivedType*
>(ptr)));
130 template<
typename DerivedClass,
typename BaseClass,
typename... U>
131 struct type_from_base_classes<DerivedClass, BaseClass, U...>
135 static_assert(has_base_class_list<BaseClass>::value,
"The parent class has no base class list defined - please use the macro RTTR_ENABLE");
136 vec.emplace_back(type::get<BaseClass>(), &rttr_cast_impl<DerivedClass, BaseClass>);
139 type_from_base_classes<DerivedClass, typename BaseClass::base_class_list>::fill(vec);
141 type_from_base_classes<DerivedClass, U...>::fill(vec);
145 template<
typename DerivedClass,
class... BaseClassList,
template<
class...>
class ClassContainer>
146 struct type_from_base_classes<DerivedClass, ClassContainer<BaseClassList...>> : type_from_base_classes<DerivedClass, BaseClassList...> { };
152 template<
typename ClassType>
157 RTTR_INLINE static void get_types_impl(info_container& vec, std::true_type)
159 type_from_base_classes<ClassType, typename ClassType::base_class_list>::fill(vec);
163 RTTR_INLINE static void get_types_impl(info_container&, std::false_type)
169 info_container result;
170 get_types_impl(result,
typename has_base_class_list<ClassType>::type());
179 static void* get_ptr(
const T& data,
typename std::enable_if<!std::is_pointer<T>::value>::type* = 0)
181 return const_cast<void*
>(
reinterpret_cast<const void*
>(&data));
188 static void* get_ptr(T& data,
typename std::enable_if<!std::is_pointer<T>::value>::type* = 0)
190 return reinterpret_cast<void*
>(&data);
197 static void* get_ptr(
const T& data,
typename std::enable_if<std::is_pointer<T>::value>::type* = 0)
199 return const_cast<void*
>(
reinterpret_cast<const void*
>(data));
206 static void* get_ptr(T& data,
typename std::enable_if<std::is_pointer<T>::value>::type* = 0)
208 return reinterpret_cast<void*
>(data);
214 static detail::derived_info get_most_derived_info_impl(
void* ptr)
216 return (static_cast<T*>(ptr)->get_derived_info());
220 static detail::derived_info get_most_derived_info_impl_none(
void* ptr)
222 return {ptr, type::get<T>()};
225 typedef detail::derived_info(*derived_func)(
void*);
232 static derived_func get_most_derived_info_check(
typename std::enable_if<detail::has_get_derived_info_func<T>::value >::type* = 0)
234 return get_most_derived_info_impl<T>;
241 static derived_func get_most_derived_info_check(
typename std::enable_if<!detail::has_get_derived_info_func<T>::value >::type* = 0)
243 return get_most_derived_info_impl_none<T>;
252 static derived_func get_most_derived_info_func()
254 return get_most_derived_info_check<typename detail::raw_type<T>::type>();
257 template<
typename source_type>
258 static variant create_variant(
void* ptr)
260 return static_cast<source_type
>(ptr);
263 template<
typename source_type>
264 static variant create_invalid_variant(
void*)
269 typedef variant(*create_variant_func)(
void*);
272 create_variant_func get_create_variant_func(
typename std::enable_if< detail::pointer_count<T>::value == 1 >::type* = 0)
274 return create_variant<T>;
278 create_variant_func get_create_variant_func(
typename std::enable_if< detail::pointer_count<T>::value != 1>::type* = 0)
280 return create_invalid_variant<T>;
286 #define TYPE_LIST(...) rttr::impl::type_list<__VA_ARGS__>
288 #define RTTR_ENABLE(...) \
290 virtual RTTR_INLINE rttr::type get_type() const { return rttr::impl::get_type_from_instance(this); } \
291 virtual RTTR_INLINE void* get_ptr() { return reinterpret_cast<void*>(this); } \
292 virtual RTTR_INLINE rttr::detail::derived_info get_derived_info() { return {reinterpret_cast<void*>(this), rttr::impl::get_type_from_instance(this)}; } \
293 typedef TYPE_LIST(__VA_ARGS__) base_class_list; \
297 #endif // __RTTR_RTTR_ENABLE_H__
#define RTTR_INLINE
Definition: core_prerequisites.h:90