MCRO
C++23 utilities for Unreal Engine.
Loading...
Searching...
No Matches
Mcro::Observable::IState< T > Struct Template Referenceabstract

#include <Observable.h>

Inheritance diagram for Mcro::Observable::IState< T >:
Mcro::Observable::IStateTag Mcro::Observable::TState< T, DefaultPolicy >

Public Types

using Type = T
 
using ReadLockVariant = TVariant<FReadScopeLock, FVoid>
 
using WriteLockVariant = TVariant<FWriteScopeLock, FVoid>
 

Public Member Functions

virtual ~IState ()=default
 
virtual T const & Get () const =0
 
virtual void Set (T const &value)=0
 
virtual void Modify (TUniqueFunction< void(T &)> &&modifier, bool alwaysNotify=true)=0
 
virtual FDelegateHandle OnChange (TDelegate< void(TChangeData< T > const &)> onChange, EInvokeMode invokeMode=DefaultInvocation)=0
 
template<CChangeListener< T > Function>
FDelegateHandle OnChange (Function const &onChange, EInvokeMode invokeMode=DefaultInvocation)
 
template<typename Object , CChangeListener< T > Function>
FDelegateHandle OnChange (Object &&object, Function const &onChange, EInvokeMode invokeMode=DefaultInvocation)
 
virtual bool HasChangedFrom (const T &nextValue)=0
 
virtual bool HasEverChanged () const =0
 
virtual bool Remove (FDelegateHandle const &handle)=0
 
virtual int32 RemoveAll (const void *object)=0
 
virtual TTuple< T const &, TUniquePtr< ReadLockVariant > > GetOnAnyThread () const =0
 
virtual TUniquePtr< ReadLockVariantReadLock () const =0
 
virtual TUniquePtr< WriteLockVariantWriteLock ()=0
 
template<typename Self >
 operator const T & (this Self &&self)
 
template<typename Self >
const T * operator-> (this Self &&self)
 
template<typename Self , CConvertibleTo< T > Other>
requires (!CState<Other>)
Self & operator= (this Self &&self, Other &&value)
 

Static Protected Member Functions

template<CChangeListener< T > Function>
static auto DelegateValueArgument (Function const &onChange, EInvokeMode invokeMode=DefaultInvocation)
 

Detailed Description

template<typename T>
struct Mcro::Observable::IState< T >

Public API and base class for TState which shouldn't concern with policy flags or thread safety

Definition at line 57 of file Observable.h.

Member Typedef Documentation

◆ ReadLockVariant

template<typename T >
using Mcro::Observable::IState< T >::ReadLockVariant = TVariant<FReadScopeLock, FVoid>

Definition at line 60 of file Observable.h.

◆ Type

template<typename T >
using Mcro::Observable::IState< T >::Type = T

Definition at line 59 of file Observable.h.

◆ WriteLockVariant

template<typename T >
using Mcro::Observable::IState< T >::WriteLockVariant = TVariant<FWriteScopeLock, FVoid>

Definition at line 61 of file Observable.h.

Constructor & Destructor Documentation

◆ ~IState()

template<typename T >
virtual Mcro::Observable::IState< T >::~IState ( )
virtualdefault

Member Function Documentation

◆ DelegateValueArgument()

template<typename T >
template<CChangeListener< T > Function>
static auto Mcro::Observable::IState< T >::DelegateValueArgument ( Function const & onChange,
EInvokeMode invokeMode = DefaultInvocation )
inlinestaticprotected

Definition at line 95 of file Observable.h.

◆ Get()

template<typename T >
virtual T const & Mcro::Observable::IState< T >::Get ( ) const
pure virtual

Get the wrapped value if for some reason the conversion operator is not enough or deleted. Thread safety is not considered in this function, use ReadLock before Get, or use GetOnAnyThread which provides a read lock, if thread safety is a concern.

Implemented in Mcro::Observable::TState< T, DefaultPolicy >.

◆ GetOnAnyThread()

template<typename T >
virtual TTuple< T const &, TUniquePtr< ReadLockVariant > > Mcro::Observable::IState< T >::GetOnAnyThread ( ) const
pure virtual

If thread safety is enabled in DefaultPolicy, get the value with a bundled read-scope-lock. Otherwise the tuple returns an empty dummy struct as its second argument. Use C++17 structured binding for convenience:

auto [value, lock] = MyState.GetOnAnyThread();

Unlike the placeholder auto keyword, the structured binding auto keyword preserves reference qualifiers. See https://godbolt.org/z/jn918fKfd

Returns
The lock is returned as TUniquePtr it's slightly more expensive because of ref-counting but it makes the API so much easier to use as TState can decide to return a real lock or just a dummy.

Implemented in Mcro::Observable::TState< T, DefaultPolicy >.

◆ HasChangedFrom()

template<typename T >
virtual bool Mcro::Observable::IState< T >::HasChangedFrom ( const T & nextValue)
pure virtual

Given value will be stored in the state only if T is equality comparable and it differs from the current state value. If T is not equality comparable this function is equivalent to Set and always returns true.

