welder 0.1.0
Bindings for annotated C++ types, from C++26 reflection
Loading...
Searching...
No Matches
rod.hpp
Go to the documentation of this file.
1#pragma once
36
37#include <cstddef>
38#include <meta>
39#include <ostream>
40#include <string>
41#include <utility>
42
43#include <welder/welder.hpp> // driver + rod contract
44#include <welder/rods/python/trampolines/document.hpp> // the emission core
45
46namespace welder::inline v0::rods::trampolines {
47
56struct rod {
57 static constexpr lang language{lang::py};
58
65 std::string prefix{};
66 };
68
72 struct class_handle {};
73
75 struct enum_handle {};
76
81 template <class> using class_handle_type = class_handle;
82 template <class> using enum_handle_type = enum_handle;
83
84 struct session {};
85
90 template <class T>
91 static constexpr bool has_native_caster = true;
92
95 static consteval const char* special_method_name(std::meta::info) {
96 return nullptr;
97 }
98
99 // --- class binding: the one primitive that emits ------------------------
100
104 template <class T, auto Bases, std::size_t... I>
105 static class_handle make_class(module_type& m, const char* /*name*/,
106 const char* /*doc*/, std::index_sequence<I...>) {
107 if constexpr (!::welder::rods::python::overridable_virtuals(^^T).empty() &&
109 m.doc->template add<^^T>();
110 return {};
111 }
112
120 template <class T, std::meta::info Decl, auto Bases, std::size_t... I>
121 static class_handle make_class(module_type& m, const char* /*name*/,
122 const char* /*doc*/, std::index_sequence<I...>) {
123 if constexpr (!::welder::rods::python::overridable_virtuals(Decl).empty() &&
124 !::welder::rods::python::bound_flat(std::meta::dealias(Decl)))
125 m.doc->template add<Decl>();
126 return {};
127 }
128
129 template <class T, auto Ctors, bool HasDefault, bool Aggregate>
131 template <std::meta::info, class = ::welder::naming::none>
132 static void add_field(class_handle&) {}
133 template <auto Fns, class = ::welder::naming::none>
134 static void add_method(class_handle&) {}
135 template <auto Fns, class = ::welder::naming::none>
137 template <auto Fns> static void add_operator(class_handle&) {}
138
139 // --- enum binding: nothing to emit --------------------------------------
140
141 template <class E>
142 static enum_handle make_enum(module_type&, const char*, const char*) {
143 return {};
144 }
145 template <std::meta::info, class = ::welder::naming::none>
147 template <class E> static void finish_enum(enum_handle&) {}
148
149 // --- namespace / module binding: threading only -------------------------
150
151 static session open_module(module_type&) { return {}; }
152 static void set_module_doc(module_type&, const char*) {}
153 template <auto Fns, class = ::welder::naming::none>
154 static void add_function(module_type&, const char* = nullptr) {}
155 template <std::meta::info, class = ::welder::naming::none>
156 static void add_variable(module_type&, session&, const char* = nullptr) {}
157
160 static module_type add_submodule(module_type& m, const char* name) {
161 return module_type{m.doc, m.prefix.empty() ? std::string{name}
162 : m.prefix + "." + name};
163 }
165
166 // --- whole-header generation (this backend's extra entry point) ---------
167
177 template <std::meta::info Ns, class Style = ::welder::naming::none>
178 static void generate(std::ostream& os) {
179 static_assert(std::meta::is_namespace(Ns),
180 "welder: trampolines::generate<Ns>: Ns must reflect a namespace");
181 document doc{};
182 module_type m{&doc, {}};
183 ::welder::welder<rod, Style>::template weld_namespace<Ns>(m);
184 os << doc.render();
185 }
186};
187
188static_assert(::welder::rod<rod>,
189 "welder::rods::trampolines::rod must satisfy welder::rod");
190
191} // namespace welder::rods::trampolines
The contract a rod (a welder backend, welder::rods::…::rod) must satisfy to plug into the generic dri...
Definition concepts.hpp:206
consteval std::vector< std::meta::info > overridable_virtuals(std::meta::info type)
Every overridable virtual slot of type — the ones welder routes through a trampoline — folding in vir...
consteval bool bound_flat(std::meta::info entity)
Does entity (a type or a member function) carry a bind_flat mark?
consteval detail::doc_spec< N > doc(const char(&s)[N])
Attach a docstring to a namespace, class, function, or function parameter.
lang
The target languages welder ships rods for — but not the whole value space.
Definition lang.hpp:42
@ py
Python (via the pybind11 and nanobind backends).
Definition lang.hpp:43
The text-emitting core of welder's trampoline-generator rod: the consteval routine that renders a han...
The growing generated header: the trampoline struct bodies and their trampoline_for registrations,...
Definition document.hpp:174
The class handle the driver threads to the per-member hooks.
Definition rod.hpp:72
A copyable handle onto the growing document that the driver threads through every emission primitive ...
Definition rod.hpp:63
No deferred module state.
Definition rod.hpp:84
static consteval const char * special_method_name(std::meta::info)
No operators are emitted (they are covered as virtuals when overridable), so none are "special".
Definition rod.hpp:95
static void add_field(class_handle &)
Definition rod.hpp:132
module_handle module_type
Definition rod.hpp:67
static void add_constructors(class_handle &)
Definition rod.hpp:130
static session open_module(module_type &)
Definition rod.hpp:151
static class_handle make_class(module_type &m, const char *, const char *, std::index_sequence< I... >)
Emit T's trampoline iff it has overridable virtuals and is not a whole-type bind_flat.
Definition rod.hpp:105
enum_handle enum_handle_type
Definition rod.hpp:82
static void add_function(module_type &, const char *=nullptr)
Definition rod.hpp:154
static void generate(std::ostream &os)
Emit the trampoline header for the welded types in namespace Ns to os.
Definition rod.hpp:178
static void add_method(class_handle &)
Definition rod.hpp:134
static void add_operator(class_handle &)
Definition rod.hpp:137
class_handle class_handle_type
The class / enum handles the per-class / per-enum hooks receive — exactly what make_class / make_enum...
Definition rod.hpp:81
static void set_module_doc(module_type &, const char *)
Definition rod.hpp:152
static constexpr lang language
Trampolines are a Python concept.
Definition rod.hpp:57
static void add_static_method(class_handle &)
Definition rod.hpp:136
static enum_handle make_enum(module_type &, const char *, const char *)
Definition rod.hpp:142
static void close_module(module_type &, session &)
Definition rod.hpp:164
static module_type add_submodule(module_type &m, const char *name)
Recurse into a nested namespace, carrying the document handle through.
Definition rod.hpp:160
static void add_variable(module_type &, session &, const char *=nullptr)
Definition rod.hpp:156
static class_handle make_class(module_type &m, const char *, const char *, std::index_sequence< I... >)
The declaring-entity-aware form the carriage prefers (see bind_type): Decl is the spelling of T — ^^T...
Definition rod.hpp:121
static constexpr bool has_native_caster
Permissive: the generator only reproduces virtual signatures (via splices, which work for any type),...
Definition rod.hpp:91
static void finish_enum(enum_handle &)
Definition rod.hpp:147
static void add_enumerator(enum_handle &)
Definition rod.hpp:146
welder's binding entry point, parameterized on a rod.
Definition welder.hpp:85
welder's binding entry point: the welder::welder struct.