From 83e02d83c451be70fc745552f52ecb6bd81e7334 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Tue, 29 Oct 2013 18:10:13 +0400 Subject: [PATCH] More user friendly 'Getting Started section' --- libs/type_index/doc/type_index.qbk | 132 ++++++++++++++++++++++++----- 1 file changed, 112 insertions(+), 20 deletions(-) diff --git a/libs/type_index/doc/type_index.qbk b/libs/type_index/doc/type_index.qbk index e11c74d..3b4a614 100644 --- a/libs/type_index/doc/type_index.qbk +++ b/libs/type_index/doc/type_index.qbk @@ -31,33 +31,125 @@ Boost.TypeIndex library was designed to work around all those issues. [section Getting started] `boost::type_info` is a drop-in replacement for `std::type_index` and `boost::type_index` is a drop-in -replacement for `std::type_index`. Unlike Standard Library versions those classes usually do not -require RTTI. +replacement for `std::type_index`. Unlike Standard Library versions those classes may work without RTTI. `boost::type_index` provides the full set of comparison operators, hashing functions and ostream operators, so it can be used with any container class. To start using Boost.TypeIndex: -* Replace headers: - * Use `` instead of `` - * Use `` instead of `` -* Replace `std::type_index`, `const std::type_info&` and `const std::type_info*` variables with `boost::type_index`. -* If you do not want to save `const`, `volatile`, `&` and `&&`: - * Use `boost::type_id()` instead of `typeid(T)`, `&typeid(T)`. -* If you want to save `const`, `volatile`, `&` and `&&`: - * Use `boost::type_id_with_cvr()` instead of `typeid(T)`, `&typeid(T)`. -* To get nice human readable names, use the `name_demangled()` member function: -`` -#include +[table:porting +[[Replace this:][With the following:]] +[[`` + #include + #include +``][`` + #include + #include +`` or `` + #include +``]] -type_index ti = type_id(); -std::string nice_name - = ti.name_demangled(); -... -std::string nice_name_with_const_volatile_ref - = type_id_with_cvr().name_demangled(); -`` +[[`` + std::type_info + std::type_index +``][`` + boost::type_info + boost::type_index +``]] + +[[`` + typeid(T) + typeid(please_save_modifiers) + typeid(T).name() // not human readable + typeid(variable) +``][`` + boost::type_id() + boost::type_id_with_cvr() + boost::type_id().name_demangled() // human readable + boost::type_id_rtti_only(variable) +``]] +] + + +Here is how TypeIndex could be used in `boost/any.hpp`: +[table:any +[[Before] [With TypeIndex]] +[[``#include ``][``#include ``]] +[[`` + virtual const std::type_info & type() const BOOST_NOEXCEPT + { + // requires RTTI + return typeid(ValueType); + } +``] [`` + virtual const boost::type_info & type() const BOOST_NOEXCEPT + { + // now works even with RTTI disabled + return boost::type_id(); + } +``]] +] + + +Here is how TypeIndex could be used in `boost/variant/variant.hpp`: +[table:variant +[[Before] [With TypeIndex]] + +[[`` +#if !defined(BOOST_NO_TYPEID) +#include // for typeid, std::type_info +#endif // BOOST_NO_TYPEID +``][`` +#include +``]] +[[`` +#if !defined(BOOST_NO_TYPEID) + +class reflect + : public static_visitor +{ +public: // visitor interfaces + + template + const std::type_info& operator()(const T&) const BOOST_NOEXCEPT + { + return typeid(T); + } + +}; + +#endif // BOOST_NO_TYPEID +``][`` +class reflect + : public static_visitor +{ +public: // visitor interfaces + + template + const boost::type_info& operator()(const T&) const BOOST_NOEXCEPT + { + return boost::type_id(); + } + +}; +``]] +[[`` +#if !defined(BOOST_NO_TYPEID) + const std::type_info& type() const + { + detail::variant::reflect visitor; + return this->apply_visitor(visitor); + } +#endif +``] [`` + const boost::type_info& type() const + { + detail::variant::reflect visitor; + return this->apply_visitor(visitor); + } +``]] +] [endsect]