C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
blackboard.hpp
Go to the documentation of this file.
1// Copyright (C) 2023 Miguel Ángel González Santamarta
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16#ifndef YASMIN__BLACKBOARD_HPP_
17#define YASMIN__BLACKBOARD_HPP_
18
19#include <cxxabi.h>
20#include <exception>
21#include <map>
22#include <memory>
23#include <mutex>
24#include <stdexcept>
25#include <string>
26#include <unordered_map>
27
28#include "yasmin/logs.hpp"
29#include "yasmin/types.hpp"
30
31namespace yasmin {
32
38inline std::string demangle_type(const std::string &mangled_name) {
39
40 std::string name = mangled_name;
41
42#ifdef __GNUG__ // If using GCC/G++
43 int status;
44 // Demangle the name using GCC's demangling function
45 char *demangled =
46 abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status);
47 if (status == 0) {
48 name = demangled;
49 }
50 free(demangled);
51#endif
52
53 return name; // Return the demangled type name
54}
55
65private:
67 mutable std::recursive_mutex mutex;
69 std::unordered_map<std::string, std::shared_ptr<void>> values;
74
79 const std::string &remap(const std::string &key) const;
80
81public:
86
87
91
96 Blackboard(const Blackboard &other);
97
104 template <class T> void set(const std::string &name, T value) {
105
106 YASMIN_LOG_DEBUG("Setting '%s' in the blackboard", name.c_str());
107
108 std::lock_guard<std::recursive_mutex> lk(this->mutex);
109
110 // Apply remapping if exists
111 std::string key = this->remap(name);
112
113 // If the type is changing, remove the old entry first
114 if (this->type_registry.find(key) != this->type_registry.end()) {
115 this->values.erase(key);
116 this->type_registry.erase(key);
117 }
118
119 // Insert value and type information if key does not exist
120 if (!this->contains(key)) {
121 this->values[key] = std::make_shared<T>(value);
122 this->type_registry[key] = demangle_type(typeid(T).name());
123
124 } else {
125 // Check if the type is the same before updating
126 if (this->type_registry.at(key) != demangle_type(typeid(T).name())) {
127 this->values[key] = std::make_shared<T>(value);
128 this->type_registry[key] = demangle_type(typeid(T).name());
129 // Update the existing value
130 } else {
131 *(std::static_pointer_cast<T>(this->values.at(key))) = value;
132 }
133 }
134 }
135
143 template <class T> T get(const std::string &key) const {
144
145 YASMIN_LOG_DEBUG("Getting '%s' from the blackboard", key.c_str());
146
147 std::lock_guard<std::recursive_mutex> lk(this->mutex);
148
149 // Check if the key exists
150 if (!this->contains(key)) {
151 throw std::runtime_error("Element '" + key +
152 "' does not exist in the blackboard");
153 }
154
155 // Return the value casted to the requested type
156 return *(std::static_pointer_cast<T>(this->values.at(this->remap(key))));
157 }
158
163 void remove(const std::string &key);
164
170 bool contains(const std::string &key) const;
171
176 int size() const;
177
184 std::string get_type(const std::string &key) const;
185
190 std::string to_string() const;
191
197
202 const Remappings &get_remappings() const noexcept;
203};
204
205} // namespace yasmin
206
207#endif // YASMIN__BLACKBOARD_HPP_
Remappings remappings
Storage for key remappings.
Definition blackboard.hpp:73
std::unordered_map< std::string, std::shared_ptr< void > > values
Storage for key-value pairs.
Definition blackboard.hpp:69
std::recursive_mutex mutex
Mutex for thread safety.
Definition blackboard.hpp:67
std::string to_string() const
Convert the contents of the blackboard to a string.
Definition blackboard.cpp:68
void set(const std::string &name, T value)
Set a value in the blackboard.
Definition blackboard.hpp:104
TypeRegistry type_registry
Storage for type information for each key.
Definition blackboard.hpp:71
Blackboard()
Default constructor for Blackboard.
const Remappings & get_remappings() const noexcept
Get the remappings of the blackboard.
Definition blackboard.cpp:96
bool contains(const std::string &key) const
Check if a key exists in the blackboard.
Definition blackboard.cpp:40
std::string get_type(const std::string &key) const
Get the type of a value stored in the blackboard.
Definition blackboard.cpp:53
void set_remappings(const Remappings &remappings)
Set the remappings of the blackboard.
Definition blackboard.cpp:92
const std::string & remap(const std::string &key) const
Internal method that acquires the maped key. In the case the key is not remaped, retruns the arg key.
Definition blackboard.cpp:82
void remove(const std::string &key)
Remove a value from the blackboard.
Definition blackboard.cpp:31
int size() const
Get the number of key-value pairs in the blackboard.
Definition blackboard.cpp:48
T get(const std::string &key) const
Retrieve a value from the blackboard.
Definition blackboard.hpp:143
#define YASMIN_LOG_DEBUG(text,...)
Definition logs.hpp:174
Definition blackboard.hpp:31
std::string demangle_type(const std::string &mangled_name)
Demangle a C++ type name to a human-readable format.
Definition blackboard.hpp:38
StringMap Remappings
Map of remappings (string to string)
Definition types.hpp:88
StringMap TypeRegistry
Registry for type information.
Definition types.hpp:92
#define YASMIN_PTR_ALIASES(ClassName)
Macro to define all pointer aliases for a class.
Definition types.hpp:52