MCRO
C++23 utilities for Unreal Engine.
Loading...
Searching...
No Matches
Observable.Fwd.h
Go to the documentation of this file.
1/** @noop License Comment
2 * @file
3 * @copyright
4 * This Source Code is subject to the terms of the Mozilla Public License, v2.0.
5 * If a copy of the MPL was not distributed with this file You can obtain one at
6 * https://mozilla.org/MPL/2.0/
7 *
8 * @author David Mórász
9 * @date 2025
10 */
11
12#pragma once
13
14/**
15 * @file
16 * This is a forward declaration for types in Observable.h. Unless the full TState type is used for class member
17 * declarations, use this header in other header files.
18 */
19
20#include "CoreMinimal.h"
21#include "Mcro/FunctionTraits.h"
22
24{
25 using namespace Mcro::FunctionTraits;
26
27 /** @brief Flags expressing how TState should handle object comparison and lifespan */
29 {
30 /**
31 * @brief
32 * When the object inside TState is != comparable TState wull only emit change events when the submitted
33 * value differs from the existing one.
34 */
35 bool NotifyOnChangeOnly = false;
36
37 /** @brief Always emit change notification when a value is set on TState and don't attempt to compare them */
38 bool AlwaysNotify = false;
39
40 /** @brief Store previous value as well. If the value is equality comparable store only when it's changed. */
41 bool StorePrevious = false;
42
43 /**
44 * @brief
45 * If the state value is equality comparable, store the previous value even when that's equal to the new value.
46 * This flag doesn't do anything unless StorePrevious is also true.
47 */
48 bool AlwaysStorePrevious = false;
49
50 /**
51 * @brief
52 * Enable mutexes during modifications, notifications and expose a public read-lock for users
53 * of the state.
54 */
55 bool ThreadSafe = false;
56
57 /** @brief Merge two policy flags */
58 FORCEINLINE constexpr FStatePolicy With(FStatePolicy const& other) const
59 {
60 return {
65 ThreadSafe || other.ThreadSafe
66 };
67 }
68
69 FORCEINLINE friend constexpr bool operator == (FStatePolicy const& lhs, FStatePolicy const& rhs)
70 {
72 && lhs.AlwaysNotify == rhs.AlwaysNotify
73 && lhs.StorePrevious == rhs.StorePrevious
75 && lhs.ThreadSafe == rhs.ThreadSafe
76 ;
77 }
78
79 FORCEINLINE friend constexpr bool operator != (FStatePolicy const& lhs, FStatePolicy const& rhs)
80 {
81 return !(lhs == rhs);
82 }
83
84 /** @brief Is this instance equivalent to a default constructed one */
85 FORCEINLINE constexpr bool IsDefault() const
86 {
87 return *this == FStatePolicy();
88 }
89 };
90
91 struct IStateTag {};
92
93 template <typename T>
94 inline constexpr FStatePolicy StatePolicyFor =
95 CClass<T>
96 ? CCoreEqualityComparable<T>
97 ? FStatePolicy {.NotifyOnChangeOnly = true}
98 : FStatePolicy {.AlwaysNotify = true}
99 : FStatePolicy {.NotifyOnChangeOnly = true, .StorePrevious = true};
100
101 template <typename T>
102 struct IState;
103
104 template <typename T>
105 struct TChangeData;
106
107 template <typename T, FStatePolicy DefaultPolicy = StatePolicyFor<T>>
108 struct TState;
109
110 /**
111 * @brief
112 * Convenience alias for shared reference to a base type of TState. Use this in APIs which may modify or get the
113 * value of a state declared elsewhere.
114 */
115 template <typename T>
116 using IStateRef = TSharedRef<IState<T>>;
117
118 /**
119 * @brief
120 * Convenience alias for shared pointer to a base type of TState. Use this in APIs which may modify or get the
121 * value of a state declared elsewhere.
122 */
123 template <typename T>
124 using IStatePtr = TSharedPtr<IState<T>>;
125
126 /**
127 * @brief
128 * Convenience alias for weak pointer to a base type of TState. Use this in APIs which may modify or get the
129 * value of a state declared elsewhere.
130 */
131 template <typename T>
132 using IStateWeakPtr = TWeakPtr<IState<T>>;
133
134 /** @brief Convenience alias for declaring a state as a shared reference. Use this only as object members */
135 template <typename T, FStatePolicy DefaultPolicy = StatePolicyFor<T>>
136 using TSharedStateRef = TSharedRef<TState<T, DefaultPolicy>>;
137
138 /** @brief Convenience alias for declaring a state as a shared pointer. Use this only as object members */
139 template <typename T, FStatePolicy DefaultPolicy = StatePolicyFor<T>>
140 using TSharedStatePtr = TSharedPtr<TState<T, DefaultPolicy>>;
141
142 /** @brief Convenience alias for declaring a thread-safe state as a shared reference. Use this only as object members */
143 template <typename T, FStatePolicy DefaultPolicy = StatePolicyFor<T>>
144 using TSharedStateTSRef = TSharedRef<TState<T, DefaultPolicy.With({.ThreadSafe = true})>>;
145
146 /** @brief Convenience alias for declaring a thread-safe state as a shared pointer. Use this only as object members */
147 template <typename T, FStatePolicy DefaultPolicy = StatePolicyFor<T>>
148 using TSharedStateTSPtr = TSharedPtr<TState<T, DefaultPolicy.With({.ThreadSafe = true})>>;
149
150 /** @brief Concept constraining given type to a state */
151 template <typename T>
152 concept CState = CDerivedFrom<T, IStateTag>;
153
154 namespace Detail
155 {
156 template <typename Function, typename T>
157 concept CChangeListenerCandidate = CFunctionLike<Function>
158 && TFunction_ArgCount<Function> > 0
159 && CConvertibleTo<T, TFunction_ArgDecay<Function, 0>>
160 ;
161 }
162
163
164 /** @brief Concept describing a function which can listen to changes to the current value of a TState only */
165 template <typename Function, typename T>
167
168 /** @brief Concept describing a function which can listen to changes to the current and the previous values of a TState */
169 template <typename Function, typename T>
172 && CConvertibleTo<TOptional<T>, TFunction_ArgDecay<Function, 1>>
173 ;
174
175 /** @brief Concept describing a function which can be a change listener on a TState */
176 template <typename Function, typename T>
178
179 /** @brief Convenience alias for thread safe states */
180 template <typename T, FStatePolicy DefaultPolicy = StatePolicyFor<T>>
181 using TStateTS = TState<T, DefaultPolicy.With({.ThreadSafe = true})>;
182
183 /** @brief Convenience alias for boolean states */
185
186 /** @brief Convenience alias for thread-safe boolean states */
188}
Concept describing a function which can be a change listener on a TState.
Concept describing a function which can listen to changes to the current value of a TState only.
Concept describing a function which can listen to changes to the current and the previous values of a...
Concept constraining given type to a state.
constexpr size_t TFunction_ArgCount
Shorthand for getting a function argument count.
typename TFunctionTraits< std::decay_t< T > >::template ArgDecay< I > TFunction_ArgDecay
Shorthand for getting a decayed type of a function argument at given position I.
TSharedRef< TState< T, DefaultPolicy > > TSharedStateRef
Convenience alias for declaring a state as a shared reference. Use this only as object members.
TSharedPtr< IState< T > > IStatePtr
Convenience alias for shared pointer to a base type of TState. Use this in APIs which may modify or g...
TSharedRef< IState< T > > IStateRef
Convenience alias for shared reference to a base type of TState. Use this in APIs which may modify or...
TSharedPtr< TState< T, DefaultPolicy > > TSharedStatePtr
Convenience alias for declaring a state as a shared pointer. Use this only as object members.
TSharedPtr< TState< T, DefaultPolicy.With({.ThreadSafe=true})> > TSharedStateTSPtr
Convenience alias for declaring a thread-safe state as a shared pointer. Use this only as object memb...
TSharedRef< TState< T, DefaultPolicy.With({.ThreadSafe=true})> > TSharedStateTSRef
Convenience alias for declaring a thread-safe state as a shared reference. Use this only as object me...
TWeakPtr< IState< T > > IStateWeakPtr
Convenience alias for weak pointer to a base type of TState. Use this in APIs which may modify or get...
constexpr FStatePolicy StatePolicyFor
Flags expressing how TState should handle object comparison and lifespan.
bool StorePrevious
Store previous value as well. If the value is equality comparable store only when it's changed.
bool AlwaysStorePrevious
If the state value is equality comparable, store the previous value even when that's equal to the new...
bool NotifyOnChangeOnly
When the object inside TState is != comparable TState wull only emit change events when the submitted...
FORCEINLINE friend constexpr bool operator!=(FStatePolicy const &lhs, FStatePolicy const &rhs)
FORCEINLINE friend constexpr bool operator==(FStatePolicy const &lhs, FStatePolicy const &rhs)
FORCEINLINE constexpr bool IsDefault() const
Is this instance equivalent to a default constructed one.
FORCEINLINE constexpr FStatePolicy With(FStatePolicy const &other) const
Merge two policy flags.
bool AlwaysNotify
Always emit change notification when a value is set on TState and don't attempt to compare them.
bool ThreadSafe
Enable mutexes during modifications, notifications and expose a public read-lock for users of the sta...
Storage wrapper for any value which state needs to be tracked or their change needs to be observed....
Definition Observable.h:368