Program Listing for File blackboard.hpp

Return to documentation for file (include/yasmin/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_HPP
#define YASMIN__BLACKBOARD_HPP

#include <cxxabi.h>
#include <exception>
#include <map>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <string>

#include "yasmin/logs.hpp"

namespace yasmin {

inline std::string demangle_type(const std::string &mangled_name) {

  std::string name = mangled_name;

#ifdef __GNUG__ // If using GCC/G++
  int status;
  // Demangle the name using GCC's demangling function
  char *demangled =
      abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status);
  if (status == 0) {
    name = demangled;
  }
  free(demangled);
#endif

  return name; // Return the demangled type name
}

class Blackboard {
private:
  std::recursive_mutex mutex;
  std::map<std::string, std::shared_ptr<void>> values;
  std::map<std::string, std::string> type_registry;
  std::map<std::string, std::string> remappings;

  const std::string &remap(const std::string &key);

public:
  Blackboard();

  Blackboard(const Blackboard &other);

  template <class T> void set(const 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);

    // Apply remapping if exists
    std::string key = this->remap(name);

    // If the type is changing, remove the old entry first
    if (this->type_registry.find(key) != this->type_registry.end()) {
      this->values.erase(key);
      this->type_registry.erase(key);
    }

    // Insert value and type information if key does not exist
    if (!this->contains(key)) {
      this->values[key] = std::make_shared<T>(value);
      this->type_registry[key] = demangle_type(typeid(T).name());

    } else {
      // Check if the type is the same before updating
      if (this->type_registry.at(key) != demangle_type(typeid(T).name())) {
        this->values[key] = std::make_shared<T>(value);
        this->type_registry[key] = demangle_type(typeid(T).name());
        // Update the existing value
      } else {
        *(std::static_pointer_cast<T>(this->values.at(key))) = value;
      }
    }
  }

  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);

    // Check if the key exists
    if (!this->contains(key)) {
      throw std::runtime_error("Element '" + key +
                               "' does not exist in the blackboard");
    }

    // Return the value casted to the requested type
    return *(std::static_pointer_cast<T>(this->values.at(this->remap(key))));
  }

  void remove(const std::string &key);

  bool contains(const std::string &key);

  int size();

  std::string get_type(const std::string &key);

  std::string to_string();

  void set_remappings(const std::map<std::string, std::string> &remappings);

  const std::map<std::string, std::string> &get_remappings();
};

} // namespace yasmin

#endif // YASMIN__BLACKBOARD_HPP