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
27#include "yasmin/logs.hpp"
28
29namespace yasmin {
30
36inline std::string demangle_type(const std::string &mangled_name) {
37
38 std::string name = mangled_name;
39
40#ifdef __GNUG__ // If using GCC/G++
41 int status;
42 // Demangle the name using GCC's demangling function
43 char *demangled =
44 abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status);
45 if (status == 0) {
46 name = demangled;
47 }
48 free(demangled);
49#endif
50
51 return name; // Return the demangled type name
52}
53
63private:
65 std::recursive_mutex mutex;
67 std::map<std::string, std::shared_ptr<void>> values;
69 std::map<std::string, std::string> type_registry;
71 std::map<std::string, std::string> remappings;
72
77 const std::string &remap(const std::string &key);
78
79public:
81 Blackboard();
82
86 Blackboard(const Blackboard &other);
87
94 template <class T> void set(const std::string &name, T value) {
95
96 YASMIN_LOG_DEBUG("Setting '%s' in the blackboard", name.c_str());
97
98 std::lock_guard<std::recursive_mutex> lk(this->mutex);
99
100 // Apply remapping if exists
101 std::string key = this->remap(name);
102
103 // If the type is changing, remove the old entry first
104 if (this->type_registry.find(key) != this->type_registry.end()) {
105 this->values.erase(key);
106 this->type_registry.erase(key);
107 }
108
109 // Insert value and type information if key does not exist
110 if (!this->contains(key)) {
111 this->values[key] = std::make_shared<T>(value);
112 this->type_registry[key] = demangle_type(typeid(T).name());
113
114 } else {
115 // Check if the type is the same before updating
116 if (this->type_registry.at(key) != demangle_type(typeid(T).name())) {
117 this->values[key] = std::make_shared<T>(value);
118 this->type_registry[key] = demangle_type(typeid(T).name());
119 // Update the existing value
120 } else {
121 *(std::static_pointer_cast<T>(this->values.at(key))) = value;
122 }
123 }
124 }
125
133 template <class T> T get(const std::string &key) {
134
135 YASMIN_LOG_DEBUG("Getting '%s' from the blackboard", key.c_str());
136
137 std::lock_guard<std::recursive_mutex> lk(this->mutex);
138
139 // Check if the key exists
140 if (!this->contains(key)) {
141 throw std::runtime_error("Element '" + key +
142 "' does not exist in the blackboard");
143 }
144
145 // Return the value casted to the requested type
146 return *(std::static_pointer_cast<T>(this->values.at(this->remap(key))));
147 }
148
153 void remove(const std::string &key);
154
160 bool contains(const std::string &key);
161
166 int size();
167
174 std::string get_type(const std::string &key);
175
180 std::string to_string();
181
186 void set_remappings(const std::map<std::string, std::string> &remappings);
187
192 const std::map<std::string, std::string> &get_remappings();
193};
194
195} // namespace yasmin
196
197#endif // YASMIN__BLACKBOARD_HPP
const std::map< std::string, std::string > & get_remappings()
Get the remappings of the blackboard.
Definition blackboard.cpp:98
const std::string & remap(const std::string &key)
Internal method that acquires the maped key. In the case the key is not remaped, retruns the arg key.
Definition blackboard.cpp:83
std::map< std::string, std::string > type_registry
Storage for type information for each key.
Definition blackboard.hpp:69
std::map< std::string, std::string > remappings
Storage for key remappings.
Definition blackboard.hpp:71
std::recursive_mutex mutex
Mutex for thread safety.
Definition blackboard.hpp:65
void set(const std::string &name, T value)
Set a value in the blackboard.
Definition blackboard.hpp:94
int size()
Get the number of key-value pairs in the blackboard.
Definition blackboard.cpp:49
Blackboard()
Default constructor for Blackboard.
Definition blackboard.cpp:23
std::string get_type(const std::string &key)
Get the type of a value stored in the blackboard.
Definition blackboard.cpp:54
void set_remappings(const std::map< std::string, std::string > &remappings)
Set the remappings of the blackboard.
Definition blackboard.cpp:93
void remove(const std::string &key)
Remove a value from the blackboard.
Definition blackboard.cpp:32
std::map< std::string, std::shared_ptr< void > > values
Storage for key-value pairs.
Definition blackboard.hpp:67
bool contains(const std::string &key)
Check if a key exists in the blackboard.
Definition blackboard.cpp:41
T get(const std::string &key)
Retrieve a value from the blackboard.
Definition blackboard.hpp:133
std::string to_string()
Convert the contents of the blackboard to a string.
Definition blackboard.cpp:69
#define YASMIN_LOG_DEBUG(text,...)
Definition logs.hpp:174
Definition blackboard.hpp:29
std::string demangle_type(const std::string &mangled_name)
Demangle a C++ type name to a human-readable format.
Definition blackboard.hpp:36