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