shared / weak proxy #234
SidneyCogdill
started this conversation in
Ideas
Replies: 1 comment
-
@SidneyCogdill Thank you for the feedback! It is an interesting idea to create a "weak proxy". It is feasible to implement with #include <iostream>
#include "proxy.h"
template <pro::facade F>
struct WeakFacade;
template <pro::facade F>
struct SharedFacade;
template <pro::facade F>
using WeakProxy = pro::proxy<WeakFacade<F>>;
template <pro::facade F>
using SharedProxy = pro::proxy<SharedFacade<F>>;
namespace details {
template <class T>
std::weak_ptr<T> GetWeak(const std::shared_ptr<T>& self) noexcept { return {self}; }
template <class F, class T>
SharedProxy<F> Lock(const std::weak_ptr<T>& self) noexcept {
auto result = self.lock();
if (static_cast<bool>(result)) { return result; }
return nullptr;
}
} // namespace details
PRO_DEF_FREE_AS_MEM_DISPATCH(MemGetWeak, details::GetWeak, GetWeak);
template <class F>
PRO_DEF_FREE_AS_MEM_DISPATCH(MemLock, details::Lock<F>, Lock);
template <pro::facade F>
struct SharedFacade : pro::facade_builder
::support_copy<pro::constraint_level::nothrow>
::add_facade<F>
::template add_direct_convention<MemGetWeak, WeakProxy<F>() const noexcept>
::build {};
template <pro::facade F>
struct WeakFacade : pro::facade_builder
::support_copy<pro::constraint_level::nothrow>
::add_direct_convention<MemLock<F>, SharedProxy<F>() const noexcept>
::build {};
struct Formattable : pro::facade_builder
::support_format
::build {};
int main() {
SharedProxy<Formattable> p = std::make_shared<int>(123);
WeakProxy<Formattable> w = p.GetWeak();
{
SharedProxy<Formattable> locked = w.Lock();
std::cout << std::format("locked.has_value() = {}, *locked = {}\n", locked.has_value(), *locked); // Prints: "locked.has_value() = true, *locked = 123"
}
p.reset();
{
SharedProxy<Formattable> locked = w.Lock();
std::cout << std::format("locked.has_value() = {}\n", locked.has_value()); // Prints: "locked.has_value() = false"
}
}
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This is more of a subtle issue: proxy supports copying, but whether the copy of the proxy is aliased to the original proxy is unknown:
I'm not aware of any existing constructions in the language that has this behavior. Note how shared_ptr and raw pointers guarantee an alias:
In fact I don't even know if any other languages has that behavior. In C#/Java copying a reference type is guaranteed to create an alias just like shared_ptr in C++ does.
As I've created #233. if the addition of an owner type is desirable, perhaps the picture could be more completed by introducing a separate pair of "shared"/"weak" proxy types? note how this roughly replicates smart pointers in C++:
unique_ptr -> owner proxy
T * (observer_ptr) -> reference proxy
shared_ptr -> shared proxy
weak_ptr -> weak proxy
I think the addition of shared / weak proxy types can also be done by creating a thin wrapper around the existing proxy type:
support_copy
to allow copying (creating alias)unique_ptr
-like pointer)but i'm not sure. i know the owner / reference proxy variants are easy to make as I've wrote a working wrapper myself. but the shared / weak proxy is different: shared_ptr <-> weak_ptr conversion rules for the type erased object, and the copying without facade's
support_copy
probably requires building a new facade on the fly as the shared/weak proxy type itself is instantiated, so it's more involved than just fiddling with constructor rules.Beta Was this translation helpful? Give feedback.
All reactions