Utility class to store an arbitrary type-erased object while another object is alive. More...
#include <owning_wrapper.hpp>
Public Types | |
| using | deleter_type = void(*)(void *) |
| Callback used to delete the owned object. | |
Public Member Functions | |
| OwningWrapper (void *obj, deleter_type deleter) | |
| Take ownership and responsibility for the destruction of an object. More... | |
| void * | release () noexcept |
| Release ownership of the underlying pointer. More... | |
| void * | get () const noexcept |
Utility class to store an arbitrary type-erased object while another object is alive.
When sending messages through Channels from Python, we typically need to keep various Python objects alive since the matching C++ objects only hold views.
For example, a C++ message payload may hold non-owning views into a Python-owned object. If we want to allow creation of such objects in Python with the ability to sink them on the C++ side we cannot rely on the Python side keeping that owner alive (the reference disappears!). Similarly when we send a message through a Channel the sender will, once pushed into the channel, drop the reference to the message payload and so, again, we need some way of keeping the payload alive.
To square this circle, such C++ objects have an OwningWrapper slot that stores a type-erased pointer with, as far as we are concerned, unique ownership semantics. When this object is destroyed, the custom deleter runs and can do whatever deallocation is necessary.
std::terminate.OwningWrapper inside a C++ object, make sure it is constructed first and destructed last. Definition at line 40 of file owning_wrapper.hpp.
|
inlineexplicit |
Take ownership and responsibility for the destruction of an object.
| obj | Type-erased object to own. |
| deleter | Function called to destruct the object. |
Definition at line 53 of file owning_wrapper.hpp.
|
inlinenoexcept |
Definition at line 68 of file owning_wrapper.hpp.
|
inlinenoexcept |
Release ownership of the underlying pointer.
Definition at line 61 of file owning_wrapper.hpp.