]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/type_index/include/boost/type_index/ctti_type_index.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / type_index / include / 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/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 on C++14 compatible compilers has following functions marked as constexpr:
96 /// * default constructor
97 /// * copy constructors and assignemnt operations
98 /// * class methods: name(), before(const ctti_type_index& rhs), equal(const ctti_type_index& rhs)
99 /// * static methods type_id<T>(), type_id_with_cvr<T>()
100 /// * comparison operators
101 ///
102 /// This class produces slightly longer type names, so consider using stl_type_index
103 /// in situations when typeid() is working.
104 class ctti_type_index: public type_index_facade<ctti_type_index, detail::ctti_data> {
105 const char* data_;
106
107 inline std::size_t get_raw_name_length() const BOOST_NOEXCEPT;
108
109 BOOST_CXX14_CONSTEXPR inline explicit ctti_type_index(const char* data) BOOST_NOEXCEPT
110 : data_(data)
111 {}
112
113 public:
114 typedef detail::ctti_data type_info_t;
115
116 BOOST_CXX14_CONSTEXPR inline ctti_type_index() BOOST_NOEXCEPT
117 : data_(boost::detail::ctti<void>::n())
118 {}
119
120 inline ctti_type_index(const type_info_t& data) BOOST_NOEXCEPT
121 : data_(reinterpret_cast<const char*>(&data))
122 {}
123
124 inline const type_info_t& type_info() const BOOST_NOEXCEPT;
125 BOOST_CXX14_CONSTEXPR inline const char* raw_name() const BOOST_NOEXCEPT;
126 BOOST_CXX14_CONSTEXPR inline const char* name() const BOOST_NOEXCEPT;
127 inline std::string pretty_name() const;
128 inline std::size_t hash_code() const BOOST_NOEXCEPT;
129
130 BOOST_CXX14_CONSTEXPR inline bool equal(const ctti_type_index& rhs) const BOOST_NOEXCEPT;
131 BOOST_CXX14_CONSTEXPR inline bool before(const ctti_type_index& rhs) const BOOST_NOEXCEPT;
132
133 template <class T>
134 BOOST_CXX14_CONSTEXPR inline static ctti_type_index type_id() BOOST_NOEXCEPT;
135
136 template <class T>
137 BOOST_CXX14_CONSTEXPR inline static ctti_type_index type_id_with_cvr() BOOST_NOEXCEPT;
138
139 template <class T>
140 inline static ctti_type_index type_id_runtime(const T& variable) BOOST_NOEXCEPT;
141 };
142
143
144 inline const ctti_type_index::type_info_t& ctti_type_index::type_info() const BOOST_NOEXCEPT {
145 return *reinterpret_cast<const detail::ctti_data*>(data_);
146 }
147
148
149 BOOST_CXX14_CONSTEXPR inline bool ctti_type_index::equal(const ctti_type_index& rhs) const BOOST_NOEXCEPT {
150 const char* const left = raw_name();
151 const char* const right = rhs.raw_name();
152 return /*left == right ||*/ !boost::typeindex::detail::constexpr_strcmp(left, right);
153 }
154
155 BOOST_CXX14_CONSTEXPR inline bool ctti_type_index::before(const ctti_type_index& rhs) const BOOST_NOEXCEPT {
156 const char* const left = raw_name();
157 const char* const right = rhs.raw_name();
158 return /*left != right &&*/ boost::typeindex::detail::constexpr_strcmp(left, right) < 0;
159 }
160
161
162 template <class T>
163 BOOST_CXX14_CONSTEXPR inline ctti_type_index ctti_type_index::type_id() BOOST_NOEXCEPT {
164 typedef BOOST_DEDUCED_TYPENAME boost::remove_reference<T>::type no_ref_t;
165 typedef BOOST_DEDUCED_TYPENAME boost::remove_cv<no_ref_t>::type no_cvr_t;
166 return ctti_type_index(boost::detail::ctti<no_cvr_t>::n());
167 }
168
169
170
171 template <class T>
172 BOOST_CXX14_CONSTEXPR inline ctti_type_index ctti_type_index::type_id_with_cvr() BOOST_NOEXCEPT {
173 return ctti_type_index(boost::detail::ctti<T>::n());
174 }
175
176
177 template <class T>
178 inline ctti_type_index ctti_type_index::type_id_runtime(const T& variable) BOOST_NOEXCEPT {
179 return variable.boost_type_index_type_id_runtime_();
180 }
181
182
183 BOOST_CXX14_CONSTEXPR inline const char* ctti_type_index::raw_name() const BOOST_NOEXCEPT {
184 return data_;
185 }
186
187
188 BOOST_CXX14_CONSTEXPR inline const char* ctti_type_index::name() const BOOST_NOEXCEPT {
189 return data_;
190 }
191
192 inline std::size_t ctti_type_index::get_raw_name_length() const BOOST_NOEXCEPT {
193 return std::strlen(raw_name() + detail::ctti_skip_size_at_end);
194 }
195
196
197 inline std::string ctti_type_index::pretty_name() const {
198 std::size_t len = get_raw_name_length();
199 while (raw_name()[len - 1] == ' ') --len; // MSVC sometimes adds whitespaces
200 return std::string(raw_name(), len);
201 }
202
203
204 inline std::size_t ctti_type_index::hash_code() const BOOST_NOEXCEPT {
205 return boost::hash_range(raw_name(), raw_name() + get_raw_name_length());
206 }
207
208
209 }} // namespace boost::typeindex
210
211 #endif // BOOST_TYPE_INDEX_CTTI_TYPE_INDEX_HPP
212