Program Listing for File blackboard.hpp
↰ Return to documentation for file (include/yasmin/blackboard/blackboard.hpp
)
// Copyright (C) 2023 Miguel Ángel González Santamarta
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef YASMIN__BLACKBOARD__BLACKBOARD_HPP
#define YASMIN__BLACKBOARD__BLACKBOARD_HPP
#include <exception>
#include <map>
#include <mutex>
#include <stdexcept>
#include <string>
#include "yasmin/blackboard/blackboard_value.hpp"
#include "yasmin/blackboard/blackboard_value_interface.hpp"
#include "yasmin/logs.hpp"
namespace yasmin {
namespace blackboard {
class Blackboard {
private:
std::recursive_mutex mutex;
std::map<std::string, BlackboardValueInterface *> values;
std::map<std::string, std::string> remapping;
const std::string &remap(const std::string &key);
public:
Blackboard();
Blackboard(const Blackboard &other);
~Blackboard();
template <class T> T get(const std::string &key) {
YASMIN_LOG_DEBUG("Getting '%s' from the blackboard", key.c_str());
std::lock_guard<std::recursive_mutex> lk(this->mutex);
if (!this->contains(key)) {
throw std::runtime_error("Element " + key +
" does not exist in the blackboard");
}
BlackboardValue<T> *b_value =
(BlackboardValue<T> *)this->values.at(this->remap(key));
return b_value->get();
}
template <class T> void set(std::string name, T value) {
YASMIN_LOG_DEBUG("Setting '%s' in the blackboard", name.c_str());
std::lock_guard<std::recursive_mutex> lk(this->mutex);
if (!this->contains(name)) {
BlackboardValue<T> *b_value = new BlackboardValue<T>(value);
this->values.insert({name, b_value});
} else {
((BlackboardValue<T> *)this->values.at(name))->set(value);
}
}
void remove(const std::string &key);
bool contains(const std::string &key);
int size();
std::string to_string();
void set_remapping(const std::map<std::string, std::string> &remapping);
const std::map<std::string, std::string> &get_remapping();
};
} // namespace blackboard
} // namespace yasmin
#endif // YASMIN__BLACKBOARD_HPP