MCRO
C++23 utilities for Unreal Engine.
Loading...
Searching...
No Matches
Mcro::Observable::TState< T, DefaultPolicy > Struct Template Reference

#include <Observable.h>

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

Public Types

template<typename ThreadSafeType , typename NaiveType >
using ThreadSafeSwitch = std::conditional_t<static_cast<bool>(DefaultPolicy & ThreadSafeState), ThreadSafeType, NaiveType>
 
using StateBase = IState<T>
 
using ReadLockType = ThreadSafeSwitch<FReadScopeLock, FVoid>
 
using WriteLockType = ThreadSafeSwitch<FWriteScopeLock, FVoid>
 
using ReadLockVariant
 
using WriteLockVariant
 
- Public Types inherited from Mcro::Observable::IState< T >
using Type = T
 
using ReadLockVariant = TVariant<FReadScopeLock, FVoid>
 
using WriteLockVariant = TVariant<FWriteScopeLock, FVoid>
 

Public Member Functions

template<CDefaultInitializable = T>
 TState ()
 
template<CCopyConstructible = T>
 TState (T const &value)
 
template<CMoveConstructible = T>
 TState (T &&value)
 
template<CCopyConstructible = T>
 TState (TState const &other)
 
template<CMoveConstructible = T>
 TState (TState &&other)
 
template<typename Arg >
requires (!CConvertibleTo<Arg, TState> && !CSameAs<Arg, T>)
 TState (Arg &&arg)
 
template<typename... Args>
requires (sizeof...(Args) > 1)
 TState (Args &&... args)
 
virtual T const & Get () const override
 
virtual TTuple< T const &, TUniquePtr< ReadLockVariant > > GetOnAnyThread () const override
 
virtual void Set (T const &value) override
 
virtual void Modify (TUniqueFunction< void(T &)> &&modifier, bool alwaysNotify=true) override
 
virtual FDelegateHandle OnChange (TDelegate< void(TChangeData< T > const &)> onChange, EInvokeMode invokeMode=DefaultInvocation) override
 
virtual bool Remove (FDelegateHandle const &handle) override
 
virtual int32 RemoveAll (const void *object) override
 
virtual bool HasChangedFrom (const T &nextValue) override
 
virtual bool HasEverChanged () const override
 
virtual TUniquePtr< ReadLockVariantReadLock () const override
 
virtual TUniquePtr< WriteLockVariantWriteLock () override
 
- Public Member Functions inherited from Mcro::Observable::IState< T >
virtual ~IState ()=default
 
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)
 
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)
 

Data Fields

int32 PolicyFlags = DefaultPolicy
 

Static Public Attributes

static constexpr int32 DefaultPolicyFlags = DefaultPolicy
 

Additional Inherited Members

- Static Protected Member Functions inherited from Mcro::Observable::IState< T >
template<CChangeListener< T > Function>
static auto DelegateValueArgument (Function const &onChange, EInvokeMode invokeMode=DefaultInvocation)
 

Detailed Description

template<typename T, int32 DefaultPolicy>
struct Mcro::Observable::TState< T, DefaultPolicy >

Storage wrapper for any value which state needs to be tracked or their change needs to be observed. By default TState is not thread-safe unless ThreadSafeState policy is active in DefaultPolicy

Definition at line 226 of file Observable.h.

Member Typedef Documentation

◆ ReadLockType

template<typename T , int32 DefaultPolicy>
using Mcro::Observable::TState< T, DefaultPolicy >::ReadLockType = ThreadSafeSwitch<FReadScopeLock, FVoid>

Definition at line 236 of file Observable.h.

◆ ReadLockVariant

template<typename T , int32 DefaultPolicy>
using Mcro::Observable::IState< T >::ReadLockVariant

Definition at line 60 of file Observable.h.

◆ StateBase

template<typename T , int32 DefaultPolicy>
using Mcro::Observable::TState< T, DefaultPolicy >::StateBase = IState<T>

Definition at line 231 of file Observable.h.

◆ ThreadSafeSwitch

template<typename T , int32 DefaultPolicy>
template<typename ThreadSafeType , typename NaiveType >
using Mcro::Observable::TState< T, DefaultPolicy >::ThreadSafeSwitch = std::conditional_t<static_cast<bool>(DefaultPolicy & ThreadSafeState), ThreadSafeType, NaiveType>

Definition at line 229 of file Observable.h.

◆ WriteLockType

template<typename T , int32 DefaultPolicy>
using Mcro::Observable::TState< T, DefaultPolicy >::WriteLockType = ThreadSafeSwitch<FWriteScopeLock, FVoid>

Definition at line 237 of file Observable.h.

◆ WriteLockVariant

template<typename T , int32 DefaultPolicy>
using Mcro::Observable::IState< T >::WriteLockVariant

Definition at line 61 of file Observable.h.

Constructor & Destructor Documentation

◆ TState() [1/7]

template<typename T , int32 DefaultPolicy>
template<CDefaultInitializable = T>
Mcro::Observable::TState< T, DefaultPolicy >::TState ( )
inline

Enable default constructor only when T is default initializable

Definition at line 243 of file Observable.h.

◆ TState() [2/7]

template<typename T , int32 DefaultPolicy>
template<CCopyConstructible = T>
Mcro::Observable::TState< T, DefaultPolicy >::TState ( T const & value)
inline

Enable copy constructor for T only when T is copy constructable

Definition at line 247 of file Observable.h.

◆ TState() [3/7]

