-
Notifications
You must be signed in to change notification settings - Fork 153
/
nb_class.h
553 lines (445 loc) · 20.3 KB
/
nb_class.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
nanobind/nb_class.h: Functionality for binding C++ classes/structs
Copyright (c) 2022 Wenzel Jakob
All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
*/
NAMESPACE_BEGIN(NB_NAMESPACE)
NAMESPACE_BEGIN(detail)
/// Flags about a type that persist throughout its lifetime
enum class type_flags : uint32_t {
/// Does the type provide a C++ destructor?
is_destructible = (1 << 0),
/// Does the type provide a C++ copy constructor?
is_copy_constructible = (1 << 1),
/// Does the type provide a C++ move constructor?
is_move_constructible = (1 << 2),
/// Is the 'destruct' field of the type_data structure set?
has_destruct = (1 << 4),
/// Is the 'copy' field of the type_data structure set?
has_copy = (1 << 5),
/// Is the 'move' field of the type_data structure set?
has_move = (1 << 6),
/// Internal: does the type maintain a list of implicit conversions?
has_implicit_conversions = (1 << 7),
/// Is this a python type that extends a bound C++ type?
is_python_type = (1 << 8),
/// This type does not permit subclassing from Python
is_final = (1 << 9),
/// Is the 'supplement' field of the type_data structure set?
has_supplement = (1 << 10),
/// Instances of this type support dynamic attribute assignment
has_dynamic_attr = (1 << 11),
/// The class uses an intrusive reference counting approach
intrusive_ptr = (1 << 12),
/// Is this a trampoline class meant to be overloaded in Python?
is_trampoline = (1 << 13),
/// Is the 'scope' field of the type_data structure set?
has_scope = (1 << 14),
/// This type is a signed enumeration
is_signed_enum = (1 << 15),
/// This type is an unsigned enumeration
is_unsigned_enum = (1 << 16),
/// This type is an arithmetic enumeration
is_arithmetic = (1 << 17),
// Two more flag bits available (18 and 19) without needing
// a larger reorganization
};
/// Flags about a type that are only relevant when it is being created.
/// These are currently stored in type_data::flags alongside the type_flags
/// for more efficient memory layout, but could move elsewhere if we run
/// out of flags.
enum class type_init_flags : uint32_t {
/// Is the 'doc' field of the type_data_prelim structure set?
has_doc = (1 << 20),
/// Is the 'base' field of the type_data_prelim structure set?
has_base = (1 << 21),
/// Is the 'base_py' field of the type_data_prelim structure set?
has_base_py = (1 << 22),
/// This type provides extra PyType_Slot fields
has_type_slots = (1 << 23),
all_init_flags = (0xf << 20)
};
/// Information about a type that persists throughout its lifetime
struct type_data {
uint32_t size;
uint32_t align : 8;
uint32_t flags : 24;
const char *name;
PyObject *scope;
const std::type_info *type;
PyTypeObject *type_py;
void (*destruct)(void *);
void (*copy)(void *, const void *);
void (*move)(void *, void *) noexcept;
const std::type_info **implicit;
bool (**implicit_py)(PyTypeObject *, PyObject *, cleanup_list *) noexcept;
void (*set_self_py)(void *, PyObject *) noexcept;
#if defined(Py_LIMITED_API)
size_t dictoffset;
#endif
};
/// Information about a type that is only relevant when it is being created
struct type_init_data : type_data {
const std::type_info *base;
PyTypeObject *base_py;
const char *doc;
const PyType_Slot *type_slots;
size_t supplement;
};
NB_INLINE void type_extra_apply(type_init_data &t, const handle &h) {
t.flags |= (uint32_t) type_init_flags::has_base_py;
t.base_py = (PyTypeObject *) h.ptr();
}
NB_INLINE void type_extra_apply(type_init_data &t, const char *doc) {
t.flags |= (uint32_t) type_init_flags::has_doc;
t.doc = doc;
}
NB_INLINE void type_extra_apply(type_init_data &t, type_slots c) {
t.flags |= (uint32_t) type_init_flags::has_type_slots;
t.type_slots = c.value;
}
template <typename T>
NB_INLINE void type_extra_apply(type_init_data &t, intrusive_ptr<T> ip) {
t.flags |= (uint32_t) type_flags::intrusive_ptr;
t.set_self_py = (void (*)(void *, PyObject *) noexcept) ip.set_self_py;
}
NB_INLINE void type_extra_apply(type_init_data &t, is_final) {
t.flags |= (uint32_t) type_flags::is_final;
}
NB_INLINE void type_extra_apply(type_init_data &t, dynamic_attr) {
t.flags |= (uint32_t) type_flags::has_dynamic_attr;
}
template <typename T>
NB_INLINE void type_extra_apply(type_init_data &t, supplement<T>) {
static_assert(std::is_trivially_default_constructible_v<T>,
"The supplement must be a POD (plain old data) type");
static_assert(alignof(T) <= alignof(void *),
"The alignment requirement of the supplement is too high.");
t.flags |= (uint32_t) type_flags::has_supplement | (uint32_t) type_flags::is_final;
t.supplement = sizeof(T);
}
// Enum-specific annotations:
NB_INLINE void type_extra_apply(type_init_data &t, is_enum e) {
if (e.is_signed)
t.flags |= (uint32_t) type_flags::is_signed_enum;
else
t.flags |= (uint32_t) type_flags::is_unsigned_enum;
}
NB_INLINE void type_extra_apply(type_init_data &t, is_arithmetic) {
t.flags |= (uint32_t) type_flags::is_arithmetic;
}
template <typename T> void wrap_copy(void *dst, const void *src) {
new ((T *) dst) T(*(const T *) src);
}
template <typename T> void wrap_move(void *dst, void *src) noexcept {
new ((T *) dst) T(std::move(*(T *) src));
}
template <typename T> void wrap_destruct(void *value) noexcept {
((T *) value)->~T();
}
template <typename, template <typename, typename> typename, typename...>
struct extract;
template <typename T, template <typename, typename> typename Pred>
struct extract<T, Pred> {
using type = T;
};
template <typename T, template <typename, typename> typename Pred,
typename Tv, typename... Ts>
struct extract<T, Pred, Tv, Ts...> {
using type = std::conditional_t<
Pred<T, Tv>::value,
Tv,
typename extract<T, Pred, Ts...>::type
>;
};
template <typename T, typename Arg> using is_alias = std::is_base_of<T, Arg>;
template <typename T, typename Arg> using is_base = std::is_base_of<Arg, T>;
enum op_id : int;
enum op_type : int;
struct undefined_t;
template <op_id id, op_type ot, typename L = undefined_t, typename R = undefined_t> struct op_;
// The header file include/nanobind/stl/detail/traits.h extends this type trait
template <typename T, typename SFINAE = int>
struct is_copy_constructible : std::is_copy_constructible<T> { };
template <typename T>
constexpr bool is_copy_constructible_v = is_copy_constructible<T>::value;
NAMESPACE_END(detail)
template <typename... Args> struct init {
template <typename T, typename... Ts> friend class class_;
NB_INLINE init() {}
private:
template <typename Class, typename... Extra>
NB_INLINE static void execute(Class &cl, const Extra&... extra) {
using Type = typename Class::Type;
using Alias = typename Class::Alias;
static_assert(
detail::make_caster<Type>::IsClass,
"Attempted to create a constructor for a type that won't be "
"handled by the nanobind's class type caster. Is it possible that "
"you forgot to add NB_MAKE_OPAQUE() somewhere?");
cl.def(
"__init__",
[](pointer_and_handle<Type> v, Args... args) {
if constexpr (!std::is_same_v<Type, Alias> &&
std::is_constructible_v<Type, Args...>) {
if (!detail::nb_inst_python_derived(v.h.ptr())) {
new ((Type *) v.p) Type{ (detail::forward_t<Args>) args... };
return;
}
}
new ((Alias *) v.p) Alias{ (detail::forward_t<Args>) args... };
},
extra...);
}
};
template <typename Arg> struct init_implicit {
template <typename T, typename... Ts> friend class class_;
NB_INLINE init_implicit() { }
private:
template <typename Class, typename... Extra>
NB_INLINE static void execute(Class &cl, const Extra&... extra) {
using Type = typename Class::Type;
using Alias = typename Class::Alias;
using Caster = detail::make_caster<Arg>;
static_assert(
detail::make_caster<Type>::IsClass,
"Attempted to create a constructor for a type that won't be "
"handled by the nanobind's class type caster. Is it possible that "
"you forgot to add NB_MAKE_OPAQUE() somewhere?");
cl.def(
"__init__",
[](pointer_and_handle<Type> v, Arg arg) {
if constexpr (!std::is_same_v<Type, Alias> &&
std::is_constructible_v<Type, Arg>) {
if (!detail::nb_inst_python_derived(v.h.ptr())) {
new ((Type *) v.p) Type{ (detail::forward_t<Arg>) arg };
return;
}
}
new ((Alias *) v.p) Alias{ (detail::forward_t<Arg>) arg };
}, is_implicit(), extra...);
if constexpr (!Caster::IsClass) {
detail::implicitly_convertible(
[](PyTypeObject *, PyObject *src,
detail::cleanup_list *cleanup) noexcept -> bool {
return Caster().from_python(
src, detail::cast_flags::convert, cleanup);
},
&typeid(Type));
}
}
};
template <typename T, typename... Ts>
class class_ : public object {
public:
NB_OBJECT_DEFAULT(class_, object, "type", PyType_Check);
using Type = T;
using Base = typename detail::extract<T, detail::is_base, Ts...>::type;
using Alias = typename detail::extract<T, detail::is_alias, Ts...>::type;
static_assert(sizeof(Alias) < (1 << 24), "instance size is too big!");
static_assert(alignof(Alias) < (1 << 8), "instance alignment is too big!");
static_assert(
sizeof...(Ts) == !std::is_same_v<Base, T> + !std::is_same_v<Alias, T>,
"nanobind::class_<> was invoked with extra arguments that could not be handled");
template <typename... Extra>
NB_INLINE class_(handle scope, const char *name, const Extra &... extra) {
detail::type_init_data d;
d.flags = (uint32_t) detail::type_flags::has_scope;
d.align = (uint8_t) alignof(Alias);
d.size = (uint32_t) sizeof(Alias);
d.name = name;
d.scope = scope.ptr();
d.type = &typeid(T);
if constexpr (!std::is_same_v<Base, T>) {
d.base = &typeid(Base);
d.flags |= (uint32_t) detail::type_init_flags::has_base;
}
if constexpr (!std::is_same_v<Alias, T>)
d.flags |= (uint32_t) detail::type_flags::is_trampoline;
if constexpr (detail::is_copy_constructible_v<T>) {
d.flags |= (uint32_t) detail::type_flags::is_copy_constructible;
if constexpr (!std::is_trivially_copy_constructible_v<T>) {
d.flags |= (uint32_t) detail::type_flags::has_copy;
d.copy = detail::wrap_copy<T>;
}
}
if constexpr (std::is_move_constructible<T>::value) {
d.flags |= (uint32_t) detail::type_flags::is_move_constructible;
if constexpr (!std::is_trivially_move_constructible_v<T>) {
d.flags |= (uint32_t) detail::type_flags::has_move;
d.move = detail::wrap_move<T>;
}
}
if constexpr (std::is_destructible_v<T>) {
d.flags |= (uint32_t) detail::type_flags::is_destructible;
if constexpr (!std::is_trivially_destructible_v<T>) {
d.flags |= (uint32_t) detail::type_flags::has_destruct;
d.destruct = detail::wrap_destruct<T>;
}
}
(detail::type_extra_apply(d, extra), ...);
m_ptr = detail::nb_type_new(&d);
}
template <typename Func, typename... Extra>
NB_INLINE class_ &def(const char *name_, Func &&f, const Extra &... extra) {
cpp_function_def((detail::forward_t<Func>) f, scope(*this), name(name_),
is_method(), extra...);
return *this;
}
template <typename... Args, typename... Extra>
NB_INLINE class_ &def(init<Args...> &&init, const Extra &... extra) {
init.execute(*this, extra...);
return *this;
}
template <typename Arg, typename... Extra>
NB_INLINE class_ &def(init_implicit<Arg> &&init, const Extra &... extra) {
init.execute(*this, extra...);
return *this;
}
template <typename Func, typename... Extra>
NB_INLINE class_ &def_static(const char *name_, Func &&f,
const Extra &... extra) {
static_assert(
!std::is_member_function_pointer_v<Func>,
"def_static(...) called with a non-static member function pointer");
cpp_function_def((detail::forward_t<Func>) f, scope(*this), name(name_),
extra...);
return *this;
}
template <typename Getter, typename Setter, typename... Extra>
NB_INLINE class_ &def_prop_rw(const char *name_, Getter &&getter,
Setter &&setter, const Extra &...extra) {
object get_p, set_p;
if constexpr (!std::is_same_v<Getter, std::nullptr_t>)
get_p = cpp_function((detail::forward_t<Getter>) getter,
scope(*this), is_method(),
rv_policy::reference_internal, extra...);
if constexpr (!std::is_same_v<Setter, std::nullptr_t>)
set_p = cpp_function((detail::forward_t<Setter>) setter,
scope(*this), is_method(), extra...);
detail::property_install(m_ptr, name_, get_p.ptr(), set_p.ptr());
return *this;
}
template <typename Getter, typename Setter, typename... Extra>
NB_INLINE class_ &def_prop_rw_static(const char *name_, Getter &&getter,
Setter &&setter,
const Extra &...extra) {
object get_p, set_p;
if constexpr (!std::is_same_v<Getter, std::nullptr_t>)
get_p = cpp_function((detail::forward_t<Getter>) getter,
scope(*this), rv_policy::reference, extra...);
if constexpr (!std::is_same_v<Setter, std::nullptr_t>)
set_p = cpp_function((detail::forward_t<Setter>) setter,
scope(*this), extra...);
detail::property_install_static(m_ptr, name_, get_p.ptr(), set_p.ptr());
return *this;
}
template <typename Getter, typename... Extra>
NB_INLINE class_ &def_prop_ro(const char *name_, Getter &&getter,
const Extra &...extra) {
return def_prop_rw(name_, getter, nullptr, extra...);
}
template <typename Getter, typename... Extra>
NB_INLINE class_ &def_prop_ro_static(const char *name_,
Getter &&getter,
const Extra &...extra) {
return def_prop_rw_static(name_, getter, nullptr, extra...);
}
template <typename C, typename D, typename... Extra>
NB_INLINE class_ &def_rw(const char *name, D C::*p,
const Extra &...extra) {
static_assert(std::is_base_of_v<C, T>,
"def_rw() requires a (base) class member!");
using Q = std::conditional_t<detail::make_caster<D>::IsClass, const D &, D &&>;
def_prop_rw(name,
[p](const T &c) -> const D & { return c.*p; },
[p](T &c, Q value) { c.*p = (Q) value; },
extra...);
return *this;
}
template <typename D, typename... Extra>
NB_INLINE class_ &def_rw_static(const char *name, D *p,
const Extra &...extra) {
using Q = std::conditional_t<detail::make_caster<D>::IsClass, const D &, D &&>;
def_prop_rw_static(name,
[p](handle) -> const D & { return *p; },
[p](handle, Q value) { *p = (Q) value; }, extra...);
return *this;
}
template <typename C, typename D, typename... Extra>
NB_INLINE class_ &def_ro(const char *name, D C::*p,
const Extra &...extra) {
static_assert(std::is_base_of_v<C, T>,
"def_ro() requires a (base) class member!");
def_prop_ro(name,
[p](const T &c) -> const D & { return c.*p; }, extra...);
return *this;
}
template <typename D, typename... Extra>
NB_INLINE class_ &def_ro_static(const char *name, D *p,
const Extra &...extra) {
def_prop_ro_static(name,
[p](handle) -> const D & { return *p; }, extra...);
return *this;
}
template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
class_ &def(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
op.execute(*this, extra...);
return *this;
}
template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
class_ & def_cast(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
op.execute_cast(*this, extra...);
return *this;
}
};
template <typename T> class enum_ : public class_<T> {
public:
static_assert(std::is_enum_v<T>, "nanobind::enum_<> requires an enumeration type!");
using Base = class_<T>;
template <typename... Extra>
NB_INLINE enum_(handle scope, const char *name, const Extra &...extra)
: Base(scope, name, extra...,
is_enum{ std::is_signed_v<std::underlying_type_t<T>> }) { }
NB_INLINE enum_ &value(const char *name, T value, const char *doc = nullptr) {
detail::nb_enum_put(Base::m_ptr, name, &value, doc);
return *this;
}
NB_INLINE enum_ &export_values() { detail::nb_enum_export(Base::m_ptr); return *this; }
};
template <typename Source, typename Target> void implicitly_convertible() {
using Caster = detail::make_caster<Source>;
if constexpr (Caster::IsClass) {
detail::implicitly_convertible(&typeid(Source), &typeid(Target));
} else {
detail::implicitly_convertible(
[](PyTypeObject *, PyObject *src,
detail::cleanup_list *cleanup) noexcept -> bool {
return Caster().from_python(src, detail::cast_flags::convert,
cleanup);
},
&typeid(Target));
}
}
// Low level access to nanobind type objects
inline bool type_check(handle h) { return detail::nb_type_check(h.ptr()); }
inline size_t type_size(handle h) { return detail::nb_type_size(h.ptr()); }
inline size_t type_align(handle h) { return detail::nb_type_align(h.ptr()); }
inline const std::type_info& type_info(handle h) { return *detail::nb_type_info(h.ptr()); }
template <typename T>
inline T &type_supplement(handle h) { return *(T *) detail::nb_type_supplement(h.ptr()); }
// Low level access to nanobind instance objects
inline bool inst_check(handle h) { return type_check(h.type()); }
inline object inst_alloc(handle h) { return steal(detail::nb_inst_alloc((PyTypeObject *) h.ptr())); }
inline object inst_wrap(handle h, void *p) { return steal(detail::nb_inst_wrap((PyTypeObject *) h.ptr(), p)); }
inline void inst_zero(handle h) { detail::nb_inst_zero(h.ptr()); }
inline void inst_set_state(handle h, bool ready, bool destruct) { detail::nb_inst_set_state(h.ptr(), ready, destruct); }
inline std::pair<bool, bool> inst_state(handle h) { return detail::nb_inst_state(h.ptr()); }
inline void inst_mark_ready(handle h) { inst_set_state(h, true, true); }
inline bool inst_ready(handle h) { return inst_state(h).first; }
inline void inst_destruct(handle h) { detail::nb_inst_destruct(h.ptr()); }
inline void inst_copy(handle dst, handle src) { detail::nb_inst_copy(dst.ptr(), src.ptr()); }
inline void inst_move(handle dst, handle src) { detail::nb_inst_move(dst.ptr(), src.ptr()); }
template <typename T> T *inst_ptr(handle h) { return (T *) detail::nb_inst_ptr(h.ptr()); }
NAMESPACE_END(NB_NAMESPACE)