diff --git a/doc/type_index.qbk b/doc/type_index.qbk index 5b81a83..8556fca 100644 --- a/doc/type_index.qbk +++ b/doc/type_index.qbk @@ -202,6 +202,15 @@ expands to nothing. [xinclude autodoc.xml] +[section Making own type_index] +[import ../examples/user_defined_typeinfo.cpp] +[type_index_userdefined_usertypes] +[type_index_userdefined_enum] +[type_index_my_type_index] +[type_index_my_type_index_usage] +[endsect] + + [section Space and Performance] * `ctti_type_info` uses macro for getting full text representation of function name which could lead to code bloat, diff --git a/examples/user_defined_typeinfo.cpp b/examples/user_defined_typeinfo.cpp index 430a904..ddd7741 100644 --- a/examples/user_defined_typeinfo.cpp +++ b/examples/user_defined_typeinfo.cpp @@ -4,13 +4,14 @@ // (See the accompanying file LICENSE_1_0.txt // or a copy at .) -//[type_index_userdefined +//[type_index_userdefined_usertypes /*` The following example shows how a user defined type_info can be created and used. Example works with and without RTTI. + + Consider situation when user uses only those types in `typeid()`: */ -#include #include #include @@ -22,8 +23,20 @@ struct my_struct; typedef std::vector my_classes; typedef std::string my_string; +} // namespace my_namespace -namespace detail { +//] [/type_index_userdefined_usertypes] + + +//[type_index_userdefined_enum +/*` + 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 + +namespace my_namespace { namespace detail { template struct typenum; template <> struct typenum{ enum {value = 0}; }; template <> struct typenum{ enum {value = 1}; }; @@ -31,7 +44,10 @@ namespace detail { template <> struct typenum{ enum {value = 3}; }; template <> struct typenum{ enum {value = 4}; }; + // my_typeinfo structure is used to save type number struct my_typeinfo { + // type_[0] will hold a type number + // type_[1] will be '\0', to have a zero terminated raw type name char type_[2]; }; @@ -40,7 +56,20 @@ namespace detail { static const my_typeinfo ret = {{ static_cast(typenum::value), '\0' }}; return ret; } -} +}} // my_namespace::detail + +//] [/type_index_userdefined_usertypes] + + +//[type_index_my_type_index +/*` + `my_type_index` is a user created type_index class. If in doubt during this phase, you can always + take a look at the `` or `` + files. Documentation for `type_index_facade` could be also useful. + + See implementation of `my_type_index`: +*/ +namespace my_namespace { class my_type_index: public boost::typeind::type_index_facade { const detail::my_typeinfo* data_; @@ -80,11 +109,27 @@ public: } }; +} // namespace my_namespace + +/*` + Note that we have used the boost::typeind::type_index_facade class as base. + That class took care about all the helper function and operators (comparison, hashing, ostreaming and others). +*/ + +//] [/type_index_my_type_index] + +namespace my_namespace { + class my_class{}; struct my_struct{}; } // namespace my_namespace +//[type_index_my_type_index_usage +/*` + Finally we can use the my_type_index class for getting type indexes: +*/ + using namespace my_namespace; #include @@ -102,4 +147,5 @@ int main() { assert(cl1.pretty_name() == "my_class"); } -//] [/type_index_userdefined] +//] [/type_index_my_type_index_usage] +