1 // Copyright 2013-2014 Antony Polukhin
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See the accompanying file LICENSE_1_0.txt
5 // or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
7 #ifndef USER_DEFINED_TYPEINFO_HPP
8 #define USER_DEFINED_TYPEINFO_HPP
10 //[type_index_userdefined_usertypes
12 The following example shows how a user defined type_info can be created and used.
13 Example works with and without RTTI.
15 Consider situation when user uses only those types in `typeid()`:
21 namespace my_namespace {
26 typedef std::vector<my_class> my_classes;
27 typedef std::string my_string;
29 } // namespace my_namespace
31 //] [/type_index_userdefined_usertypes]
34 //[type_index_userdefined_enum
36 In that case user may wish to save space in binary and create it's own type system.
37 For that case `detail::typenum<>` meta function is added. Depending on the input type T
38 this function will return different numeric values.
40 #include <boost/type_index/type_index_facade.hpp>
42 namespace my_namespace { namespace detail {
43 template <class T> struct typenum;
44 template <> struct typenum<void>{ enum {value = 0}; };
45 template <> struct typenum<my_class>{ enum {value = 1}; };
46 template <> struct typenum<my_struct>{ enum {value = 2}; };
47 template <> struct typenum<my_classes>{ enum {value = 3}; };
48 template <> struct typenum<my_string>{ enum {value = 4}; };
52 #pragma warning(disable: 4510 4512 4610) // non-copyable non-constructable type
55 // my_typeinfo structure is used to save type number
57 const char* const type_;
64 const my_typeinfo infos[5] = {
65 {"void"}, {"my_class"}, {"my_struct"}, {"my_classes"}, {"my_string"}
69 inline const my_typeinfo& my_typeinfo_construct() {
70 return infos[typenum<T>::value];
72 }} // my_namespace::detail
74 //] [/type_index_userdefined_usertypes]
77 //[type_index_my_type_index
79 `my_type_index` is a user created type_index class. If in doubt during this phase, you can always
80 take a look at the `<boost/type_index/ctti_type_index.hpp>` or `<boost/type_index/stl_type_index.hpp>`
81 files. Documentation for `type_index_facade` could be also useful.
85 Since we are not going to override `type_index_facade::hash_code()` we must additionally include
86 `<boost/functional/hash.hpp>`.
88 #include <boost/functional/hash.hpp>
91 See implementation of `my_type_index`:
93 namespace my_namespace {
95 class my_type_index: public boost::typeindex::type_index_facade<my_type_index, detail::my_typeinfo> {
96 const detail::my_typeinfo* data_;
99 typedef detail::my_typeinfo type_info_t;
101 inline my_type_index() BOOST_NOEXCEPT
102 : data_(&detail::my_typeinfo_construct<void>())
105 inline my_type_index(const type_info_t& data) BOOST_NOEXCEPT
109 inline const type_info_t& type_info() const BOOST_NOEXCEPT {
113 inline const char* raw_name() const BOOST_NOEXCEPT {
117 inline std::string pretty_name() const {
122 inline static my_type_index type_id() BOOST_NOEXCEPT {
123 return detail::my_typeinfo_construct<T>();
127 inline static my_type_index type_id_with_cvr() BOOST_NOEXCEPT {
128 return detail::my_typeinfo_construct<T>();
132 inline static my_type_index type_id_runtime(const T& variable) BOOST_NOEXCEPT;
135 } // namespace my_namespace
138 Note that we have used the boost::typeindex::type_index_facade class as base.
139 That class took care about all the helper function and operators (comparison, hashing, ostreaming and others).
142 //] [/type_index_my_type_index]
144 //[type_index_my_type_index_register_class
146 Usually to allow runtime type info we need to register class with some macro.
147 Let's see how a `MY_TYPEINDEX_REGISTER_CLASS` macro could be implemented for our `my_type_index` class:
149 namespace my_namespace { namespace detail {
152 inline const my_typeinfo& my_typeinfo_construct_ref(const T*) {
153 return my_typeinfo_construct<T>();
156 #define MY_TYPEINDEX_REGISTER_CLASS \
157 virtual const my_namespace::detail::my_typeinfo& type_id_runtime() const { \
158 return my_namespace::detail::my_typeinfo_construct_ref(this); \
161 }} // namespace my_namespace::detail
163 //] [/type_index_my_type_index_register_class]
165 //[type_index_my_type_index_type_id_runtime_implmentation
167 Now when we have a MY_TYPEINDEX_REGISTER_CLASS, let's implement a `my_type_index::type_id_runtime` method:
169 namespace my_namespace {
171 my_type_index my_type_index::type_id_runtime(const T& variable) BOOST_NOEXCEPT {
172 // Classes that were marked with `MY_TYPEINDEX_REGISTER_CLASS` will have a
173 // `type_id_runtime()` method.
174 return variable.type_id_runtime();
177 //] [/type_index_my_type_index_type_id_runtime_implmentation]
179 //[type_index_my_type_index_type_id_runtime_classes
181 Consider the situation, when `my_class` and `my_struct` are polymorphic classes:
184 namespace my_namespace {
188 MY_TYPEINDEX_REGISTER_CLASS
189 virtual ~my_class() {}
192 struct my_struct: public my_class {
193 MY_TYPEINDEX_REGISTER_CLASS
196 } // namespace my_namespace
198 //] [/type_index_my_type_index_type_id_runtime_classes]
201 //[type_index_my_type_index_worldwide_typedefs
203 You'll also need to add some typedefs and macro to your "user_defined_typeinfo.hpp" header file:
205 #define BOOST_TYPE_INDEX_REGISTER_CLASS MY_TYPEINDEX_REGISTER_CLASS
206 namespace boost { namespace typeindex {
207 typedef my_namespace::my_type_index type_index;
209 //] [/type_index_my_type_index_worldwide_typedefs]
212 #endif // USER_DEFINED_TYPEINFO_HPP