1 | // |
2 | // Copyright (c) Antony Polukhin, 2013-2014. |
3 | // |
4 | // |
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) |
7 | // |
8 | |
9 | #ifndef BOOST_TYPE_INDEX_CTTI_TYPE_INDEX_HPP |
10 | #define BOOST_TYPE_INDEX_CTTI_TYPE_INDEX_HPP |
11 | |
12 | /// \file ctti_type_index.hpp |
13 | /// \brief Contains boost::typeindex::ctti_type_index class. |
14 | /// |
15 | /// boost::typeindex::ctti_type_index class can be used as a drop-in replacement |
16 | /// for std::type_index. |
17 | /// |
18 | /// It is used in situations when typeid() method is not available or |
19 | /// BOOST_TYPE_INDEX_FORCE_NO_RTTI_COMPATIBILITY macro is defined. |
20 | |
21 | #include <boost/type_index/type_index_facade.hpp> |
22 | #include <boost/type_index/detail/compile_time_type_info.hpp> |
23 | |
24 | #include <cstring> |
25 | #include <boost/static_assert.hpp> |
26 | #include <boost/type_traits/remove_cv.hpp> |
27 | #include <boost/type_traits/remove_reference.hpp> |
28 | |
29 | #ifdef BOOST_HAS_PRAGMA_ONCE |
30 | # pragma once |
31 | #endif |
32 | |
33 | namespace boost { namespace typeindex { |
34 | |
35 | namespace detail { |
36 | |
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` |
43 | // |
44 | // Solution would be the following: |
45 | |
46 | /// \class ctti_data |
47 | /// Standard-layout class with private constructors and assignment operators. |
48 | /// |
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. |
51 | /// |
52 | /// \b Example: |
53 | /// \code |
54 | /// const detail::ctti_data& foo(); |
55 | /// ... |
56 | /// type_index ti = type_index(foo()); |
57 | /// std::cout << ti.pretty_name(); |
58 | /// \endcode |
59 | class ctti_data { |
60 | #ifndef BOOST_NO_CXX11_DELETED_FUNCTIONS |
61 | public: |
62 | ctti_data() = delete; |
63 | ctti_data(const ctti_data&) = delete; |
64 | ctti_data& operator=(const ctti_data&) = delete; |
65 | #else |
66 | private: |
67 | ctti_data(); |
68 | ctti_data(const ctti_data&); |
69 | ctti_data& operator=(const ctti_data&); |
70 | #endif |
71 | }; |
72 | |
73 | } // namespace detail |
74 | |
75 | /// Helper method for getting detail::ctti_data of a template parameter T. |
76 | template <class 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 |
85 | // value. |
86 | // |
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()); |
89 | } |
90 | |
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. |
94 | /// |
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_; |
99 | |
100 | inline std::size_t get_raw_name_length() const BOOST_NOEXCEPT; |
101 | |
102 | public: |
103 | typedef detail::ctti_data type_info_t; |
104 | |
105 | inline ctti_type_index() BOOST_NOEXCEPT |
106 | : data_(&ctti_construct<void>()) |
107 | {} |
108 | |
109 | inline ctti_type_index(const type_info_t& data) BOOST_NOEXCEPT |
110 | : data_(&data) |
111 | {} |
112 | |
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; |
117 | |
118 | template <class T> |
119 | inline static ctti_type_index type_id() BOOST_NOEXCEPT; |
120 | |
121 | template <class T> |
122 | inline static ctti_type_index type_id_with_cvr() BOOST_NOEXCEPT; |
123 | |
124 | template <class T> |
125 | inline static ctti_type_index type_id_runtime(const T& variable) BOOST_NOEXCEPT; |
126 | }; |
127 | |
128 | |
129 | inline const ctti_type_index::type_info_t& ctti_type_index::type_info() const BOOST_NOEXCEPT { |
130 | return *data_; |
131 | } |
132 | |
133 | |
134 | template <class T> |
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>(); |
139 | } |
140 | |
141 | |
142 | |
143 | template <class T> |
144 | inline ctti_type_index ctti_type_index::type_id_with_cvr() BOOST_NOEXCEPT { |
145 | return ctti_construct<T>(); |
146 | } |
147 | |
148 | |
149 | template <class 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_(); |
152 | } |
153 | |
154 | |
155 | inline const char* ctti_type_index::raw_name() const BOOST_NOEXCEPT { |
156 | return reinterpret_cast<const char*>(data_); |
157 | } |
158 | |
159 | inline std::size_t ctti_type_index::get_raw_name_length() const BOOST_NOEXCEPT { |
160 | return std::strlen(s: raw_name() + detail::ctti_skip_size_at_end); |
161 | } |
162 | |
163 | |
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); |
168 | } |
169 | |
170 | |
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()); |
173 | } |
174 | |
175 | |
176 | }} // namespace boost::typeindex |
177 | |
178 | #endif // BOOST_TYPE_INDEX_CTTI_TYPE_INDEX_HPP |
179 | |
180 | |