//
-// Copyright (c) Antony Polukhin, 2013-2014.
+// Copyright (c) Antony Polukhin, 2013-2018.
//
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
#define BOOST_TYPE_INDEX_CTTI_TYPE_INDEX_HPP
/// \file ctti_type_index.hpp
-/// \brief Contains boost::typeindex::ctti_type_index class.
+/// \brief Contains boost::typeindex::ctti_type_index class that is constexpr if C++14 constexpr is supported by compiler.
///
-/// boost::typeindex::ctti_type_index class can be used as a drop-in replacement
+/// boost::typeindex::ctti_type_index class can be used as a drop-in replacement
/// for std::type_index.
///
/// It is used in situations when typeid() method is not available or
#include <boost/type_index/detail/compile_time_type_info.hpp>
#include <cstring>
+#include <boost/container_hash/hash.hpp>
#include <boost/static_assert.hpp>
#include <boost/type_traits/remove_cv.hpp>
#include <boost/type_traits/remove_reference.hpp>
}
/// \class ctti_type_index
-/// This class is a wrapper that pretends to work exactly like stl_type_index, but does
+/// This class is a wrapper that pretends to work exactly like stl_type_index, but does
/// not require RTTI support. \b For \b description \b of \b functions \b see type_index_facade.
///
-/// This class produces slightly longer type names, so consider using stl_type_index
+/// This class on C++14 compatible compilers has following functions marked as constexpr:
+/// * default constructor
+/// * copy constructors and assignemnt operations
+/// * class methods: name(), before(const ctti_type_index& rhs), equal(const ctti_type_index& rhs)
+/// * static methods type_id<T>(), type_id_with_cvr<T>()
+/// * comparison operators
+///
+/// This class produces slightly longer type names, so consider using stl_type_index
/// in situations when typeid() is working.
class ctti_type_index: public type_index_facade<ctti_type_index, detail::ctti_data> {
- const detail::ctti_data* data_;
+ const char* data_;
inline std::size_t get_raw_name_length() const BOOST_NOEXCEPT;
+ BOOST_CXX14_CONSTEXPR inline explicit ctti_type_index(const char* data) BOOST_NOEXCEPT
+ : data_(data)
+ {}
+
public:
typedef detail::ctti_data type_info_t;
- inline ctti_type_index() BOOST_NOEXCEPT
- : data_(&ctti_construct<void>())
+ BOOST_CXX14_CONSTEXPR inline ctti_type_index() BOOST_NOEXCEPT
+ : data_(boost::detail::ctti<void>::n())
{}
inline ctti_type_index(const type_info_t& data) BOOST_NOEXCEPT
- : data_(&data)
+ : data_(reinterpret_cast<const char*>(&data))
{}
- inline const type_info_t& type_info() const BOOST_NOEXCEPT;
- inline const char* raw_name() const BOOST_NOEXCEPT;
+ inline const type_info_t& type_info() const BOOST_NOEXCEPT;
+ BOOST_CXX14_CONSTEXPR inline const char* raw_name() const BOOST_NOEXCEPT;
+ BOOST_CXX14_CONSTEXPR inline const char* name() const BOOST_NOEXCEPT;
inline std::string pretty_name() const;
inline std::size_t hash_code() const BOOST_NOEXCEPT;
+ BOOST_CXX14_CONSTEXPR inline bool equal(const ctti_type_index& rhs) const BOOST_NOEXCEPT;
+ BOOST_CXX14_CONSTEXPR inline bool before(const ctti_type_index& rhs) const BOOST_NOEXCEPT;
+
template <class T>
- inline static ctti_type_index type_id() BOOST_NOEXCEPT;
+ BOOST_CXX14_CONSTEXPR inline static ctti_type_index type_id() BOOST_NOEXCEPT;
template <class T>
- inline static ctti_type_index type_id_with_cvr() BOOST_NOEXCEPT;
+ BOOST_CXX14_CONSTEXPR inline static ctti_type_index type_id_with_cvr() BOOST_NOEXCEPT;
template <class T>
inline static ctti_type_index type_id_runtime(const T& variable) BOOST_NOEXCEPT;
inline const ctti_type_index::type_info_t& ctti_type_index::type_info() const BOOST_NOEXCEPT {
- return *data_;
+ return *reinterpret_cast<const detail::ctti_data*>(data_);
+}
+
+
+BOOST_CXX14_CONSTEXPR inline bool ctti_type_index::equal(const ctti_type_index& rhs) const BOOST_NOEXCEPT {
+ const char* const left = raw_name();
+ const char* const right = rhs.raw_name();
+ return /*left == right ||*/ !boost::typeindex::detail::constexpr_strcmp(left, right);
+}
+
+BOOST_CXX14_CONSTEXPR inline bool ctti_type_index::before(const ctti_type_index& rhs) const BOOST_NOEXCEPT {
+ const char* const left = raw_name();
+ const char* const right = rhs.raw_name();
+ return /*left != right &&*/ boost::typeindex::detail::constexpr_strcmp(left, right) < 0;
}
template <class T>
-inline ctti_type_index ctti_type_index::type_id() BOOST_NOEXCEPT {
+BOOST_CXX14_CONSTEXPR inline ctti_type_index ctti_type_index::type_id() BOOST_NOEXCEPT {
typedef BOOST_DEDUCED_TYPENAME boost::remove_reference<T>::type no_ref_t;
typedef BOOST_DEDUCED_TYPENAME boost::remove_cv<no_ref_t>::type no_cvr_t;
- return ctti_construct<no_cvr_t>();
+ return ctti_type_index(boost::detail::ctti<no_cvr_t>::n());
}
template <class T>
-inline ctti_type_index ctti_type_index::type_id_with_cvr() BOOST_NOEXCEPT {
- return ctti_construct<T>();
+BOOST_CXX14_CONSTEXPR inline ctti_type_index ctti_type_index::type_id_with_cvr() BOOST_NOEXCEPT {
+ return ctti_type_index(boost::detail::ctti<T>::n());
}
}
-inline const char* ctti_type_index::raw_name() const BOOST_NOEXCEPT {
- return reinterpret_cast<const char*>(data_);
+BOOST_CXX14_CONSTEXPR inline const char* ctti_type_index::raw_name() const BOOST_NOEXCEPT {
+ return data_;
+}
+
+
+BOOST_CXX14_CONSTEXPR inline const char* ctti_type_index::name() const BOOST_NOEXCEPT {
+ return data_;
}
inline std::size_t ctti_type_index::get_raw_name_length() const BOOST_NOEXCEPT {