Add some examples of how to create type_indexes

This commit is contained in:
Antony Polukhin
2014-02-07 15:08:14 +04:00
parent 0137b9b43f
commit beaa53f460
2 changed files with 86 additions and 0 deletions

View File

@ -0,0 +1,51 @@
// Copyright 2013-2014 Antony Polukhin
// Distributed under the Boost Software License, Version 1.0.
// (See the accompanying file LICENSE_1_0.txt
// or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
//[type_index_my_type_index_sources
/*`
The following example shows how to create an own type_info clas and how
to use it all around the project.
*/
#define BOOST_TYPE_INDEX_USER_TYPEINFO \
"../../../libs/type_index/examples/my_type_index.hpp"
#define BOOST_TYPE_INDEX_USER_TYPEINFO_NAME my_namespace::my_type_info
#include <boost/type_index.hpp>
#include <cassert>
class Base {
public:
virtual my_type_index type_info() const BOOST_NOEXCEPT {
return boost::typeind::type_info<Base>();
}
virtual ~Base(){}
};
class Derived: public Base {
public:
virtual my_type_index type_info() const BOOST_NOEXCEPT {
return boost::typeind::type_id<Derived>();
}
};
using boost::typeind;
int main() {
Base base;
Derived derived;
Base& derived_as_base;
assert(type_id_runtime(derived) == type_id_runtime(derived_as_base));
assert(type_id_runtime(base) != type_id_runtime(derived_as_base));
assert(type_id_runtime(base) != type_id_runtime(derived));
assert(type_id_runtime(derived_as_base).pretty_name() == "Derived");
}
//] [/type_index_registry_example]
//] [/type_index_my_type_index_sources]

View File

@ -0,0 +1,35 @@
// Copyright 2013-2014 Antony Polukhin
// Distributed under the Boost Software License, Version 1.0.
// (See the accompanying file LICENSE_1_0.txt
// or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
//[type_index_my_type_index
/*`
The following example shows how to create an own type_info clas and how
to use it all around the project.
*/
#ifndef MY_NAMESPACE_MY_TYPE_INDEX_HPP
#define MY_NAMESPACE_MY_TYPE_INDEX_HPP
// We'll use ctti_type_info as a base class for my_type_info
#include <boost/type_index/ctti_type_info.hpp>
namespace my_namespace {
struct my_type_index: public ctti_type_info {
my_type_index(const ctti_type_info::type_info_t& data) BOOST_NOEXCEPT
: ctti_type_info(data)
{}
template <class T>
static inline my_type_info construct_runtime(const T& value) BOOST_NOEXCEPT {
return value.type_info();
}
};
} // namespace my_namespace
#endif // MY_NAMESPACE_MY_TYPE_INDEX_HPP
//] [/type_index_my_type_index]