Files
openmethod/doc/with_vptr.adoc
Jean-Louis Leroy 5e0fa8ee4b inception
2025-03-08 15:31:25 -05:00

78 lines
1.9 KiB
Plaintext

## with_vptr
### Synopsis
Defined in <boost/openmethod/core.hpp>.
```c++
namespace boost::openmethod {
template<class Class, class Policy = BOOST_OPENMETHOD_DEFAULT_POLICY>
class with_vptr {
protected:
with_vptr();
~with_vptr();
friend auto boost_openmethod_vptr(const Class& obj) -> vptr_type;
};
template<class Class, class Base, class... MoreBases>
class with_vptr {
protected:
with_vptr();
~with_vptr();
friend auto boost_openmethod_vptr(const Class& obj) -> vptr_type;
// if sizeof(MoreBases...) > 0
};
} // namespace boost::openmethod
```
### Description
`with_vptr` is a CRTP class template that embeds and manages a vptr across a
class hierarchy.
If `Class` has no `Bases`, `with_vptr` adds a `boost_openmethod_vptr` private
member to `Class`. In either case, it sets the vptr to the v-table of `Class`
from `Policy`. It also creates a `boost_openmethod_vptr` friend function that
takes a a `const Class&` and returns the embedded vptr.
If `Class` has has more than one base, the `boost_openmethod_vptr` friend
function is also created. It returns one of the embedded vptrs (it doesn't
matter which one, as they all have the same value). This is to resolve
ambiguities
As part of its implementation, `with_vptr` may also declare one or two free
functions (`boost_openmethod_policy` and `boost_openmethod_bases`) at certain
levels of the hierarchy.
### Members
#### constructor
```c++
with_vptr();
```
Sets the vptr to the v-table for Class, obtained from `Policy`. If `Policy`
contains `indirect_vptr`, an additional level of indirection is added, thus
preserving the validity of the pointer across calls to `initialize`.
#### destructor
```c++
~with_vptr();
```
For each `Base`, sets the vptr to the v-table for that base.
#### Free Functions
```c++
auto boost_openmethod_vptr(const Class& obj) -> vptr_type;
```
Returns the vptr embedded in `obj`.