13#include <rpp/disposables/fwd.hpp>
15#include <rpp/defs.hpp>
16#include <rpp/disposables/interface_disposable.hpp>
17#include <rpp/utils/utils.hpp>
24 template<rpp::constra
int::decayed_type TDisposable>
25 class enable_wrapper_from_this;
27 template<rpp::constra
int::decayed_type TDisposable>
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;
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)
77 : m_disposable{std::move(disposable)}
82 : m_disposable{std::move(disposable)}
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;
140 template<rpp::constra
int::decayed_type TDisposable>
143 using TDefaultMake = std::conditional_t<std::same_as<TDisposable, interface_composite_disposable>,
composite_disposable, TDisposable>;
146 template<constra
int::decayed_type TTarget>
149 template<rpp::constra
int::decayed_type TTarget>
162 template<std::derived_from<TDisposable> TTarget = TDefaultMake,
typename... TArgs>
163 requires (std::constructible_from<TTarget, TArgs && ...>)
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));
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>
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;
248namespace rpp::details
250 template<rpp::constra
int::decayed_type TStrategy>
254 template<rpp::constra
int::decayed_type TSource>
260 void set_weak_self(std::weak_ptr<interface_disposable> weak)
262 m_weak = std::move(weak);
272 std::weak_ptr<interface_disposable> m_weak{};
Disposable which can keep some other sub-disposables. When this root disposable is disposed,...
Definition composite_disposable.hpp:175
Definition disposable_wrapper.hpp:55
Definition disposable_wrapper.hpp:252
Main RPP wrapper over disposables.
Definition fwd.hpp:27
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
static disposable_wrapper_impl empty()
Creates disposable_wrapper which behaves like disposed disposable.
Definition disposable_wrapper.hpp:178
Definition constraints.hpp:34
Definition constraints.hpp:31
disposable_wrapper_impl< interface_disposable > disposable_wrapper
Wrapper to keep "simple" disposable. Specialization of rpp::disposable_wrapper_impl.
Definition fwd.hpp:34