* Performance update for GGC 4.5 and newer

* type_id_rtti_only function added
* Documentation and tests updates
This commit is contained in:
Antony Polukhin
2012-06-26 21:17:42 +04:00
parent 303f0e0ba4
commit b88b75b329
3 changed files with 74 additions and 3 deletions

View File

@@ -225,9 +225,9 @@ template_index template_id_with_cvr() BOOST_NOEXCEPT {
// 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__ >= 3) \
# if (defined(__GNUC__) && __GNUC__ >= 3 && __GNUC_MINOR__ < 5) \
|| defined(_AIX) \
|| ( defined(__sgi) && defined(__host_mips)) \
|| (defined(__sgi) && defined(__host_mips)) \
|| (defined(__hpux) && defined(__HP_aCC)) \
|| (defined(linux) && defined(__INTEL_COMPILER) && defined(__ICC))
# define BOOST_CLASSINFO_COMPARE_BY_NAMES
@@ -279,6 +279,20 @@ public:
return type_index(typeid(no_cvr_t));
}
/// 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 {
@@ -428,6 +442,19 @@ type_index type_id() {
return type_index::construct<T>();
}
/// 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>
type_index type_id_rtti_only(T& rtti_val) {
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.
template <class T>
type_index type_id_rtti_only(T* rtti_val) {
return type_index::construct_rtti_only(rtti_val);
}
/* *************** type_index free functions ******************* */

View File

@@ -23,6 +23,8 @@ Sometimes getting and storing at runtime information about template type is requ
Boost.TypeIndex was designed to work around those issues.
[note `T` means here type. Think of it, as of `T` in `template <class T>` ]
[warning This library is not accepted to Boost, it is currrently waiting for review. ]
[endsect]
@@ -30,7 +32,7 @@ Boost.TypeIndex was designed to work around those issues.
[section Getting started]
In short:
Just replace `&typeid(T)`, `typeid(T)` with `boost::type_id<T>()` and `const std::type_info&`, `std::type_index` with `boost::type_index`. That's all, you are now using Boost.TypeIndex.
Just replace `&typeid(T)`, `typeid(T)` with `boost::type_id<T>()` and `const std::type_info&`, `std::type_index` with `boost::type_index`. For cases when RTTI is really required, replace `typeid(variable)` with `boost::type_id_rtti_only(variable)`. That's all, you are now using Boost.TypeIndex.
To get nice human readable name, use `name_demangled()` member function:
``

View File

@@ -309,4 +309,46 @@ BOOST_AUTO_TEST_CASE(template_index_user_defined_class_test)
}
#ifndef BOOST_NO_RTTI
class A { public: virtual ~A(){} };
class B: public A{};
class C: public B {};
BOOST_AUTO_TEST_CASE(comparators_type_id_rtti_only)
{
C c1;
B b1;
A* pc1 = &c1;
A& rc1 = c1;
A* pb1 = &b1;
A& rb1 = b1;
BOOST_CHECK(typeid(rc1) == typeid(*pc1));
BOOST_CHECK(typeid(rb1) == typeid(*pb1));
BOOST_CHECK(typeid(rc1) != typeid(*pb1));
BOOST_CHECK(typeid(rb1) != typeid(*pc1));
BOOST_CHECK(typeid(&rc1) == typeid(pb1));
BOOST_CHECK(typeid(&rb1) == typeid(pc1));
BOOST_CHECK_EQUAL(boost::type_id_rtti_only(rc1), boost::type_id_rtti_only(*pc1));
BOOST_CHECK_EQUAL(boost::type_id_rtti_only(rb1), boost::type_id_rtti_only(*pb1));
BOOST_CHECK_NE(boost::type_id_rtti_only(rc1), boost::type_id_rtti_only(*pb1));
BOOST_CHECK_NE(boost::type_id_rtti_only(rb1), boost::type_id_rtti_only(*pc1));
BOOST_CHECK_EQUAL(boost::type_id_rtti_only(&rc1), boost::type_id_rtti_only(pb1));
BOOST_CHECK_EQUAL(boost::type_id_rtti_only(&rb1), boost::type_id_rtti_only(pc1));
BOOST_CHECK(boost::type_id_rtti_only(rc1) == typeid(*pc1));
BOOST_CHECK(boost::type_id_rtti_only(rb1) == typeid(*pb1));
BOOST_CHECK(boost::type_id_rtti_only(rc1) != typeid(*pb1));
BOOST_CHECK(boost::type_id_rtti_only(rb1) != typeid(*pc1));
BOOST_CHECK(boost::type_id_rtti_only(&rc1) == typeid(pb1));
BOOST_CHECK(boost::type_id_rtti_only(&rb1) == typeid(pc1));
}
#endif // BOOST_NO_RTTI