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

source code of boost/libs/type_index/include/boost/type_index/ctti_type_index.hpp