Many fixes, tests now compile and work

This commit is contained in:
Antony Polukhin
2013-10-28 14:58:30 +04:00
parent 8789bff181
commit 8ab45702a0
14 changed files with 106 additions and 124 deletions

View File

@@ -28,6 +28,7 @@
#include <boost/type_index/type_info.hpp>
#include <boost/type_index/type_index.hpp>
#include <boost/type_index/template_info.hpp>
#include <boost/type_index/template_index.hpp>
#endif // BOOST_TYPE_INDEX_HPP

View File

@@ -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 <boost/config.hpp>
#include <boost/type_index/type_info.hpp>
#if !defined(BOOST_NO_RTTI) && !defined(BOOST_TYPE_INDEX_FORCE_NORTTI_COMPATIBILITY)
#if !defined(BOOST_NO_IOSTREAM)
#if !defined(BOOST_NO_IOSFWD)
#include <iosfwd> // 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<const type_info*>(&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<const type_info*>(&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 <boost/type_index/template_index.hpp>
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

View File

@@ -101,10 +101,10 @@ public:
, "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));
return static_cast<const boost::type_info&>(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<T>()`.
@@ -118,21 +118,21 @@ public:
T
>::type type;
return reinterpret_cast<const boost::type_info&>(typeid(type));
return static_cast<const boost::type_info&>(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 <class T>
static const type_index& construct_rtti_only(T& rtti_val) BOOST_NOEXCEPT {
return reinterpret_cast<const boost::type_info&>(typeid(rtti_val));
static const type_info& construct_rtti_only(T& rtti_val) BOOST_NOEXCEPT {
return static_cast<const boost::type_info&>(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 <class T>
static const type_index& construct_rtti_only(T* rtti_val) {
return reinterpret_cast<const boost::type_info&>(typeid(rtti_val));
static const type_info& construct_rtti_only(T* rtti_val) {
return static_cast<const boost::type_info&>(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<const detail::stl_type_info&>(*this) == static_cast<const detail::stl_type_info&>(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<const boost::type_info&>(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<const boost::type_info&>(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<T>();
}
/// 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 <class T>
inline const type_info& type_id_rtti_only(T& rtti_val) BOOST_NOEXCEPT {
return reinterpret_cast<const type_info&>(typeid(rtti_val));
return static_cast<const type_info&>(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 <class T>
inline const type_info& type_id_rtti_only(T* rtti_val) {
return reinterpret_cast<const type_info&>(typeid(rtti_val));
return static_cast<const type_info&>(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<const boost::type_info&>(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 <class T>
inline const type_index& type_id() BOOST_NOEXCEPT {
inline const type_info& type_id() BOOST_NOEXCEPT {
return template_info::construct<T>();
}
template <class T>
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<T>();
}
template <class T>
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<void>();
}
template <class T>
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<void>();
}
} // namespace boost

View File

@@ -12,7 +12,7 @@
*/
#include <boost/type_index/type_index_minimal.hpp>
#include <boost/type_index/type_info.hpp>
#include <iostream>
template <class T>

View File

@@ -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 <boost/type_index.hpp>
#include <boost/type_index/type_index.hpp>
#include <iostream>
#include <stdexcept>
#include <cassert>
class type_erased_unary_function {
void* function_ptr_;
boost::template_index exact_param_t_;
boost::type_index exact_param_t_;
public:
template <class ParamT>
type_erased_unary_function(void(*ptr)(ParamT))
: function_ptr_(reinterpret_cast<void*>(ptr)) // ptr - is a pointer to function returning `void` and accepting parameter of type `ParamT`
, exact_param_t_(boost::template_id_with_cvr<ParamT>())
, exact_param_t_(boost::type_id_with_cvr<ParamT>())
{}
template <class ParamT>
void call(ParamT v) {
if (exact_param_t_ != boost::template_id_with_cvr<ParamT>()) {
if (exact_param_t_ != boost::type_id_with_cvr<ParamT>()) {
throw std::runtime_error("Incorrect `ParamT`");
}

View File

@@ -12,7 +12,7 @@
"boost::type_id_rtti_only(T&) requires RTTI"
*/
#include <boost/type_index/type_index_minimal.hpp>
#include <boost/type_index/type_info.hpp>
#include <iostream>
struct A { virtual ~A(){} };

View File

@@ -10,7 +10,7 @@
Example works with and without RTTI.
*/
#include <boost/type_index/type_index_minimal.hpp>
#include <boost/type_index/type_index.hpp>
#include <boost/unordered_set.hpp>
#include <cassert>

View File

@@ -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) : : : <rtti>off ]
[ run testing_minimal.cpp $(tlib) ]
[ run testing_minimal_no_rtti.cpp $(tlib) : : : <rtti>off ]
[ run testing_crossmodule.cpp test_lib_rtti $(tlib) ]
[ run testing_crossmodule.cpp test_lib_nortti $(tlib) : : : <rtti>off : testing_crossmodule_no_rtti ]

View File

@@ -8,10 +8,7 @@
#define BOOST_TEST_MODULE template_index_test_module
#include <boost/test/unit_test.hpp>
// Byapssing internal assertion for correct header inclusion
#define BOOST_TYPE_INDEX_TYPE_INDEX_MINIMAL_HPP
#include <boost/type_index/template_info.hpp>
#include <boost/type_index/template_index.hpp>
namespace my_namespace1 {
class my_class{};

View File

@@ -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

View File

@@ -8,7 +8,7 @@
#define BOOST_TEST_MODULE testing_crossmodule_module
#include <boost/test/unit_test.hpp>
#include <boost/type_index/type_index_minimal.hpp>
#include <boost/type_index/type_index.hpp>
#include "test_lib.hpp"
namespace user_defined_namespace {

View File

@@ -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 <boost/test/unit_test.hpp>
#include <boost/type_index/type_index_minimal.hpp>
namespace my_namespace1 {
class my_class{};
}
namespace my_namespace2 {
class my_class{};
}
#include "type_index_tests.ipp"

View File

@@ -8,10 +8,7 @@
#define BOOST_TEST_MODULE type_index_test_module
#include <boost/test/unit_test.hpp>
// Byapssing internal assertion for correct header inclusion
#define BOOST_TYPE_INDEX_TYPE_INFO_HPP
#include <boost/type_index/type_info_impl.hpp>
#include <boost/type_index/type_index.hpp>
namespace my_namespace1 {
class my_class{};

View File

@@ -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