Returns
True if the given value was different from the previous state value. Always returns true when T is is not equality comparable.

Implemented in Mcro::Observable::TState< T, DefaultPolicy >.

◆ HasEverChanged()

template<typename T >
virtual bool Mcro::Observable::IState< T >::HasEverChanged ( ) const
pure virtual

Returns true if this state has ever been changed from its initial value given at construction.

Implemented in Mcro::Observable::TState< T, DefaultPolicy >.

◆ Modify()

template<typename T >
virtual void Mcro::Observable::IState< T >::Modify ( TUniqueFunction< void(T &)> && modifier,
bool alwaysNotify = true )
pure virtual

Modify this state via an l-value ref in a functor

Parameters
modifierThe functor which modifies this value
alwaysNotifyNotify observers about the change even when the previous state is not different after the modification. This is only applicable when T is copyable, comparable, StorePrevious flag is set and AlwaysNotify flag is not set via policy.

Implemented in Mcro::Observable::TState< T, DefaultPolicy >.

◆ OnChange() [1/3]

template<typename T >
template<CChangeListener< T > Function>
FDelegateHandle Mcro::Observable::IState< T >::OnChange ( Function const & onChange,
EInvokeMode invokeMode = DefaultInvocation )
inline

Add a function without object binding which either has one or two arguments with the following signature:

[](T const& next, [TOptional<T> const& previous])

Where the argument previous is optional (to have, not its type). The argument previous when it is present is TOptional because it may only have a value when StorePrevious policy is active and T is copyable.

Definition at line 121 of file Observable.h.

◆ OnChange() [2/3]

template<typename T >
template<typename Object , CChangeListener< T > Function>
FDelegateHandle Mcro::Observable::IState< T >::OnChange ( Object && object,
Function const & onChange,
EInvokeMode invokeMode = DefaultInvocation )
inline

Add a function with an object binding which either has one or two arguments with the following signature:

[](T const& next, [TOptional<T> const& previous])

Where the argument previous is optional (to have, not its type). The argument previous when it is present is TOptional because it may only have a value when StorePrevious policy is active and T is copyable.

Definition at line 137 of file Observable.h.

◆ OnChange() [3/3]

template<typename T >
virtual FDelegateHandle Mcro::Observable::IState< T >::OnChange ( TDelegate< void(TChangeData< T > const &)> onChange,
EInvokeMode invokeMode = DefaultInvocation )
pure virtual

Add a delegate which gets a TChangeData<T> const& if this state has been set.

Implemented in Mcro::Observable::TState< T, DefaultPolicy >.

◆ operator const T &()

template<typename T >
template<typename Self >
Mcro::Observable::IState< T >::operator const T & ( this Self && self)
inline

Definition at line 198 of file Observable.h.

◆ operator->()

template<typename T >
template<typename Self >
const T * Mcro::Observable::IState< T >::operator-> ( this Self && self)
inline

Definition at line 204 of file Observable.h.

◆ operator=()

template<typename T >
template<typename Self , CConvertibleTo< T > Other>
requires (!CState<Other>)
Self & Mcro::Observable::IState< T >::operator= ( this Self && self,
Other && value )
inline

Definition at line 211 of file Observable.h.

◆ ReadLock()

template<typename T >
virtual TUniquePtr< ReadLockVariant > Mcro::Observable::IState< T >::ReadLock ( ) const
pure virtual

Lock this state for reading for the current scope

Returns
The lock is returned as TUniquePtr it's slightly more expensive because of ref-counting but it makes the API so much easier to use as TState can decide to return a real lock or just a dummy.

Implemented in Mcro::Observable::TState< T, DefaultPolicy >.

◆ Remove()

template<typename T >
virtual bool Mcro::Observable::IState< T >::Remove ( FDelegateHandle const & handle)
pure virtual

Equivalent to TMulticastDelegate::Remove

Implemented in Mcro::Observable::TState< T, DefaultPolicy >.

◆ RemoveAll()

template<typename T >
virtual int32 Mcro::Observable::IState< T >::RemoveAll ( const void * object)
pure virtual

Equivalent to TMulticastDelegate::RemoveAll

Implemented in Mcro::Observable::TState< T, DefaultPolicy >.

◆ Set()

template<typename T >
virtual void Mcro::Observable::IState< T >::Set ( T const & value)
pure virtual

Set the wrapped value if for some reason the assignment operator is not enough or deleted. When thread safety is enabled Set will automatically lock this state for writing.

Warning
Setting this state from within its change listeners is prohibited and will trigger a check()

Implemented in Mcro::Observable::TState< T, DefaultPolicy >.

◆ WriteLock()

template<typename T >
virtual TUniquePtr< WriteLockVariant > Mcro::Observable::IState< T >::WriteLock ( )
pure virtual

Lock this state for writing for the current scope

Returns
The lock is returned as TUniquePtr it's slightly more expensive because of ref-counting but it makes the API so much easier to use as TState can decide to return a real lock or just a dummy.

Implemented in Mcro::Observable::TState< T, DefaultPolicy >.


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