1 #ifndef GRAIL_BLACKBOARD_H
2 #define GRAIL_BLACKBOARD_H
5 #include "Logger/LoggerManager.hh"
24 friend bool operator == (
const UnsafeAny& first,
const UnsafeAny& second);
46 std::function<
const std::any&(
const std::any&,
const std::any&)> firstStrategy =
OursStrategy,
47 std::function<
const std::any&(
const std::any&,
const std::any&)> secondStrategy =
OursStrategy);
50 std::function<
const UnsafeAny&(
const UnsafeAny&,
const UnsafeAny&)> firstStrategy =
OursStrategy,
51 std::function<
const UnsafeAny&(
const UnsafeAny&,
const UnsafeAny&)> secondStrategy =
OursStrategy);
60 static const std::any&
OursStrategy(
const std::any& ours,
const std::any& theirs);
62 static const UnsafeAny&
OursStrategy(
const UnsafeAny& ours,
const UnsafeAny& theirs);
72 static const std::any&
TheirsStrategy(
const std::any& ours,
const std::any& theirs);
74 static const UnsafeAny&
TheirsStrategy(
const UnsafeAny& ours,
const UnsafeAny& theirs);
85 void Merge(
const Blackboard& other, std::function<
const UnsafeAny&(
const UnsafeAny&,
const UnsafeAny&)> mergeStrategy =
OursStrategy);
114 void SetValue(
const std::string& key,
const T& value)
116 std::lock_guard<std::mutex> lock{mutex};
118 std::any any = value;
121 auto deleter = [](
void* memory)
123 T* dataPointer =
static_cast<T*
>(memory);
126 auto cloner = [](
void* memory)
128 T* dataPointer =
static_cast<T*
>(memory);
129 return new T{*dataPointer};
132 data[key] = UnsafeAny(
new T{value}, deleter, cloner);
136 template<typename T, std::enable_if_t<!std::is_reference<T>::value>* =
nullptr>
144 std::lock_guard<std::mutex> lock{mutex};
146 data[key] = std::any{value};
148 auto deleter = [](
void* memory)
150 T* dataPointer =
static_cast<T*
>(memory);
153 auto cloner = [](
void* memory)
155 T* dataPointer =
static_cast<T*
>(memory);
156 return new T{*dataPointer};
158 data[key] = UnsafeAny(
new T{value}, deleter, cloner);
169 std::lock_guard<std::mutex> lock{mutex};
171 data[key] = std::any{T{}};
173 auto deleter = [](
void* memory)
175 T* dataPointer =
static_cast<T*
>(memory);
178 auto cloner = [](
void* memory)
180 T* dataPointer =
static_cast<T*
>(memory);
181 return new T{*dataPointer};
183 data[key] = UnsafeAny(
new T{}, deleter, cloner);
195 std::lock_guard<std::mutex> lock{mutex};
196 auto iterator = data.find(key);
197 if(iterator == data.end())
199 GRAIL_LOG(consts::DEFAULT_GRAIL_LOG_GROUP, Severity::CRITICAL,
"Invalid key (" + key +
")");
202 return std::any_cast<const T&>(iterator->second);
204 return *
static_cast<T*
>(iterator->second.GetDataPointer());
216 std::lock_guard<std::mutex> lock{ mutex };
218 return std::any_cast<T&>(data[key]);
220 auto iterator = data.find(key);
221 if(iterator == data.end())
223 auto deleter = [](
void* memory)
225 T* dataPointer =
static_cast<T*
>(memory);
228 auto cloner = [](
void* memory)
230 T* dataPointer =
static_cast<T*
>(memory);
231 return new T{*dataPointer};
233 iterator = data.emplace(std::piecewise_construct, std::forward_as_tuple(key),
234 std::forward_as_tuple(
new T{}, deleter, cloner)).first;
236 return *
static_cast<T*
>(iterator->second.GetDataPointer());
249 std::lock_guard<std::mutex> lock{mutex};
250 auto iter = data.find(key);
251 if (iter != data.end())
254 outValue = std::any_cast<T>(iter->second);
256 outValue = *
static_cast<T*
>(iter->second.GetDataPointer());
263 void AddDataToNameMap(std::map<std::string, std::string>& outMap)
const;
267 std::map<std::string, std::any> data{};
271 friend bool operator == (
const UnsafeAny& first,
const UnsafeAny& second);
274 UnsafeAny() =
default;
275 UnsafeAny(
void* dataPointer, std::function<
void(
void*)> deleter, std::function<
void*(
void*)> cloner);
277 UnsafeAny(
const UnsafeAny& other);
278 UnsafeAny(UnsafeAny&& other);
282 UnsafeAny& operator = (
const UnsafeAny& other);
283 UnsafeAny& operator = (UnsafeAny&& other);
285 void* GetDataPointer()
const;
288 void* dataPointer{
nullptr};
289 std::function<void(
void*)> deleter{[](
void*){}};
290 std::function<
void*(
void*)> cloner{[](
void*){
return nullptr; }};
293 std::map<std::string, UnsafeAny> data{};
295 mutable std::mutex mutex{};
The Blackboard class - grail's universal data container.
Definition: Blackboard.hh:21
static const std::any & TheirsStrategy(const std::any &ours, const std::any &theirs)
TheirsStrategy.
Definition: Blackboard.cpp:52
void SetValue(const std::string &key, T &&value)
SetValue - Inserts value keyed with name into this blackboard. If key already exists new value is ass...
Definition: Blackboard.hh:142
bool ContainsKey(const std::string &key) const
ContainsKey - Checks whether this blackboard contains given key.
Definition: Blackboard.cpp:98
T & GetOrAddValue(const std::string &key)
GetOrAddValue - Returns value keyed with name. If blackboard does not contain key,...
Definition: Blackboard.hh:214
void SetValue(const std::string &key, const T &value)
SetValue - Inserts value keyed with name into this blackboard. If key already exists new value is ass...
Definition: Blackboard.hh:114
size_t Size() const
Size.
Definition: Blackboard.cpp:104
void SetDefaultValue(const std::string &key)
SetDefaultValue - Inserts default value of given type keyed with name into this blackboard....
Definition: Blackboard.hh:167
void RemoveEntry(const std::string &key)
RemoveValue - Deletes value keyed with given name from this blackboard.
Definition: Blackboard.cpp:92
const T & GetValue(const std::string &key) const
GetValue - Returns value keyed with name. This method assumes that this blackboard contains given key...
Definition: Blackboard.hh:193
bool TryGetValue(const std::string &key, T &outValue) const
TryGetValue - Checks whether blackboard contains given key. If it does, assigns keyed value to refere...
Definition: Blackboard.hh:247
static const std::any & OursStrategy(const std::any &ours, const std::any &theirs)
OursStrategy.
Definition: Blackboard.cpp:43
void Merge(const Blackboard &other, std::function< const std::any &(const std::any &, const std::any &)> mergeStrategy=OursStrategy)
Merge - Merges data from other blackboard into this one. All conflicts are solved in favor of other b...
Definition: Blackboard.cpp:61
static void TwoWayMerge(Blackboard &first, Blackboard &second, std::function< const std::any &(const std::any &, const std::any &)> firstStrategy=OursStrategy, std::function< const std::any &(const std::any &, const std::any &)> secondStrategy=OursStrategy)
TwoWayMerge - merges second blackboard into first and first into second, uses provided merge strategi...
Definition: Blackboard.cpp:29