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

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

#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
 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.
 
virtual void Set (T const &value)=0
 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.
 
virtual void Modify (TUniqueFunction< void(T &)> &&modifier, bool alwaysNotify=true)=0
 Modify this state via an l-value ref in a functor.
 
FDelegateHandle OnChange (TDelegate< void(TChangeData< T > const &)> onChange, FEventPolicy const &eventPolicy={})
 Add a delegate which gets a TChangeData<T> const& if this state has been set.
 
template<CChangeListener< T > Function>
FDelegateHandle OnChange (Function const &onChange, FEventPolicy const &eventPolicy={})
 Add a function without object binding which either has one or two arguments with the following signature: [](T const& next, [TOptional<T> const& previous])
 
template<typename Object , CChangeListener< T > Function>
FDelegateHandle OnChange (Object &&object, Function const &onChange, FEventPolicy const &eventPolicy={})
 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])
 
template<CConvertibleToDecayed< T > Other, typename Guard >
void SyncPull (Guard &&object, IState< Other > &otherState)
 Pull changes from another state, syncing the value between the two. Values will be copied.
 
template<typename Other , typename Guard >
requires CConvertibleToDecayed<Other, T>
void SyncPush (Guard &&object, IState< Other > &otherState)
 Push changes from another state, syncing the value between the two. Values will be copied.
 
virtual bool HasChangedFrom (const T &nextValue)=0
 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.
 
virtual bool HasEverChanged () const =0
 Returns true if this state has ever been changed from its initial value given at construction.
 
virtual bool Remove (FDelegateHandle const &handle)=0
 Equivalent to TMulticastDelegate::Remove
 
virtual int32 RemoveAll (const void *object)=0
 Equivalent to TMulticastDelegate::RemoveAll
 
virtual TTuple< T const &, TUniquePtr< ReadLockVariant > > GetOnAnyThread () const =0
 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.
 
virtual TUniquePtr< ReadLockVariantReadLock () const =0
 Lock this state for reading for the current scope.
 
virtual TUniquePtr< WriteLockVariantWriteLock ()=0
 Lock this state for writing for the current scope.
 
virtual TOptional< T > const & GetPrevious () const =0
 Get the previous value if StorePrevious is enabled and there was at least one change.
 
virtual T const & GetPrevious (T const &fallback) const =0
 Get the previous value if StorePrevious is enabled and there was at least one change. Get a fallback value otherwise.
 
virtual T const & GetPreviousOrCurrent () const =0
 Get the previous value if StorePrevious is enabled and there was at least one change or the current value otherwise.
 
virtual void NormalizePrevious ()=0
 Set the previous value to the current one. Useful in Ticks.
 
template<CBooleanTestable = T>
bool BecameTrue (bool fallback=false) const
 Returns true when this state is currently true, but previously it wasn't.
 
template<CBooleanTestable = T>
bool OnDown (bool fallback=false) const
 Returns true when this state is currently true, but previously it wasn't.
 
template<CBooleanTestable = T>
bool BecameFalse (bool fallback=false) const
 Returns true when this state is currently false, but previously it wasn't.
 
template<CBooleanTestable = T>
bool OnUp (bool fallback=false) const
 Returns true when this state is currently false, but previously it wasn't.
 
template<CCoreEqualityComparable = T>
bool HasChanged () const
 Returns true when current value is not equal to previous one.
 
template<CCoreEqualityComparable = T>
bool HasChanged (T const &fallback) const
 Returns true when current value is not equal to previous one.
 
template<typename Self >
 operator T const & (this Self &&self)
 
template<typename Self >
const T * operator-> (this Self &&self)
 

Protected Member Functions

virtual FDelegateHandle OnChangeImpl (TDelegate< void(TChangeData< T > const &)> &&onChange, FEventPolicy const &eventPolicy={})=0
 

Static Protected Member Functions

template<CChangeListener< T > Function>
static auto DelegateValueArgument (Function const &onChange)
 

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

◆ BecameFalse()

template<typename T >
template<CBooleanTestable = T>
bool Mcro::Observable::IState< T >::BecameFalse ( bool fallback = false) const
inline

Returns true when this state is currently false, but previously it wasn't.

Parameters
fallbackUse this as the fallback previous state.

Definition at line 302 of file Observable.h.

◆ BecameTrue()

