core-adapters 0.1.1
Third-party adapters for snicholls-core: nanobind helpers, nlohmann/json hooks, the snicholls_core Python package
Loading...
Searching...
No Matches
gil.hpp
Go to the documentation of this file.
1//
2// gil.hpp
3// core-adapters
4//
5// Copyright 2010-2026 Saxon Herschel Nicholls
6//
7
70
73
74#ifndef snicholls_core_adapters_nanobind_gil_hpp
75#define snicholls_core_adapters_nanobind_gil_hpp
76
77#include <nanobind/nanobind.h>
78
79#include <functional>
80#include <type_traits>
81#include <utility>
82
83static_assert(NB_VERSION_MAJOR >= 3, "snicholls::python: the nanobind helpers need nanobind 3 or newer");
84
85namespace snicholls::python {
86
92using release_gil = ::nanobind::call_guard<::nanobind::gil_scoped_release>;
93
101template <class F>
102decltype(auto) without_gil(F&& f)
103{
104 ::nanobind::gil_scoped_release released;
105 return std::invoke(std::forward<F>(f));
106}
107
114template <class F>
115decltype(auto) with_gil(F&& f)
116{
117 ::nanobind::gil_scoped_acquire acquired;
118 return std::invoke(std::forward<F>(f));
119}
120
124template <class Signature>
126
131template <class R, class... A>
132class python_function<R(A...)>
133{
134public:
136 explicit python_function(::nanobind::object f) noexcept : f_(std::move(f)) {}
137
139 python_function(const python_function& o) : f_(with_gil([&] { return o.f_; })) {}
140
142 python_function(python_function&& o) noexcept = default;
143
146 {
147 with_gil([&] { f_ = o.f_; });
148 return *this;
149 }
150
153 {
154 with_gil([&] { f_ = std::move(o.f_); });
155 return *this;
156 }
157
160 {
161 if (f_.is_valid())
162 with_gil([&] { f_.reset(); });
163 }
164
169 R operator()(A... a) const
170 {
171 return with_gil([&]() -> R {
172 ::nanobind::object out = f_(std::forward<A>(a)...);
173 if constexpr (!std::is_void_v<R>) {
174 std::remove_cvref_t<R> value{};
175 if (!::nanobind::try_cast(out, value))
176 throw ::nanobind::type_error("snicholls::python::python_function: the callable's result does not "
177 "convert to the declared result type");
178 return value;
179 }
180 });
181 }
182
184 [[nodiscard]] const ::nanobind::object& object() const noexcept { return f_; }
185
186private:
187 ::nanobind::object f_;
188};
189
190} // namespace snicholls::python
191
192#endif /* snicholls_core_adapters_nanobind_gil_hpp */
python_function(const python_function &o)
Copies, taking the GIL.
Definition gil.hpp:139
python_function & operator=(const python_function &o)
Copy-assigns, taking the GIL.
Definition gil.hpp:145
R operator()(A... a) const
Calls the Python callable with the GIL held.
Definition gil.hpp:169
const ::nanobind::object & object() const noexcept
The callable.
Definition gil.hpp:184
python_function & operator=(python_function &&o) noexcept
Move-assigns, taking the GIL to drop the old reference.
Definition gil.hpp:152
~python_function()
Drops the reference, taking the GIL.
Definition gil.hpp:159
python_function(::nanobind::object f) noexcept
Wraps f.
Definition gil.hpp:136
python_function(python_function &&o) noexcept=default
Moves (no reference count changes, no GIL needed).
A Python callable as a C++ callable of a fixed signature, safe to copy, call and destroy on any threa...
Definition gil.hpp:125
decltype(auto) with_gil(F &&f)
Runs f() holding the GIL (taking it if this thread does not hold it) and returns its result.
Definition gil.hpp:115
::nanobind::call_guard<::nanobind::gil_scoped_release > release_gil
nanobind's call guard that releases the GIL for the duration of a bound call.
Definition gil.hpp:92
decltype(auto) without_gil(F &&f)
Runs f() with the GIL released and returns its result.
Definition gil.hpp:102