1//
2// Copyright 2012-2023 Antony Polukhin.
3//
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7
8#ifndef BOOST_TYPE_INDEX_HPP
9#define BOOST_TYPE_INDEX_HPP
10
11/// \file boost/type_index.hpp
12/// \brief Includes minimal set of headers required to use the Boost.TypeIndex library.
13///
14/// By inclusion of this file most optimal type index classes will be included and used
15/// as a boost::typeindex::type_index and boost::typeindex::type_info.
16
17#include <boost/config.hpp>
18
19#ifdef BOOST_HAS_PRAGMA_ONCE
20# pragma once
21#endif
22
23
24
25#include <boost/config/pragma_message.hpp>
26#if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) || \
27 defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) || \
28 defined(BOOST_NO_CXX11_CONSTEXPR) || \
29 defined(BOOST_NO_CXX11_NULLPTR) || \
30 defined(BOOST_NO_CXX11_NOEXCEPT) || \
31 defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) || \
32 defined(BOOST_NO_CXX11_FINAL) || \
33 defined(BOOST_NO_CXX11_ALIGNOF) || \
34 defined(BOOST_NO_CXX11_STATIC_ASSERT) || \
35 defined(BOOST_NO_CXX11_SMART_PTR) || \
36 defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) || \
37 defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
38
39BOOST_PRAGMA_MESSAGE("C++03 support is deprecated in Boost.TypeIndex 1.82 and will be removed in Boost.TypeIndex 1.84.")
40
41#endif
42
43#if defined(BOOST_TYPE_INDEX_USER_TYPEINDEX)
44# include BOOST_TYPE_INDEX_USER_TYPEINDEX
45# ifdef BOOST_HAS_PRAGMA_DETECT_MISMATCH
46# pragma detect_mismatch( "boost__type_index__abi", "user defined type_index class is used: " BOOST_STRINGIZE(BOOST_TYPE_INDEX_USER_TYPEINDEX))
47# endif
48#elif (!defined(BOOST_NO_RTTI) && !defined(BOOST_TYPE_INDEX_FORCE_NO_RTTI_COMPATIBILITY)) || defined(BOOST_MSVC)
49# include <boost/type_index/stl_type_index.hpp>
50# if defined(BOOST_NO_RTTI) || defined(BOOST_TYPE_INDEX_FORCE_NO_RTTI_COMPATIBILITY)
51# include <boost/type_index/detail/stl_register_class.hpp>
52# ifdef BOOST_HAS_PRAGMA_DETECT_MISMATCH
53# pragma detect_mismatch( "boost__type_index__abi", "RTTI is off - typeid() is used only for templates")
54# endif
55# else
56# ifdef BOOST_HAS_PRAGMA_DETECT_MISMATCH
57# pragma detect_mismatch( "boost__type_index__abi", "RTTI is used")
58# endif
59# endif
60#else
61# include <boost/type_index/ctti_type_index.hpp>
62# include <boost/type_index/detail/ctti_register_class.hpp>
63# ifdef BOOST_HAS_PRAGMA_DETECT_MISMATCH
64# pragma detect_mismatch( "boost__type_index__abi", "RTTI is off - using CTTI")
65# endif
66#endif
67
68#ifndef BOOST_TYPE_INDEX_REGISTER_CLASS
69#define BOOST_TYPE_INDEX_REGISTER_CLASS
70#endif
71
72namespace boost { namespace typeindex {
73
74#if defined(BOOST_TYPE_INDEX_DOXYGEN_INVOKED)
75
76/// \def BOOST_TYPE_INDEX_FUNCTION_SIGNATURE
77/// BOOST_TYPE_INDEX_FUNCTION_SIGNATURE is used by boost::typeindex::ctti_type_index class to
78/// deduce the name of a type. If your compiler is not recognized
79/// by the TypeIndex library and you wish to work with boost::typeindex::ctti_type_index, you may
80/// define this macro by yourself.
81///
82/// BOOST_TYPE_INDEX_FUNCTION_SIGNATURE must be defined to a compiler specific macro
83/// that outputs the \b whole function signature \b including \b template \b parameters.
84///
85/// If your compiler is not recognised and BOOST_TYPE_INDEX_FUNCTION_SIGNATURE is not defined,
86/// then a compile-time error will arise at any attempt to use boost::typeindex::ctti_type_index classes.
87///
88/// See BOOST_TYPE_INDEX_REGISTER_CTTI_PARSING_PARAMS and BOOST_TYPE_INDEX_CTTI_USER_DEFINED_PARSING
89/// for an information of how to tune the implementation to make a nice pretty_name() output.
90#define BOOST_TYPE_INDEX_FUNCTION_SIGNATURE BOOST_CURRENT_FUNCTION
91
92/// \def BOOST_TYPE_INDEX_CTTI_USER_DEFINED_PARSING
93/// This is a helper macro for making correct pretty_names() with RTTI off.
94///
95/// BOOST_TYPE_INDEX_CTTI_USER_DEFINED_PARSING macro may be defined to
96/// '(begin_skip, end_skip, runtime_skip, runtime_skip_until)' with parameters for adding a
97/// support for compilers, that by default are not recognized by TypeIndex library.
98///
99/// \b Example:
100///
101/// Imagine the situation when
102/// \code boost::typeindex::ctti_type_index::type_id<int>().pretty_name() \endcode
103/// returns the following string:
104/// \code "static const char *boost::detail::ctti<int>::n() [T = int]" \endcode
105/// and \code boost::typeindex::ctti_type_index::type_id<short>().pretty_name() \endcode returns the following:
106/// \code "static const char *boost::detail::ctti<short>::n() [T = short]" \endcode
107///
108/// As we may see first 39 characters are "static const char *boost::detail::ctti<" and they do not depend on
109/// the type T. After first 39 characters we have a human readable type name which is duplicated at the end
110/// of a string. String always ends on ']', which consumes 1 character.
111///
112/// Now if we define `BOOST_TYPE_INDEX_CTTI_USER_DEFINED_PARSING` to
113/// `(39, 1, false, "")` we'll be getting \code "int>::n() [T = int" \endcode
114/// for `boost::typeindex::ctti_type_index::type_id<int>().pretty_name()` and \code "short>::n() [T = short" \endcode
115/// for `boost::typeindex::ctti_type_index::type_id<short>().pretty_name()`.
116///
117/// Now we need to take additional care of the characters that go before the last mention of our type. We'll
118/// do that by telling the macro that we need to cut off everything that goes before the "T = " including the "T = "
119/// itself:
120///
121/// \code (39, 1, true, "T = ") \endcode
122///
123/// In case of GCC or Clang command line we need to add the following line while compiling all the sources:
124///
125/// \code
126/// -DBOOST_TYPE_INDEX_CTTI_USER_DEFINED_PARSING='(39, 1, true, "T = ")'
127/// \endcode
128/// \param begin_skip How many characters must be skipped at the beginning of the type holding string.
129/// Must be a compile time constant.
130/// \param end_skip How many characters must be skipped at the end of the type holding string.
131/// Must be a compile time constant.
132/// \param runtime_skip Do we need additional checks at runtime to cut off the more characters.
133/// Must be `true` or `false`.
134/// \param runtime_skip_until Skip all the characters before the following string (including the string itself).
135/// Must be a compile time array of characters.
136///
137/// See [RTTI emulation limitations](boost_typeindex/rtti_emulation_limitations.html) for more info.
138#define BOOST_TYPE_INDEX_CTTI_USER_DEFINED_PARSING (0, 0, false, "")
139
140
141 /// Depending on a compiler flags, optimal implementation of type_index will be used
142 /// as a default boost::typeindex::type_index.
143 ///
144 /// Could be a boost::typeindex::stl_type_index, boost::typeindex::ctti_type_index or
145 /// user defined type_index class.
146 ///
147 /// \b See boost::typeindex::type_index_facade for a full description of type_index functions.
148 typedef platform_specific type_index;
149#elif defined(BOOST_TYPE_INDEX_USER_TYPEINDEX)
150 // Nothing to do
151#elif (!defined(BOOST_NO_RTTI) && !defined(BOOST_TYPE_INDEX_FORCE_NO_RTTI_COMPATIBILITY)) || defined(BOOST_MSVC)
152 typedef boost::typeindex::stl_type_index type_index;
153#else
154 typedef boost::typeindex::ctti_type_index type_index;
155#endif
156
157/// Depending on a compiler flags, optimal implementation of type_info will be used
158/// as a default boost::typeindex::type_info.
159///
160/// Could be a std::type_info, boost::typeindex::detail::ctti_data or
161/// some user defined class.
162///
163/// type_info \b is \b not copyable or default constructible. It is \b not assignable too!
164typedef type_index::type_info_t type_info;
165
166#if defined(BOOST_TYPE_INDEX_DOXYGEN_INVOKED)
167
168/// \def BOOST_TYPE_INDEX_USER_TYPEINDEX
169/// BOOST_TYPE_INDEX_USER_TYPEINDEX can be defined to the path to header file
170/// with user provided implementation of type_index.
171///
172/// See [Making a custom type_index](boost_typeindex/making_a_custom_type_index.html) section
173/// of documentation for usage example.
174#define BOOST_TYPE_INDEX_USER_TYPEINDEX <full/absolute/path/to/header/with/type_index.hpp>
175
176
177/// \def BOOST_TYPE_INDEX_REGISTER_CLASS
178/// BOOST_TYPE_INDEX_REGISTER_CLASS is used to help to emulate RTTI.
179/// Put this macro into the public section of polymorphic class to allow runtime type detection.
180///
181/// Depending on the typeid() availability this macro will expand to nothing or to virtual helper function
182/// `virtual const type_info& boost_type_info_type_id_runtime_() const noexcept`.
183///
184/// \b Example:
185/// \code
186/// class A {
187/// public:
188/// BOOST_TYPE_INDEX_REGISTER_CLASS
189/// virtual ~A(){}
190/// };
191///
192/// struct B: public A {
193/// BOOST_TYPE_INDEX_REGISTER_CLASS
194/// };
195///
196/// struct C: public B {
197/// BOOST_TYPE_INDEX_REGISTER_CLASS
198/// };
199///
200/// ...
201///
202/// C c1;
203/// A* pc1 = &c1;
204/// assert(boost::typeindex::type_id<C>() == boost::typeindex::type_id_runtime(*pc1));
205/// \endcode
206#define BOOST_TYPE_INDEX_REGISTER_CLASS nothing-or-some-virtual-functions
207
208/// \def BOOST_TYPE_INDEX_FORCE_NO_RTTI_COMPATIBILITY
209/// BOOST_TYPE_INDEX_FORCE_NO_RTTI_COMPATIBILITY is a helper macro that must be defined if mixing
210/// RTTI on/off modules. See
211/// [Mixing sources with RTTI on and RTTI off](boost_typeindex/mixing_sources_with_rtti_on_and_.html)
212/// section of documentation for more info.
213#define BOOST_TYPE_INDEX_FORCE_NO_RTTI_COMPATIBILITY
214
215#endif // defined(BOOST_TYPE_INDEX_DOXYGEN_INVOKED)
216
217
218/// Function to get boost::typeindex::type_index for a type T.
219/// Removes const, volatile && and & modifiers from T.
220///
221/// \b Example:
222/// \code
223/// type_index ti = type_id<int&>();
224/// std::cout << ti.pretty_name(); // Outputs 'int'
225/// \endcode
226///
227/// \tparam T Type for which type_index must be created.
228/// \throw Nothing.
229/// \return boost::typeindex::type_index with information about the specified type T.
230template <class T>
231inline type_index type_id() BOOST_NOEXCEPT {
232 return type_index::type_id<T>();
233}
234
235/// Function for constructing boost::typeindex::type_index instance for type T.
236/// Does not remove const, volatile, & and && modifiers from T.
237///
238/// If T has no const, volatile, & and && modifiers, then returns exactly
239/// the same result as in case of calling `type_id<T>()`.
240///
241/// \b Example:
242/// \code
243/// type_index ti = type_id_with_cvr<int&>();
244/// std::cout << ti.pretty_name(); // Outputs 'int&'
245/// \endcode
246///
247/// \tparam T Type for which type_index must be created.
248/// \throw Nothing.
249/// \return boost::typeindex::type_index with information about the specified type T.
250template <class T>
251inline type_index type_id_with_cvr() BOOST_NOEXCEPT {
252 return type_index::type_id_with_cvr<T>();
253}
254
255/// Function that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index.
256///
257/// Returns runtime information about specified type.
258///
259/// \b Requirements: RTTI available or Base and Derived classes must be marked with BOOST_TYPE_INDEX_REGISTER_CLASS.
260///
261/// \b Example:
262/// \code
263/// struct Base { virtual ~Base(){} };
264/// struct Derived: public Base {};
265/// ...
266/// Derived d;
267/// Base& b = d;
268/// type_index ti = type_id_runtime(b);
269/// std::cout << ti.pretty_name(); // Outputs 'Derived'
270/// \endcode
271///
272/// \param runtime_val Variable which runtime type must be returned.
273/// \throw Nothing.
274/// \return boost::typeindex::type_index with information about the specified variable.
275template <class T>
276inline type_index type_id_runtime(const T& runtime_val) BOOST_NOEXCEPT {
277 return type_index::type_id_runtime(runtime_val);
278}
279
280}} // namespace boost::typeindex
281
282
283
284#endif // BOOST_TYPE_INDEX_HPP
285
286