]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/type_index/ctti_type_index.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / type_index / ctti_type_index.hpp
1 //
2 // Copyright (c) Antony Polukhin, 2013-2016.
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 that is constexpr if C++14 constexpr is supported by compiler.
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/functional/hash.hpp>
26 #include <boost/static_assert.hpp>
27 #include <boost/type_traits/remove_cv.hpp>
28 #include <boost/type_traits/remove_reference.hpp>
29
30 #ifdef BOOST_HAS_PRAGMA_ONCE
31 # pragma once
32 #endif
33
34 namespace boost { namespace typeindex {
35
36 namespace detail {
37
38 // That's the most trickiest part of the TypeIndex library:
39 // 1) we do not want to give user ability to manually construct and compare `struct-that-represents-type`
40 // 2) we need to distinguish between `struct-that-represents-type` and `const char*`
41 // 3) we need a thread-safe way to have references to instances `struct-that-represents-type`
42 // 4) we need a compile-time control to make sure that user does not copy or
43 // default construct `struct-that-represents-type`
44 //
45 // Solution would be the following:
46
47 /// \class ctti_data
48 /// Standard-layout class with private constructors and assignment operators.
49 ///
50 /// You can not work with this class directly. The purpose of this class is to hold type info
51 /// \b when \b RTTI \b is \b off and allow ctti_type_index construction from itself.
52 ///
53 /// \b Example:
54 /// \code
55 /// const detail::ctti_data& foo();
56 /// ...
57 /// type_index ti = type_index(foo());
58 /// std::cout << ti.pretty_name();
59 /// \endcode
60 class ctti_data {
61 #ifndef BOOST_NO_CXX11_DELETED_FUNCTIONS
62 public:
63 ctti_data() = delete;
64 ctti_data(const ctti_data&) = delete;
65 ctti_data& operator=(const ctti_data&) = delete;
66 #else
67 private:
68 ctti_data();
69 ctti_data(const ctti_data&);
70 ctti_data& operator=(const ctti_data&);
71 #endif
72 };
73
74 } // namespace detail
75
76 /// Helper method for getting detail::ctti_data of a template parameter T.
77 template <class T>
78 inline const detail::ctti_data& ctti_construct() BOOST_NOEXCEPT {
79 // Standard C++11, 5.2.10 Reinterpret cast:
80 // An object pointer can be explicitly converted to an object pointer of a different type. When a prvalue
81 // v of type "pointer to T1" is converted to the type "pointer to cv T2", the result is static_cast<cv
82 // T2*>(static_cast<cv void*>(v)) if both T1 and T2 are standard-layout types (3.9) and the alignment
83 // requirements of T2 are no stricter than those of T1, or if either type is void. Converting a prvalue of type
84 // "pointer to T1" to the type "pointer to T2" (where T1 and T2 are object types and where the alignment
85 // requirements of T2 are no stricter than those of T1) and back to its original type yields the original pointer
86 // value.
87 //
88 // Alignments are checked in `type_index_test_ctti_alignment.cpp` test.
89 return *reinterpret_cast<const detail::ctti_data*>(boost::detail::ctti<T>::n());
90 }
91
92 /// \class ctti_type_index
93 /// This class is a wrapper that pretends to work exactly like stl_type_index, but does
94 /// not require RTTI support. \b For \b description \b of \b functions \b see type_index_facade.
95 ///
96 /// This class on C++14 compatible compilers has following functions marked as constexpr:
97 /// * default constructor
98 /// * copy constructors and assignemnt operations
99 /// * class methods: name(), before(const ctti_type_index& rhs), equal(const ctti_type_index& rhs)
100 /// * static methods type_id<T>(), type_id_with_cvr<T>()
101 /// * comparison operators
102 ///
103 /// This class produces slightly longer type names, so consider using stl_type_index
104 /// in situations when typeid() is working.
105 class ctti_type_index: public type_index_facade<ctti_type_index, detail::ctti_data> {
106 const char* data_;
107
108 inline std::size_t get_raw_name_length() const BOOST_NOEXCEPT;
109
110 BOOST_CXX14_CONSTEXPR inline explicit ctti_type_index(const char* data) BOOST_NOEXCEPT
111 : data_(data)
112 {}
113
114 public:
115 typedef detail::ctti_data type_info_t;
116
117 BOOST_CXX14_CONSTEXPR inline ctti_type_index() BOOST_NOEXCEPT
118 : data_(boost::detail::ctti<void>::n())
119 {}
120
121 inline ctti_type_index(const type_info_t& data) BOOST_NOEXCEPT
122 : data_(reinterpret_cast<const char*>(&data))
123 {}
124
125 inline const type_info_t& type_info() const BOOST_NOEXCEPT;
126 BOOST_CXX14_CONSTEXPR inline const char* raw_name() const BOOST_NOEXCEPT;
127 BOOST_CXX14_CONSTEXPR inline const char* name() const BOOST_NOEXCEPT;
128 inline std::string pretty_name() const;
129 inline std::size_t hash_code() const BOOST_NOEXCEPT;
130
131 BOOST_CXX14_CONSTEXPR inline bool equal(const ctti_type_index& rhs) const BOOST_NOEXCEPT;
132 BOOST_CXX14_CONSTEXPR inline bool before(const ctti_type_index& rhs) const BOOST_NOEXCEPT;
133
134 template <class T>
135 BOOST_CXX14_CONSTEXPR inline static ctti_type_index type_id() BOOST_NOEXCEPT;
136
137 template <class T>
138 BOOST_CXX14_CONSTEXPR inline static ctti_type_index type_id_with_cvr() BOOST_NOEXCEPT;
139
140 template <class T>
141 inline static ctti_type_index type_id_runtime(const T& variable) BOOST_NOEXCEPT;
142 };
143
144
145 inline const ctti_type_index::type_info_t& ctti_type_index::type_info() const BOOST_NOEXCEPT {
146 return *reinterpret_cast<const detail::ctti_data*>(data_);
147 }
148
149
150 BOOST_CXX14_CONSTEXPR inline bool ctti_type_index::equal(const ctti_type_index& rhs) const BOOST_NOEXCEPT {
151 const char* const left = raw_name();
152 const char* const right = rhs.raw_name();
153 return /*left == right ||*/ !boost::typeindex::detail::constexpr_strcmp(left, right);
154 }
155
156 BOOST_CXX14_CONSTEXPR inline bool ctti_type_index::before(const ctti_type_index& rhs) const BOOST_NOEXCEPT {
157 const char* const left = raw_name();
158 const char* const right = rhs.raw_name();
159 return /*left != right &&*/ boost::typeindex::detail::constexpr_strcmp(left, right) < 0;
160 }
161
162
163 template <class T>
164 BOOST_CXX14_CONSTEXPR inline ctti_type_index ctti_type_index::type_id() BOOST_NOEXCEPT {
165 typedef BOOST_DEDUCED_TYPENAME boost::remove_reference<T>::type no_ref_t;
166 typedef BOOST_DEDUCED_TYPENAME boost::remove_cv<no_ref_t>::type no_cvr_t;
167 return ctti_type_index(boost::detail::ctti<no_cvr_t>::n());
168 }
169
170
171
172 template <class T>
173 BOOST_CXX14_CONSTEXPR inline ctti_type_index ctti_type_index::type_id_with_cvr() BOOST_NOEXCEPT {
174 return ctti_type_index(boost::detail::ctti<T>::n());
175 }
176
177
178 template <class T>
179 inline ctti_type_index ctti_type_index::type_id_runtime(const T& variable) BOOST_NOEXCEPT {
180 return variable.boost_type_index_type_id_runtime_();
181 }
182
183
184 BOOST_CXX14_CONSTEXPR inline const char* ctti_type_index::raw_name() const BOOST_NOEXCEPT {
185 return data_;
186 }
187
188
189 BOOST_CXX14_CONSTEXPR inline const char* ctti_type_index::name() const BOOST_NOEXCEPT {
190 return data_;
191 }
192
193 inline std::size_t ctti_type_index::get_raw_name_length() const BOOST_NOEXCEPT {
194 return std::strlen(raw_name() + detail::ctti_skip_size_at_end);
195 }
196
197
198 inline std::string ctti_type_index::pretty_name() const {
199 std::size_t len = get_raw_name_length();
200 while (raw_name()[len - 1] == ' ') --len; // MSVC sometimes adds whitespaces
201 return std::string(raw_name(), len);
202 }
203
204
205 inline std::size_t ctti_type_index::hash_code() const BOOST_NOEXCEPT {
206 return boost::hash_range(raw_name(), raw_name() + get_raw_name_length());
207 }
208
209
210 }} // namespace boost::typeindex
211
212 #endif // BOOST_TYPE_INDEX_CTTI_TYPE_INDEX_HPP
213