Fixed issues noted by Paul A. Bristow during review

This commit is contained in:
Antony Polukhin
2014-05-04 14:22:25 +04:00
parent 2e11dc75d4
commit a66ffcfa50
5 changed files with 26 additions and 32 deletions

View File

@ -37,9 +37,6 @@ is a drop-in replacement for `std::type_index`. Unlike Standard Library versions
`type_index` provides the full set of comparison operators, hashing functions and ostream
operators, so it can be used with any container class.
Through all the examples, we'll assume that the following namespace alias is in effect:
``namespace bti = boost::typeind;``
[section How to use]
To start using Boost.TypeIndex:
@ -56,7 +53,7 @@ To start using Boost.TypeIndex:
[[``
std::type_index
``][``
bti::type_index
boost::typeind::type_index
``]]
[[``
@ -65,18 +62,18 @@ To start using Boost.TypeIndex:
typeid(T).name() // not human readable
typeid(variable)
``][``
bti::type_id<T>()
bti::type_id_with_cvr<T>()
bti::type_id<T>().pretty_name() // human readable
bti::type_id_runtime(variable)
boost::typeind::type_id<T>()
boost::typeind::type_id_with_cvr<T>()
boost::typeind::type_id<T>().pretty_name() // human readable
boost::typeind::type_id_runtime(variable)
``]]
[[``
const std::type_info& v1 = typeid(int); // when reference to `std::type_info` is required
const std::type_info* v2 = &typeid(int); // other cases
``][``
const bti::type_info& v1 = bti::type_id<int>().type_info();
bti::type_index v2 = bti::type_id<int>();
const boost::typeind::type_info& v1 = boost::typeind::type_id<int>().type_info();
boost::typeind::type_index v2 = boost::typeind::type_id<int>();
``]]
]
@ -98,10 +95,10 @@ Here is how TypeIndex could be used in `boost/any.hpp`:
return typeid(ValueType);
}
``] [``
virtual const bti::type_info & type() const BOOST_NOEXCEPT
virtual const boost::typeind::type_info & type() const BOOST_NOEXCEPT
{
// now works even with RTTI disabled
return bti::type_id<ValueType>().type_info();
return boost::typeind::type_id<ValueType>().type_info();
}
``]]
]
@ -141,14 +138,14 @@ public: // visitor interfaces
#endif // BOOST_NO_TYPEID
``][``
class reflect
: public static_visitor<const bti::type_info&>
: public static_visitor<const boost::typeind::type_info&>
{
public: // visitor interfaces
template <typename T>
const bti::type_info& operator()(const T&) const BOOST_NOEXCEPT
const boost::typeind::type_info& operator()(const T&) const BOOST_NOEXCEPT
{
return bti::type_id<T>().type_info();
return boost::typeind::type_id<T>().type_info();
}
};
@ -162,7 +159,7 @@ public: // visitor interfaces
}
#endif
``] [``
const bti::type_info& type() const
const boost::typeind::type_info& type() const
{
detail::variant::reflect visitor;
return this->apply_visitor(visitor);

View File

@ -18,8 +18,8 @@ namespace bti = boost::typeind;
template <class T>
void foo(T) {
std::cout << "\n Short name: " << bti::type_id<T>().raw_name();
std::cout << "\n Readable name: " << bti::type_id<T>().pretty_name();
std::cout << "\n Short name: " << boost::typeind::type_id<T>().raw_name();
std::cout << "\n Readable name: " << boost::typeind::type_id<T>().pretty_name();
}
struct user_defined_type{};

View File

@ -18,22 +18,21 @@
#include <iostream>
#include <stdexcept>
#include <cassert>
namespace bti = boost::typeind;
class type_erased_unary_function {
void* function_ptr_;
bti::type_index exact_param_t_;
void* function_ptr_;
boost::typeind::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_(bti::type_id_with_cvr<ParamT>())
, exact_param_t_(boost::typeind::type_id_with_cvr<ParamT>())
{}
template <class ParamT>
void call(ParamT v) {
if (exact_param_t_ != bti::type_id_with_cvr<ParamT>()) {
if (exact_param_t_ != boost::typeind::type_id_with_cvr<ParamT>()) {
throw std::runtime_error("Incorrect `ParamT`");
}

View File

@ -14,7 +14,6 @@
#include <boost/type_index.hpp>
#include <iostream>
namespace bti = boost::typeind;
struct A {
BOOST_TYPE_INDEX_REGISTER_CLASS
@ -24,7 +23,7 @@ struct B: public A { BOOST_TYPE_INDEX_REGISTER_CLASS };
struct C: public B { BOOST_TYPE_INDEX_REGISTER_CLASS };
void print_real_type(const A& a) {
std::cout << bti::type_id_runtime(a).pretty_name() << '\n';
std::cout << boost::typeind::type_id_runtime(a).pretty_name() << '\n';
}
int main() {

View File

@ -14,25 +14,24 @@
#include <boost/unordered_set.hpp>
#include <boost/functional/hash.hpp>
#include <cassert>
namespace bti = boost::typeind;
int main() {
boost::unordered_set<bti::type_index> types;
boost::unordered_set<boost::typeind::type_index> types;
// Storing some `boost::type_info`s
types.insert(bti::type_id<int>());
types.insert(bti::type_id<float>());
types.insert(boost::typeind::type_id<int>());
types.insert(boost::typeind::type_id<float>());
// `types` variable contains two `boost::type_index`es:
assert(types.size() == 2);
// Const, volatile and reference will be striped from the type:
bool is_inserted = types.insert(bti::type_id<const int>()).second;
bool is_inserted = types.insert(boost::typeind::type_id<const int>()).second;
assert(!is_inserted);
assert(types.erase(bti::type_id<float&>()) == 1);
assert(types.erase(boost::typeind::type_id<float&>()) == 1);
// We have erased the `float` type, only `int` remains
assert(*types.begin() == bti::type_id<int>());
assert(*types.begin() == boost::typeind::type_id<int>());
}
//] [/type_index_registry_example]