2 // Copyright (c) Chris Glover, 2016.
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_TYPE_INDEX_RUNTIME_CAST_REGISTER_RUNTIME_CLASS_HPP
10 #define BOOST_TYPE_INDEX_RUNTIME_CAST_REGISTER_RUNTIME_CLASS_HPP
12 /// \file register_runtime_class.hpp
13 /// \brief Contains the macro BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS
14 #include <boost/type_index.hpp>
15 #include <boost/preprocessor/seq/for_each.hpp>
17 #ifdef BOOST_HAS_PRAGMA_ONCE
21 namespace boost { namespace typeindex {
26 inline type_index runtime_class_construct_type_id(T const*) {
34 #define BOOST_TYPE_INDEX_CHECK_BASE_(r, data, Base) \
35 if(void const* ret_val = this->Base::boost_type_index_find_instance_(idx)) return ret_val;
39 /// \def BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS
40 /// \brief Macro used to make a class compatible with boost::typeindex::runtime_cast
42 /// BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS generates a virtual function
43 /// in the current class that, when combined with the supplied base class information, allows
44 /// boost::typeindex::runtime_cast to accurately convert between dynamic types of instances of
45 /// the current class.
50 /// BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS(BOOST_TYPE_INDEX_NO_BASE_CLASS)
55 /// BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS(BOOST_TYPE_INDEX_NO_BASE_CLASS)
59 /// struct derived1 : base1 {
60 /// BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS((base1))
63 /// struct derived2 : base1, base2 {
64 /// BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS((base1)(base2))
69 /// base1* pb1 = get_object();
70 /// if(derived2* pb2 = boost::typeindex::runtime_cast<derived2*>(pb1))
74 /// \param base_class_seq A Boost.Preprocessor sequence of the current class' direct bases, or
75 /// BOOST_PP_SEQ_NIL if this class has no direct base classes.
76 #define BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS(base_class_seq) \
77 BOOST_TYPE_INDEX_REGISTER_CLASS \
78 virtual void const* boost_type_index_find_instance_(boost::typeindex::type_index const& idx) const BOOST_NOEXCEPT { \
79 if(idx == boost::typeindex::detail::runtime_class_construct_type_id(this)) \
81 BOOST_PP_SEQ_FOR_EACH(BOOST_TYPE_INDEX_CHECK_BASE_, _, base_class_seq) \
84 }} // namespace boost::typeindex
86 #define BOOST_TYPE_INDEX_NO_BASE_CLASS BOOST_PP_SEQ_NIL
88 #endif // BOOST_TYPE_INDEX_RUNTIME_CAST_REGISTER_RUNTIME_CLASS_HPP