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;
47
48public:
50 Blackboard();
51
55 Blackboard(const Blackboard &other);
56
59
67 template <class T> T get(std::string name) {
68
69 YASMIN_LOG_DEBUG("Getting '%s' from the blackboard", name.c_str());
70
71 std::lock_guard<std::recursive_mutex> lk(this->mutex);
72
73 if (!this->contains(name)) {
74 throw std::runtime_error("Element " + name +
75 " does not exist in the blackboard");
76 }
77
78 BlackboardValue<T> *b_value = (BlackboardValue<T> *)this->values.at(name);
79 return b_value->get();
80 }
81
88 template <class T> void set(std::string name, T value) {
89
90 YASMIN_LOG_DEBUG("Setting '%s' in the blackboard", name.c_str());
91
92 std::lock_guard<std::recursive_mutex> lk(this->mutex);
93
94 if (!this->contains(name)) {
95 BlackboardValue<T> *b_value = new BlackboardValue<T>(value);
96 this->values.insert({name, b_value});
97
98 } else {
99 ((BlackboardValue<T> *)this->values.at(name))->set(value);
100 }
101 }
102
107 void remove(std::string name);
108
114 bool contains(std::string name);
115
120 int size();
121
126 std::string to_string();
127};
128
129} // namespace blackboard
130} // namespace yasmin
131
132#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
A thread-safe storage for key-value pairs of varying types.
Definition blackboard.hpp:41
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:88
int size()
Get the number of key-value pairs in the blackboard.
Definition blackboard.cpp:53
T get(std::string name)
Retrieve a value from the blackboard.
Definition blackboard.hpp:67
Blackboard()
Default constructor for Blackboard.
Definition blackboard.cpp:23
bool contains(std::string name)
Check if a key exists in the blackboard.
Definition blackboard.cpp:46
~Blackboard()
Destructor for Blackboard.
Definition blackboard.cpp:31
std::recursive_mutex mutex
Mutex for thread safety.
Definition blackboard.hpp:44
void remove(std::string name)
Remove a value from the blackboard.
Definition blackboard.cpp:38
std::string to_string()
Convert the contents of the blackboard to a string.
Definition blackboard.cpp:58
#define YASMIN_LOG_DEBUG(text,...)
Definition logs.hpp:74
Definition blackboard.hpp:29