Sometimes there may be a need to create your own type info system. This may
be useful if you wish to store some more info about types (PODness, size of
a type, pointers to common functions...) or if you have an idea of a more compact
types representations.

In that case user may wish to save space in binary and create it's own type
system. For that case detail::typenum<> meta function is added. Depending
on the input type T this function will return different numeric values.

#include<boost/type_index/type_index_facade.hpp>namespacemy_namespace{namespacedetail{template<classT>structtypenum;template<>structtypenum<void>{enum{value=0};};template<>structtypenum<my_class>{enum{value=1};};template<>structtypenum<my_struct>{enum{value=2};};template<>structtypenum<my_classes>{enum{value=3};};template<>structtypenum<my_string>{enum{value=4};};// my_typeinfo structure is used to save type numberstructmy_typeinfo{constchar*consttype_;};constmy_typeinfoinfos[5]={{"void"},{"my_class"},{"my_struct"},{"my_classes"},{"my_string"}};template<classT>inlineconstmy_typeinfo&my_typeinfo_construct(){returninfos[typenum<T>::value];}}}// my_namespace::detail

my_type_index is a user created
type_index class. If in doubt during this phase, you can always take a look
at the <boost/type_index/ctti_type_index.hpp>
or <boost/type_index/stl_type_index.hpp>
files. Documentation for type_index_facade
could be also useful.

Now when we have a MY_TYPEINDEX_REGISTER_CLASS, let's implement a my_type_index::type_id_runtime method:

namespacemy_namespace{template<classT>my_type_indexmy_type_index::type_id_runtime(constT&variable)BOOST_NOEXCEPT{// Classes that were marked with `MY_TYPEINDEX_REGISTER_CLASS` will have a// `type_id_runtime()` method.returnvariable.type_id_runtime();}}

Consider the situation, when my_class
and my_struct are polymorphic
classes:

There is an easy way to force boost::typeindex::type_id
to use your own type_index class.

All we need to do is just define BOOST_TYPE_INDEX_USER_TYPEINDEX
to the full path to header file of your type index class:

// BOOST_TYPE_INDEX_USER_TYPEINDEX must be defined *BEFORE* first inclusion of <boost/type_index.hpp>#defineBOOST_TYPE_INDEX_USER_TYPEINDEX<boost/../libs/type_index/examples/user_defined_typeinfo.hpp>#include<boost/type_index.hpp>

You'll also need to add some typedefs and macro to your "user_defined_typeinfo.hpp"
header file: