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
22#include "yasmin/blackboard.hpp"
24#include "yasmin/types.hpp"
25
26namespace py = pybind11;
27
28namespace yasmin {
29namespace pybind11_utils {
30
44convert_blackboard_from_python(py::object blackboard_obj) {
45 Blackboard::SharedPtr blackboard;
46
47 // Case 1: None or not provided - create new Blackboard
48 if (blackboard_obj.is_none()) {
50 }
51 // Case 2: Check if it's a BlackboardPyWrapper
52 else if (py::isinstance<yasmin::BlackboardPyWrapper>(blackboard_obj)) {
53 auto wrapper = blackboard_obj.cast<yasmin::BlackboardPyWrapper>();
54 // Get the shared pointer directly instead of copying
55 blackboard = wrapper.get_cpp_blackboard();
56 }
57 // Case 3: Check if it's a Blackboard
58 else if (py::isinstance<yasmin::Blackboard>(blackboard_obj)) {
59 blackboard = blackboard_obj.cast<Blackboard::SharedPtr>();
60 }
61 // Case 4: Unknown type - create a new blackboard
62 else {
64 }
65
66 return blackboard;
67}
68
81template <typename Func> inline auto wrap_blackboard_callback(py::function cb) {
82 return [cb](Blackboard::SharedPtr blackboard, auto... args) {
83 py::gil_scoped_acquire acquire;
84 yasmin::BlackboardPyWrapper wrapper(blackboard);
85 cb(wrapper, args...);
86 };
87}
88
101template <typename ReturnType>
102inline auto wrap_blackboard_callback_with_return(py::function cb) {
103 return [cb](Blackboard::SharedPtr blackboard) -> ReturnType {
104 py::gil_scoped_acquire acquire;
105 yasmin::BlackboardPyWrapper wrapper(blackboard);
106 return cb(wrapper).cast<ReturnType>();
107 };
108}
109
124template <typename ClassType, typename StateType>
125inline void add_call_operator(ClassType &cls) {
126 cls.def(
127 "__call__",
128 [](StateType &self, py::object blackboard_obj = py::none()) {
129 auto blackboard = convert_blackboard_from_python(blackboard_obj);
130 // Release GIL to allow C++ threads (important for Concurrence) to run
131 py::gil_scoped_release release;
132 return self(blackboard);
133 },
134 "Execute the state and return the outcome",
135 py::arg("blackboard") = py::none());
136}
137
138} // namespace pybind11_utils
139} // namespace yasmin
140
141#endif // YASMIN_PYBIND11_UTILS_HPP_
A wrapper around the C++ Blackboard that stores Python objects and native types.
Definition blackboard_pywrapper.hpp:45
std::shared_ptr< Blackboard > SharedPtr
Shared pointer type for Blackboard.
Definition blackboard.hpp:85
static std::shared_ptr< Blackboard > make_shared(Args &&...args)
Definition blackboard.hpp:85
Definition pybind11_utils.hpp:29
Blackboard::SharedPtr convert_blackboard_from_python(py::object blackboard_obj)
Convert a Python blackboard object to a C++ Blackboard shared pointer.
Definition pybind11_utils.hpp:44
void add_call_operator(ClassType &cls)
Helper to define the standard call method for State classes.
Definition pybind11_utils.hpp:125
auto wrap_blackboard_callback_with_return(py::function cb)
Wrap a C++ callback to handle BlackboardPyWrapper conversion with return value.
Definition pybind11_utils.hpp:102
auto wrap_blackboard_callback(py::function cb)
Wrap a C++ callback to handle BlackboardPyWrapper conversion (void return).
Definition pybind11_utils.hpp:81
Definition blackboard.hpp:31