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