C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
pybind11_utils.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_PYBIND11_UTILS_HPP
17#define YASMIN_PYBIND11_UTILS_HPP
18
19#include <memory>
20#include <pybind11/pybind11.h>
21
24
25namespace py = pybind11;
26
27namespace yasmin {
28namespace pybind11_utils {
29
42inline std::shared_ptr<yasmin::blackboard::Blackboard>
43convert_blackboard_from_python(py::object blackboard_obj) {
44 std::shared_ptr<yasmin::blackboard::Blackboard> blackboard;
45
46 // Case 1: None or not provided - create new Blackboard
47 if (blackboard_obj.is_none()) {
48 blackboard = std::make_shared<yasmin::blackboard::Blackboard>();
49 }
50 // Case 2: Check if it's a BlackboardPyWrapper
51 else if (py::isinstance<yasmin::blackboard::BlackboardPyWrapper>(
52 blackboard_obj)) {
53 auto wrapper =
54 blackboard_obj.cast<yasmin::blackboard::BlackboardPyWrapper>();
55 // Get the shared pointer directly instead of copying
56 blackboard = wrapper.get_cpp_blackboard();
57 }
58 // Case 3: Check if it's a Blackboard
59 else if (py::isinstance<yasmin::blackboard::Blackboard>(blackboard_obj)) {
61 blackboard_obj.cast<std::shared_ptr<yasmin::blackboard::Blackboard>>();
62 }
63 // Case 4: Unknown type - create a new blackboard
64 else {
65 blackboard = std::make_shared<yasmin::blackboard::Blackboard>();
66 }
67
68 return blackboard;
69}
70
83template <typename Func> inline auto wrap_blackboard_callback(py::function cb) {
84 return [cb](std::shared_ptr<yasmin::blackboard::Blackboard> blackboard,
85 auto... args) {
86 py::gil_scoped_acquire acquire;
88 cb(wrapper, args...);
89 };
90}
91
104template <typename ReturnType>
105inline auto wrap_blackboard_callback_with_return(py::function cb) {
106 return [cb](std::shared_ptr<yasmin::blackboard::Blackboard> blackboard)
107 -> ReturnType {
108 py::gil_scoped_acquire acquire;
110 return cb(wrapper).cast<ReturnType>();
111 };
112}
113
128template <typename ClassType, typename StateType>
129inline void add_call_operator(ClassType &cls) {
130 cls.def(
131 "__call__",
132 [](StateType &self, py::object blackboard_obj = py::none()) {
133 auto blackboard = convert_blackboard_from_python(blackboard_obj);
134 // Release GIL to allow C++ threads (important for Concurrence) to run
135 py::gil_scoped_release release;
136 return self(blackboard);
137 },
138 "Execute the state and return the outcome",
139 py::arg("blackboard") = py::none());
140}
141
142} // namespace pybind11_utils
143} // namespace yasmin
144
145#endif // YASMIN_PYBIND11_UTILS_HPP
A wrapper around the C++ Blackboard that stores Python objects and native types.
Definition blackboard_pywrapper.hpp:50
Definition blackboard.hpp:30
Definition pybind11_utils.hpp:28
void add_call_operator(ClassType &cls)
Helper to define the standard call method for State classes.
Definition pybind11_utils.hpp:129
std::shared_ptr< yasmin::blackboard::Blackboard > convert_blackboard_from_python(py::object blackboard_obj)
Convert a Python blackboard object to a C++ Blackboard shared pointer.
Definition pybind11_utils.hpp:43
auto wrap_blackboard_callback_with_return(py::function cb)
Wrap a C++ callback to handle BlackboardPyWrapper conversion with return value.
Definition pybind11_utils.hpp:105
auto wrap_blackboard_callback(py::function cb)
Wrap a C++ callback to handle BlackboardPyWrapper conversion (void return).
Definition pybind11_utils.hpp:83
Definition blackboard.hpp:29