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