C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
blackboard_key_info.hpp
Go to the documentation of this file.
1// Copyright (C) 2026 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_KEY_INFO_HPP_
17#define YASMIN__BLACKBOARD_KEY_INFO_HPP_
18
19#include <functional>
20#include <memory>
21#include <string>
22#include <type_traits>
23#include <unordered_map>
24#include <vector>
25
26#include "yasmin/blackboard.hpp"
27
28namespace yasmin {
29
37 std::string name;
39 std::string description;
41 bool has_default{false};
44 std::shared_ptr<void> default_value{};
46 std::string default_value_type{};
48 std::function<void(Blackboard &, const std::string &)> inject_default{};
49
53 BlackboardKeyInfo() = default;
54
59 explicit BlackboardKeyInfo(std::string key_name)
60 : name(std::move(key_name)) {}
61
67 BlackboardKeyInfo(std::string key_name, std::string key_description)
68 : name(std::move(key_name)), description(std::move(key_description)) {}
69
77 template <typename T>
78 BlackboardKeyInfo(std::string key_name, std::string key_description, T value)
79 : name(std::move(key_name)), description(std::move(key_description)),
80 has_default(true), default_value(std::make_shared<std::decay_t<T>>(
81 std::forward<T>(value))),
82 default_value_type(demangle_type(typeid(std::decay_t<T>).name())) {
83 using ValueType = std::decay_t<T>;
84 auto stored = std::static_pointer_cast<ValueType>(this->default_value);
85 inject_default = [stored](Blackboard &bb, const std::string &key) {
86 bb.set<ValueType>(key, *stored);
87 };
88 }
89
97 BlackboardKeyInfo(std::string key_name, std::string key_description,
98 const char *value)
99 : name(std::move(key_name)), description(std::move(key_description)),
100 has_default(true), default_value(std::make_shared<std::string>(value)),
101 default_value_type(demangle_type(typeid(std::string).name())) {
102 auto stored = std::static_pointer_cast<std::string>(this->default_value);
103 inject_default = [stored](Blackboard &bb, const std::string &key) {
104 bb.set<std::string>(key, *stored);
105 };
106 }
107
113 template <typename T> T get_default_value() const {
114 return *std::static_pointer_cast<T>(this->default_value);
115 }
116};
117
125 std::string description;
127 std::unordered_map<std::string, std::string> outcome_descriptions;
129 std::vector<BlackboardKeyInfo> input_keys;
131 std::vector<BlackboardKeyInfo> output_keys;
133 std::vector<BlackboardKeyInfo> parameters;
134};
135
136} // namespace yasmin
137
138#endif // YASMIN__BLACKBOARD_KEY_INFO_HPP_
A thread-safe storage for key-value pairs of varying types.
Definition blackboard.hpp:67
void set(const std::string &name, T value)
Set a value in the blackboard.
Definition blackboard.hpp:122
Definition blackboard.hpp:34
std::string demangle_type(const std::string &mangled_name)
Demangle a C++ type name to a human-readable format.
Definition blackboard.hpp:41
bool has_default
Whether this key has a default value.
Definition blackboard_key_info.hpp:41
BlackboardKeyInfo(std::string key_name)
Constructor with key name only.
Definition blackboard_key_info.hpp:59
BlackboardKeyInfo(std::string key_name, std::string key_description)
Constructor with key name and description.
Definition blackboard_key_info.hpp:67
T get_default_value() const
Retrieve the default value as the given type.
Definition blackboard_key_info.hpp:113
BlackboardKeyInfo(std::string key_name, std::string key_description, const char *value)
Constructor with key name, default value and description for const char *.
Definition blackboard_key_info.hpp:97
std::string description
Human-readable description of the key.
Definition blackboard_key_info.hpp:39
std::string name
The name of the key.
Definition blackboard_key_info.hpp:37
std::function< void(Blackboard &, const std::string &)> inject_default
Function to inject the default value into a blackboard at the given key.
Definition blackboard_key_info.hpp:48
std::string default_value_type
The type name of the default value.
Definition blackboard_key_info.hpp:46
BlackboardKeyInfo(std::string key_name, std::string key_description, T value)
Constructor with key name, default value and description.
Definition blackboard_key_info.hpp:78
BlackboardKeyInfo()=default
Default constructor.
std::shared_ptr< void > default_value
Definition blackboard_key_info.hpp:44
Metadata for a state including description and blackboard key information.
Definition blackboard_key_info.hpp:123
std::vector< BlackboardKeyInfo > output_keys
Information about output keys produced by this state.
Definition blackboard_key_info.hpp:131
std::vector< BlackboardKeyInfo > parameters
Information about parameters declared by this state.
Definition blackboard_key_info.hpp:133
std::string description
Description of the state.
Definition blackboard_key_info.hpp:125
std::vector< BlackboardKeyInfo > input_keys
Information about input keys required by this state.
Definition blackboard_key_info.hpp:129
std::unordered_map< std::string, std::string > outcome_descriptions
Human-readable descriptions for outcomes, indexed by outcome name.
Definition blackboard_key_info.hpp:127