boost::openmethod::inplace_vptr_base

Embed a v‐table pointer in a class.

Synopsis

template<
    class Class,
    class Registry = boost::openmethod::default_registry>
class inplace_vptr_base
    : protected /* implementation-defined */

Protected Base Classes

Name Description

/* implementation-defined */

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.

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_class or BOOST_OPENMETHOD_REGISTER

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).

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

#include <boost/openmethod.hpp>
#include <boost/openmethod/inplace_vptr.hpp>
#include <boost/openmethod/initialize.hpp>

using namespace boost::openmethod;

struct Animal : inplace_vptr_base<Animal> {};

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

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

BOOST_OPENMETHOD(
    poke, (virtual_<Animal&> animal, std::ostream& os), void);

BOOST_OPENMETHOD_OVERRIDE(poke, (Cat&, std::ostream& os), void) {
    os << "hiss\n";
}

BOOST_OPENMETHOD_OVERRIDE(poke, (Dog&, std::ostream& os), void) {
    os << "bark\n";
}

int main() {
    initialize();

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

    poke(*a, std::cout); // hiss
    poke(*b, std::cout); // bark

    return 0;
}

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.

Created with MrDocs