2 // Copyright (c) Antony Polukhin, 2013-2014.
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_CTTI_TYPE_INDEX_HPP
10 #define BOOST_TYPE_INDEX_CTTI_TYPE_INDEX_HPP
12 /// \file ctti_type_index.hpp
13 /// \brief Contains boost::typeindex::ctti_type_index class.
15 /// boost::typeindex::ctti_type_index class can be used as a drop-in replacement
16 /// for std::type_index.
18 /// It is used in situations when typeid() method is not available or
19 /// BOOST_TYPE_INDEX_FORCE_NO_RTTI_COMPATIBILITY macro is defined.
21 #include <boost/type_index/type_index_facade.hpp>
22 #include <boost/type_index/detail/compile_time_type_info.hpp>
25 #include <boost/static_assert.hpp>
26 #include <boost/type_traits/remove_cv.hpp>
27 #include <boost/type_traits/remove_reference.hpp>
29 #ifdef BOOST_HAS_PRAGMA_ONCE
33 namespace boost { namespace typeindex {
37 // That's the most trickiest part of the TypeIndex library:
38 // 1) we do not want to give user ability to manually construct and compare `struct-that-represents-type`
39 // 2) we need to distinguish between `struct-that-represents-type` and `const char*`
40 // 3) we need a thread-safe way to have references to instances `struct-that-represents-type`
41 // 4) we need a compile-time control to make sure that user does not copy or
42 // default construct `struct-that-represents-type`
44 // Solution would be the following:
47 /// Standard-layout class with private constructors and assignment operators.
49 /// You can not work with this class directly. The purpose of this class is to hold type info
50 /// \b when \b RTTI \b is \b off and allow ctti_type_index construction from itself.
54 /// const detail::ctti_data& foo();
56 /// type_index ti = type_index(foo());
57 /// std::cout << ti.pretty_name();
60 #ifndef BOOST_NO_CXX11_DELETED_FUNCTIONS
63 ctti_data(const ctti_data&) = delete;
64 ctti_data& operator=(const ctti_data&) = delete;
68 ctti_data(const ctti_data&);
69 ctti_data& operator=(const ctti_data&);
75 /// Helper method for getting detail::ctti_data of a template parameter T.
77 inline const detail::ctti_data& ctti_construct() BOOST_NOEXCEPT {
78 // Standard C++11, 5.2.10 Reinterpret cast:
79 // An object pointer can be explicitly converted to an object pointer of a different type. When a prvalue
80 // v of type "pointer to T1" is converted to the type "pointer to cv T2", the result is static_cast<cv
81 // T2*>(static_cast<cv void*>(v)) if both T1 and T2 are standard-layout types (3.9) and the alignment
82 // requirements of T2 are no stricter than those of T1, or if either type is void. Converting a prvalue of type
83 // "pointer to T1" to the type "pointer to T2" (where T1 and T2 are object types and where the alignment
84 // requirements of T2 are no stricter than those of T1) and back to its original type yields the original pointer
87 // Alignments are checked in `type_index_test_ctti_alignment.cpp` test.
88 return *reinterpret_cast<const detail::ctti_data*>(boost::detail::ctti<T>::n());
91 /// \class ctti_type_index
92 /// This class is a wrapper that pretends to work exactly like stl_type_index, but does
93 /// not require RTTI support. \b For \b description \b of \b functions \b see type_index_facade.
95 /// This class produces slightly longer type names, so consider using stl_type_index
96 /// in situations when typeid() is working.
97 class ctti_type_index: public type_index_facade<ctti_type_index, detail::ctti_data> {
98 const detail::ctti_data* data_;
100 inline std::size_t get_raw_name_length() const BOOST_NOEXCEPT;
103 typedef detail::ctti_data type_info_t;
105 inline ctti_type_index() BOOST_NOEXCEPT
106 : data_(&ctti_construct<void>())
109 inline ctti_type_index(const type_info_t& data) BOOST_NOEXCEPT
113 inline const type_info_t& type_info() const BOOST_NOEXCEPT;
114 inline const char* raw_name() const BOOST_NOEXCEPT;
115 inline std::string pretty_name() const;
116 inline std::size_t hash_code() const BOOST_NOEXCEPT;
119 inline static ctti_type_index type_id() BOOST_NOEXCEPT;
122 inline static ctti_type_index type_id_with_cvr() BOOST_NOEXCEPT;
125 inline static ctti_type_index type_id_runtime(const T& variable) BOOST_NOEXCEPT;
129 inline const ctti_type_index::type_info_t& ctti_type_index::type_info() const BOOST_NOEXCEPT {
135 inline ctti_type_index ctti_type_index::type_id() BOOST_NOEXCEPT {
136 typedef BOOST_DEDUCED_TYPENAME boost::remove_reference<T>::type no_ref_t;
137 typedef BOOST_DEDUCED_TYPENAME boost::remove_cv<no_ref_t>::type no_cvr_t;
138 return ctti_construct<no_cvr_t>();
144 inline ctti_type_index ctti_type_index::type_id_with_cvr() BOOST_NOEXCEPT {
145 return ctti_construct<T>();
150 inline ctti_type_index ctti_type_index::type_id_runtime(const T& variable) BOOST_NOEXCEPT {
151 return variable.boost_type_index_type_id_runtime_();
155 inline const char* ctti_type_index::raw_name() const BOOST_NOEXCEPT {
156 return reinterpret_cast<const char*>(data_);
159 inline std::size_t ctti_type_index::get_raw_name_length() const BOOST_NOEXCEPT {
160 return std::strlen(raw_name() + detail::ctti_skip_size_at_end);
164 inline std::string ctti_type_index::pretty_name() const {
165 std::size_t len = get_raw_name_length();
166 while (raw_name()[len - 1] == ' ') --len; // MSVC sometimes adds whitespaces
167 return std::string(raw_name(), len);
171 inline std::size_t ctti_type_index::hash_code() const BOOST_NOEXCEPT {
172 return boost::hash_range(raw_name(), raw_name() + get_raw_name_length());
176 }} // namespace boost::typeindex
178 #endif // BOOST_TYPE_INDEX_CTTI_TYPE_INDEX_HPP