boost::openmethod::weak_virtual_ptr
Weak pointer to an object, remembering its v‐table pointer
Synopsis
Declared in <boost/openmethod/interop/std_weak_ptr.hpp>
template<
class Class,
class Registry = registry_affinity<Class>>
class weak_virtual_ptr;
Description
A weak_virtual_ptr tracks an object with a std::weak_ptr, and remembers its v‐table pointer. It is a storage facility, not a virtual_ptr: it cannot be dereferenced, compared, or used as a virtual parameter, because the object may no longer exist. It can be passed to a method as an ordinary parameter. Call lock() to obtain a shared_virtual_ptr, then use it as usual. Since the v‐table pointer is copied from the weak pointer, lock() costs no more than std::weak_ptr::lock(): no hash table lookup is needed.
Remembering the v‐table pointer is safe with respect to the lifetime of the object: a std::weak_ptr keeps the control block alive, so once the object is destroyed, the weak pointer stays expired, and the v‐table pointer can never be applied to another object.
|
As for any |
Example
struct Animal {
virtual ~Animal() = default;
};
struct Dog : Animal {};
struct Cat : Animal {};
BOOST_OPENMETHOD_CLASSES(Animal, Dog, Cat);
shared_virtual_ptr<Animal> animal = make_shared_virtual<Dog>();
weak_virtual_ptr<Animal> observer = animal;
std::cout << poke(observer.lock()) << "\n"; // bark
animal = nullptr;
std::cout << std::boolalpha << observer.expired() << "\n"; // true
Member Functions
Name |
Description |
|
Constructors |
Assignment operators |
|
Lock the weak pointer |
|
Check whether the object still exists |
|
Get the number of |
|
|
|
Release the reference to the object |
|
Swap with another |
|
Get the weak pointer to the object |
|
Get the v‐table pointer |
Template Parameters
Name |
Description |
Class |
The class of the object, possibly cv‐qualified |
Registry |
The registry in which |
See Also
Created with MrDocs