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> remapping;
49
54 const std::string &remap(const std::string &key);
55
56public:
58 Blackboard();
59
63 Blackboard(const Blackboard &other);
64
67
75 template <class T> T get(const std::string &key) {
76
77 YASMIN_LOG_DEBUG("Getting '%s' from the blackboard", key.c_str());
78
79 std::lock_guard<std::recursive_mutex> lk(this->mutex);
80
81 if (!this->contains(key)) {
82 throw std::runtime_error("Element " + key +
83 " does not exist in the blackboard");
84 }
85
86 BlackboardValue<T> *b_value =
87 (BlackboardValue<T> *)this->values.at(this->remap(key));
88 return b_value->get();
89 }
90
97 template <class T> void set(std::string name, T value) {
98
99 YASMIN_LOG_DEBUG("Setting '%s' in the blackboard", name.c_str());
100
101 std::lock_guard<std::recursive_mutex> lk(this->mutex);
102
103 if (!this->contains(name)) {
104 BlackboardValue<T> *b_value = new BlackboardValue<T>(value);
105 this->values.insert({name, b_value});
106
107 } else {
108 ((BlackboardValue<T> *)this->values.at(name))->set(value);
109 }
110 }
111
116 void remove(const std::string &key);
117
123 bool contains(const std::string &key);
124
129 int size();
130
135 std::string to_string();
136
141 void set_remapping(const std::map<std::string, std::string> &remapping);
142
147 const std::map<std::string, std::string> &get_remapping();
148};
149
150} // namespace blackboard
151} // namespace yasmin
152
153#endif // YASMIN__BLACKBOARD_HPP
A template class that wraps a value of type T.
Definition blackboard_value.hpp:41
T get()
Retrieve the stored value.
Definition blackboard_value.hpp:56
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:74
T get(const std::string &key)
Retrieve a value from the blackboard.
Definition blackboard.hpp:75
std::map< std::string, std::string > remapping
Storage for key-value pairs.
Definition blackboard.hpp:48
std::map< std::string, BlackboardValueInterface * > values
Storage for key-value pairs.
Definition blackboard.hpp:46
void set(std::string name, T value)
Set a value in the blackboard.
Definition blackboard.hpp:97
int size()
Get the number of key-value pairs in the blackboard.
Definition blackboard.cpp:56
Blackboard()
Default constructor for Blackboard.
Definition blackboard.cpp:23
void set_remapping(const std::map< std::string, std::string > &remapping)
Set the remapping of the blackboard.
Definition blackboard.cpp:82
const std::map< std::string, std::string > & get_remapping()
Get the remapping of the blackboard.
Definition blackboard.cpp:87
void remove(const std::string &key)
Remove a value from the blackboard.
Definition blackboard.cpp:38
~Blackboard()
Destructor for Blackboard.
Definition blackboard.cpp:31
bool contains(const std::string &key)
Check if a key exists in the blackboard.
Definition blackboard.cpp:47
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:61
#define YASMIN_LOG_DEBUG(text,...)
Definition logs.hpp:159
Definition blackboard.hpp:30
Definition blackboard.hpp:29