template<typename T >
template<CBooleanTestable = T>
bool Mcro::Observable::IState< T >::BecameTrue ( bool fallback = false) const
inline

Returns true when this state is currently true, but previously it wasn't.

Parameters
fallbackUse this as the fallback previous state.

Definition at line 279 of file Observable.h.

◆ DelegateValueArgument()

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

Definition at line 97 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();
Remarks
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 >.

◆ GetPrevious() [1/2]

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

Get the previous value if StorePrevious is enabled and there was at least one change.

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

◆ GetPrevious() [2/2]

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

Get the previous value if StorePrevious is enabled and there was at least one change. Get a fallback value otherwise.

Parameters
fallbackReturn this value if there's no previous one available

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

◆ GetPreviousOrCurrent()

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

Get the previous value if StorePrevious is enabled and there was at least one change or the current value otherwise.

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

◆ HasChanged() [1/2]

template<typename T >
template<CCoreEqualityComparable = T>
bool Mcro::Observable::IState< T >::HasChanged ( ) const
inline

Returns true when current value is not equal to previous one.

Definition at line 321 of file Observable.h.

◆ HasChanged() [2/2]

template<typename T >
template<CCoreEqualityComparable = T>
bool Mcro::Observable::IState< T >::HasChanged ( T const & fallback) const
inline

Returns true when current value is not equal to previous one.

Definition at line 328 of file Observable.h.

◆ 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 >.

◆ NormalizePrevious()

template<typename T >
virtual void Mcro::Observable::IState< T >::NormalizePrevious ( )
pure virtual

Set the previous value to the current one. Useful in Ticks.

It will not trigger change notifications but it will use a write-lock when thread-safety is enabled. If StorePrevious is disabled it will do nothing.

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,
FEventPolicy const & eventPolicy = {} )
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 126 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,
FEventPolicy const & eventPolicy = {} )
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 140 of file Observable.h.

◆ OnChange() [3/3]

template<typename T >
FDelegateHandle Mcro::Observable::IState< T >::OnChange ( TDelegate< void(TChangeData< T > const &)> onChange,
FEventPolicy const & eventPolicy = {} )
inline

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

Definition at line 112 of file Observable.h.

◆ OnChangeImpl()

template<typename T >
virtual FDelegateHandle Mcro::Observable::IState< T >::OnChangeImpl ( TDelegate< void(TChangeData< T > const &)> && onChange,
FEventPolicy const & eventPolicy = {} )
protectedpure virtual

◆ OnDown()

template<typename T >
template<CBooleanTestable = T>
bool Mcro::Observable::IState< T >::OnDown ( bool fallback = false) const
inline

Returns true when this state is currently true, but previously it wasn't.

Parameters
fallbackUse this as the fallback previous state.

Definition at line 294 of file Observable.h.

◆ OnUp()

template<typename T >
template<CBooleanTestable = T>
bool Mcro::Observable::IState< T >::OnUp ( bool fallback = false) const
inline

Returns true when this state is currently false, but previously it wasn't.

Parameters
fallbackUse this as the fallback previous state.

Definition at line 317 of file Observable.h.

◆ operator T const &()

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

Definition at line 334 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 340 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 >.

◆ SyncPull()

template<typename T >
template<CConvertibleToDecayed< T > Other, typename Guard >
void Mcro::Observable::IState< T >::SyncPull ( Guard && object,
IState< Other > & otherState )
inline

Pull changes from another state, syncing the value between the two. Values will be copied.

Template Parameters
OtherType convertible to the value of this state
GuardThe type of the lifespan guarding object
Parameters
objectA lifespan guarding object in the context of this state, shared/weak pointer or UObject recommended.
otherStateA reference to the other state.

Definition at line 157 of file Observable.h.

◆ SyncPush()

template<typename T >
template<typename Other , typename Guard >
requires CConvertibleToDecayed<Other, T>
void Mcro::Observable::IState< T >::SyncPush ( Guard && object,
IState< Other > & otherState )
inline

Push changes from another state, syncing the value between the two. Values will be copied.

Template Parameters
OtherThe value of this state convertible to this type
GuardThe type of the lifespan guarding object
Parameters
objectA lifespan guarding object in the context of the other state, shared/weak pointer or UObject recommended.
otherStateA reference to the other state.

Definition at line 179 of file Observable.h.

◆ 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: