C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
blackboard_value.hpp
Go to the documentation of this file.
1// Copyright (C) 2023 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__BLACKBOARD_VALUE_HPP
17#define YASMIN__BLACKBOARD__BLACKBOARD_VALUE_HPP
18
19#include <string>
20#include <typeinfo>
21
22#ifdef __GNUG__ // If using GCC/G++
23#include <cxxabi.h> // For abi::__cxa_demangle
24#endif
25
27
28namespace yasmin {
29namespace blackboard {
30
41template <class T> class BlackboardValue : public BlackboardValueInterface {
42private:
44
45public:
51
56 T get() { return this->value; }
57
62 void set(T value) { this->value = value; }
63
71 std::string get_type() {
72 std::string name = typeid(T).name(); // Get the mangled name of the type
73
74#ifdef __GNUG__ // If using GCC/G++
75 int status;
76 // Demangle the name using GCC's demangling function
77 char *demangled =
78 abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status);
79 if (status == 0) {
80 name = demangled;
81 }
82 free(demangled);
83#endif
84
85 return name; // Return the demangled type name
86 }
87
95 std::string to_string() { return this->get_type(); }
96};
97
98} // namespace blackboard
99} // namespace yasmin
100
101#endif // YASMIN__BLACKBOARD__BLACKBOARD_VALUE_HPP
Interface for blackboard value types.
Definition blackboard_value_interface.hpp:32
A template class that wraps a value of type T.
Definition blackboard_value.hpp:41
void set(T value)
Set a new value.
Definition blackboard_value.hpp:62
std::string to_string()
Convert the stored value's type information to a string.
Definition blackboard_value.hpp:95
std::string get_type()
Get the type of the stored value as a string.
Definition blackboard_value.hpp:71
T value
The stored value of type T.
Definition blackboard_value.hpp:43
BlackboardValue(T value)
Constructs a BlackboardValue with the specified value.
Definition blackboard_value.hpp:50
T get()
Retrieve the stored value.
Definition blackboard_value.hpp:56
Definition blackboard.hpp:29