.. _program_listing_file_include_yasmin_blackboard_blackboard.hpp: Program Listing for File blackboard.hpp ======================================= |exhale_lsh| :ref:`Return to documentation for file ` (``include/yasmin/blackboard/blackboard.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // 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 . #ifndef YASMIN__BLACKBOARD__BLACKBOARD_HPP #define YASMIN__BLACKBOARD__BLACKBOARD_HPP #include #include #include #include #include #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 values; std::map remapping; const std::string &remap(const std::string &key); public: Blackboard(); Blackboard(const Blackboard &other); ~Blackboard(); template T get(const std::string &key) { YASMIN_LOG_DEBUG("Getting '%s' from the blackboard", key.c_str()); std::lock_guard lk(this->mutex); if (!this->contains(key)) { throw std::runtime_error("Element " + key + " does not exist in the blackboard"); } BlackboardValue *b_value = (BlackboardValue *)this->values.at(this->remap(key)); return b_value->get(); } template void set(std::string name, T value) { YASMIN_LOG_DEBUG("Setting '%s' in the blackboard", name.c_str()); std::lock_guard lk(this->mutex); if (!this->contains(name)) { BlackboardValue *b_value = new BlackboardValue(value); this->values.insert({name, b_value}); } else { ((BlackboardValue *)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 &remapping); const std::map &get_remapping(); }; } // namespace blackboard } // namespace yasmin #endif // YASMIN__BLACKBOARD_HPP