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__BLACKBOARD_PYWRAPPER_HPP
17#define YASMIN__BLACKBOARD__BLACKBOARD_PYWRAPPER_HPP
18
19#include <list>
20#include <map>
21#include <pybind11/cast.h>
22#include <pybind11/pybind11.h>
23#include <pybind11/stl.h>
24#include <string>
25#include <tuple>
26#include <type_traits>
27#include <typeinfo>
28#include <vector>
29
32
33namespace py = pybind11;
34
35namespace yasmin {
36namespace blackboard {
37
38// Forward declaration
40
51private:
53 std::shared_ptr<Blackboard> blackboard;
54
55public:
56 BlackboardPyWrapper() : blackboard(std::make_shared<Blackboard>()) {}
57
62 : blackboard(std::make_shared<Blackboard>(std::move(other))) {}
63
67 explicit BlackboardPyWrapper(std::shared_ptr<Blackboard> bb_ptr)
68 : blackboard(bb_ptr) {}
69
75 void set(const std::string &key, py::object value) {
76 if (py::isinstance<py::bool_>(value)) {
77 this->blackboard->set<bool>(key, value.cast<bool>());
78 } else if (py::isinstance<py::int_>(value)) {
79 try {
80 this->blackboard->set<int>(key, value.cast<int>());
81 } catch (...) {
82 this->blackboard->set<long>(key, value.cast<long>());
83 }
84 } else if (py::isinstance<py::float_>(value)) {
85 this->blackboard->set<double>(key, value.cast<double>());
86 } else if (py::isinstance<py::str>(value)) {
87 this->blackboard->set<std::string>(key, value.cast<std::string>());
88 } else if (py::isinstance<py::list>(value)) {
89 this->blackboard->set<py::object>(key, value);
90 } else if (py::isinstance<py::dict>(value)) {
91 this->blackboard->set<py::object>(key, value);
92 } else if (py::isinstance<py::tuple>(value)) {
93 this->blackboard->set<py::object>(key, value);
94 } else if (py::isinstance<py::set>(value)) {
95 this->blackboard->set<py::object>(key, value);
96 } else {
97 this->blackboard->set<py::object>(key, value);
98 }
99 }
100
107 py::object get(const std::string &key) {
108 // Get the type of the stored value
109 std::string type = this->blackboard->get_type(key);
110
111 // Check if it's a pybind11::object (Python object - includes all Python
112 // types)
113 if (type.find("pybind11::object") != std::string::npos ||
114 type.find("pybind11::int_") != std::string::npos ||
115 type.find("pybind11::float_") != std::string::npos ||
116 type.find("pybind11::str") != std::string::npos ||
117 type.find("pybind11::bool_") != std::string::npos ||
118 type.find("pybind11::list") != std::string::npos ||
119 type.find("pybind11::dict") != std::string::npos ||
120 type.find("pybind11::set") != std::string::npos ||
121 type.find("pybind11::tuple") != std::string::npos ||
122 type.find("pybind11::bytes") != std::string::npos ||
123 type.find("pybind11::none") != std::string::npos) {
124 return this->blackboard->get<py::object>(key);
125 }
126 // Check if it's a std::string (C++ string) - convert to Python str
127 else if (type.find("std::string") != std::string::npos ||
128 type.find("std::__cxx11::basic_string") != std::string::npos) {
129 std::string cpp_value = this->blackboard->get<std::string>(key);
130 return py::cast(cpp_value);
131 }
132 // Check if it's a std::vector (C++ vector) - convert to Python list
133 else if (type.find("std::vector") != std::string::npos) {
134 return this->blackboard->get<py::object>(key);
135 }
136 // Check if it's a std::map (C++ map) - convert to Python dict
137 else if (type.find("std::map") != std::string::npos ||
138 type.find("std::unordered_map") != std::string::npos) {
139 return this->blackboard->get<py::object>(key);
140 }
141 // Check if it's a std::set (C++ set) - convert to Python set
142 else if (type.find("std::set") != std::string::npos ||
143 type.find("std::unordered_set") != std::string::npos) {
144 return this->blackboard->get<py::object>(key);
145 }
146 // Check if it's a std::list (C++ list) - convert to Python list
147 else if (type.find("std::list") != std::string::npos) {
148 return this->blackboard->get<py::object>(key);
149 }
150 // Check if it's a std::tuple (C++ tuple) - convert to Python tuple
151 else if (type.find("std::tuple") != std::string::npos) {
152 return this->blackboard->get<py::object>(key);
153 }
154 // Check if it's an int (C++ int) - convert to Python int
155 else if (type.find("int") != std::string::npos) {
156 int cpp_value = this->blackboard->get<int>(key);
157 return py::cast(cpp_value);
158 }
159 // Check if it's a long (C++ long) - convert to Python int
160 else if (type.find("long") != std::string::npos) {
161 long cpp_value = this->blackboard->get<long>(key);
162 return py::cast(cpp_value);
163 }
164 // Check if it's a float or double (C++ float/double) - convert to Python
165 // float
166 else if (type.find("float") != std::string::npos ||
167 type.find("double") != std::string::npos) {
168 double cpp_value = this->blackboard->get<double>(key);
169 return py::cast(cpp_value);
170 }
171 // Check if it's a bool (C++ bool) - convert to Python bool
172 else if (type.find("bool") != std::string::npos) {
173 bool cpp_value = this->blackboard->get<bool>(key);
174 return py::cast(cpp_value);
175 }
176 // Default: try to get as py::object
177 else {
178 return this->blackboard->get<py::object>(key);
179 }
180 }
181
186 void remove(const std::string &key) { this->blackboard->remove(key); }
187
193 bool contains(const std::string &key) {
194 return this->blackboard->contains(key);
195 }
196
201 int size() { return this->blackboard->size(); }
202
207 std::string to_string() { return this->blackboard->to_string(); }
208
213 void set_remappings(const std::map<std::string, std::string> &remappings) {
214 this->blackboard->set_remappings(remappings);
215 }
216
221 std::map<std::string, std::string> get_remappings() {
222 return this->blackboard->get_remappings();
223 }
224
229 std::shared_ptr<Blackboard> get_cpp_blackboard() { return this->blackboard; }
230};
231
232} // namespace blackboard
233} // namespace yasmin
234
235#endif // YASMIN__BLACKBOARD__BLACKBOARD_PYWRAPPER_HPP
A wrapper around the C++ Blackboard that stores Python objects and native types.
Definition blackboard_pywrapper.hpp:50
BlackboardPyWrapper(Blackboard &&other)
Construct from an existing C++ Blackboard (move constructor)
Definition blackboard_pywrapper.hpp:61
std::string to_string()
Convert the contents of the blackboard to a string.
Definition blackboard_pywrapper.hpp:207
std::map< std::string, std::string > get_remappings()
Get the remappings of the blackboard.
Definition blackboard_pywrapper.hpp:221
void remove(const std::string &key)
Remove a value from the blackboard.
Definition blackboard_pywrapper.hpp:186
BlackboardPyWrapper(std::shared_ptr< Blackboard > bb_ptr)
Construct by wrapping a shared_ptr to a C++ Blackboard.
Definition blackboard_pywrapper.hpp:67
std::shared_ptr< Blackboard > blackboard
Underlying C++ Blackboard instance.
Definition blackboard_pywrapper.hpp:53
std::shared_ptr< Blackboard > get_cpp_blackboard()
Get a shared pointer to the underlying C++ Blackboard.
Definition blackboard_pywrapper.hpp:229
bool contains(const std::string &key)
Check if a key exists in the blackboard.
Definition blackboard_pywrapper.hpp:193
py::object get(const std::string &key)
Get a Python object from the blackboard.
Definition blackboard_pywrapper.hpp:107
int size()
Get the number of key-value pairs in the blackboard.
Definition blackboard_pywrapper.hpp:201
BlackboardPyWrapper()
Definition blackboard_pywrapper.hpp:56
void set(const std::string &key, py::object value)
Set a Python object in the blackboard.
Definition blackboard_pywrapper.hpp:75
void set_remappings(const std::map< std::string, std::string > &remappings)
Set the remappings of the blackboard.
Definition blackboard_pywrapper.hpp:213
A thread-safe storage for key-value pairs of varying types.
Definition blackboard.hpp:41
Definition blackboard.hpp:30
Definition blackboard.hpp:29