Botcraft 1.21.5
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
65 {
66 for (auto o : observers)
67 {
68 if (o != nullptr)
69 {
70 o->OnReset();
71 }
72 }
73 }
74
75 void Blackboard::NotifyKeyRemoved(const std::string& key) const
76 {
77 for (auto o : observers)
78 {
79 if (o != nullptr)
80 {
81 o->OnValueRemoved(key);
82 }
83 }
84 }
85
86 void Blackboard::NotifyKeyChanged(const std::string& key, const std::any& value) const
87 {
88 for (auto o : observers)
89 {
90 if (o != nullptr)
91 {
92 o->OnValueChanged(key, value);
93 }
94 }
95 }
96}
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 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.