boost::openmethod::policies::static_rtti
Minimal implementation of the rtti policy.
Synopsis
Declared in <boost/openmethod/policies/static_rtti.hpp>
struct static_rtti
: rtti
Description
static_rtti implements only the static parts of the rtti policy. It uses the addresses of a per‐class static variables as a type_id. It categorizes all types as non‐polymorphic. As a consequence, the virtual_ptr constructors that take a pointer or a reference to a polymorphic object are disabled. virtual_ptr must be constructed using final_virtual_ptr (or its equivalents for smart pointers).
Example
Selecting the policy, which has to happen before <boost/openmethod.hpp> is included:
#include <boost/openmethod/default_registry.hpp>
#include <boost/openmethod/policies/static_rtti.hpp>
struct static_registry
: boost::openmethod::registry<boost::openmethod::policies::static_rtti> {};
#define BOOST_OPENMETHOD_DEFAULT_REGISTRY static_registry
The classes and the method need no RTTI, and need not be polymorphic:
// polymorphism not required: there is no RTTI to consult
struct Animal {};
struct Cat : Animal {};
struct Dog : Animal {};
BOOST_OPENMETHOD_CLASSES(Animal, Cat, Dog);
BOOST_OPENMETHOD(trick, (virtual_ptr<Animal>), std::string);
BOOST_OPENMETHOD_OVERRIDE(trick, (virtual_ptr<Dog>), std::string) {
return "spin";
}
BOOST_OPENMETHOD_OVERRIDE(trick, (virtual_ptr<Cat>), std::string) {
return "sulk";
}
Every virtual_ptr has to be created where the exact class is known:
// the exact class must be known where the pointer is created
unique_virtual_ptr<Animal> a = make_unique_virtual<Cat>();
unique_virtual_ptr<Animal> b = make_unique_virtual<Dog>();
std::cout << trick(a) << "\n"; // sulk
std::cout << trick(b) << "\n"; // spin
See Also
Created with MrDocs