Plasma Engine  2.0
Loading...
Searching...
No Matches
Blackboard.h
1#pragma once
2
3#include <Core/CoreDLL.h>
4#include <Foundation/Communication/Event.h>
5#include <Foundation/Strings/HashedString.h>
6#include <Foundation/Types/SharedPtr.h>
7#include <Foundation/Types/Variant.h>
8
10class plStreamWriter;
11
13struct PL_CORE_DLL plBlackboardEntryFlags
14{
15 using StorageType = plUInt16;
16
17 enum Enum
18 {
19 None = 0,
20 Save = PL_BIT(0),
21 OnChangeEvent = PL_BIT(1),
22
23 UserFlag0 = PL_BIT(7),
24 UserFlag1 = PL_BIT(8),
25 UserFlag2 = PL_BIT(9),
26 UserFlag3 = PL_BIT(10),
27 UserFlag4 = PL_BIT(11),
28 UserFlag5 = PL_BIT(12),
29 UserFlag6 = PL_BIT(13),
30 UserFlag7 = PL_BIT(14),
31
32 Invalid = PL_BIT(15),
33
34 Default = None
35 };
36
37 struct Bits
38 {
39 StorageType Save : 1;
40 StorageType OnChangeEvent : 1;
41 StorageType Reserved : 5;
42 StorageType UserFlag0 : 1;
43 StorageType UserFlag1 : 1;
44 StorageType UserFlag2 : 1;
45 StorageType UserFlag3 : 1;
46 StorageType UserFlag4 : 1;
47 StorageType UserFlag5 : 1;
48 StorageType UserFlag6 : 1;
49 StorageType UserFlag7 : 1;
50 StorageType Invalid : 1;
51 };
52};
53
54PL_DECLARE_FLAGS_OPERATORS(plBlackboardEntryFlags);
55PL_DECLARE_REFLECTABLE_TYPE(PL_CORE_DLL, plBlackboardEntryFlags);
56
57
65class PL_CORE_DLL plBlackboard : public plRefCounted
66{
67private:
68 plBlackboard(bool bIsGlobal);
69
70public:
72
73 bool IsGlobalBlackboard() const { return m_bIsGlobal; }
74
83
93 static plSharedPtr<plBlackboard> GetOrCreateGlobal(const plHashedString& sBlackboardName, plAllocator* pAllocator = plFoundation::GetDefaultAllocator());
94
96 static plSharedPtr<plBlackboard> FindGlobal(const plTempHashedString& sBlackboardName);
97
102 void SetName(plStringView sName);
103 const char* GetName() const { return m_sName; }
104 const plHashedString& GetNameHashed() const { return m_sName; }
105
106 struct Entry
107 {
108 plVariant m_Value;
110
113 plUInt32 m_uiChangeCounter = 0;
114 };
115
117 {
118 plHashedString m_sName;
119 plVariant m_OldValue;
120 const Entry* m_pEntry;
121 };
122
124 void RemoveEntry(const plHashedString& sName);
125
127 void RemoveAllEntries();
128
130 bool HasEntry(const plTempHashedString& sName) const;
131
141 void SetEntryValue(plStringView sName, const plVariant& value);
142
149 void SetEntryValue(const plHashedString& sName, const plVariant& value);
150
152 const Entry* GetEntry(const plTempHashedString& sName) const;
153
155 plBitflags<plBlackboardEntryFlags> GetEntryFlags(const plTempHashedString& sName) const;
156
158 plResult SetEntryFlags(const plTempHashedString& sName, plBitflags<plBlackboardEntryFlags> flags);
159
161 plVariant GetEntryValue(const plTempHashedString& sName, const plVariant& fallback = plVariant()) const;
162
164 plVariant IncrementEntryValue(const plTempHashedString& sName);
165
167 plVariant DecrementEntryValue(const plTempHashedString& sName);
168
170 const plHashTable<plHashedString, Entry>& GetAllEntries() const { return m_Entries; }
171
173 const plEvent<EntryEvent>& OnEntryEvent() const { return m_EntryEvents; }
174
178 plUInt32 GetBlackboardChangeCounter() const { return m_uiBlackboardChangeCounter; }
179
183 plUInt32 GetBlackboardEntryChangeCounter() const { return m_uiBlackboardEntryChangeCounter; }
184
186 plResult Serialize(plStreamWriter& inout_stream) const;
187
192 plResult Deserialize(plStreamReader& inout_stream);
193
194private:
195 PL_ALLOW_PRIVATE_PROPERTIES(plBlackboard);
196
197 static plBlackboard* Reflection_GetOrCreateGlobal(const plHashedString& sName);
198 static plBlackboard* Reflection_FindGlobal(plTempHashedString sName);
199 void Reflection_SetEntryValue(plStringView sName, const plVariant& value);
200
201 void ImplSetEntryValue(const plHashedString& sName, Entry& entry, const plVariant& value);
202
203 bool m_bIsGlobal = false;
204 plHashedString m_sName;
205 plEvent<EntryEvent> m_EntryEvents;
206 plUInt32 m_uiBlackboardChangeCounter = 0;
207 plUInt32 m_uiBlackboardEntryChangeCounter = 0;
209
210 PL_MAKE_SUBSYSTEM_STARTUP_FRIEND(Core, Blackboard);
211 static plMutex s_GlobalBlackboardsMutex;
213};
214
215PL_DECLARE_REFLECTABLE_TYPE(PL_CORE_DLL, plBlackboard);
216
218
219struct PL_CORE_DLL plBlackboardCondition
220{
221 plHashedString m_sEntryName;
222 double m_fComparisonValue = 0.0;
224
225 bool IsConditionMet(const plBlackboard& blackboard) const;
226
227 plResult Serialize(plStreamWriter& inout_stream) const;
228 plResult Deserialize(plStreamReader& inout_stream);
229};
230
231PL_DECLARE_REFLECTABLE_TYPE(PL_CORE_DLL, plBlackboardCondition);
Base class for all memory allocators.
Definition Allocator.h:23
A blackboard is a key/value store that provides OnChange events to be informed when a value changes.
Definition Blackboard.h:66
plUInt32 GetBlackboardEntryChangeCounter() const
This counter is increased every time any entry's value is modified.
Definition Blackboard.h:183
const plHashTable< plHashedString, Entry > & GetAllEntries() const
Grants read access to the entire map of entries.
Definition Blackboard.h:170
const plEvent< EntryEvent > & OnEntryEvent() const
Allows you to register to the OnEntryEvent. This is broadcast whenever an entry is modified that has ...
Definition Blackboard.h:173
plUInt32 GetBlackboardChangeCounter() const
This counter is increased every time an entry is added or removed (but not when it is modified).
Definition Blackboard.h:178
Definition Event.h:177
static PL_ALWAYS_INLINE plAllocator * GetDefaultAllocator()
The default allocator can be used for any kind of allocation if no alignment is required.
Definition Basics.h:82
Definition HashTable.h:333
This class is optimized to take nearly no memory (sizeof(void*)) and to allow very fast checks whethe...
Definition HashedString.h:25
Provides a simple mechanism for mutual exclusion to prevent multiple threads from accessing a shared ...
Definition Mutex.h:13
Base class for reference counted objects.
Definition RefCounted.h:52
A Shared ptr manages a shared object and destroys that object when no one references it anymore....
Definition SharedPtr.h:10
Interface for binary in (read) streams.
Definition Stream.h:22
Interface for binary out (write) streams.
Definition Stream.h:107
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
A class to use together with plHashedString for quick comparisons with temporary strings that need no...
Definition HashedString.h:151
plVariant is a class that can store different types of variables, which is useful in situations where...
Definition Variant.h:44
The plBitflags class allows you to work with type-safe bitflags.
Definition Bitflags.h:82
Definition Blackboard.h:117
Definition Blackboard.h:107
Definition Blackboard.h:220
Definition Blackboard.h:38
Flags for entries in plBlackboard.
Definition Blackboard.h:14
Enum
Definition Blackboard.h:18
A custom enum implementation that allows to define the underlying storage type to control its memory ...
Definition Enum.h:37
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54