welder 0.1.0
Bindings for annotated C++ types, from C++26 reflection
Loading...
Searching...
No Matches
array_interface.hpp File Reference

The NumPy array-interface descriptor for an opaque std::vector<T> whose element T is a plain-old-data struct of arithmetic fields. More...

#include <array>
#include <cstddef>
#include <meta>
#include <string>
#include <type_traits>
#include <utility>
Include dependency graph for array_interface.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  welder
namespace  welder::rods
namespace  welder::rods::python

Functions

consteval std::string welder::rods::python::ai_uint_string (std::size_t n)
 Decimal render of n (constexpr std::to_string is unavailable on gcc-16).
consteval bool welder::rods::python::ai_is_unsigned (std::meta::info t)
 Is t one of the unsigned integer fundamental types?
consteval std::string welder::rods::python::numpy_typestr (std::meta::info t)
 The NumPy typestr for arithmetic type t ('<' little-endian, '|' for a single byte), or "" if t has no portable NumPy scalar type (a non-arithmetic type, or long double).
template<class E>
consteval const char * welder::rods::python::ai_typestr ()
 The array-interface typestr for the whole element E — an opaque |V<sizeof> void field; the per-field ai_descr carries the meaning.
consteval bool welder::rods::python::ai_all_fields_numpy (std::meta::info E)
 Whether every one of a POD struct's fields maps to a NumPy scalar (so the descr is complete).
template<class E>
consteval bool welder::rods::python::pod_array_eligible ()
 Is std::vector<E> viewable as a NumPy structured array — i.e.
consteval std::size_t welder::rods::python::ai_entry_count (std::meta::info E)
 The number of array-interface descr entries for E — one per field, plus one per padding gap (interior or trailing).
template<std::meta::info E>
consteval std::array< std::pair< const char *, const char * >, ai_entry_count(E)> welder::rods::python::ai_descr ()
 The NumPy array-interface descr for E: a splice-ready std::array of (name, typestr) pairs — each field in declaration order, with ("", "|V<gap>") void entries for interior and trailing padding so the itemsize and field offsets match E's layout exactly.

Detailed Description

The NumPy array-interface descriptor for an opaque std::vector<T> whose element T is a plain-old-data struct of arithmetic fields.

A contiguous std::vector<T> is a block of a fixed layout, so NumPy can view its data() zero-copy. For a scalar T the Python rods expose the buffer protocol; for a POD struct T (Vec3 { float x, y, z; }, Particle { double pos[3]; float mass; }) they instead expose the [NumPy array-interface protocol] (https://numpy.org/doc/stable/reference/arrays.interface.html) — a plain __array_interface__ dict numpy.asarray reads. That gives a structured array (named fields), is zero-copy and writable, works identically on both Python rods (it is just a Python attribute), and needs no NumPy at build or import time (unlike pybind11's PYBIND11_NUMPY_DTYPE, which pulls in <pybind11/numpy.h> and does not even compile under gcc-16 reflection).

This header supplies the backend-neutral pieces the rods build the dict from: the C++-arithmetic-to-NumPy typestr map, the field descr (with padding), the whole itemsize typestr, and the eligibility predicate. Only std::meta / std here — no welder vocabulary — so it is a light include for both Python rods.

Definition in file array_interface.hpp.