ReactivePlusPlus
ReactiveX implementation for C++20
Loading...
Searching...
No Matches
rpp::disposable_wrapper_impl< TDisposable > Class Template Reference

Main RPP wrapper over disposables. More...

#include <disposable_wrapper.hpp>

Public Member Functions

bool operator== (const disposable_wrapper_impl &) const =default
 
template<rpp::constraint::is_nothrow_invocable Fn>
requires std::derived_from<TDisposable, interface_composite_disposable>
disposable_wrapper add (Fn &&invocable)
 
void add (disposable_wrapper other) const
 
void remove (const disposable_wrapper &other) const
 
void clear () const
 
std::shared_ptr< TDisposable > lock () const noexcept
 
disposable_wrapper_impl as_weak () const
 
template<constraint::decayed_type TTarget>
requires rpp::constraint::static_pointer_convertible_to<TDisposable, TTarget>
 operator disposable_wrapper_impl< TTarget > () const
 
- Public Member Functions inherited from rpp::details::disposable_wrapper_base
bool operator== (const disposable_wrapper_base &other) const
 
bool is_disposed () const noexcept
 
void dispose () const noexcept
 

Static Public Member Functions

template<std::derived_from< TDisposable > TTarget = TDefaultMake, typename... TArgs>
requires (std::constructible_from<TTarget, TArgs && ...>)
static disposable_wrapper_impl make (TArgs &&... args)
 Main way to create disposable_wrapper. Passed TTarget type can be any type derived from TDisposable.
 
static disposable_wrapper_impl empty ()
 Creates disposable_wrapper which behaves like disposed disposable.
 

Friends

template<rpp::constraint::decayed_type TTarget>
class details::enable_wrapper_from_this
 

Additional Inherited Members

- Protected Member Functions inherited from rpp::details::disposable_wrapper_base
 disposable_wrapper_base (std::shared_ptr< interface_disposable > &&disposable)
 
 disposable_wrapper_base (std::weak_ptr< interface_disposable > &&disposable)
 
std::pair< std::shared_ptr< interface_disposable >, bool > get () const noexcept
 

Detailed Description

template<rpp::constraint::decayed_type TDisposable>
class rpp::disposable_wrapper_impl< TDisposable >

Main RPP wrapper over disposables.

This wrapper invented to provide safe and easy-to-use access to disposables. It has next core points:

  • disposable_wrapper is kind of smart_pointer (like std::shared_ptr) but for disposables. So, default constructed wrapper is empty wrapper.
  • disposable_wrapper shares ownership like std::shared_ptr
  • any disposable created via disposable_wrapper would have call dispose() during it's destruction (during destruction of last disposable_wrapper owning it)
  • disposable_wrapper's methods is safe to use over empty/gone/disposed/weak disposables.
  • as soon as disposable can be actually "any internal state" it provides access to "raw" shared_ptr and it can be nullptr in case of disposable empty/ptr gone.
  • disposable_wrapper can be strong or weak (same as std::shared_ptr). weak disposable is important, for example, when it keeps observer and this observer should keep this disposable at the same time.
  • disposable_wrapper has popluar methods to work with disposable: dispose(), is_disposed() and add()/remove()/clear() (for interface_composite_disposable).

To construct wrapper you have to use make method:

auto d = rpp::disposable_wrapper::make<SomeSpecificDisposableType>(some_arguments, to_construct_it);
static disposable_wrapper_impl make(TArgs &&... args)
Definition disposable_wrapper.hpp:164

To achieve desired performance RPP is avoiding to returning disposable by default. So, it is why subscribe method is not returning anything by default. If you want to attach disposable to observer you can use overloading method accepting disposable as first argument like this:

observable.subscribe(d, [](int v){});
Base class for any observable used in RPP. It handles core callbacks of observable.
Definition observable.hpp:38
void subscribe(observer< Type, ObserverStrategy > &&observer) const
Subscribes passed observer to emissions from this observable.
Definition observable.hpp:58

or use subscribe_with_disposable method instead

composite_disposable_wrapper subscribe_with_disposable(observer< Type, ObserverStrategy > &&observer) const
Subscribes passed observer to emissions from this observable.
Definition observable.hpp:144
Note
rpp has 2 predefined disposable_wrappers for most popular cases:

Member Function Documentation

◆ make()

template<rpp::constraint::decayed_type TDisposable>
template<std::derived_from< TDisposable > TTarget = TDefaultMake, typename... TArgs>
requires (std::constructible_from<TTarget, TArgs && ...>)
static disposable_wrapper_impl rpp::disposable_wrapper_impl< TDisposable >::make ( TArgs &&... args)
inlinestaticnodiscard

Main way to create disposable_wrapper. Passed TTarget type can be any type derived from TDisposable.

Example:

The documentation for this class was generated from the following files: