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}; };
50 // my_typeinfo structure is used to save type number
52 const char* const type_;
55 const my_typeinfo infos[5] = {
56 {"void"}, {"my_class"}, {"my_struct"}, {"my_classes"}, {"my_string"}
60 inline const my_typeinfo& my_typeinfo_construct() {
61 return infos[typenum<T>::value];
63 }} // my_namespace::detail
65 //] [/type_index_userdefined_usertypes]
68 //[type_index_my_type_index
70 `my_type_index` is a user created type_index class. If in doubt during this phase, you can always
71 take a look at the `<boost/type_index/ctti_type_index.hpp>` or `<boost/type_index/stl_type_index.hpp>`
72 files. Documentation for `type_index_facade` could be also useful.
74 See implementation of `my_type_index`:
76 namespace my_namespace {
78 class my_type_index: public boost::typeindex::type_index_facade<my_type_index, detail::my_typeinfo> {
79 const detail::my_typeinfo* data_;
82 typedef detail::my_typeinfo type_info_t;
84 inline my_type_index() BOOST_NOEXCEPT
85 : data_(&detail::my_typeinfo_construct<void>())
88 inline my_type_index(const type_info_t& data) BOOST_NOEXCEPT
92 inline const type_info_t& type_info() const BOOST_NOEXCEPT {
96 inline const char* raw_name() const BOOST_NOEXCEPT {
100 inline std::string pretty_name() const {
105 inline static my_type_index type_id() BOOST_NOEXCEPT {
106 return detail::my_typeinfo_construct<T>();
110 inline static my_type_index type_id_with_cvr() BOOST_NOEXCEPT {
111 return detail::my_typeinfo_construct<T>();
115 inline static my_type_index type_id_runtime(const T& variable) BOOST_NOEXCEPT;
118 } // namespace my_namespace
121 Note that we have used the boost::typeindex::type_index_facade class as base.
122 That class took care about all the helper function and operators (comparison, hashing, ostreaming and others).
125 //] [/type_index_my_type_index]
127 //[type_index_my_type_index_register_class
129 Usually to allow runtime type info we need to register class with some macro.
130 Let's see how a `MY_TYPEINDEX_REGISTER_CLASS` macro could be implemented for our `my_type_index` class:
132 namespace my_namespace { namespace detail {
135 inline const my_typeinfo& my_typeinfo_construct_ref(const T*) {
136 return my_typeinfo_construct<T>();
139 #define MY_TYPEINDEX_REGISTER_CLASS \
140 virtual const my_namespace::detail::my_typeinfo& type_id_runtime() const { \
141 return my_namespace::detail::my_typeinfo_construct_ref(this); \
144 }} // namespace my_namespace::detail
146 //] [/type_index_my_type_index_register_class]
148 //[type_index_my_type_index_type_id_runtime_implmentation
150 Now when we have a MY_TYPEINDEX_REGISTER_CLASS, let's implement a `my_type_index::type_id_runtime` method:
152 namespace my_namespace {
154 my_type_index my_type_index::type_id_runtime(const T& variable) BOOST_NOEXCEPT {
155 // Classes that were marked with `MY_TYPEINDEX_REGISTER_CLASS` will have a
156 // `type_id_runtime()` method.
157 return variable.type_id_runtime();
160 //] [/type_index_my_type_index_type_id_runtime_implmentation]
162 //[type_index_my_type_index_type_id_runtime_classes
164 Consider the situation, when `my_class` and `my_struct` are polymorphic classes:
167 namespace my_namespace {
171 MY_TYPEINDEX_REGISTER_CLASS
172 virtual ~my_class() {}
175 struct my_struct: public my_class {
176 MY_TYPEINDEX_REGISTER_CLASS
179 } // namespace my_namespace
181 //] [/type_index_my_type_index_type_id_runtime_classes]
184 //[type_index_my_type_index_worldwide_typedefs
186 You'll also need to add some typedefs and macro to your "user_defined_typeinfo.hpp" header file:
188 #define BOOST_TYPE_INDEX_REGISTER_CLASS MY_TYPEINDEX_REGISTER_CLASS
189 namespace boost { namespace typeindex {
190 typedef my_namespace::my_type_index type_index;
192 //] [/type_index_my_type_index_worldwide_typedefs]
195 #endif // USER_DEFINED_TYPEINFO_HPP