16#ifndef YASMIN__BLACKBOARD_PYWRAPPER_HPP_
17#define YASMIN__BLACKBOARD_PYWRAPPER_HPP_
19#include <pybind11/cast.h>
20#include <pybind11/pybind11.h>
21#include <pybind11/stl.h>
29namespace py = pybind11;
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)) {
75 this->blackboard->set<
int>(key, value.cast<
int>());
77 this->blackboard->set<
long>(key, value.cast<
long>());
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>());
84 this->blackboard->set<py::object>(key, value);
94 py::object
get(
const std::string &key)
const {
96 std::string type = this->blackboard->get_type(key);
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);
105 else if (type.find(
"int") != std::string::npos) {
106 int cpp_value = this->blackboard->get<
int>(key);
107 return py::cast(cpp_value);
110 else if (type.find(
"long") != std::string::npos) {
111 long cpp_value = this->blackboard->get<
long>(key);
112 return py::cast(cpp_value);
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);
122 else if (type.find(
"bool") != std::string::npos) {
123 bool cpp_value = this->blackboard->get<
bool>(key);
124 return py::cast(cpp_value);
128 return this->blackboard->get<py::object>(key);
136 void remove(
const std::string &key) { this->blackboard->remove(key); }
144 return this->blackboard->contains(key);
151 int size()
const {
return this->blackboard->size(); }
157 std::string
to_string()
const {
return this->blackboard->to_string(); }
164 this->blackboard->set_remappings(remappings);
172 return this->blackboard->get_remappings();
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