C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
blackboard_pywrapper.hpp
Go to the documentation of this file.
1// Copyright (C) 2025 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_PYWRAPPER_HPP_
17#define YASMIN__BLACKBOARD_PYWRAPPER_HPP_
18
19#include <pybind11/cast.h>
20#include <pybind11/pybind11.h>
21#include <pybind11/stl.h>
22#include <string>
23#include <type_traits>
24#include <typeinfo>
25
26#include "yasmin/blackboard.hpp"
27#include "yasmin/types.hpp"
28
29namespace py = pybind11;
30
31namespace yasmin {
32
33// Forward declaration
35
46private:
48 Blackboard ::SharedPtr blackboard;
49
50public:
52
57 : blackboard(Blackboard::make_shared(std::move(other))) {}
58
63 : blackboard(bb_ptr) {}
64
70 void set(const std::string &key, py::object value) {
71 if (py::isinstance<py::bool_>(value)) {
72 this->blackboard->set<bool>(key, value.cast<bool>());
73 } else if (py::isinstance<py::int_>(value)) {
74 try {
75 this->blackboard->set<int>(key, value.cast<int>());
76 } catch (...) {
77 this->blackboard->set<long>(key, value.cast<long>());
78 }
79 } else if (py::isinstance<py::float_>(value)) {
80 this->blackboard->set<double>(key, value.cast<double>());
81 } else if (py::isinstance<py::str>(value)) {
82 this->blackboard->set<std::string>(key, value.cast<std::string>());
83 } else {
84 this->blackboard->set<py::object>(key, value);
85 }
86 }
87
94 py::object get(const std::string &key) const {
95 // Get the type of the stored value
96 std::string type = this->blackboard->get_type(key);
97
98 // Check if it's a std::string (C++ string) - convert to Python str
99 if (type.find("std::string") != std::string::npos ||
100 type.find("std::__cxx11::basic_string") != std::string::npos) {
101 std::string cpp_value = this->blackboard->get<std::string>(key);
102 return py::cast(cpp_value);
103 }
104 // Check if it's an int (C++ int) - convert to Python int
105 else if (type.find("int") != std::string::npos) {
106 int cpp_value = this->blackboard->get<int>(key);
107 return py::cast(cpp_value);
108 }
109 // Check if it's a long (C++ long) - convert to Python int
110 else if (type.find("long") != std::string::npos) {
111 long cpp_value = this->blackboard->get<long>(key);
112 return py::cast(cpp_value);
113 }
114 // Check if it's a float or double (C++ float/double) - convert to Python
115 // float
116 else if (type.find("float") != std::string::npos ||
117 type.find("double") != std::string::npos) {
118 double cpp_value = this->blackboard->get<double>(key);
119 return py::cast(cpp_value);
120 }
121 // Check if it's a bool (C++ bool) - convert to Python bool
122 else if (type.find("bool") != std::string::npos) {
123 bool cpp_value = this->blackboard->get<bool>(key);
124 return py::cast(cpp_value);
125 }
126 // Default: try to get as py::object
127 else {
128 return this->blackboard->get<py::object>(key);
129 }
130 }
131
136 void remove(const std::string &key) { this->blackboard->remove(key); }
137
143 bool contains(const std::string &key) const {
144 return this->blackboard->contains(key);
145 }
146
151 int size() const { return this->blackboard->size(); }
152
157 std::string to_string() const { return this->blackboard->to_string(); }
158
163 void set_remappings(const Remappings &remappings) {
164 this->blackboard->set_remappings(remappings);
165 }
166
171 const Remappings &get_remappings() const {
172 return this->blackboard->get_remappings();
173 }
174
179 Blackboard::SharedPtr get_cpp_blackboard() const { return this->blackboard; }
180};
181
182} // namespace yasmin
183
184#endif // YASMIN__BLACKBOARD_PYWRAPPER_HPP_
A wrapper around the C++ Blackboard that stores Python objects and native types.
Definition blackboard_pywrapper.hpp:45
void set_remappings(const Remappings &remappings)
Set the remappings of the blackboard.
Definition blackboard_pywrapper.hpp:163
Blackboard::SharedPtr blackboard
Underlying C++ Blackboard instance.
Definition blackboard_pywrapper.hpp:48
BlackboardPyWrapper(Blackboard &&other)
Construct from an existing C++ Blackboard (move constructor)
Definition blackboard_pywrapper.hpp:56
int size() const
Get the number of key-value pairs in the blackboard.
Definition blackboard_pywrapper.hpp:151
BlackboardPyWrapper()
Definition blackboard_pywrapper.hpp:51
py::object get(const std::string &key) const
Get a Python object from the blackboard.
Definition blackboard_pywrapper.hpp:94
const Remappings & get_remappings() const
Get the remappings of the blackboard.
Definition blackboard_pywrapper.hpp:171
void set(const std::string &key, py::object value)
Set a Python object in the blackboard.
Definition blackboard_pywrapper.hpp:70
std::string to_string() const
Convert the contents of the blackboard to a string.
Definition blackboard_pywrapper.hpp:157
Blackboard::SharedPtr get_cpp_blackboard() const
Get a shared pointer to the underlying C++ Blackboard.
Definition blackboard_pywrapper.hpp:179
BlackboardPyWrapper(Blackboard::SharedPtr bb_ptr)
Construct by wrapping a shared_ptr to a C++ Blackboard.
Definition blackboard_pywrapper.hpp:62
void remove(const std::string &key)
Remove a value from the blackboard.
Definition blackboard_pywrapper.hpp:136
bool contains(const std::string &key) const
Check if a key exists in the blackboard.
Definition blackboard_pywrapper.hpp:143
A thread-safe storage for key-value pairs of varying types.
Definition blackboard.hpp:64
std::shared_ptr< Blackboard > SharedPtr
Shared pointer type for Blackboard.
Definition blackboard.hpp:85
Definition blackboard.hpp:31
StringMap Remappings
Map of remappings (string to string)
Definition types.hpp:88