Docs improved

This commit is contained in:
Antony Polukhin
2014-02-19 17:22:37 +04:00
parent 276101f1b3
commit 98e5bbe6ac
5 changed files with 84 additions and 54 deletions
+3 -2
View File
@@ -14,11 +14,12 @@
#include <boost/type_index.hpp>
#include <iostream>
using bti = boost::typeind;
template <class T>
void foo(T) {
std::cout << "\n Short name: " << boost::typeind::type_id<T>().raw_name();
std::cout << "\n Readable name: " << boost::typeind::type_id<T>().pretty_name();
std::cout << "\n Short name: " << bti::type_id<T>().raw_name();
std::cout << "\n Readable name: " << bti::type_id<T>().pretty_name();
}
struct user_defined_type{};
+6 -5
View File
@@ -6,7 +6,7 @@
//[type_index_exact_type_match_example
/*`
The following example shows that `boost::type_index` (and `boost::type_info`) is able to store the exact type,
The following example shows that `type_index` (and `type_info`) is able to store the exact type,
without stripping const, volatile and references. Example works with and without RTTI.
In this example we'll create a class, that stores pointer to function and remembers the exact type of a
@@ -18,21 +18,22 @@
#include <iostream>
#include <stdexcept>
#include <cassert>
using bti = boost::typeind;
class type_erased_unary_function {
void* function_ptr_;
boost::typeind::type_index exact_param_t_;
void* function_ptr_;
bti::type_index exact_param_t_;
public:
template <class ParamT>
type_erased_unary_function(void(*ptr)(ParamT))
: function_ptr_(reinterpret_cast<void*>(ptr)) // ptr - is a pointer to function returning `void` and accepting parameter of type `ParamT`
, exact_param_t_(boost::typeind::type_id_with_cvr<ParamT>())
, exact_param_t_(bti::type_id_with_cvr<ParamT>())
{}
template <class ParamT>
void call(ParamT v) {
if (exact_param_t_ != boost::typeind::type_id_with_cvr<ParamT>()) {
if (exact_param_t_ != bti::type_id_with_cvr<ParamT>()) {
throw std::runtime_error("Incorrect `ParamT`");
}
+3 -2
View File
@@ -6,7 +6,7 @@
//[type_index_derived_example
/*`
The following example shows that `boost::type_info` is able to store the real type, successfully getting through
The following example shows that `type_info` is able to store the real type, successfully getting through
all the inheritances.
Example works with and without RTTI."
@@ -14,6 +14,7 @@
#include <boost/type_index.hpp>
#include <iostream>
using bti = boost::typeind;
struct A {
BOOST_TYPE_INDEX_REGISTER_CLASS
@@ -23,7 +24,7 @@ struct B: public A { BOOST_TYPE_INDEX_REGISTER_CLASS };
struct C: public B { BOOST_TYPE_INDEX_REGISTER_CLASS };
void print_real_type(const A& a) {
std::cout << boost::typeind::type_id_runtime(a).pretty_name() << '\n';
std::cout << bti::type_id_runtime(a).pretty_name() << '\n';
}
int main() {
+7 -6
View File
@@ -14,24 +14,25 @@
#include <boost/unordered_set.hpp>
#include <boost/functional/hash.hpp>
#include <cassert>
using bti = boost::typeind;
int main() {
boost::unordered_set<boost::typeind::type_index> types;
boost::unordered_set<bti::type_index> types;
// Storing some `boost::type_info`s
types.insert(boost::typeind::type_id<int>());
types.insert(boost::typeind::type_id<float>());
types.insert(bti::type_id<int>());
types.insert(bti::type_id<float>());
// `types` variable contains two `boost::type_index`es:
assert(types.size() == 2);
// Const, volatile and reference will be striped from the type:
bool is_inserted = types.insert(boost::typeind::type_id<const int>()).second;
bool is_inserted = types.insert(bti::type_id<const int>()).second;
assert(!is_inserted);
assert(types.erase(boost::typeind::type_id<float&>()) == 1);
assert(types.erase(bti::type_id<float&>()) == 1);
// We have erased the `float` type, only `int` remains
assert(*types.begin() == boost::typeind::type_id<int>());
assert(*types.begin() == bti::type_id<int>());
}
//] [/type_index_registry_example]