Alias template weak_proxy
Class template weak_facade
Header:
proxy.h
Module:proxy
Namespace:pro::inline v4
Since: 3.3.0
template <facade F>
struct weak_facade;
template <facade F>
using weak_proxy = proxy<weak_facade<F>>;
weak_proxy<F> is a non-owning handle that participates in the weak ownership model of an object that (when alive) models proxiable_target<T, F>. It is analogous to std::weak_ptr relative to std::shared_ptr: it never extends the lifetime of the underlying object, but it can attempt to produce a (strong) proxy<F> via lock().
weak_facade<F> adapts the original facade F so that:
- A
lock()member (direct convention) is provided, returning aproxy<F>that contains a value if and only if the referenced object is still alive at the time of the call. - All direct substitution conversions that would have produced a
proxy<G>become conversions that produce aweak_proxy<G>instead (so that "weak-ness" is preserved across facade-substitution). - No reflections from
Fare preserved.
Member Types of weak_facade
Member Constants of weak_facade
Example
#include <iostream>
#include <proxy/proxy.h>
struct Formattable : pro::facade_builder //
::add_skill<pro::skills::format> //
::build {};
int main() {
std::shared_ptr<int> val = std::make_shared<int>(123);
pro::weak_proxy<Formattable> wp = std::weak_ptr{val};
pro::proxy<Formattable> p = wp.lock();
std::cout << std::boolalpha << p.has_value() << "\n"; // Prints "true"
std::cout << std::format("{}\n", *p); // Prints "123"
p.reset();
val.reset();
p = wp.lock();
std::cout << p.has_value() << "\n"; // Prints "false"
}
