From 8ab45702a09b54c5be4ae569279125f747117e6b Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Mon, 28 Oct 2013 14:58:30 +0400 Subject: [PATCH] Many fixes, tests now compile and work --- boost/type_index.hpp | 1 + boost/type_index/type_index.hpp | 99 +++++++++---------- boost/type_index/type_info.hpp | 75 +++++++++----- libs/type_index/examples/demangled_names.cpp | 2 +- .../type_index/examples/exact_types_match.cpp | 10 +- libs/type_index/examples/inheritance.cpp | 2 +- libs/type_index/examples/registry.cpp | 2 +- libs/type_index/test/Jamfile.v2 | 2 - libs/type_index/test/template_index_test.cpp | 5 +- libs/type_index/test/template_index_tests.ipp | 2 +- libs/type_index/test/testing_crossmodule.cpp | 2 +- libs/type_index/test/testing_minimal.cpp | 22 ----- libs/type_index/test/type_index_test.cpp | 5 +- libs/type_index/test/type_index_tests.ipp | 1 + 14 files changed, 106 insertions(+), 124 deletions(-) delete mode 100644 libs/type_index/test/testing_minimal.cpp diff --git a/boost/type_index.hpp b/boost/type_index.hpp index b6c9ce0..3ad0f09 100644 --- a/boost/type_index.hpp +++ b/boost/type_index.hpp @@ -28,6 +28,7 @@ #include #include #include +#include #endif // BOOST_TYPE_INDEX_HPP diff --git a/boost/type_index/type_index.hpp b/boost/type_index/type_index.hpp index fc727ad..f678c56 100644 --- a/boost/type_index/type_index.hpp +++ b/boost/type_index/type_index.hpp @@ -18,10 +18,11 @@ /// /// 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 #include +#if !defined(BOOST_NO_RTTI) && !defined(BOOST_TYPE_INDEX_FORCE_NORTTI_COMPATIBILITY) + #if !defined(BOOST_NO_IOSTREAM) #if !defined(BOOST_NO_IOSFWD) #include // for std::basic_ostream @@ -34,27 +35,29 @@ namespace boost { /// Copyable type_index class that requires RTTI. class type_index { -public: - typedef boost::type_info stl_type_info; - private: - const stl_type_info* pinfo_; + const type_info* pinfo_; public: /// Default constructor. type_index() BOOST_NOEXCEPT - : pinfo_(&typeid(void)) + : pinfo_(static_cast(&typeid(void))) + {} + + /// Constructs type_index from an instance of boost::type_info. + type_index(const type_info& inf) BOOST_NOEXCEPT + : pinfo_(&inf) {} /// Constructs type_index from an instance of std::type_info. - explicit type_index(const stl_type_info& inf) BOOST_NOEXCEPT - : pinfo_(&inf) + type_index(const detail::stl_type_info& inf) BOOST_NOEXCEPT + : pinfo_(static_cast(&inf)) {} /// 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); + return pinfo_->before(*rhs.pinfo_); } /// Returns raw name @@ -69,23 +72,15 @@ public: #ifndef BOOST_TYPE_INDEX_DOXYGEN_INVOKED bool operator == (type_index const& rhs) const BOOST_NOEXCEPT { - #ifdef BOOST_CLASSINFO_COMPARE_BY_NAMES - return !std::strcmp(pinfo_->name(), rhs.pinfo_->name()); - #else - return *pinfo_ == *rhs.pinfo_; - #endif + return *pinfo_ == *rhs.pinfo_; } bool operator != (type_index const& rhs) const BOOST_NOEXCEPT { - return !(*this == rhs); + return !(*pinfo_ == *rhs.pinfo_); } bool operator < (type_index const& rhs) const BOOST_NOEXCEPT { - #ifdef BOOST_CLASSINFO_COMPARE_BY_NAMES - return std::strcmp(pinfo_->name(), rhs.pinfo_->name()) < 0; - #else - return before(rhs); - #endif + return before(rhs); } bool operator > (type_index const& rhs) const BOOST_NOEXCEPT { @@ -100,50 +95,35 @@ public: return !(*this < rhs); } - bool operator == (stl_type_info const& rhs) const BOOST_NOEXCEPT { - #ifdef BOOST_CLASSINFO_COMPARE_BY_NAMES - return !std::strcmp(pinfo_->name(), rhs.name()); - #else - return *pinfo_ == rhs; - #endif + + bool operator == (detail::stl_type_info const& rhs) const BOOST_NOEXCEPT { + return *this == type_index(rhs); } - bool operator != (stl_type_info const& rhs) const BOOST_NOEXCEPT { - return !(*this == rhs); + bool operator != (detail::stl_type_info const& rhs) const BOOST_NOEXCEPT { + return *this != type_index(rhs); } - bool operator < (stl_type_info const& rhs) const BOOST_NOEXCEPT { - #ifdef BOOST_CLASSINFO_COMPARE_BY_NAMES - return std::strcmp(pinfo_->name(), rhs.name()) < 0; - #else - return !!pinfo_->before(rhs); - #endif + bool operator < (detail::stl_type_info const& rhs) const BOOST_NOEXCEPT { + return *this < type_index(rhs); } - bool operator > (stl_type_info const& rhs) const BOOST_NOEXCEPT { - #ifdef BOOST_CLASSINFO_COMPARE_BY_NAMES - return std::strcmp(pinfo_->name(), rhs.name()) > 0; - #else - return !!rhs.before(*pinfo_); - #endif + bool operator > (detail::stl_type_info const& rhs) const BOOST_NOEXCEPT { + return *this > type_index(rhs); } - bool operator <= (stl_type_info const& rhs) const BOOST_NOEXCEPT { - return !(*this > rhs); + bool operator <= (detail::stl_type_info const& rhs) const BOOST_NOEXCEPT { + return *this <= type_index(rhs); } - bool operator >= (stl_type_info const& rhs) const BOOST_NOEXCEPT { - return !(*this < rhs); + bool operator >= (detail::stl_type_info const& rhs) const BOOST_NOEXCEPT { + return *this >= type_index(rhs); } #endif // BOOST_TYPE_INDEX_DOXYGEN_INVOKED /// 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 pinfo_->hash_code(); -#else - return boost::hash_range(name(), name() + std::strlen(name())); -#endif } }; @@ -151,27 +131,27 @@ public: /* *************** type_index free functions ******************* */ -inline bool operator == (type_index::stl_type_info const& lhs, type_index const& rhs) BOOST_NOEXCEPT { +inline bool operator == (detail::stl_type_info const& lhs, type_index const& rhs) BOOST_NOEXCEPT { return rhs == lhs; // Operation is commutative } -inline bool operator != (type_index::stl_type_info const& lhs, type_index const& rhs) BOOST_NOEXCEPT { +inline bool operator != (detail::stl_type_info const& lhs, type_index const& rhs) BOOST_NOEXCEPT { return rhs != lhs; // Operation is commutative } -inline bool operator < (type_index::stl_type_info const& lhs, type_index const& rhs) BOOST_NOEXCEPT { +inline bool operator < (detail::stl_type_info const& lhs, type_index const& rhs) BOOST_NOEXCEPT { return rhs > lhs; } -inline bool operator > (type_index::stl_type_info const& lhs, type_index const& rhs) BOOST_NOEXCEPT { +inline bool operator > (detail::stl_type_info const& lhs, type_index const& rhs) BOOST_NOEXCEPT { return rhs < lhs; } -inline bool operator <= (type_index::stl_type_info const& lhs, type_index const& rhs) BOOST_NOEXCEPT { +inline bool operator <= (detail::stl_type_info const& lhs, type_index const& rhs) BOOST_NOEXCEPT { return rhs >= lhs; } -inline bool operator >= (type_index::stl_type_info const& lhs, type_index const& rhs) BOOST_NOEXCEPT { +inline bool operator >= (detail::stl_type_info const& lhs, type_index const& rhs) BOOST_NOEXCEPT { return rhs <= lhs; } @@ -217,6 +197,15 @@ inline std::size_t hash_value(type_index const& v) BOOST_NOEXCEPT { } // namespace boost +#else // !defined(BOOST_NO_RTTI) && !defined(BOOST_TYPE_INDEX_FORCE_NORTTI_COMPATIBILITY) + +#include +namespace boost { + +typedef template_index type_index; + +} + #endif // !defined(BOOST_NO_RTTI) && !defined(BOOST_TYPE_INDEX_FORCE_NORTTI_COMPATIBILITY) #endif // BOOST_TYPE_INDEX_TYPE_INDEX_HPP diff --git a/boost/type_index/type_info.hpp b/boost/type_index/type_info.hpp index 04a0c70..94bcd43 100644 --- a/boost/type_index/type_info.hpp +++ b/boost/type_index/type_info.hpp @@ -101,10 +101,10 @@ public: , "Your EDG-based compiler seems to mistakenly distinguish 'int' from 'signed int', in typeid() expressions."); #endif - return reinterpret_cast(typeid(no_cvr_t)); + return static_cast(typeid(no_cvr_t)); } - /// Factory method for constructing boost::type_index instance for type T. + /// Factory method for constructing boost::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 `construct()`. @@ -118,21 +118,21 @@ public: T >::type type; - return reinterpret_cast(typeid(type)); + return static_cast(typeid(type)); } - /// Factory function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index. + /// Factory function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_info. /// This method available only with RTTI enabled. template - static const type_index& construct_rtti_only(T& rtti_val) BOOST_NOEXCEPT { - return reinterpret_cast(typeid(rtti_val)); + static const type_info& construct_rtti_only(T& rtti_val) BOOST_NOEXCEPT { + return static_cast(typeid(rtti_val)); } - /// Factory function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index. + /// Factory function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_info. /// This method available only with RTTI enabled. template - static const type_index& construct_rtti_only(T* rtti_val) { - return reinterpret_cast(typeid(rtti_val)); + static const type_info& construct_rtti_only(T* rtti_val) { + return static_cast(typeid(rtti_val)); } const char* name() const BOOST_NOEXCEPT { @@ -180,28 +180,40 @@ public: return ret.substr(pos, end - pos); } - bool operator == (type_index const& rhs) const BOOST_NOEXCEPT { + bool operator == (type_info const& rhs) const BOOST_NOEXCEPT { #ifdef BOOST_CLASSINFO_COMPARE_BY_NAMES return !std::strcmp(name(), rhs.name()); #else - return *this == rhs; + return static_cast(*this) == static_cast(rhs); #endif } - bool operator != (type_index const& rhs) const BOOST_NOEXCEPT { + bool operator != (type_info const& rhs) const BOOST_NOEXCEPT { + return !(*this == rhs); + } + + bool operator == (detail::stl_type_info const& rhs) const BOOST_NOEXCEPT { + return *this == static_cast(rhs); + } + + bool operator != (detail::stl_type_info 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 { + bool before(type_info const& rhs) const BOOST_NOEXCEPT { #ifdef BOOST_CLASSINFO_COMPARE_BY_NAMES return std::strcmp(name(), rhs.name()) < 0; #else - return this->before(rhs); + return detail::stl_type_info::before(rhs); #endif } + bool before(detail::stl_type_info const& rhs) const BOOST_NOEXCEPT { + return before(static_cast(rhs)); + } + /// 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__)) @@ -231,26 +243,35 @@ inline const type_info& type_id_with_cvr() BOOST_NOEXCEPT { return type_info::construct_with_cvr(); } -/// Function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index. +/// Function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_info. /// 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 inline const type_info& type_id_rtti_only(T& rtti_val) BOOST_NOEXCEPT { - return reinterpret_cast(typeid(rtti_val)); + return static_cast(typeid(rtti_val)); } -/// Function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index. +/// Function, that works exactly like C++ typeid(rtti_val) call, but returns boost::type_info. /// 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 inline const type_info& type_id_rtti_only(T* rtti_val) { - return reinterpret_cast(typeid(rtti_val)); + return static_cast(typeid(rtti_val)); } -/* *************** type_index free functions ******************* */ +/* *************** type_info free functions ******************* */ + + +inline bool operator == (detail::stl_type_info const& lhs, type_info const& rhs) BOOST_NOEXCEPT { + return rhs == static_cast(lhs); +} + +inline bool operator != (detail::stl_type_info const& lhs, type_info const& rhs) BOOST_NOEXCEPT { + return !(lhs == rhs); +} /// hash_value function overload for boost::type_info. -inline std::size_t hash_value(type_index const& v) BOOST_NOEXCEPT { +inline std::size_t hash_value(type_info const& v) BOOST_NOEXCEPT { return v.hash_code(); } @@ -262,28 +283,28 @@ inline std::size_t hash_value(type_index const& v) BOOST_NOEXCEPT { namespace boost { -typedef template_info type_index; +typedef template_info type_info; template -inline const type_index& type_id() BOOST_NOEXCEPT { +inline const type_info& type_id() BOOST_NOEXCEPT { return template_info::construct(); } template -inline const type_index& type_id_with_cvr() BOOST_NOEXCEPT { +inline const type_info& type_id_with_cvr() BOOST_NOEXCEPT { return template_info::construct_with_cvr(); } template -inline const type_index& type_id_rtti_only(T& rtti_val) BOOST_NOEXCEPT { +inline const type_info& type_id_rtti_only(T& rtti_val) BOOST_NOEXCEPT { BOOST_STATIC_ASSERT_MSG(sizeof(T) && false, "boost::type_id_rtti_only(T&) requires RTTI"); - return type_index(); + return template_info::construct_with_cvr(); } template -inline const type_index& type_id_rtti_only(T* rtti_val) { +inline const type_info& type_id_rtti_only(T* rtti_val) { BOOST_STATIC_ASSERT_MSG(sizeof(T) && false, "boost::type_id_rtti_only(T*) requires RTTI"); - return type_index(); + return template_info::construct_with_cvr(); } } // namespace boost diff --git a/libs/type_index/examples/demangled_names.cpp b/libs/type_index/examples/demangled_names.cpp index c3dc67e..2c4d4b7 100644 --- a/libs/type_index/examples/demangled_names.cpp +++ b/libs/type_index/examples/demangled_names.cpp @@ -12,7 +12,7 @@ */ -#include +#include #include template diff --git a/libs/type_index/examples/exact_types_match.cpp b/libs/type_index/examples/exact_types_match.cpp index 04f1366..181e565 100644 --- a/libs/type_index/examples/exact_types_match.cpp +++ b/libs/type_index/examples/exact_types_match.cpp @@ -6,32 +6,32 @@ //[type_index_exact_type_match_example /*` - The following example shows that `boost::template_index` is able to store the exact type, without stripping const, volatile and references. + The following example shows that `boost::type_index` (and `boost::type_info`) is able to store the exact type, without stripping const, volatile and references. Example works with and without RTTI. In this example we'll create a class, that stores pointer to function and remembers the exact type of a parameter that function accepts. When an attempt to call the stored function will be made, type of input parameter will be checked for exact match with initaily erased type of function. */ -#include +#include #include #include #include class type_erased_unary_function { void* function_ptr_; - boost::template_index exact_param_t_; + boost::type_index exact_param_t_; public: template type_erased_unary_function(void(*ptr)(ParamT)) : function_ptr_(reinterpret_cast(ptr)) // ptr - is a pointer to function returning `void` and accepting parameter of type `ParamT` - , exact_param_t_(boost::template_id_with_cvr()) + , exact_param_t_(boost::type_id_with_cvr()) {} template void call(ParamT v) { - if (exact_param_t_ != boost::template_id_with_cvr()) { + if (exact_param_t_ != boost::type_id_with_cvr()) { throw std::runtime_error("Incorrect `ParamT`"); } diff --git a/libs/type_index/examples/inheritance.cpp b/libs/type_index/examples/inheritance.cpp index 3684297..ae54d17 100644 --- a/libs/type_index/examples/inheritance.cpp +++ b/libs/type_index/examples/inheritance.cpp @@ -12,7 +12,7 @@ "boost::type_id_rtti_only(T&) requires RTTI" */ -#include +#include #include struct A { virtual ~A(){} }; diff --git a/libs/type_index/examples/registry.cpp b/libs/type_index/examples/registry.cpp index a0bcc02..16f247c 100644 --- a/libs/type_index/examples/registry.cpp +++ b/libs/type_index/examples/registry.cpp @@ -10,7 +10,7 @@ Example works with and without RTTI. */ -#include +#include #include #include diff --git a/libs/type_index/test/Jamfile.v2 b/libs/type_index/test/Jamfile.v2 index e7be57c..9a886ab 100644 --- a/libs/type_index/test/Jamfile.v2 +++ b/libs/type_index/test/Jamfile.v2 @@ -37,8 +37,6 @@ test-suite type_index [ run template_index_test.cpp $(tlib) ] [ run testing_both.cpp $(tlib) ] [ run testing_both_no_rtti.cpp $(tlib) : : : off ] - [ run testing_minimal.cpp $(tlib) ] - [ run testing_minimal_no_rtti.cpp $(tlib) : : : off ] [ run testing_crossmodule.cpp test_lib_rtti $(tlib) ] [ run testing_crossmodule.cpp test_lib_nortti $(tlib) : : : off : testing_crossmodule_no_rtti ] diff --git a/libs/type_index/test/template_index_test.cpp b/libs/type_index/test/template_index_test.cpp index 2605900..056ed8a 100644 --- a/libs/type_index/test/template_index_test.cpp +++ b/libs/type_index/test/template_index_test.cpp @@ -8,10 +8,7 @@ #define BOOST_TEST_MODULE template_index_test_module #include -// Byapssing internal assertion for correct header inclusion -#define BOOST_TYPE_INDEX_TYPE_INDEX_MINIMAL_HPP - -#include +#include namespace my_namespace1 { class my_class{}; diff --git a/libs/type_index/test/template_index_tests.ipp b/libs/type_index/test/template_index_tests.ipp index 4237896..4a1d1c8 100644 --- a/libs/type_index/test/template_index_tests.ipp +++ b/libs/type_index/test/template_index_tests.ipp @@ -1,5 +1,5 @@ // -// Copyright Antony Polukhin, 2012. +// Copyright Antony Polukhin, 2012-2013. // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at diff --git a/libs/type_index/test/testing_crossmodule.cpp b/libs/type_index/test/testing_crossmodule.cpp index d95c61b..106937e 100644 --- a/libs/type_index/test/testing_crossmodule.cpp +++ b/libs/type_index/test/testing_crossmodule.cpp @@ -8,7 +8,7 @@ #define BOOST_TEST_MODULE testing_crossmodule_module #include -#include +#include #include "test_lib.hpp" namespace user_defined_namespace { diff --git a/libs/type_index/test/testing_minimal.cpp b/libs/type_index/test/testing_minimal.cpp deleted file mode 100644 index 1581487..0000000 --- a/libs/type_index/test/testing_minimal.cpp +++ /dev/null @@ -1,22 +0,0 @@ -// -// Copyright 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) - -#define BOOST_TEST_MODULE testing_both_test_module -#include -#include - -namespace my_namespace1 { - class my_class{}; -} - - -namespace my_namespace2 { - class my_class{}; -} - -#include "type_index_tests.ipp" - diff --git a/libs/type_index/test/type_index_test.cpp b/libs/type_index/test/type_index_test.cpp index fc63707..0d242d0 100644 --- a/libs/type_index/test/type_index_test.cpp +++ b/libs/type_index/test/type_index_test.cpp @@ -8,10 +8,7 @@ #define BOOST_TEST_MODULE type_index_test_module #include -// Byapssing internal assertion for correct header inclusion -#define BOOST_TYPE_INDEX_TYPE_INFO_HPP - -#include +#include namespace my_namespace1 { class my_class{}; diff --git a/libs/type_index/test/type_index_tests.ipp b/libs/type_index/test/type_index_tests.ipp index f9d05d7..00e1619 100644 --- a/libs/type_index/test/type_index_tests.ipp +++ b/libs/type_index/test/type_index_tests.ipp @@ -99,6 +99,7 @@ BOOST_AUTO_TEST_CASE(comparators_type_id_vs_type_info) BOOST_CHECK(typeid(int) <= t_double); BOOST_CHECK(t_int <= typeid(double)); } + } #endif // BOOST_NO_RTTI