16#ifndef YASMIN__BLACKBOARD_PYWRAPPER_HPP
17#define YASMIN__BLACKBOARD_PYWRAPPER_HPP
21#include <pybind11/cast.h>
22#include <pybind11/pybind11.h>
23#include <pybind11/stl.h>
32namespace py = pybind11;
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)) {
78 this->blackboard->set<
int>(key, value.cast<
int>());
80 this->blackboard->set<
long>(key, value.cast<
long>());
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);
95 this->blackboard->set<py::object>(key, value);
105 py::object
get(
const std::string &key) {
107 std::string type = this->blackboard->get_type(key);
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);
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);
131 else if (type.find(
"std::vector") != std::string::npos) {
132 return this->blackboard->get<py::object>(key);
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);
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);
145 else if (type.find(
"std::list") != std::string::npos) {
146 return this->blackboard->get<py::object>(key);
149 else if (type.find(
"std::tuple") != std::string::npos) {
150 return this->blackboard->get<py::object>(key);
153 else if (type.find(
"int") != std::string::npos) {
154 int cpp_value = this->blackboard->get<
int>(key);
155 return py::cast(cpp_value);
158 else if (type.find(
"long") != std::string::npos) {
159 long cpp_value = this->blackboard->get<
long>(key);
160 return py::cast(cpp_value);
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);
170 else if (type.find(
"bool") != std::string::npos) {
171 bool cpp_value = this->blackboard->get<
bool>(key);
172 return py::cast(cpp_value);
176 return this->blackboard->get<py::object>(key);
184 void remove(
const std::string &key) { this->blackboard->remove(key); }
192 return this->blackboard->contains(key);
199 int size() {
return this->blackboard->size(); }
205 std::string
to_string() {
return this->blackboard->to_string(); }
212 this->blackboard->set_remappings(remappings);
220 return this->blackboard->get_remappings();
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