template<typename T , int32 DefaultPolicy>
template<CMoveConstructible = T>
Mcro::Observable::TState< T, DefaultPolicy >::TState ( T && value)
inline

Enable move constructor for T only when T is move constructable

Definition at line 251 of file Observable.h.

◆ TState() [4/7]

template<typename T , int32 DefaultPolicy>
template<CCopyConstructible = T>
Mcro::Observable::TState< T, DefaultPolicy >::TState ( TState< T, DefaultPolicy > const & other)
inline

Enable copy constructor for the state only when T is copy constructable

Definition at line 255 of file Observable.h.

◆ TState() [5/7]

template<typename T , int32 DefaultPolicy>
template<CMoveConstructible = T>
Mcro::Observable::TState< T, DefaultPolicy >::TState ( TState< T, DefaultPolicy > && other)
inline

Enable move constructor for the state only when T is move constructable

Definition at line 259 of file Observable.h.

◆ TState() [6/7]

template<typename T , int32 DefaultPolicy>
template<typename Arg >
requires (!CConvertibleTo<Arg, TState> && !CSameAs<Arg, T>)
Mcro::Observable::TState< T, DefaultPolicy >::TState ( Arg && arg)
inline

Construct value in-place with non-semantic single argument constructor

Definition at line 264 of file Observable.h.

◆ TState() [7/7]

template<typename T , int32 DefaultPolicy>
template<typename... Args>
requires (sizeof...(Args) > 1)
Mcro::Observable::TState< T, DefaultPolicy >::TState ( Args &&... args)
inline

Construct value in-place with multiple argument constructor

Definition at line 269 of file Observable.h.

Member Function Documentation

◆ Get()

template<typename T , int32 DefaultPolicy>
virtual T const & Mcro::Observable::TState< T, DefaultPolicy >::Get ( ) const
inlineoverridevirtual

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.

Implements Mcro::Observable::IState< T >.

Definition at line 271 of file Observable.h.

◆ GetOnAnyThread()

template<typename T , int32 DefaultPolicy>
virtual TTuple< T const &, TUniquePtr< ReadLockVariant > > Mcro::Observable::TState< T, DefaultPolicy >::GetOnAnyThread ( ) const
inlineoverridevirtual

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.

Implements Mcro::Observable::IState< T >.

Definition at line 273 of file Observable.h.

◆ HasChangedFrom()

template<typename T , int32 DefaultPolicy>
virtual bool Mcro::Observable::TState< T, DefaultPolicy >::HasChangedFrom ( const T & nextValue)
inlineoverridevirtual

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.

Implements Mcro::Observable::IState< T >.

Definition at line 348 of file Observable.h.

◆ HasEverChanged()

template<typename T , int32 DefaultPolicy>
virtual bool Mcro::Observable::TState< T, DefaultPolicy >::HasEverChanged ( ) const
inlineoverridevirtual

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

Implements Mcro::Observable::IState< T >.

Definition at line 363 of file Observable.h.

◆ Modify()

template<typename T , int32 DefaultPolicy>
virtual void Mcro::Observable::TState< T, DefaultPolicy >::Modify ( TUniqueFunction< void(T &)> && modifier,
bool alwaysNotify = true )
inlineoverridevirtual

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.

Implements Mcro::Observable::IState< T >.

Definition at line 300 of file Observable.h.

◆ OnChange()

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

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

Implements Mcro::Observable::IState< T >.

Definition at line 330 of file Observable.h.

◆ ReadLock()

template<typename T , int32 DefaultPolicy>
virtual TUniquePtr< ReadLockVariant > Mcro::Observable::TState< T, DefaultPolicy >::ReadLock ( ) const
inlineoverridevirtual

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.

Implements Mcro::Observable::IState< T >.

Definition at line 368 of file Observable.h.

◆ Remove()

template<typename T , int32 DefaultPolicy>
virtual bool Mcro::Observable::TState< T, DefaultPolicy >::Remove ( FDelegateHandle const & handle)
inlineoverridevirtual

Equivalent to TMulticastDelegate::Remove

Implements Mcro::Observable::IState< T >.

Definition at line 336 of file Observable.h.

◆ RemoveAll()

template<typename T , int32 DefaultPolicy>
virtual int32 Mcro::Observable::TState< T, DefaultPolicy >::RemoveAll ( const void * object)
inlineoverridevirtual

Equivalent to TMulticastDelegate::RemoveAll

Implements Mcro::Observable::IState< T >.

Definition at line 342 of file Observable.h.

◆ Set()

template<typename T , int32 DefaultPolicy>
virtual void Mcro::Observable::TState< T, DefaultPolicy >::Set ( T const & value)
inlineoverridevirtual

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()

Implements Mcro::Observable::IState< T >.

Definition at line 278 of file Observable.h.

◆ WriteLock()

template<typename T , int32 DefaultPolicy>
virtual TUniquePtr< WriteLockVariant > Mcro::Observable::TState< T, DefaultPolicy >::WriteLock ( )
inlineoverridevirtual

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.

Implements Mcro::Observable::IState< T >.

Definition at line 373 of file Observable.h.

Field Documentation

◆ DefaultPolicyFlags

template<typename T , int32 DefaultPolicy>
int32 Mcro::Observable::TState< T, DefaultPolicy >::DefaultPolicyFlags = DefaultPolicy
staticconstexpr

Definition at line 239 of file Observable.h.

◆ PolicyFlags

template<typename T , int32 DefaultPolicy>
int32 Mcro::Observable::TState< T, DefaultPolicy >::PolicyFlags = DefaultPolicy

Definition at line 378 of file Observable.h.


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