boost::openmethod::inplace_vptr_base

Embed a v‐table pointer in a class.

Synopsis

Declared in <boost/openmethod/inplace_vptr.hpp>

template<
    class Class,
    class Registry = boost::openmethod::default_registry>
class inplace_vptr_base;

Description

inplace_vptr_base is a CRTP mixin that embeds a v‐table pointer at the root of a class hierarchy. It also declares a boost_openmethod_vptr free function that returns the v‐table pointer stored in the object.

inplace_vptr_base registers the class in Registry. It is not necessary to register the class with use_classes or BOOST_OPENMETHOD_CLASSES.

The v‐table pointer is obtained directly from the Registry's static_vptr variable. No hashing is involved. If all the classes in Registry inherit from inplace_vptr_base, the registry needs not have a policies::vptr policy, nor any policy it depends on (like policies::type_hash).

An object that embeds its v‐table pointer does not need to be wrapped in a virtual_ptr

  • the two fill the same goal, fast access to the v‐table pointer ‐ and wrapping one is rejected at compile time.

If Registry contains the has_indirect_vptr policy, the v‐table pointer is stored as a pointer to a pointer, and remains valid after a call to initialize.

The default value of Registry can be changed by defining BOOST_OPENMETHOD_DEFAULT_REGISTRY.

Example

struct Animal : inplace_vptr_base<Animal> {};

struct Cat : Animal, inplace_vptr_derived<Cat, Animal> {};

struct Dog : Animal, inplace_vptr_derived<Dog, Animal> {};

BOOST_OPENMETHOD(trick, (virtual_<Animal&> animal), std::string);

BOOST_OPENMETHOD_OVERRIDE(trick, (Cat&), std::string) {
    return "sulk";
}

BOOST_OPENMETHOD_OVERRIDE(trick, (Dog&), std::string) {
    return "spin";
}

initialize();

std::unique_ptr<Animal> a = std::make_unique<Cat>();
std::unique_ptr<Animal> b = std::make_unique<Dog>();

std::cout << trick(*a) << "\n"; // sulk
std::cout << trick(*b) << "\n"; // spin

Type Aliases

Name

Description

boost_openmethod_registry

The registry Class has an affinity for, i.e. Registry. Read back by registry_affinity, and inherited by the classes derived from Class ‐ inside their own bodies too.

Protected Member Functions

Name

Description

inplace_vptr_base [constructor]

Set the vptr to Class's v‐table.

~inplace_vptr_base [destructor]

Set the vptr to nullptr.

Friends

Name

Description

boost::openmethod::boost_openmethod_vptr

boost::openmethod::boost_openmethod_bases

boost::openmethod::boost_openmethod_registry

Template Parameters

Name

Description

Class

The class in which to embed the v‐table pointer.

Registry

The registry in which Class and its derived classes are registered.

See Also