mirror of
https://github.com/boostorg/type_index.git
synced 2025-07-31 12:57:17 +02:00
Still in the middle of heavy changes
This commit is contained in:
@ -11,18 +11,23 @@
|
||||
/// \file type_index.hpp
|
||||
/// \brief Includes all the headers of the Boost.TypeIndex library.
|
||||
///
|
||||
/// By inclusion of this file both classes (boost::type_index if RTTI is on and boost::template_index) will be available.
|
||||
/// By inclusion of this file all classes (boost::type_info + boost::type_index if RTTI is on
|
||||
/// and boost::template_index) will be available.
|
||||
///
|
||||
/// Consider including <boost/type_index/type_index_minimal.hpp> if you do not whant to include
|
||||
/// boost::template_index class while RTTI is available (this is recommended).
|
||||
/// Consider including <boost/type_index/type_index.hpp> if you do not whant to include
|
||||
/// boost::template_index class while RTTI is available.
|
||||
///
|
||||
/// Consider including <boost/type_index/type_info.hpp> if you do not whant to include
|
||||
/// boost::template_index and boost::type_index classes while RTTI is available.
|
||||
|
||||
// MS compatible compilers support #pragma once
|
||||
#if defined(_MSC_VER)
|
||||
# pragma once
|
||||
#endif
|
||||
|
||||
#include <boost/type_index/type_index_minimal.hpp>
|
||||
#include <boost/type_index/template_index_impl.hpp>
|
||||
#include <boost/type_index/type_info.hpp>
|
||||
#include <boost/type_index/type_index.hpp>
|
||||
#include <boost/type_index/template_info.hpp>
|
||||
|
||||
#endif // BOOST_TYPE_INDEX_HPP
|
||||
|
||||
|
@ -18,6 +18,8 @@
|
||||
///
|
||||
/// boost::type_index class is used in situations when RTTI is enabled.
|
||||
|
||||
#if !defined(BOOST_NO_RTTI) && !defined(BOOST_TYPE_INDEX_FORCE_NORTTI_COMPATIBILITY)
|
||||
|
||||
#include <boost/type_index/type_info.hpp>
|
||||
|
||||
#if !defined(BOOST_NO_IOSTREAM)
|
||||
@ -49,102 +51,20 @@ public:
|
||||
: pinfo_(&inf)
|
||||
{}
|
||||
|
||||
/// Factory method for constructing type_index instance for type T.
|
||||
/// Strips const, volatile and & modifiers from T.
|
||||
template <class T>
|
||||
static type_index construct() 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;
|
||||
|
||||
# if (defined(__EDG_VERSION__) && __EDG_VERSION__ < 245) \
|
||||
|| (defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 744)
|
||||
BOOST_STATIC_ASSERT_MSG( !boost::is_arithmetic<no_cvr_t>::type::value
|
||||
, "Your EDG-based compiler seems to mistakenly distinguish 'int' from 'signed int', in typeid() expressions.");
|
||||
#endif
|
||||
|
||||
return type_index(typeid(no_cvr_t));
|
||||
}
|
||||
|
||||
/// Factory method for constructing type_index instance for type T.
|
||||
/// Does not strip const, volatile, & and && modifiers from T.
|
||||
/// If T has no const, volatile, & and && modifiers, then returns exactly
|
||||
/// the same result as in case of calling `construct<T>()`.
|
||||
template <class T>
|
||||
static type_index construct_with_cvr() BOOST_NOEXCEPT {
|
||||
typedef typename boost::mpl::if_c<
|
||||
boost::is_reference<T>::value
|
||||
|| boost::is_const<T>::value
|
||||
|| boost::is_volatile<T>::value,
|
||||
detail::cvr_saver<T>,
|
||||
T
|
||||
>::type type;
|
||||
return construct<type>();
|
||||
}
|
||||
|
||||
/// Factory function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index.
|
||||
/// This method available only with RTTI enabled.
|
||||
template <class T>
|
||||
static type_index construct_rtti_only(T& rtti_val) {
|
||||
return type_index(typeid(rtti_val));
|
||||
}
|
||||
|
||||
/// Factory function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index.
|
||||
/// This method available only with RTTI enabled.
|
||||
template <class T>
|
||||
static type_index construct_rtti_only(T* rtti_val) {
|
||||
return type_index(typeid(rtti_val));
|
||||
}
|
||||
|
||||
/// Returns true if the type precedes the type of rhs in the collation order.
|
||||
/// The collation order is just an internal order.
|
||||
bool before(type_index const& rhs) const BOOST_NOEXCEPT {
|
||||
return !!pinfo_->before(*rhs.pinfo_);
|
||||
return pinfo_->before(rhs);
|
||||
}
|
||||
|
||||
/// Returns raw name
|
||||
const char* name() const BOOST_NOEXCEPT {
|
||||
#ifdef _MSC_VER
|
||||
return pinfo_->raw_name();
|
||||
#else
|
||||
return pinfo_->name();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// Returns user-friendly name
|
||||
std::string name_demangled() const {
|
||||
#if defined(__GNUC__)
|
||||
std::string ret;
|
||||
int status = 0;
|
||||
char* demang = abi::__cxa_demangle(pinfo_->name(), NULL, 0, &status);
|
||||
BOOST_ASSERT(!status);
|
||||
|
||||
BOOST_TRY {
|
||||
ret = demang; // may throw out of memory exception
|
||||
} BOOST_CATCH (...) {
|
||||
free(demang);
|
||||
BOOST_RETHROW;
|
||||
} BOOST_CATCH_END
|
||||
|
||||
free(demang);
|
||||
#else
|
||||
std::string ret = pinfo_->name();
|
||||
#endif
|
||||
std::string::size_type pos = ret.find("boost::detail::cvr_saver<");
|
||||
if (pos == std::string::npos) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
pos += sizeof("boost::detail::cvr_saver<") - 1;
|
||||
while (ret[pos] == ' ') {
|
||||
++ pos;
|
||||
}
|
||||
std::string::size_type end = ret.rfind(">");
|
||||
BOOST_ASSERT(end != std::string::npos);
|
||||
while (ret[end] == ' ') {
|
||||
-- end;
|
||||
}
|
||||
|
||||
return ret.substr(pos, end - pos);
|
||||
return pinfo_->name_demangled();
|
||||
}
|
||||
|
||||
#ifndef BOOST_TYPE_INDEX_DOXYGEN_INVOKED
|
||||
@ -263,37 +183,6 @@ inline bool operator >= (type_index::stl_type_info const& lhs, type_index const&
|
||||
|
||||
/// @endcond
|
||||
|
||||
/// Function, to get type_index for a type T. Strips const, volatile and & modifiers from T.
|
||||
template <class T>
|
||||
inline type_index type_id() BOOST_NOEXCEPT {
|
||||
return type_index::construct<T>();
|
||||
}
|
||||
|
||||
/// Function for constructing type_index instance for type T.
|
||||
/// Does not strip const, volatile, & and && modifiers from T.
|
||||
/// If T has no const, volatile, & and && modifiers, then returns exactly
|
||||
/// the same result as in case of calling `type_id<T>()`.
|
||||
template <class T>
|
||||
inline type_index type_id_with_cvr() BOOST_NOEXCEPT {
|
||||
return type_index::construct_with_cvr<T>();
|
||||
}
|
||||
|
||||
/// Function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index.
|
||||
/// This method available only with RTTI enabled. Without RTTI support it won't compile,
|
||||
/// producing a compile-time error with message: "boost::type_id_rtti_only(T&) requires RTTI"
|
||||
template <class T>
|
||||
inline type_index type_id_rtti_only(T& rtti_val) BOOST_NOEXCEPT {
|
||||
return type_index::construct_rtti_only(rtti_val);
|
||||
}
|
||||
|
||||
/// Function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index.
|
||||
/// This method available only with RTTI enabled. Without RTTI support it won't compile,
|
||||
/// producing a compile-time error with message: "boost::type_id_rtti_only(T*) requires RTTI"
|
||||
template <class T>
|
||||
inline type_index type_id_rtti_only(T* rtti_val) {
|
||||
return type_index::construct_rtti_only(rtti_val);
|
||||
}
|
||||
|
||||
/* *************** type_index free functions ******************* */
|
||||
|
||||
/// @cond
|
||||
@ -326,9 +215,9 @@ inline std::size_t hash_value(type_index const& v) BOOST_NOEXCEPT {
|
||||
|
||||
/// @endcond
|
||||
|
||||
#endif // BOOST_NO_RTTI
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // !defined(BOOST_NO_RTTI) && !defined(BOOST_TYPE_INDEX_FORCE_NORTTI_COMPATIBILITY)
|
||||
|
||||
#endif // BOOST_TYPE_INDEX_TYPE_INDEX_HPP
|
||||
|
||||
|
@ -14,17 +14,249 @@
|
||||
# pragma once
|
||||
#endif
|
||||
|
||||
/// \file type_index_minimal.hpp
|
||||
/// \brief This is the header that required for ussage of boost::type_index with/without RTTI.
|
||||
/// \file type_info.hpp
|
||||
/// \brief Contains implementation of boost::type_info class.
|
||||
///
|
||||
/// It includes only the minamally required headers and does the 'typedef template_index type_index;'
|
||||
/// when RTTI is disabled.
|
||||
/// boost::type_info class is used in situations when RTTI is enabled.
|
||||
/// When RTTI is disabled or BOOST_TYPE_INDEX_FORCE_NORTTI_COMPATIBILITY macro is defined boost::template_info
|
||||
/// is used instead of it.
|
||||
///
|
||||
/// boost::type_info class can be used as a drop-in replacement for std::type_info, but unlike std::type_info
|
||||
/// this class has a name_demangled() function for getting human-readable type names.
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if !defined(BOOST_NO_RTTI) && !defined(BOOST_TYPE_INDEX_FORCE_NORTTI_COMPATIBILITY)
|
||||
# include <boost/type_index/type_info_impl.hpp>
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <boost/type_traits/is_arithmetic.hpp>
|
||||
#include <boost/type_traits/is_const.hpp>
|
||||
#include <boost/type_traits/is_reference.hpp>
|
||||
#include <boost/type_traits/is_volatile.hpp>
|
||||
#include <boost/type_traits/remove_cv.hpp>
|
||||
#include <boost/type_traits/remove_reference.hpp>
|
||||
#include <boost/mpl/if.hpp>
|
||||
#include <boost/detail/no_exceptions_support.hpp>
|
||||
#include <boost/functional/hash_fwd.hpp>
|
||||
|
||||
#include <typeinfo>
|
||||
|
||||
#ifdef __GNUC__
|
||||
#include <cxxabi.h>
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
|
||||
#ifndef BOOST_TYPE_INDEX_DOXYGEN_INVOKED
|
||||
|
||||
// for this compiler at least, cross-shared-library type_info
|
||||
// comparisons don't work, so use typeid(x).name() instead. It's not
|
||||
// yet clear what the best default strategy is.
|
||||
# if (defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 5))) \
|
||||
|| defined(_AIX) \
|
||||
|| (defined(__sgi) && defined(__host_mips)) \
|
||||
|| (defined(__hpux) && defined(__HP_aCC)) \
|
||||
|| (defined(linux) && defined(__INTEL_COMPILER) && defined(__ICC))
|
||||
# define BOOST_CLASSINFO_COMPARE_BY_NAMES
|
||||
# endif
|
||||
|
||||
#endif // BOOST_TYPE_INDEX_DOXYGEN_INVOKED
|
||||
|
||||
namespace detail {
|
||||
#ifdef BOOST_NO_STD_TYPEINFO
|
||||
typedef type_info stl_type_info;
|
||||
#else
|
||||
typedef std::type_info stl_type_info;
|
||||
#endif
|
||||
|
||||
template <class T> class cvr_saver{};
|
||||
}
|
||||
|
||||
/// boost::type_info is a class that can be used as a drop-in replacement for std::type_info.
|
||||
///
|
||||
/// boost::type_info class is used in situations when RTTI is enabled.
|
||||
/// When RTTI is disabled or BOOST_TYPE_INDEX_FORCE_NORTTI_COMPATIBILITY macro is defined boost::template_info
|
||||
/// is used instead of it.
|
||||
///
|
||||
/// Unlike std::type_info this class:
|
||||
/// * has a name_demangled() function for getting human-readable type names
|
||||
/// * name() function always noexcept and returns simple mangled name as character array
|
||||
/// * workarounds some cimpiler issues
|
||||
class type_info: public detail::stl_type_info {
|
||||
public:
|
||||
/// Factory method for constructing boost::type_info instance for type T.
|
||||
/// Strips const, volatile and & modifiers from T.
|
||||
template <class T>
|
||||
static const boost::type_info& construct() 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;
|
||||
|
||||
# if (defined(__EDG_VERSION__) && __EDG_VERSION__ < 245) \
|
||||
|| (defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 744)
|
||||
BOOST_STATIC_ASSERT_MSG( !boost::is_arithmetic<no_cvr_t>::type::value
|
||||
, "Your EDG-based compiler seems to mistakenly distinguish 'int' from 'signed int', in typeid() expressions.");
|
||||
#endif
|
||||
|
||||
return reinterpret_cast<const boost::type_info&>(typeid(no_cvr_t));
|
||||
}
|
||||
|
||||
/// Factory method for constructing boost::type_index instance for type T.
|
||||
/// Does not strip const, volatile, & and && modifiers from T.
|
||||
/// If T has no const, volatile, & and && modifiers, then returns exactly
|
||||
/// the same result as in case of calling `construct<T>()`.
|
||||
template <class T>
|
||||
static const boost::type_info& construct_with_cvr() BOOST_NOEXCEPT {
|
||||
typedef typename boost::mpl::if_c<
|
||||
boost::is_reference<T>::value
|
||||
|| boost::is_const<T>::value
|
||||
|| boost::is_volatile<T>::value,
|
||||
detail::cvr_saver<T>,
|
||||
T
|
||||
>::type type;
|
||||
|
||||
return reinterpret_cast<const boost::type_info&>(typeid(type));
|
||||
}
|
||||
|
||||
/// Factory function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index.
|
||||
/// This method available only with RTTI enabled.
|
||||
template <class T>
|
||||
static const type_index& construct_rtti_only(T& rtti_val) BOOST_NOEXCEPT {
|
||||
return reinterpret_cast<const boost::type_info&>(typeid(rtti_val));
|
||||
}
|
||||
|
||||
/// Factory function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index.
|
||||
/// This method available only with RTTI enabled.
|
||||
template <class T>
|
||||
static const type_index& construct_rtti_only(T* rtti_val) {
|
||||
return reinterpret_cast<const boost::type_info&>(typeid(rtti_val));
|
||||
}
|
||||
|
||||
const char* name() const BOOST_NOEXCEPT {
|
||||
#ifdef _MSC_VER
|
||||
return detail::stl_type_info::raw_name();
|
||||
#else
|
||||
return detail::stl_type_info::name();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// Returns user-friendly name
|
||||
std::string name_demangled() const {
|
||||
#if defined(_MSC_VER)
|
||||
std::string ret = detail::stl_type_info::name();
|
||||
#else
|
||||
std::string ret;
|
||||
int status = 0;
|
||||
char* demang = abi::__cxa_demangle(name(), NULL, 0, &status);
|
||||
BOOST_ASSERT(!status);
|
||||
|
||||
BOOST_TRY {
|
||||
ret = demang; // may throw out of memory exception
|
||||
} BOOST_CATCH (...) {
|
||||
free(demang);
|
||||
BOOST_RETHROW;
|
||||
} BOOST_CATCH_END
|
||||
|
||||
free(demang);
|
||||
#endif
|
||||
std::string::size_type pos = ret.find("boost::detail::cvr_saver<");
|
||||
if (pos == std::string::npos) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
pos += sizeof("boost::detail::cvr_saver<") - 1;
|
||||
while (ret[pos] == ' ') {
|
||||
++ pos;
|
||||
}
|
||||
std::string::size_type end = ret.rfind(">");
|
||||
BOOST_ASSERT(end != std::string::npos);
|
||||
while (ret[end] == ' ') {
|
||||
-- end;
|
||||
}
|
||||
|
||||
return ret.substr(pos, end - pos);
|
||||
}
|
||||
|
||||
bool operator == (type_index const& rhs) const BOOST_NOEXCEPT {
|
||||
#ifdef BOOST_CLASSINFO_COMPARE_BY_NAMES
|
||||
return !std::strcmp(name(), rhs.name());
|
||||
#else
|
||||
return *this == rhs;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool operator != (type_index const& rhs) const BOOST_NOEXCEPT {
|
||||
return !(*this == rhs);
|
||||
}
|
||||
|
||||
/// Returns true if the type precedes the type of rhs in the collation order.
|
||||
/// The collation order is just an internal order.
|
||||
bool before(type_index const& rhs) const BOOST_NOEXCEPT {
|
||||
#ifdef BOOST_CLASSINFO_COMPARE_BY_NAMES
|
||||
return std::strcmp(name(), rhs.name()) < 0;
|
||||
#else
|
||||
return this->before(rhs);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// Function for getting hash value
|
||||
std::size_t hash_code() const BOOST_NOEXCEPT {
|
||||
#if _MSC_VER >= 1600 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5 && defined(__GXX_EXPERIMENTAL_CXX0X__))
|
||||
return detail::stl_type_info::hash_code();
|
||||
#else
|
||||
return boost::hash_range(name(), name() + std::strlen(name()));
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef BOOST_CLASSINFO_COMPARE_BY_NAMES
|
||||
#undef BOOST_CLASSINFO_COMPARE_BY_NAMES
|
||||
#endif
|
||||
|
||||
/// Function, to get std::type_info for a type T. Strips const, volatile and & modifiers from T.
|
||||
template <class T>
|
||||
inline const type_info& type_id() BOOST_NOEXCEPT {
|
||||
return type_info::construct<T>();
|
||||
}
|
||||
|
||||
/// Function for constructing std::type_info instance for type T.
|
||||
/// Does not strip const, volatile, & and && modifiers from T.
|
||||
/// If T has no const, volatile, & and && modifiers, then returns exactly
|
||||
/// the same result as in case of calling `type_id<T>()`.
|
||||
template <class T>
|
||||
inline const type_info& type_id_with_cvr() BOOST_NOEXCEPT {
|
||||
return type_info::construct_with_cvr<T>();
|
||||
}
|
||||
|
||||
/// Function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index.
|
||||
/// This method available only with RTTI enabled. Without RTTI support it won't compile,
|
||||
/// producing a compile-time error with message: "boost::type_id_rtti_only(T&) requires RTTI"
|
||||
template <class T>
|
||||
inline const type_info& type_id_rtti_only(T& rtti_val) BOOST_NOEXCEPT {
|
||||
return reinterpret_cast<const type_info&>(typeid(rtti_val));
|
||||
}
|
||||
|
||||
/// Function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index.
|
||||
/// This method available only with RTTI enabled. Without RTTI support it won't compile,
|
||||
/// producing a compile-time error with message: "boost::type_id_rtti_only(T*) requires RTTI"
|
||||
template <class T>
|
||||
inline const type_info& type_id_rtti_only(T* rtti_val) {
|
||||
return reinterpret_cast<const type_info&>(typeid(rtti_val));
|
||||
}
|
||||
|
||||
/* *************** type_index free functions ******************* */
|
||||
|
||||
/// hash_value function overload for boost::type_info.
|
||||
inline std::size_t hash_value(type_index const& v) BOOST_NOEXCEPT {
|
||||
return v.hash_code();
|
||||
}
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#else // !defined(BOOST_NO_RTTI) && !defined(BOOST_TYPE_INDEX_FORCE_NORTTI_COMPATIBILITY)
|
||||
# include <boost/type_index/template_info.hpp>
|
||||
# include <boost/static_assert.hpp>
|
||||
|
||||
@ -56,7 +288,7 @@ inline const type_index& type_id_rtti_only(T* rtti_val) {
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_NO_RTTI
|
||||
#endif // !defined(BOOST_NO_RTTI) && !defined(BOOST_TYPE_INDEX_FORCE_NORTTI_COMPATIBILITY)
|
||||
|
||||
#if defined(BOOST_TYPE_INDEX_DOXYGEN_INVOKED)
|
||||
/// \def BOOST_TYPE_INDEX_FORCE_NORTTI_COMPATIBILITY
|
||||
|
@ -1,259 +0,0 @@
|
||||
//
|
||||
// Copyright (c) Antony Polukhin, 2012-2013.
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef BOOST_TYPE_INDEX_TYPE_INFO_IMPL_HPP
|
||||
#define BOOST_TYPE_INDEX_TYPE_INFO_IMPL_HPP
|
||||
|
||||
// MS compatible compilers support #pragma once
|
||||
#if defined(_MSC_VER)
|
||||
# pragma once
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_TYPE_INDEX_TYPE_INFO_HPP
|
||||
#error "Header <boost/type_index/type_info_impl.hpp> must not be included directly."
|
||||
#error "Include <boost/type_index/type_info.hpp> or <boost/type_index.hpp> instead."
|
||||
#endif
|
||||
|
||||
/// \file type_info_impl.hpp
|
||||
/// \brief Contains implementation of boost::type_info class.
|
||||
///
|
||||
/// boost::type_info class is used in situations when RTTI is enabled.
|
||||
/// When RTTI is disabled or BOOST_TYPE_INDEX_FORCE_NORTTI_COMPATIBILITY macro is defined boost::template_info
|
||||
/// is used instead.
|
||||
///
|
||||
/// Consider including <boost/type_index/type_info.hpp> or <boost/type_index.hpp> instead of this file.
|
||||
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <boost/type_traits/is_arithmetic.hpp>
|
||||
#include <boost/type_traits/is_const.hpp>
|
||||
#include <boost/type_traits/is_reference.hpp>
|
||||
#include <boost/type_traits/is_volatile.hpp>
|
||||
#include <boost/type_traits/remove_cv.hpp>
|
||||
#include <boost/type_traits/remove_reference.hpp>
|
||||
#include <boost/mpl/if.hpp>
|
||||
#include <boost/detail/no_exceptions_support.hpp>
|
||||
#include <boost/functional/hash_fwd.hpp>
|
||||
|
||||
#ifndef BOOST_NO_RTTI
|
||||
#include <typeinfo>
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
#include <cxxabi.h>
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
|
||||
#ifndef BOOST_NO_RTTI
|
||||
|
||||
#ifndef BOOST_TYPE_INDEX_DOXYGEN_INVOKED
|
||||
|
||||
// for this compiler at least, cross-shared-library type_info
|
||||
// comparisons don't work, so use typeid(x).name() instead. It's not
|
||||
// yet clear what the best default strategy is.
|
||||
# if (defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 5))) \
|
||||
|| defined(_AIX) \
|
||||
|| (defined(__sgi) && defined(__host_mips)) \
|
||||
|| (defined(__hpux) && defined(__HP_aCC)) \
|
||||
|| (defined(linux) && defined(__INTEL_COMPILER) && defined(__ICC))
|
||||
# define BOOST_CLASSINFO_COMPARE_BY_NAMES
|
||||
# endif
|
||||
|
||||
#endif // BOOST_TYPE_INDEX_DOXYGEN_INVOKED
|
||||
|
||||
namespace detail {
|
||||
#ifdef BOOST_NO_STD_TYPEINFO
|
||||
typedef type_info stl_type_info;
|
||||
#else
|
||||
typedef std::type_info stl_type_info;
|
||||
#endif
|
||||
|
||||
template <class T> class cvr_saver{};
|
||||
}
|
||||
|
||||
/// boost::type_info is a class that can be used as a drop-in replacement for std::type_info.
|
||||
/// It has all the workarounds for compatability and also has a name_demangled() method for
|
||||
/// getting human-readable type names.
|
||||
class type_info: public detail::stl_type_info {
|
||||
public:
|
||||
/// Factory method for constructing boost::type_info instance for type T.
|
||||
/// Strips const, volatile and & modifiers from T.
|
||||
template <class T>
|
||||
static const boost::type_info& construct() 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;
|
||||
|
||||
# if (defined(__EDG_VERSION__) && __EDG_VERSION__ < 245) \
|
||||
|| (defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 744)
|
||||
BOOST_STATIC_ASSERT_MSG( !boost::is_arithmetic<no_cvr_t>::type::value
|
||||
, "Your EDG-based compiler seems to mistakenly distinguish 'int' from 'signed int', in typeid() expressions.");
|
||||
#endif
|
||||
|
||||
return reinterpret_cast<const boost::type_info&>(typeid(no_cvr_t));
|
||||
}
|
||||
|
||||
/// Factory method for constructing boost::type_index instance for type T.
|
||||
/// Does not strip const, volatile, & and && modifiers from T.
|
||||
/// If T has no const, volatile, & and && modifiers, then returns exactly
|
||||
/// the same result as in case of calling `construct<T>()`.
|
||||
template <class T>
|
||||
static const boost::type_info& construct_with_cvr() BOOST_NOEXCEPT {
|
||||
typedef typename boost::mpl::if_c<
|
||||
boost::is_reference<T>::value
|
||||
|| boost::is_const<T>::value
|
||||
|| boost::is_volatile<T>::value,
|
||||
detail::cvr_saver<T>,
|
||||
T
|
||||
>::type type;
|
||||
|
||||
return reinterpret_cast<const boost::type_info&>(typeid(type));
|
||||
}
|
||||
|
||||
/// Factory function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index.
|
||||
/// This method available only with RTTI enabled.
|
||||
template <class T>
|
||||
static const type_index& construct_rtti_only(T& rtti_val) BOOST_NOEXCEPT {
|
||||
return reinterpret_cast<const boost::type_info&>(typeid(rtti_val));
|
||||
}
|
||||
|
||||
/// Factory function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index.
|
||||
/// This method available only with RTTI enabled.
|
||||
template <class T>
|
||||
static const type_index& construct_rtti_only(T* rtti_val) {
|
||||
return reinterpret_cast<const boost::type_info&>(typeid(rtti_val));
|
||||
}
|
||||
|
||||
const char* name() const BOOST_NOEXCEPT {
|
||||
#ifdef _MSC_VER
|
||||
return detail::stl_type_info::raw_name();
|
||||
#else
|
||||
return detail::stl_type_info::name();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// Returns user-friendly name
|
||||
std::string name_demangled() const {
|
||||
#if defined(_MSC_VER)
|
||||
std::string ret = detail::stl_type_info::name();
|
||||
#else
|
||||
std::string ret;
|
||||
int status = 0;
|
||||
char* demang = abi::__cxa_demangle(name(), NULL, 0, &status);
|
||||
BOOST_ASSERT(!status);
|
||||
|
||||
BOOST_TRY {
|
||||
ret = demang; // may throw out of memory exception
|
||||
} BOOST_CATCH (...) {
|
||||
free(demang);
|
||||
BOOST_RETHROW;
|
||||
} BOOST_CATCH_END
|
||||
|
||||
free(demang);
|
||||
#endif
|
||||
std::string::size_type pos = ret.find("boost::detail::cvr_saver<");
|
||||
if (pos == std::string::npos) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
pos += sizeof("boost::detail::cvr_saver<") - 1;
|
||||
while (ret[pos] == ' ') {
|
||||
++ pos;
|
||||
}
|
||||
std::string::size_type end = ret.rfind(">");
|
||||
BOOST_ASSERT(end != std::string::npos);
|
||||
while (ret[end] == ' ') {
|
||||
-- end;
|
||||
}
|
||||
|
||||
return ret.substr(pos, end - pos);
|
||||
}
|
||||
|
||||
bool operator == (type_index const& rhs) const BOOST_NOEXCEPT {
|
||||
#ifdef BOOST_CLASSINFO_COMPARE_BY_NAMES
|
||||
return !std::strcmp(name(), rhs.name());
|
||||
#else
|
||||
return *this == rhs;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool operator != (type_index const& rhs) const BOOST_NOEXCEPT {
|
||||
return !(*this == rhs);
|
||||
}
|
||||
|
||||
/// Returns true if the type precedes the type of rhs in the collation order.
|
||||
/// The collation order is just an internal order.
|
||||
bool before(type_index const& rhs) const BOOST_NOEXCEPT {
|
||||
#ifdef BOOST_CLASSINFO_COMPARE_BY_NAMES
|
||||
return std::strcmp(name(), rhs.name()) < 0;
|
||||
#else
|
||||
return this->before(rhs);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// Function for getting hash value
|
||||
std::size_t hash_code() const BOOST_NOEXCEPT {
|
||||
#if _MSC_VER >= 1600 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5 && defined(__GXX_EXPERIMENTAL_CXX0X__))
|
||||
return detail::stl_type_info::hash_code();
|
||||
#else
|
||||
return boost::hash_range(name(), name() + std::strlen(name()));
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef BOOST_CLASSINFO_COMPARE_BY_NAMES
|
||||
#undef BOOST_CLASSINFO_COMPARE_BY_NAMES
|
||||
#endif
|
||||
|
||||
/// Function, to get std::type_info for a type T. Strips const, volatile and & modifiers from T.
|
||||
template <class T>
|
||||
inline const type_info& type_id() BOOST_NOEXCEPT {
|
||||
return type_info::construct<T>();
|
||||
}
|
||||
|
||||
/// Function for constructing std::type_info instance for type T.
|
||||
/// Does not strip const, volatile, & and && modifiers from T.
|
||||
/// If T has no const, volatile, & and && modifiers, then returns exactly
|
||||
/// the same result as in case of calling `type_id<T>()`.
|
||||
template <class T>
|
||||
inline const type_info& type_id_with_cvr() BOOST_NOEXCEPT {
|
||||
return type_info::construct_with_cvr<T>();
|
||||
}
|
||||
|
||||
/// Function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index.
|
||||
/// This method available only with RTTI enabled. Without RTTI support it won't compile,
|
||||
/// producing a compile-time error with message: "boost::type_id_rtti_only(T&) requires RTTI"
|
||||
template <class T>
|
||||
inline const type_info& type_id_rtti_only(T& rtti_val) BOOST_NOEXCEPT {
|
||||
return reinterpret_cast<const type_info&>(typeid(rtti_val));
|
||||
}
|
||||
|
||||
/// Function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index.
|
||||
/// This method available only with RTTI enabled. Without RTTI support it won't compile,
|
||||
/// producing a compile-time error with message: "boost::type_id_rtti_only(T*) requires RTTI"
|
||||
template <class T>
|
||||
inline const type_info& type_id_rtti_only(T* rtti_val) {
|
||||
return reinterpret_cast<const type_info&>(typeid(rtti_val));
|
||||
}
|
||||
|
||||
/* *************** type_index free functions ******************* */
|
||||
|
||||
/// hash_value function overload for boost::type_info.
|
||||
inline std::size_t hash_value(type_index const& v) BOOST_NOEXCEPT {
|
||||
return v.hash_code();
|
||||
}
|
||||
|
||||
#endif // BOOST_NO_RTTI
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_TYPE_INDEX_TYPE_INFO_IMPL_HPP
|
||||
|
Reference in New Issue
Block a user