Botcraft 26.3
Loading...
Searching...
No Matches
Blackboard.cpp
Go to the documentation of this file.
2
3namespace Botcraft
4{
9
14
15 void Blackboard::Copy(const std::string& src, const std::string& dst)
16 {
17 const std::any& source = blackboard.at(src);
18 blackboard[dst] = source;
19 NotifyKeyChanged(dst, source);
20 }
21
22 void Blackboard::Erase(const std::string& key)
23 {
24 blackboard.erase(key);
26 }
27
28 void Blackboard::Reset(const std::map<std::string, std::any>& values)
29 {
30 blackboard.clear();
32 for (const auto& [k, v] : values)
33 {
34 blackboard[k] = v;
35 NotifyKeyChanged(k, v);
36 }
37 }
38
40 {
41 observers.push_back(observer);
42 }
43
45 {
46 for (auto it = observers.begin(); it != observers.end(); )
47 {
48 if (*it == observer)
49 {
50 it = observers.erase(it);
51 }
52 else
53 {
54 ++it;
55 }
56 }
57 }
58
59 bool Blackboard::Contains(const std::string& key) const
60 {
61 return blackboard.find(key) != blackboard.end();
62 }
63
64 void Blackboard::Trace(const std::string& key, const std::any& value, const bool add_trace_prefix) const
65 {
66 NotifyKeyChanged((add_trace_prefix ? "Trace." : "") + key, value);
67 }
68
70 {
71 for (auto o : observers)
72 {
73 if (o != nullptr)
74 {
75 o->OnReset();
76 }
77 }
78 }
79
80 void Blackboard::NotifyKeyRemoved(const std::string& key) const
81 {
82 for (auto o : observers)
83 {
84 if (o != nullptr)
85 {
86 o->OnValueRemoved(key);
87 }
88 }
89 }
90
91 void Blackboard::NotifyKeyChanged(const std::string& key, const std::any& value) const
92 {
93 for (auto o : observers)
94 {
95 if (o != nullptr)
96 {
97 o->OnValueChanged(key, value);
98 }
99 }
100 }
101}
void NotifyKeyChanged(const std::string &key, const std::any &value) const
void Erase(const std::string &key)
Remove a map entry if present.
void Subscribe(BlackboardObserver *observer)
void Unsubscribe(BlackboardObserver *observer)
bool Contains(const std::string &key) const
Check if a specific key is present in the blackboard.
void Copy(const std::string &src, const std::string &dst)
Copy a blackboard value.
std::map< std::string, std::any > blackboard
void Trace(const std::string &key, const std::any &value, const bool add_trace_prefix=true) const
Notify observers of a value without storing it (debug view only)
void NotifyCleared() const
void NotifyKeyRemoved(const std::string &key) const
std::vector< BlackboardObserver * > observers
void Reset(const std::map< std::string, std::any > &values={})
Clear all the entries in the blackboard and load new ones.