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 <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
30#include "yasmin/blackboard.hpp"
31
32namespace py = pybind11;
33
34namespace yasmin {
35
36// Forward declaration
38
49private:
51 std::shared_ptr<Blackboard> blackboard;
52
53public:
54 BlackboardPyWrapper() : blackboard(std::make_shared<Blackboard>()) {}
55
60 : blackboard(std::make_shared<Blackboard>(std::move(other))) {}
61
65 explicit BlackboardPyWrapper(std::shared_ptr<Blackboard> bb_ptr)
66 : blackboard(bb_ptr) {}
67
73 void set(const std::string &key, py::object value) {
74 if (py::isinstance<py::bool_>(value)) {
75 this->blackboard->set<bool>(key, value.cast<bool>());
76 } else if (py::isinstance<py::int_>(value)) {
77 try {
78 this->blackboard->set<int>(key, value.cast<int>());
79 } catch (...) {
80 this->blackboard->set<long>(key, value.cast<long>());
81 }
82 } else if (py::isinstance<py::float_>(value)) {
83 this->blackboard->set<double>(key, value.cast<double>());
84 } else if (py::isinstance<py::str>(value)) {
85 this->blackboard->set<std::string>(key, value.cast<std::string>());
86 } else if (py::isinstance<py::list>(value)) {
87 this->blackboard->set<py::object>(key, value);
88 } else if (py::isinstance<py::dict>(value)) {
89 this->blackboard->set<py::object>(key, value);
90 } else if (py::isinstance<py::tuple>(value)) {
91 this->blackboard->set<py::object>(key, value);
92 } else if (py::isinstance<py::set>(value)) {
93 this->blackboard->set<py::object>(key, value);
94 } else {
95 this->blackboard->set<py::object>(key, value);
96 }
97 }
98
105 py::object get(const std::string &key) {
106 // Get the type of the stored value
107 std::string type = this->blackboard->get_type(key);
108
109 // Check if it's a pybind11::object (Python object - includes all Python
110 // types)
111 if (type.find("pybind11::object") != std::string::npos ||
112 type.find("pybind11::int_") != std::string::npos ||
113 type.find("pybind11::float_") != std::string::npos ||
114 type.find("pybind11::str") != std::string::npos ||
115 type.find("pybind11::bool_") != std::string::npos ||
116 type.find("pybind11::list") != std::string::npos ||
117 type.find("pybind11::dict") != std::string::npos ||
118 type.find("pybind11::set") != std::string::npos ||
119 type.find("pybind11::tuple") != std::string::npos ||
120 type.find("pybind11::bytes") != std::string::npos ||
121 type.find("pybind11::none") != std::string::npos) {
122 return this->blackboard->get<py::object>(key);
123 }
124 // Check if it's a std::string (C++ string) - convert to Python str
125 else if (type.find("std::string") != std::string::npos ||
126 type.find("std::__cxx11::basic_string") != std::string::npos) {
127 std::string cpp_value = this->blackboard->get<std::string>(key);
128 return py::cast(cpp_value);
129 }
130 // Check if it's a std::vector (C++ vector) - convert to Python list
131 else if (type.find("std::vector") != std::string::npos) {
132 return this->blackboard->get<py::object>(key);
133 }
134 // Check if it's a std::map (C++ map) - convert to Python dict
135 else if (type.find("std::map") != std::string::npos ||
136 type.find("std::unordered_map") != std::string::npos) {
137 return this->blackboard->get<py::object>(key);
138 }
139 // Check if it's a std::set (C++ set) - convert to Python set
140 else if (type.find("std::set") != std::string::npos ||
141 type.find("std::unordered_set") != std::string::npos) {
142 return this->blackboard->get<py::object>(key);
143 }
144 // Check if it's a std::list (C++ list) - convert to Python list
145 else if (type.find("std::list") != std::string::npos) {
146 return this->blackboard->get<py::object>(key);
147 }
148 // Check if it's a std::tuple (C++ tuple) - convert to Python tuple
149 else if (type.find("std::tuple") != std::string::npos) {
150 return this->blackboard->get<py::object>(key);
151 }
152 // Check if it's an int (C++ int) - convert to Python int
153 else if (type.find("int") != std::string::npos) {
154 int cpp_value = this->blackboard->get<int>(key);
155 return py::cast(cpp_value);
156 }
157 // Check if it's a long (C++ long) - convert to Python int
158 else if (type.find("long") != std::string::npos) {
159 long cpp_value = this->blackboard->get<long>(key);
160 return py::cast(cpp_value);
161 }
162 // Check if it's a float or double (C++ float/double) - convert to Python
163 // float
164 else if (type.find("float") != std::string::npos ||
165 type.find("double") != std::string::npos) {
166 double cpp_value = this->blackboard->get<double>(key);
167 return py::cast(cpp_value);
168 }
169 // Check if it's a bool (C++ bool) - convert to Python bool
170 else if (type.find("bool") != std::string::npos) {
171 bool cpp_value = this->blackboard->get<bool>(key);
172 return py::cast(cpp_value);
173 }
174 // Default: try to get as py::object
175 else {
176 return this->blackboard->get<py::object>(key);
177 }
178 }
179
184 void remove(const std::string &key) { this->blackboard->remove(key); }
185
191 bool contains(const std::string &key) {
192 return this->blackboard->contains(key);
193 }
194
199 int size() { return this->blackboard->size(); }
200
205 std::string to_string() { return this->blackboard->to_string(); }
206
211 void set_remappings(const std::map<std::string, std::string> &remappings) {
212 this->blackboard->set_remappings(remappings);
213 }
214
219 std::map<std::string, std::string> get_remappings() {
220 return this->blackboard->get_remappings();
221 }
222
227 std::shared_ptr<Blackboard> get_cpp_blackboard() { return this->blackboard; }
228};
229
230} // namespace yasmin
231
232#endif // YASMIN__BLACKBOARD_PYWRAPPER_HPP
A wrapper around the C++ Blackboard that stores Python objects and native types.
Definition blackboard_pywrapper.hpp:48
py::object get(const std::string &key)
Get a Python object from the blackboard.
Definition blackboard_pywrapper.hpp:105
std::shared_ptr< Blackboard > blackboard
Underlying C++ Blackboard instance.
Definition blackboard_pywrapper.hpp:51
void set_remappings(const std::map< std::string, std::string > &remappings)
Set the remappings of the blackboard.
Definition blackboard_pywrapper.hpp:211
std::map< std::string, std::string > get_remappings()
Get the remappings of the blackboard.
Definition blackboard_pywrapper.hpp:219
int size()
Get the number of key-value pairs in the blackboard.
Definition blackboard_pywrapper.hpp:199
BlackboardPyWrapper(Blackboard &&other)
Construct from an existing C++ Blackboard (move constructor)
Definition blackboard_pywrapper.hpp:59
BlackboardPyWrapper()
Definition blackboard_pywrapper.hpp:54
std::shared_ptr< Blackboard > get_cpp_blackboard()
Get a shared pointer to the underlying C++ Blackboard.
Definition blackboard_pywrapper.hpp:227
BlackboardPyWrapper(std::shared_ptr< Blackboard > bb_ptr)
Construct by wrapping a shared_ptr to a C++ Blackboard.
Definition blackboard_pywrapper.hpp:65
std::string to_string()
Convert the contents of the blackboard to a string.
Definition blackboard_pywrapper.hpp:205
bool contains(const std::string &key)
Check if a key exists in the blackboard.
Definition blackboard_pywrapper.hpp:191
void set(const std::string &key, py::object value)
Set a Python object in the blackboard.
Definition blackboard_pywrapper.hpp:73
void remove(const std::string &key)
Remove a value from the blackboard.
Definition blackboard_pywrapper.hpp:184
A thread-safe storage for key-value pairs of varying types.
Definition blackboard.hpp:62
Definition blackboard.hpp:29