60#ifndef snicholls_core_adapters_nanobind_bytes_hpp
61#define snicholls_core_adapters_nanobind_bytes_hpp
63#include <nanobind/nanobind.h>
70namespace snicholls::python {
78 held_buffer() noexcept = default;
79 held_buffer(const held_buffer&) = delete;
80 held_buffer& operator=(const held_buffer&) = delete;
81 held_buffer(held_buffer&& o) noexcept : buffer_(o.buffer_), held_(std::exchange(o.held_, false)),
82 text_(std::exchange(o.text_,
nullptr)), data_(o.data_), size_(o.size_)
85 held_buffer& operator=(held_buffer&& o)
noexcept
90 held_ = std::exchange(o.held_,
false);
91 text_ = std::exchange(o.text_,
nullptr);
97 ~held_buffer() { release(); }
100 bool acquire_buffer(PyObject* src,
bool writable)
noexcept
103 if (PyUnicode_Check(src))
105 if (PyObject_GetBuffer(src, &buffer_, writable ? (PyBUF_SIMPLE | PyBUF_WRITABLE) : PyBUF_SIMPLE) != 0) {
111 size_ =
static_cast<std::size_t
>(buffer_.len);
116 bool acquire_text(PyObject* src)
noexcept
119 if (!PyUnicode_Check(src))
120 return acquire_buffer(src,
false);
122 const char* s = PyUnicode_AsUTF8AndSize(src, &n);
129 data_ =
const_cast<char*
>(s);
130 size_ =
static_cast<std::size_t
>(n);
134 [[nodiscard]]
void* data() const noexcept {
return data_; }
135 [[nodiscard]] std::size_t size() const noexcept {
return size_; }
138 void release() noexcept
140 if (!held_ && text_ ==
nullptr)
142 const PyGILState_STATE state = PyGILState_Ensure();
144 PyBuffer_Release(&buffer_);
146 PyGILState_Release(state);
155 PyObject* text_ =
nullptr;
156 void* data_ =
nullptr;
157 std::size_t size_ = 0;
168 [[nodiscard]] std::span<const std::byte>
bytes() const noexcept
170 return {
static_cast<const std::byte*
>(held_.data()), held_.size()};
173 [[nodiscard]] std::string_view
chars() const noexcept {
return {
static_cast<const char*
>(held_.data()), held_.size()}; }
175 [[nodiscard]] std::size_t
size() const noexcept {
return held_.size(); }
178 bool acquire(PyObject* src)
noexcept {
return held_.acquire_buffer(src,
false); }
181 detail::held_buffer held_;
190 [[nodiscard]] std::span<std::byte>
bytes() const noexcept {
return {
static_cast<std::byte*
>(held_.data()), held_.size()}; }
192 [[nodiscard]] std::size_t
size() const noexcept {
return held_.size(); }
195 bool acquire(PyObject* src)
noexcept {
return held_.acquire_buffer(src,
true); }
198 detail::held_buffer held_;
207 [[nodiscard]] std::string_view
view() const noexcept {
return {
static_cast<const char*
>(held_.data()), held_.size()}; }
209 [[nodiscard]] std::size_t
size() const noexcept {
return held_.size(); }
212 bool acquire(PyObject* src)
noexcept {
return held_.acquire_text(src); }
215 detail::held_buffer held_;
224[[nodiscard]] inline ::nanobind::bytes
to_bytes(std::span<const std::byte> b)
226 return ::nanobind::bytes(b.data(), b.size());
234[[nodiscard]] inline ::nanobind::bytes
to_bytes(std::string_view s) { return ::nanobind::bytes(s.data(), s.size()); }
238namespace nanobind::detail {
241template <
class View, auto Descr>
242struct snicholls_view_caster
244 NB_TYPE_CASTER(View, Descr)
246 bool from_python(handle src, uint32_t, cleanup_list*)
noexcept {
return value.acquire(src.ptr()); }
248 static handle from_cpp(
const View&, rv_policy, cleanup_list*)
noexcept
250 PyErr_SetString(PyExc_TypeError,
"snicholls::python: a byte or text view is an argument type only");
256struct type_caster<snicholls::python::byte_view>
257 : snicholls_view_caster<snicholls::python::byte_view, const_name("collections.abc.Buffer")>
262struct type_caster<snicholls::python::mutable_byte_view>
263 : snicholls_view_caster<snicholls::python::mutable_byte_view, const_name("collections.abc.Buffer")>
268struct type_caster<snicholls::python::text_view>
269 : snicholls_view_caster<snicholls::python::text_view, const_name("str | collections.abc.Buffer")>
A read-only view of a Python bytes-like object's contiguous buffer.
bool acquire(PyObject *src) noexcept
Takes src's buffer (the type caster's step).
std::string_view chars() const noexcept
The bytes as characters.
std::span< const std::byte > bytes() const noexcept
The bytes.
std::size_t size() const noexcept
The length in bytes.
A writable view of a Python object's contiguous buffer.
bool acquire(PyObject *src) noexcept
Takes src's writable buffer (the type caster's step).
std::size_t size() const noexcept
The length in bytes.
std::span< std::byte > bytes() const noexcept
The bytes.
A view of a str's UTF-8, or of a bytes-like buffer taken as UTF-8.
bool acquire(PyObject *src) noexcept
Takes src's UTF-8 or buffer (the type caster's step).
std::string_view view() const noexcept
The text.
std::size_t size() const noexcept
The length in bytes of UTF-8.
::nanobind::bytes to_bytes(std::span< const std::byte > b)
A new Python bytes holding a copy of b.