From beaa53f460e9f18c970019c0c548dc645df304e7 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Fri, 7 Feb 2014 15:08:14 +0400 Subject: [PATCH] Add some examples of how to create type_indexes --- examples/my_type_index.cpp_ | 51 +++++++++++++++++++++++++++++++++++++ examples/my_type_index.hpp | 35 +++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 examples/my_type_index.cpp_ create mode 100644 examples/my_type_index.hpp diff --git a/examples/my_type_index.cpp_ b/examples/my_type_index.cpp_ new file mode 100644 index 0000000..e03a125 --- /dev/null +++ b/examples/my_type_index.cpp_ @@ -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 .) + +//[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 +#include + +class Base { +public: + virtual my_type_index type_info() const BOOST_NOEXCEPT { + return boost::typeind::type_info(); + } + virtual ~Base(){} +}; + +class Derived: public Base { +public: + virtual my_type_index type_info() const BOOST_NOEXCEPT { + return boost::typeind::type_id(); + } +}; + + +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] diff --git a/examples/my_type_index.hpp b/examples/my_type_index.hpp new file mode 100644 index 0000000..a1d6deb --- /dev/null +++ b/examples/my_type_index.hpp @@ -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 .) + +//[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 +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 + 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] +