2 // Copyright (c) 2013-2019 Antony Polukhin.
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_STL_TYPE_INDEX_HPP
10 #define BOOST_TYPE_INDEX_STL_TYPE_INDEX_HPP
12 /// \file stl_type_index.hpp
13 /// \brief Contains boost::typeindex::stl_type_index class.
15 /// boost::typeindex::stl_type_index class can be used as a drop-in replacement
16 /// for std::type_index.
18 /// It is used in situations when RTTI is enabled or typeid() method is available.
19 /// When typeid() is disabled or BOOST_TYPE_INDEX_FORCE_NO_RTTI_COMPATIBILITY macro
20 /// is defined boost::typeindex::ctti is usually used instead of boost::typeindex::stl_type_index.
22 #include <boost/type_index/type_index_facade.hpp>
24 // MSVC is capable of calling typeid(T) even when RTTI is off
25 #if defined(BOOST_NO_RTTI) && !defined(BOOST_MSVC)
26 #error "File boost/type_index/stl_type_index.ipp is not usable when typeid() is not available."
30 #include <cstring> // std::strcmp, std::strlen, std::strstr
32 #include <boost/static_assert.hpp>
33 #include <boost/throw_exception.hpp>
34 #include <boost/core/demangle.hpp>
35 #include <boost/type_traits/conditional.hpp>
36 #include <boost/type_traits/is_const.hpp>
37 #include <boost/type_traits/is_reference.hpp>
38 #include <boost/type_traits/is_volatile.hpp>
39 #include <boost/type_traits/remove_cv.hpp>
40 #include <boost/type_traits/remove_reference.hpp>
42 #if (defined(_MSC_VER) && _MSC_VER > 1600) \
43 || (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ > 5 && defined(__GXX_EXPERIMENTAL_CXX0X__)) \
44 || (defined(__GNUC__) && __GNUC__ > 4 && __cplusplus >= 201103)
45 # define BOOST_TYPE_INDEX_STD_TYPE_INDEX_HAS_HASH_CODE
47 # include <boost/container_hash/hash.hpp>
50 #if (defined(__EDG_VERSION__) && __EDG_VERSION__ < 245) \
51 || (defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 744)
52 # include <boost/type_traits/is_signed.hpp>
53 # include <boost/type_traits/make_signed.hpp>
54 # include <boost/type_traits/type_identity.hpp>
57 #ifdef BOOST_HAS_PRAGMA_ONCE
61 namespace boost { namespace typeindex {
63 /// \class stl_type_index
64 /// This class is a wrapper around std::type_info, that workarounds issues and provides
65 /// much more rich interface. \b For \b description \b of \b functions \b see type_index_facade.
67 /// This class requires typeid() to work. For cases when RTTI is disabled see ctti_type_index.
69 : public type_index_facade<
71 #ifdef BOOST_NO_STD_TYPEINFO
79 #ifdef BOOST_NO_STD_TYPEINFO
80 typedef type_info type_info_t;
82 typedef std::type_info type_info_t;
86 const type_info_t* data_;
89 inline stl_type_index() BOOST_NOEXCEPT
90 : data_(&typeid(void))
93 inline stl_type_index(const type_info_t& data) BOOST_NOEXCEPT
97 inline const type_info_t& type_info() const BOOST_NOEXCEPT;
99 inline const char* raw_name() const BOOST_NOEXCEPT;
100 inline const char* name() const BOOST_NOEXCEPT;
101 inline std::string pretty_name() const;
103 inline std::size_t hash_code() const BOOST_NOEXCEPT;
104 inline bool equal(const stl_type_index& rhs) const BOOST_NOEXCEPT;
105 inline bool before(const stl_type_index& rhs) const BOOST_NOEXCEPT;
108 inline static stl_type_index type_id() BOOST_NOEXCEPT;
111 inline static stl_type_index type_id_with_cvr() BOOST_NOEXCEPT;
114 inline static stl_type_index type_id_runtime(const T& value) BOOST_NOEXCEPT;
117 inline const stl_type_index::type_info_t& stl_type_index::type_info() const BOOST_NOEXCEPT {
122 inline const char* stl_type_index::raw_name() const BOOST_NOEXCEPT {
124 return data_->raw_name();
126 return data_->name();
130 inline const char* stl_type_index::name() const BOOST_NOEXCEPT {
131 return data_->name();
134 inline std::string stl_type_index::pretty_name() const {
135 static const char cvr_saver_name[] = "boost::typeindex::detail::cvr_saver<";
136 static BOOST_CONSTEXPR_OR_CONST std::string::size_type cvr_saver_name_len = sizeof(cvr_saver_name) - 1;
138 // In case of MSVC demangle() is a no-op, and name() already returns demangled name.
139 // In case of GCC and Clang (on non-Windows systems) name() returns mangled name and demangle() undecorates it.
140 const boost::core::scoped_demangled_name demangled_name(data_->name());
142 const char* begin = demangled_name.get();
144 boost::throw_exception(std::runtime_error("Type name demangling failed"));
147 const std::string::size_type len = std::strlen(begin);
148 const char* end = begin + len;
150 if (len > cvr_saver_name_len) {
151 const char* b = std::strstr(begin, cvr_saver_name);
153 b += cvr_saver_name_len;
155 // Trim leading spaces
156 while (*b == ' ') { // the string is zero terminated, we won't exceed the buffer size
160 // Skip the closing angle bracket
161 const char* e = end - 1;
162 while (e > b && *e != '>') {
166 // Trim trailing spaces
167 while (e > b && *(e - 1) == ' ') {
172 // Parsing seems to have succeeded, the type name is not empty
179 return std::string(begin, end);
183 inline std::size_t stl_type_index::hash_code() const BOOST_NOEXCEPT {
184 #ifdef BOOST_TYPE_INDEX_STD_TYPE_INDEX_HAS_HASH_CODE
185 return data_->hash_code();
187 return boost::hash_range(raw_name(), raw_name() + std::strlen(raw_name()));
194 // for this compiler at least, cross-shared-library type_info
195 // comparisons don't work, so we are using typeid(x).name() instead.
196 # if (defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 5))) \
198 || (defined(__sgi) && defined(__host_mips)) \
199 || (defined(__hpux) && defined(__HP_aCC)) \
200 || (defined(linux) && defined(__INTEL_COMPILER) && defined(__ICC))
201 # define BOOST_TYPE_INDEX_CLASSINFO_COMPARE_BY_NAMES
206 inline bool stl_type_index::equal(const stl_type_index& rhs) const BOOST_NOEXCEPT {
207 #ifdef BOOST_TYPE_INDEX_CLASSINFO_COMPARE_BY_NAMES
208 return raw_name() == rhs.raw_name() || !std::strcmp(raw_name(), rhs.raw_name());
210 return !!(*data_ == *rhs.data_);
214 inline bool stl_type_index::before(const stl_type_index& rhs) const BOOST_NOEXCEPT {
215 #ifdef BOOST_TYPE_INDEX_CLASSINFO_COMPARE_BY_NAMES
216 return raw_name() != rhs.raw_name() && std::strcmp(raw_name(), rhs.raw_name()) < 0;
218 return !!data_->before(*rhs.data_);
222 #undef BOOST_TYPE_INDEX_CLASSINFO_COMPARE_BY_NAMES
226 inline stl_type_index stl_type_index::type_id() BOOST_NOEXCEPT {
227 typedef BOOST_DEDUCED_TYPENAME boost::remove_reference<T>::type no_ref_t;
228 typedef BOOST_DEDUCED_TYPENAME boost::remove_cv<no_ref_t>::type no_cvr_prefinal_t;
230 # if (defined(__EDG_VERSION__) && __EDG_VERSION__ < 245) \
231 || (defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 744)
233 // Old EDG-based compilers seem to mistakenly distinguish 'integral' from 'signed integral'
234 // in typeid() expressions. Full template specialization for 'integral' fixes that issue:
235 typedef BOOST_DEDUCED_TYPENAME boost::conditional<
236 boost::is_signed<no_cvr_prefinal_t>::value,
237 boost::make_signed<no_cvr_prefinal_t>,
238 boost::type_identity<no_cvr_prefinal_t>
239 >::type no_cvr_prefinal_lazy_t;
241 typedef BOOST_DEDUCED_TYPENAME no_cvr_prefinal_t::type no_cvr_t;
243 typedef no_cvr_prefinal_t no_cvr_t;
246 return typeid(no_cvr_t);
250 template <class T> class cvr_saver{};
254 inline stl_type_index stl_type_index::type_id_with_cvr() BOOST_NOEXCEPT {
255 typedef BOOST_DEDUCED_TYPENAME boost::conditional<
256 boost::is_reference<T>::value || boost::is_const<T>::value || boost::is_volatile<T>::value,
257 detail::cvr_saver<T>,
266 inline stl_type_index stl_type_index::type_id_runtime(const T& value) BOOST_NOEXCEPT {
268 return value.boost_type_index_type_id_runtime_();
270 return typeid(value);
274 }} // namespace boost::typeindex
276 #undef BOOST_TYPE_INDEX_STD_TYPE_INDEX_HAS_HASH_CODE
278 #endif // BOOST_TYPE_INDEX_STL_TYPE_INDEX_HPP