28 class auto_dispose_wrapper final
31 static_assert(std::derived_from<TDisposable, interface_disposable>);
33 template<
typename... TArgs>
35 explicit auto_dispose_wrapper(TArgs&&... args)
36 : m_data{std::forward<TArgs>(args)...}
40 auto_dispose_wrapper(
const auto_dispose_wrapper&) =
delete;
41 auto_dispose_wrapper(auto_dispose_wrapper&&)
noexcept =
delete;
43 ~auto_dispose_wrapper()
noexcept
45 static_cast<interface_disposable&
>(m_data).dispose_impl(rpp::interface_disposable::Mode::Destroying);
48 TDisposable* get() {
return &m_data; }
51 RPP_NO_UNIQUE_ADDRESS TDisposable m_data;
54 class disposable_wrapper_base
57 bool operator==(
const disposable_wrapper_base& other)
const
59 return get().first == other.get().first;
62 bool is_disposed()
const noexcept
64 if (
const auto locked = get().first)
65 return locked->is_disposed();
69 void dispose()
const noexcept
71 if (
const auto locked = get().first)
76 explicit disposable_wrapper_base(std::shared_ptr<interface_disposable>&& disposable)
77 : m_disposable{std::move(disposable)}
81 explicit disposable_wrapper_base(std::weak_ptr<interface_disposable>&& disposable)
82 : m_disposable{std::move(disposable)}
86 disposable_wrapper_base() =
default;
88 std::pair<std::shared_ptr<interface_disposable>,
bool> get()
const noexcept
90 if (
const auto ptr_ptr = std::get_if<std::shared_ptr<interface_disposable>>(&m_disposable))
91 return {*ptr_ptr,
true};
93 if (
const auto ptr_ptr = std::get_if<std::weak_ptr<interface_disposable>>(&m_disposable))
94 return {ptr_ptr->lock(),
false};
96 return {
nullptr,
true};
100 std::variant<std::monostate, std::shared_ptr<interface_disposable>, std::weak_ptr<interface_disposable>> m_disposable;
143 using TDefaultMake = std::conditional_t<std::same_as<TDisposable, interface_composite_disposable>,
composite_disposable, TDisposable>;
146 template<constra
int::decayed_type TTarget>
147 friend class disposable_wrapper_impl;
149 template<rpp::constra
int::decayed_type TTarget>
152 bool operator==(
const disposable_wrapper_impl&)
const =
default;
162 template<std::derived_from<TDisposable> TTarget = TDefaultMake,
typename... TArgs>
163 requires (std::constructible_from<TTarget, TArgs && ...>)
164 [[nodiscard]]
static disposable_wrapper_impl
make(TArgs&&... args)
166 const auto ptr = std::make_shared<details::auto_dispose_wrapper<TTarget>>(std::forward<TArgs>(args)...);
167 auto base_ptr = std::shared_ptr<TDisposable>{ptr,
static_cast<TDisposable*
>(ptr->get())};
170 base_ptr->set_weak_self(std::weak_ptr<interface_disposable>(base_ptr));
172 return disposable_wrapper_impl{std::static_pointer_cast<interface_disposable>(std::move(base_ptr))};
178 [[nodiscard]]
static disposable_wrapper_impl
empty()
180 return disposable_wrapper_impl{};
183 template<rpp::constra
int::is_nothrow_invocable Fn>
185 requires std::derived_from<TDisposable, interface_composite_disposable>
187 auto d = make_callback_disposable(std::forward<Fn>(invocable));
193 requires std::derived_from<TDisposable, interface_composite_disposable>
195 if (
const auto locked = lock())
196 locked->add(std::move(other));
202 requires std::derived_from<TDisposable, interface_composite_disposable>
204 if (
const auto locked = lock())
205 locked->remove(other);
209 requires std::derived_from<TDisposable, interface_composite_disposable>
211 if (
const auto locked = lock())
215 [[nodiscard]] std::shared_ptr<TDisposable> lock() const noexcept
217 return std::static_pointer_cast<TDisposable>(get().first);
220 [[nodiscard]] disposable_wrapper_impl as_weak()
const
222 auto [locked, is_shared] = get();
224 return disposable_wrapper_impl{std::weak_ptr<interface_disposable>{locked}};
228 template<constra
int::decayed_type TTarget>
229 requires rpp::constraint::static_pointer_convertible_to<TDisposable, TTarget>
230 operator disposable_wrapper_impl<TTarget>()
const
232 auto [locked, is_shared] = get();
236 auto res = disposable_wrapper_impl<TTarget>{std::move(locked)};
240 return res.as_weak();
244 using details::disposable_wrapper_base::disposable_wrapper_base;
251 class enable_wrapper_from_this
254 template<rpp::constra
int::decayed_type TSource>
258 enable_wrapper_from_this() =
default;
260 void set_weak_self(std::weak_ptr<interface_disposable> weak)
262 m_weak = std::move(weak);
266 disposable_wrapper_impl<TStrategy> wrapper_from_this()
const
268 return disposable_wrapper_impl<TStrategy>(std::static_pointer_cast<interface_disposable>(m_weak.lock()));
272 std::weak_ptr<interface_disposable> m_weak{};
static disposable_wrapper_impl make(TArgs &&... args)
Main way to create disposable_wrapper. Passed TTarget type can be any type derived from TDisposable.
Definition disposable_wrapper.hpp:164