![]() |
MCRO
C++23 utilities for Unreal Engine.
|
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 FStatePolicy::ThreadSafe policy is active in DefaultPolicy. More...
#include <Observable.h>
Public Types | |
| template<typename ThreadSafeType , typename NaiveType > | |
| using | ThreadSafeSwitch = std::conditional_t<DefaultPolicy.ThreadSafe, 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 () | |
| Enable default constructor only when T is default initializable. | |
| template<CCopyConstructible = T> | |
| TState (T const &value) | |
| Enable copy constructor for T only when T is copy constructable. | |
| template<CMoveConstructible = T> | |
| TState (T &&value) | |
| Enable move constructor for T only when T is move constructable. | |
| template<CCopyConstructible = T> | |
| TState (TState const &other) | |
| Enable copy constructor for the state only when T is copy constructable. | |
| template<CMoveConstructible = T> | |
| TState (TState &&other) | |
| Enable move constructor for the state only when T is move constructable. | |
| template<typename Arg > requires (!CConvertibleTo<Arg, TState> && !CSameAs<Arg, T>) | |
| TState (Arg &&arg) | |
| Construct value in-place with non-semantic single argument constructor. | |
| template<typename... Args> requires (sizeof...(Args) > 1) | |
| TState (Args &&... args) | |
| Construct value in-place with multiple argument constructor. | |
| virtual T const & | Get () const override |
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 TTuple< T const &, TUniquePtr< ReadLockVariant > > | GetOnAnyThread () const override |
| 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 void | Set (T const &value) override |
| 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) override |
| Modify this state via an l-value ref in a functor. | |
| virtual bool | Remove (FDelegateHandle const &handle) override |
Equivalent to TMulticastDelegate::Remove | |
| virtual int32 | RemoveAll (const void *object) override |
Equivalent to TMulticastDelegate::RemoveAll | |
| virtual bool | HasChangedFrom (const T &nextValue) override |
| 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 override |
| Returns true if this state has ever been changed from its initial value given at construction. | |
| virtual TUniquePtr< ReadLockVariant > | ReadLock () const override |
| Lock this state for reading for the current scope. | |
| virtual TUniquePtr< WriteLockVariant > | WriteLock () override |
| Lock this state for writing for the current scope. | |
| virtual TOptional< T > const & | GetPrevious () const override |
| Get the previous value if StorePrevious is enabled and there was at least one change. | |
| virtual T const & | GetPrevious (T const &fallback) const override |
| 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 override |
| Get the previous value if StorePrevious is enabled and there was at least one change or the current value otherwise. | |
| virtual void | NormalizePrevious () override |
| Set the previous value to the current one. Useful in Ticks. | |
| template<CConvertibleTo< T > Other> requires (!CState<Other>) | |
| TState & | operator= (Other &&value) |
Public Member Functions inherited from Mcro::Observable::IState< T > | |
| virtual | ~IState ()=default |
| 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. | |
| 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 > | |
| auto && | operator-> (this Self &&self) |
Data Fields | |
| FStatePolicy | PolicyFlags { DefaultPolicy } |
Static Public Attributes | |
| static constexpr FStatePolicy | DefaultPolicyFlags = DefaultPolicy |
Protected Member Functions | |
| virtual FDelegateHandle | OnChangeImpl (TDelegate< void(TChangeData< T > const &)> &&onChange, FEventPolicy const &eventPolicy={}) override |
Protected Member Functions inherited from Mcro::Observable::IState< T > | |
Additional Inherited Members | |
Static Protected Member Functions inherited from Mcro::Observable::IState< T > | |
| template<CChangeListener< T > Function> | |
| static auto | DelegateValueArgument (Function const &onChange) |
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 FStatePolicy::ThreadSafe policy is active in DefaultPolicy.
TState and IState allows developers an expressive API for tracking/syncing changes of a stateful entity. Like button presses, resolution changes or storing the last error. It doesn't only give a storage for the value but triggers events when the value changes. This alleviates the need for explicit OnStuffChanged events in the API where TState is used.
Use TState on class members where a default set of policy can be declared in compile time, and use IState where these states should be referenced in a function argument for example. In the latter case the policy flags are erased so multiple states with different policies are compatible with each-other. Although that is not necessary and not recommended if we would only need to consume their value, as states can have implicit conversion to their value (returning a const-ref).
If given value is equality comparable, TState will only trigger change events when the previous and the current values are different. Unless that behavior is overridden by FStatePolicy flags. A default set of flags are determined by StatePolicyFor template for any given type.
Definition at line 373 of file Observable.h.
| using Mcro::Observable::TState< T, DefaultPolicy >::ReadLockType = ThreadSafeSwitch<FReadScopeLock, FVoid> |
Definition at line 383 of file Observable.h.
| using Mcro::Observable::IState< T >::ReadLockVariant |
Definition at line 60 of file Observable.h.
| using Mcro::Observable::TState< T, DefaultPolicy >::StateBase = IState<T> |
Definition at line 378 of file Observable.h.
| using Mcro::Observable::TState< T, DefaultPolicy >::ThreadSafeSwitch = std::conditional_t<DefaultPolicy.ThreadSafe, ThreadSafeType, NaiveType> |
Definition at line 376 of file Observable.h.
| using Mcro::Observable::TState< T, DefaultPolicy >::WriteLockType = ThreadSafeSwitch<FWriteScopeLock, FVoid> |
Definition at line 384 of file Observable.h.
| using Mcro::Observable::IState< T >::WriteLockVariant |
Definition at line 61 of file Observable.h.
|
inline |
Enable default constructor only when T is default initializable.
Definition at line 390 of file Observable.h.
|
inlineexplicit |
Enable copy constructor for T only when T is copy constructable.
Definition at line 394 of file Observable.h.
|
inlineexplicit |
Enable move constructor for T only when T is move constructable.
Definition at line 398 of file Observable.h.
|
inline |
Enable copy constructor for the state only when T is copy constructable.
Definition at line 402 of file Observable.h.
|
inline |
Enable move constructor for the state only when T is move constructable.
Definition at line 406 of file Observable.h.
|
inline |
Construct value in-place with non-semantic single argument constructor.
Definition at line 411 of file Observable.h.
|
inline |
Construct value in-place with multiple argument constructor.
Definition at line 416 of file Observable.h.
|
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 418 of file Observable.h.
|
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 keyword, the structured binding auto keyword preserves reference qualifiers. See https://godbolt.org/z/jn918fKfdImplements Mcro::Observable::IState< T >.
Definition at line 420 of file Observable.h.
|
inlineoverridevirtual |
Get the previous value if StorePrevious is enabled and there was at least one change.
Implements Mcro::Observable::IState< T >.
Definition at line 528 of file Observable.h.
|
inlineoverridevirtual |
Get the previous value if StorePrevious is enabled and there was at least one change. Get a fallback value otherwise.
| fallback | Return this value if there's no previous one available |
Implements Mcro::Observable::IState< T >.
Definition at line 533 of file Observable.h.
|
inlineoverridevirtual |
Get the previous value if StorePrevious is enabled and there was at least one change or the current value otherwise.
Implements Mcro::Observable::IState< T >.
Definition at line 538 of file Observable.h.
|
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.
Implements Mcro::Observable::IState< T >.
Definition at line 498 of file Observable.h.
|
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 513 of file Observable.h.
|
inlineoverridevirtual |
Modify this state via an l-value ref in a functor.
| modifier | The functor which modifies this value |
| alwaysNotify | Notify 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 448 of file Observable.h.
|
inlineoverridevirtual |
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.
Implements Mcro::Observable::IState< T >.
Definition at line 543 of file Observable.h.
|
inlineoverrideprotectedvirtual |
Implements Mcro::Observable::IState< T >.
Definition at line 479 of file Observable.h.
|
inline |
Definition at line 556 of file Observable.h.
|
inlineoverridevirtual |
Lock this state for reading for the current scope.
Implements Mcro::Observable::IState< T >.
Definition at line 518 of file Observable.h.
|
inlineoverridevirtual |
Equivalent to TMulticastDelegate::Remove
Implements Mcro::Observable::IState< T >.
Definition at line 486 of file Observable.h.
|
inlineoverridevirtual |
Equivalent to TMulticastDelegate::RemoveAll
Implements Mcro::Observable::IState< T >.
Definition at line 492 of file Observable.h.
|
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.
Implements Mcro::Observable::IState< T >.
Definition at line 425 of file Observable.h.
|
inlineoverridevirtual |
Lock this state for writing for the current scope.
Implements Mcro::Observable::IState< T >.
Definition at line 523 of file Observable.h.
|
staticconstexpr |
Definition at line 386 of file Observable.h.
| FStatePolicy Mcro::Observable::TState< T, DefaultPolicy >::PolicyFlags { DefaultPolicy } |
Definition at line 565 of file Observable.h.