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 <unordered_map>
#include <utility>
#include <vector>

#include "yasmin/logs.hpp"
#include "yasmin/types.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:
  struct SharedStorage {
    mutable std::recursive_mutex mutex;
    std::unordered_map<std::string, std::shared_ptr<void>> values;
    TypeRegistry type_registry;
  };

  std::shared_ptr<SharedStorage> storage;
  Remappings remappings;

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

public:
  YASMIN_PTR_ALIASES(Blackboard)


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

    // Apply remapping if exists
    const std::string &key = this->remap(name);
    const std::string type_name = demangle_type(typeid(T).name());

    auto type_it = this->storage->type_registry.find(key);
    if (type_it != this->storage->type_registry.end() &&
        type_it->second == type_name) {
      // Same type: update existing value in-place (avoids allocation)
      *(std::static_pointer_cast<T>(this->storage->values.at(key))) = value;
    } else {
      // New key or different type: (re)create entry
      this->storage->values[key] = std::make_shared<T>(value);
      this->storage->type_registry[key] = type_name;
    }
  }

  template <class T> T get(const std::string &key) const {

    YASMIN_LOG_DEBUG("Getting '%s' from the blackboard", key.c_str());

    std::lock_guard<std::recursive_mutex> lk(this->storage->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->storage->values.at(this->remap(key))));
  }

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

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

  void copy_value_from(const Blackboard &other, const std::string &source_key,
                       const std::string &target_key);

  int size() const;

  std::vector<std::string> keys() const;

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

  std::string to_string() const;

  void set_remappings(const Remappings &remappings);

  const Remappings &get_remappings() const noexcept;
};

} // namespace yasmin

#endif // YASMIN__BLACKBOARD_HPP_