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::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::overridable_virtuals(Decl).empty() &&
124 !::welder::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, bool Copyable,
130 class Style = ::welder::naming::none>
132 template <std::meta::info, class = ::welder::naming::none>
133 static void add_field(class_handle&) {}
134 template <class T, std::meta::info Getter, std::meta::info Setter>
135 static void add_property(class_handle&, const char*) {}
136 template <auto Fns, class = ::welder::naming::none>
137 static void add_method(class_handle&) {}
138 template <auto Fns, class = ::welder::naming::none>
140 template <class T, auto Fns> static void add_operator(class_handle&) {}
141 template <class T, auto Fns, auto Covered>
143 template <class T, std::meta::info Fn>
145
146 // --- enum binding: nothing to emit --------------------------------------
147
148 template <class E>
149 static enum_handle make_enum(module_type&, const char*, const char*) {
150 return {};
151 }
152 template <std::meta::info, class = ::welder::naming::none>
154 template <class E> static void finish_enum(enum_handle&) {}
155
156 // --- namespace / module binding: threading only -------------------------
157
158 static session open_module(module_type&) { return {}; }
159 static void set_module_doc(module_type&, const char*) {}
160 template <auto Fns, class = ::welder::naming::none>
161 static void add_function(module_type&, const char* = nullptr) {}
162 template <std::meta::info, class = ::welder::naming::none>
163 static void add_variable(module_type&, session&, const char* = nullptr) {}
164
167 static module_type add_submodule(module_type& m, const char* name) {
168 return module_type{m.doc, m.prefix.empty() ? std::string{name}
169 : m.prefix + "." + name};
170 }
172
173 // --- whole-header generation (this backend's extra entry point) ---------
174
184 template <std::meta::info Ns, class Style = ::welder::naming::none>
185 static void generate(std::ostream& os) {
186 static_assert(std::meta::is_namespace(Ns),
187 "welder: trampolines::generate<Ns>: Ns must reflect a namespace");
188 document doc{};
189 module_type m{&doc, {}};
190 ::welder::welder<rod, Style>::template weld_namespace<Ns>(m);
191 os << doc.render();
192 }
193};
194
195static_assert(::welder::rod<rod>,
196 "welder::rods::trampolines::rod must satisfy welder::rod");
197
198} // 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:263
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
consteval bool bound_flat(std::meta::info entity)
Does entity (a type or a member function) carry a bind_flat mark?
Definition virtuals.hpp:63
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...
Definition virtuals.hpp:174
The text-emitting core of welder's trampoline-generator rod: the consteval routine that renders a han...
The identity style: bind every C++ identifier unchanged.
Definition naming.hpp:259
The growing generated header: the trampoline struct bodies and their trampoline_for registrations,...
Definition document.hpp:196
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_operator(class_handle &)
Definition rod.hpp:140
static void add_field(class_handle &)
Definition rod.hpp:133
module_handle module_type
Definition rod.hpp:67
static session open_module(module_type &)
Definition rod.hpp:158
static void add_stringifier(class_handle &)
Definition rod.hpp:144
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:161
static void generate(std::ostream &os)
Emit the trampoline header for the welded types in namespace Ns to os.
Definition rod.hpp:185
static void add_method(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:159
static constexpr lang language
Trampolines are a Python concept.
Definition rod.hpp:57
static void add_constructors(class_handle &)
Definition rod.hpp:131
static void add_static_method(class_handle &)
Definition rod.hpp:139
static void add_comparisons(class_handle &)
Definition rod.hpp:142
static enum_handle make_enum(module_type &, const char *, const char *)
Definition rod.hpp:149
static void close_module(module_type &, session &)
Definition rod.hpp:171
static module_type add_submodule(module_type &m, const char *name)
Recurse into a nested namespace, carrying the document handle through.
Definition rod.hpp:167
static void add_property(class_handle &, const char *)
Definition rod.hpp:135
static void add_variable(module_type &, session &, const char *=nullptr)
Definition rod.hpp:163
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:154
static void add_enumerator(enum_handle &)
Definition rod.hpp:153
welder's binding entry point, parameterized on a rod.
Definition welder.hpp:85
welder's binding entry point: the welder::welder struct.