mirror of
https://github.com/boostorg/type_index.git
synced 2025-08-03 14:24:25 +02:00
New project structure to satisfy Modular Boost requirements.
This commit is contained in:
50
examples/demangled_names.cpp
Normal file
50
examples/demangled_names.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
// Copyright 2013 Antony Polukhin
|
||||
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See the accompanying file LICENSE_1_0.txt
|
||||
// or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
|
||||
|
||||
|
||||
//[type_index_names_example
|
||||
/*`
|
||||
The following example shows how short (mangled) and human readable type names could be obtained from a type.
|
||||
Works with and without RTTI.
|
||||
*/
|
||||
|
||||
|
||||
#include <boost/type_index/type_info.hpp>
|
||||
#include <iostream>
|
||||
|
||||
template <class T>
|
||||
void foo(T) {
|
||||
std::cout << "\n Short name: " << boost::type_id<T>().name();
|
||||
std::cout << "\n Readable name: " << boost::type_id<T>().name_demangled();
|
||||
}
|
||||
|
||||
struct user_defined_type{};
|
||||
|
||||
int main() {
|
||||
// Call to
|
||||
foo(1);
|
||||
// will output something like this:
|
||||
//
|
||||
// (RTTI on) (RTTI off)
|
||||
// Short name: i Short name: int]
|
||||
// Readable name: int Readable name: int
|
||||
|
||||
user_defined_type t;
|
||||
foo(t);
|
||||
// Will output:
|
||||
//
|
||||
// (RTTI on) (RTTI off)
|
||||
// Short name: 17user_defined_type user_defined_type]
|
||||
// Readable name: user_defined_type user_defined_type
|
||||
}
|
||||
|
||||
/*`
|
||||
Short names are very compiler dependant: some compiler will output `.H`, others `i`.
|
||||
|
||||
Readable names may also differ between compilers: `struct user_defined_type`, `user_defined_type`.
|
||||
*/
|
||||
|
||||
//] [/type_index_names_example]
|
59
examples/exact_types_match.cpp
Normal file
59
examples/exact_types_match.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
// Copyright 2013 Antony Polukhin
|
||||
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See the accompanying file LICENSE_1_0.txt
|
||||
// or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
|
||||
|
||||
//[type_index_exact_type_match_example
|
||||
/*`
|
||||
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/type_index.hpp>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <cassert>
|
||||
|
||||
class type_erased_unary_function {
|
||||
void* function_ptr_;
|
||||
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::type_id_with_cvr<ParamT>())
|
||||
{}
|
||||
|
||||
template <class ParamT>
|
||||
void call(ParamT v) {
|
||||
if (exact_param_t_ != boost::type_id_with_cvr<ParamT>()) {
|
||||
throw std::runtime_error("Incorrect `ParamT`");
|
||||
}
|
||||
|
||||
return (reinterpret_cast<void(*)(ParamT)>(function_ptr_))(v);
|
||||
}
|
||||
};
|
||||
|
||||
void foo(int){}
|
||||
|
||||
int main() {
|
||||
type_erased_unary_function func(&foo);
|
||||
func.call(100); // OK, `100` has type `int`
|
||||
|
||||
try {
|
||||
int i = 100;
|
||||
|
||||
// An attempt to convert stored function to a function accepting reference
|
||||
func.call<int&>(i); // Will throw, because types `int&` and `int` missmatch
|
||||
|
||||
assert(false);
|
||||
} catch (const std::runtime_error& /*e*/) {}
|
||||
}
|
||||
|
||||
//] [/type_index_exact_type_match_example]
|
34
examples/inheritance.cpp
Normal file
34
examples/inheritance.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
// Copyright 2013 Antony Polukhin
|
||||
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See the accompanying file LICENSE_1_0.txt
|
||||
// or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
|
||||
|
||||
//[type_index_derived_example
|
||||
/*`
|
||||
The following example shows that `boost::type_info` is able to store the real type, successfully getting through
|
||||
all the inheritances.
|
||||
|
||||
Example works with RTTI only. Without RTTI support it won't compile, producing a compile-time error with message:
|
||||
"boost::type_id_rtti_only(T&) requires RTTI"
|
||||
*/
|
||||
|
||||
#include <boost/type_index/type_info.hpp>
|
||||
#include <iostream>
|
||||
|
||||
struct A { virtual ~A(){} };
|
||||
struct B: public A {};
|
||||
struct C: public B {};
|
||||
|
||||
void print_real_type(const A& a) {
|
||||
std::cout << boost::type_id_rtti_only(a).name_demangled() << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
C c;
|
||||
const A& c_as_a = c;
|
||||
print_real_type(c_as_a); // Outputs `struct C`
|
||||
print_real_type(B()); // Outputs `struct B`
|
||||
}
|
||||
|
||||
//] [/type_index_derived_example]
|
36
examples/registry.cpp
Normal file
36
examples/registry.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright 2013 Antony Polukhin
|
||||
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See the accompanying file LICENSE_1_0.txt
|
||||
// or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
|
||||
|
||||
//[type_index_registry_example
|
||||
/*`
|
||||
The following example shows how an information about a type could be stored.
|
||||
Example works with and without RTTI.
|
||||
*/
|
||||
|
||||
#include <boost/type_index/type_index.hpp>
|
||||
#include <boost/unordered_set.hpp>
|
||||
#include <cassert>
|
||||
|
||||
int main() {
|
||||
boost::unordered_set<boost::type_index> types;
|
||||
|
||||
// Storing some `boost::type_info`s
|
||||
types.insert(boost::type_id<int>());
|
||||
types.insert(boost::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(boost::type_id<const int>()).second;
|
||||
assert(!is_inserted);
|
||||
assert(types.erase(boost::type_id<float&>()) == 1);
|
||||
|
||||
// We have erased the `float` type, only `int` remains
|
||||
assert(*types.begin() == boost::type_id<int>());
|
||||
}
|
||||
|
||||
//] [/type_index_registry_example]
|
Reference in New Issue
Block a user