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__BLACKBOARD_HPP
17#define YASMIN__BLACKBOARD__BLACKBOARD_HPP
18
19#include <exception>
20#include <map>
21#include <mutex>
22#include <stdexcept>
23#include <string>
24
27#include "yasmin/logs.hpp"
28
29namespace yasmin {
30namespace blackboard {
31
42private:
44 std::recursive_mutex mutex;
46 std::map<std::string, BlackboardValueInterface *> values;
48 std::map<std::string, std::string> type_registry;
50 std::map<std::string, std::string> remappings;
51
56 const std::string &remap(const std::string &key);
57
58public:
60 Blackboard();
61
65 Blackboard(const Blackboard &other);
66
68 virtual ~Blackboard();
69
76 template <class T> void set(const std::string &name, T value) {
77
78 YASMIN_LOG_DEBUG("Setting '%s' in the blackboard", name.c_str());
79
80 std::lock_guard<std::recursive_mutex> lk(this->mutex);
81
82 BlackboardValue<T> *b_value = new BlackboardValue<T>(value);
83
84 // Apply remapping if exists
85 std::string key = this->remap(name);
86
87 // If the type is changing, remove the old entry first
88 if (this->type_registry.find(key) != this->type_registry.end()) {
89 this->values.erase(key);
90 this->type_registry.erase(key);
91 }
92
93 // Insert or update the value
94 if (!this->contains(key)) {
95 this->values.insert({key, b_value});
96 this->type_registry.insert({key, b_value->get_type()});
97
98 } else {
99 b_value = (BlackboardValue<T> *)this->values.at(key);
100 b_value->set(value);
101 this->type_registry[key] = b_value->get_type();
102 }
103 }
104
112 template <class T> T get(const std::string &key) {
113
114 YASMIN_LOG_DEBUG("Getting '%s' from the blackboard", key.c_str());
115
116 std::lock_guard<std::recursive_mutex> lk(this->mutex);
117
118 if (!this->contains(key)) {
119 throw std::runtime_error("Element '" + key +
120 "' does not exist in the blackboard");
121 }
122
123 BlackboardValue<T> *b_value =
124 (BlackboardValue<T> *)this->values.at(this->remap(key));
125 return b_value->get();
126 }
127
132 void remove(const std::string &key);
133
139 bool contains(const std::string &key);
140
145 int size();
146
153 std::string get_type(const std::string &key);
154
159 std::string to_string();
160
165 void set_remappings(const std::map<std::string, std::string> &remappings);
166
171 const std::map<std::string, std::string> &get_remappings();
172};
173
174} // namespace blackboard
175} // namespace yasmin
176
177#endif // YASMIN__BLACKBOARD_HPP
A template class that wraps a value of type T.
Definition blackboard_value.hpp:41
void set(T value)
Set a new value.
Definition blackboard_value.hpp:62
std::string get_type()
Get the type of the stored value as a string.
Definition blackboard_value.hpp:71
T get()
Retrieve the stored value.
Definition blackboard_value.hpp:56
const std::map< std::string, std::string > & get_remappings()
Get the remappings of the blackboard.
Definition blackboard.cpp:103
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:90
void set(const std::string &name, T value)
Set a value in the blackboard.
Definition blackboard.hpp:76
T get(const std::string &key)
Retrieve a value from the blackboard.
Definition blackboard.hpp:112
std::map< std::string, std::string > type_registry
Storage for type information for each key.
Definition blackboard.hpp:48
std::map< std::string, std::string > remappings
Storage for key remappings.
Definition blackboard.hpp:50
std::map< std::string, BlackboardValueInterface * > values
Storage for key-value pairs.
Definition blackboard.hpp:46
int size()
Get the number of key-value pairs in the blackboard.
Definition blackboard.cpp:58
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:63
void set_remappings(const std::map< std::string, std::string > &remappings)
Set the remappings of the blackboard.
Definition blackboard.cpp:98
void remove(const std::string &key)
Remove a value from the blackboard.
Definition blackboard.cpp:39
virtual ~Blackboard()
Virtual destructor for Blackboard.
Definition blackboard.cpp:32
bool contains(const std::string &key)
Check if a key exists in the blackboard.
Definition blackboard.cpp:49
std::recursive_mutex mutex
Mutex for thread safety.
Definition blackboard.hpp:44
std::string to_string()
Convert the contents of the blackboard to a string.
Definition blackboard.cpp:77
#define YASMIN_LOG_DEBUG(text,...)
Definition logs.hpp:174
Definition blackboard.hpp:30
Definition blackboard.hpp:29