-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathnb_class.h
894 lines (745 loc) · 34.2 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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
/*
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),
/// Instances of this type support dynamic attribute assignment
has_dynamic_attr = (1 << 10),
/// The class uses an intrusive reference counting approach
intrusive_ptr = (1 << 11),
/// Is this a class that inherits from enable_shared_from_this?
/// If so, type_data::keep_shared_from_this_alive is also set.
has_shared_from_this = (1 << 12),
/// Instances of this type can be referenced by 'weakref'
is_weak_referenceable = (1 << 13),
/// A custom signature override was specified
has_signature = (1 << 14),
/// The class implements __class_getitem__ similar to typing.Generic
is_generic = (1 << 15),
/// Does the type implement a custom __new__ operator?
has_new = (1 << 16),
/// Does the type implement a custom __new__ operator that can take no args
/// (except the type object)?
has_nullary_new = (1 << 17)
// One more bit available 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 'supplement' field of the type_init_data structure set?
has_supplement = (1 << 19),
/// Is the 'doc' field of the type_init_data structure set?
has_doc = (1 << 20),
/// Is the 'base' field of the type_init_data structure set?
has_base = (1 << 21),
/// Is the 'base_py' field of the type_init_data structure set?
has_base_py = (1 << 22),
/// This type provides extra PyType_Slot fields
has_type_slots = (1 << 23),
all_init_flags = (0x1f << 19)
};
// See internals.h
struct nb_alias_chain;
/// 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;
const std::type_info *type;
PyTypeObject *type_py;
nb_alias_chain *alias_chain;
#if defined(Py_LIMITED_API)
PyObject* (*vectorcall)(PyObject *, PyObject * const*, size_t, PyObject *);
#endif
void *init; // Constructor nb_func
void (*destruct)(void *);
void (*copy)(void *, const void *);
void (*move)(void *, void *) noexcept;
union {
// Implicit conversions for C++ type bindings
struct {
const std::type_info **cpp;
bool (**py)(PyTypeObject *, PyObject *, cleanup_list *) noexcept;
} implicit;
// Forward and reverse mappings for enumerations
struct {
void *fwd;
void *rev;
} enum_tbl;
};
void (*set_self_py)(void *, PyObject *) noexcept;
bool (*keep_shared_from_this_alive)(PyObject *) noexcept;
#if defined(Py_LIMITED_API)
uint32_t dictoffset;
uint32_t weaklistoffset;
#endif
};
/// Information about a type T used as nb::supplement<T>()
struct supplement_data {
// These aliases are required to work around a MSVC bug
using constructor_t = void (*)(void *supp, PyTypeObject *tp);
using destructor_t = void (*)(void *supp);
using init_metaclass_t = void (*)(PyTypeObject *metatype);
const std::type_info *type;
size_t size;
constructor_t construct; // nullptr if trivial
destructor_t destruct; // nullptr if trivial
init_metaclass_t init_metaclass; // nullptr if not defined
};
template <typename T, typename = void>
inline supplement_data::init_metaclass_t init_metaclass_for = nullptr;
template <typename T>
inline supplement_data::init_metaclass_t init_metaclass_for<
T, std::void_t<decltype(&T::init_metaclass)>> = &T::init_metaclass;
template <typename T, typename = int>
inline supplement_data::constructor_t construct_supplement_for =
std::is_trivially_default_constructible_v<T> ? nullptr :
+[](void *p, PyTypeObject *) { new (p) T{}; };
template <typename T>
inline supplement_data::constructor_t construct_supplement_for<
T, enable_if_t<std::is_constructible_v<T, PyTypeObject *>>> =
+[](void *p, PyTypeObject *tp) { new (p) T{tp}; };
template <typename T>
inline const supplement_data supplement_data_for = {
&typeid(T), sizeof(T),
construct_supplement_for<T>,
std::is_trivially_destructible_v<T> ? nullptr :
+[](void *p) { ((T *) p)->~T(); },
init_metaclass_for<T>,
};
/// Information about a type that is only relevant when it is being created
struct type_init_data : type_data {
PyObject *scope;
const std::type_info *base;
PyTypeObject *base_py;
const char *doc;
const PyType_Slot *type_slots;
const supplement_data *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;
}
NB_INLINE void type_extra_apply(type_init_data & t, is_weak_referenceable) {
t.flags |= (uint32_t) type_flags::is_weak_referenceable;
}
NB_INLINE void type_extra_apply(type_init_data & t, is_generic) {
t.flags |= (uint32_t) type_flags::is_generic;
}
NB_INLINE void type_extra_apply(type_init_data & t, const sig &s) {
t.flags |= (uint32_t) type_flags::has_signature;
t.name = s.value;
}
template <typename T>
NB_INLINE void type_extra_apply(type_init_data &t, supplement<T> supp) {
static_assert(alignof(T) <= alignof(void *),
"The alignment requirement of the supplement is too high.");
t.flags |= (uint32_t) type_init_flags::has_supplement;
if (!supp.is_inheritable)
t.flags |= (uint32_t) type_flags::is_final;
t.supplement = &supplement_data_for<T>;
}
enum class enum_flags : uint32_t {
/// Is this an arithmetic enumeration?
is_arithmetic = (1 << 1),
/// Is the number type underlying the enumeration signed?
is_signed = (1 << 2),
/// Is the underlying enumeration type Flag?
is_flag = (1 << 3)
};
struct enum_init_data {
const std::type_info *type;
PyObject *scope;
const char *name;
const char *docstr;
uint32_t flags;
};
NB_INLINE void enum_extra_apply(enum_init_data &e, is_arithmetic) {
e.flags |= (uint32_t) enum_flags::is_arithmetic;
}
NB_INLINE void enum_extra_apply(enum_init_data &e, is_flag) {
e.flags |= (uint32_t) enum_flags::is_flag;
}
NB_INLINE void enum_extra_apply(enum_init_data &e, const char *doc) {
e.docstr = doc;
}
template <typename T>
NB_INLINE void enum_extra_apply(enum_init_data &, T) {
static_assert(
std::is_void_v<T>,
"Invalid enum binding annotation. The implementation of "
"enums changed nanobind 2.0.0: only nb::is_arithmetic and "
"docstrings can be passed since this change.");
}
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)
// 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()); }
template <typename T>
inline bool type_has_supplement(handle h) { return detail::nb_type_has_supplement(h.ptr(), &typeid(T)); }
inline str type_name(handle h) { return steal<str>(detail::nb_type_name(h.ptr())); }
// Low level access to nanobind instance objects
inline bool inst_check(handle h) { return type_check(h.type()); }
inline str inst_name(handle h) {
return steal<str>(detail::nb_inst_name(h.ptr()));
}
inline object inst_alloc(handle h) {
return steal(detail::nb_inst_alloc((PyTypeObject *) h.ptr()));
}
inline object inst_alloc_zero(handle h) {
return steal(detail::nb_inst_alloc_zero((PyTypeObject *) h.ptr()));
}
inline object inst_take_ownership(handle h, void *p) {
return steal(detail::nb_inst_take_ownership((PyTypeObject *) h.ptr(), p));
}
inline object inst_reference(handle h, void *p, handle parent = handle()) {
return steal(detail::nb_inst_reference((PyTypeObject *) h.ptr(), p, parent.ptr()));
}
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()); }
inline void inst_replace_copy(handle dst, handle src) { detail::nb_inst_replace_copy(dst.ptr(), src.ptr()); }
inline void inst_replace_move(handle dst, handle src) { detail::nb_inst_replace_move(dst.ptr(), src.ptr()); }
template <typename T> T *inst_ptr(handle h) { return (T *) detail::nb_inst_ptr(h.ptr()); }
inline void *type_get_slot(handle h, int slot_id) {
#if NB_TYPE_GET_SLOT_IMPL
return detail::type_get_slot((PyTypeObject *) h.ptr(), slot_id);
#else
return PyType_GetSlot((PyTypeObject *) h.ptr(), slot_id);
#endif
}
template <typename Visitor> struct def_visitor {
protected:
// Ensure def_visitor<T> can only be derived from, not constructed
// directly
def_visitor() {
static_assert(std::is_base_of_v<def_visitor, Visitor>,
"def_visitor uses CRTP: def_visitor<T> should be "
"a base of T");
}
};
template <typename... Args> struct init : def_visitor<init<Args...>> {
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;
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 (v.p) Type{ (detail::forward_t<Args>) args... };
return;
}
}
new ((void *) v.p) Alias{ (detail::forward_t<Args>) args... };
},
extra...);
}
};
template <typename Arg> struct init_implicit : def_visitor<init_implicit<Arg>> {
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;
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...);
using Caster = detail::make_caster<Arg>;
if constexpr (!detail::is_class_caster_v<Caster>) {
detail::implicitly_convertible(
[](PyTypeObject *, PyObject *src,
detail::cleanup_list *cleanup) noexcept -> bool {
return Caster().from_python(
src, detail::cast_flags::convert, cleanup);
},
&typeid(Type));
}
}
};
namespace detail {
// This is 'inline' so we can define it in a header and not pay
// for it if unused, and also 'noinline' so we don't generate
// multiple copies and produce code bloat.
NB_NOINLINE inline void wrap_base_new(handle cls, bool do_wrap) {
if (PyCFunction_Check(cls.attr("__new__").ptr())) {
if (do_wrap) {
cpp_function_def(
[](handle type) {
if (!type_check(type))
detail::raise_cast_error();
return inst_alloc(type);
},
scope(cls), name("__new__"));
}
} else {
if (!do_wrap) {
// We already defined the wrapper, so this zero-arg overload
// would be unreachable. Raise an error rather than hiding it.
raise("nanobind: %s must define its zero-argument __new__ "
"before any other overloads", type_name(cls).c_str());
}
}
}
// Call policy that ensures __new__ returns an instance of the correct
// Python type, even when deriving from the C++ class in Python
struct new_returntype_fixup_policy {
static inline void precall(PyObject **, size_t,
detail::cleanup_list *) {}
NB_NOINLINE static inline void postcall(PyObject **args, size_t,
PyObject *&ret) {
handle type_requested = args[0];
if (ret == nullptr || !type_requested.is_type())
return; // somethign strange about this call; don't meddle
handle type_created = Py_TYPE(ret);
if (type_created.is(type_requested))
return; // already created the requested type so no fixup needed
if (type_check(type_created) &&
PyType_IsSubtype((PyTypeObject *) type_requested.ptr(),
(PyTypeObject *) type_created.ptr()) &&
type_info(type_created) == type_info(type_requested)) {
// The new_ constructor returned an instance of a bound type T.
// The user wanted an instance of some python subclass S of T.
// Since both wrap the same C++ type, we can satisfy the request
// by returning a pyobject of type S that wraps a C++ T*, and
// handling the lifetimes by having that pyobject keep the
// already-created T pyobject alive.
object wrapper = inst_reference(type_requested,
inst_ptr<void>(ret),
/* parent = */ ret);
handle(ret).dec_ref();
ret = wrapper.release().ptr();
}
}
};
}
template <typename Func, typename Sig = detail::function_signature_t<Func>>
struct new_;
template <typename Func, typename Return, typename... Args>
struct new_<Func, Return(Args...)> : def_visitor<new_<Func, Return(Args...)>> {
std::remove_reference_t<Func> func;
new_(Func &&f) : func((detail::forward_t<Func>) f) {}
template <typename Class, typename... Extra>
NB_INLINE void execute(Class &cl, const Extra&... extra) {
// If this is the first __new__ overload we're defining, then wrap
// nanobind's built-in __new__ so we overload with it instead of
// replacing it; this is important for pickle support.
// We can't do this if the user-provided __new__ takes no
// arguments, because it would make an ambiguous overload set.
constexpr size_t num_defaults =
((std::is_same_v<Extra, arg_v> ||
std::is_same_v<Extra, arg_locked_v>) + ... + 0);
constexpr size_t num_varargs =
((std::is_same_v<detail::intrinsic_t<Args>, args> ||
std::is_same_v<detail::intrinsic_t<Args>, kwargs>) + ... + 0);
detail::wrap_base_new(cl, sizeof...(Args) > num_defaults + num_varargs);
auto wrapper = [func_ = (detail::forward_t<Func>) func](handle, Args... args) {
return func_((detail::forward_t<Args>) args...);
};
auto policy = call_policy<detail::new_returntype_fixup_policy>();
if constexpr ((std::is_base_of_v<arg, Extra> || ...)) {
// If any argument annotations are specified, add another for the
// extra class argument that we don't forward to Func, so visible
// arg() annotations stay aligned with visible function arguments.
cl.def_static("__new__", std::move(wrapper), arg("cls"), extra...,
policy);
} else {
cl.def_static("__new__", std::move(wrapper), extra..., policy);
}
cl.def("__init__", [](handle, Args...) {}, extra...);
}
};
template <typename Func> new_(Func&& f) -> new_<Func>;
template <typename T> struct for_setter {
T value;
for_setter(const T &value) : value(value) { }
};
template <typename T> struct for_getter {
T value;
for_getter(const T &value) : value(value) { }
};
template <typename T> for_getter(T) -> for_getter<std::decay_t<T>>;
template <typename T> for_setter(T) -> for_setter<std::decay_t<T>>;
namespace detail {
template <typename T> auto filter_getter(const T &v) { return v; }
template <typename T> auto filter_getter(const for_getter<T> &v) { return v.value; }
template <typename T> std::nullptr_t filter_getter(const for_setter<T> &) { return nullptr; }
template <typename T> auto filter_setter(const T &v) { return v; }
template <typename T> auto filter_setter(const for_setter<T> &v) { return v.value; }
template <typename T> std::nullptr_t filter_setter(const for_getter<T> &) { return nullptr; }
}
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) < (((uint64_t) 1) << 32), "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");
static_assert(
detail::is_base_caster_v<detail::make_caster<Type>>,
"You attempted to bind a type that is already intercepted by a type "
"caster. Having both at the same time is not allowed. Are you perhaps "
"binding an STL type, while at the same time including a matching "
"type caster from <nanobind/stl/*>? Or did you perhaps forget to "
"declare NB_MAKE_OPAQUE(..) to specifically disable the type caster "
"catch-all for a specific type? Please review the documentation "
"to learn about the difference between bindings and type casters.");
template <typename... Extra>
NB_INLINE class_(handle scope, const char *name, const Extra &... extra) {
detail::type_init_data d;
d.flags = 0;
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 (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>;
}
}
if constexpr (detail::has_shared_from_this_v<T>) {
d.flags |= (uint32_t) detail::type_flags::has_shared_from_this;
d.keep_shared_from_this_alive = [](PyObject *self) noexcept {
// weak_from_this().lock() is equivalent to shared_from_this(),
// except that it returns an empty shared_ptr instead of
// throwing an exception if there is no active shared_ptr
// for this object. (Added in C++17.)
if (auto sp = inst_ptr<T>(self)->weak_from_this().lock()) {
detail::keep_alive(self, new auto(std::move(sp)),
[](void *p) noexcept {
delete (decltype(sp) *) p;
});
return true;
}
return false;
};
}
(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<T>((detail::forward_t<Func>) f, scope(*this),
name(name_), is_method(), extra...);
return *this;
}
template <typename Visitor, typename... Extra>
NB_INLINE class_ &def(def_visitor<Visitor> &&arg, const Extra &... extra) {
static_cast<Visitor&&>(arg).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<T>((detail::forward_t<Getter>) getter,
is_method(), is_getter(),
rv_policy::reference_internal,
detail::filter_getter(extra)...);
if constexpr (!std::is_same_v<Setter, std::nullptr_t>)
set_p = cpp_function<T>((detail::forward_t<Setter>) setter,
is_method(), detail::filter_setter(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, is_getter(),
rv_policy::reference,
detail::filter_getter(extra)...);
if constexpr (!std::is_same_v<Setter, std::nullptr_t>)
set_p = cpp_function((detail::forward_t<Setter>) setter,
detail::filter_setter(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) {
// Unions never satisfy is_base_of, thus the is_same alternative
static_assert(std::is_base_of_v<C, T> || std::is_same_v<C, T>,
"def_rw() requires a (base) class member!");
using Q =
std::conditional_t<detail::is_base_caster_v<detail::make_caster<D>>,
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::is_base_caster_v<detail::make_caster<D>>,
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) {
// Unions never satisfy is_base_of, thus the is_same alternative
static_assert(std::is_base_of_v<C, T> || std::is_same_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 object {
public:
static_assert(std::is_enum_v<T>, "nanobind::enum_<> requires an enumeration type!");
using Base = class_<T>;
using Underlying = std::underlying_type_t<T>;
template <typename... Extra>
NB_INLINE enum_(handle scope, const char *name, const Extra &... extra) {
detail::enum_init_data ed { };
ed.type = &typeid(T);
ed.scope = scope.ptr();
ed.name = name;
ed.flags = std::is_signed_v<Underlying>
? (uint32_t) detail::enum_flags::is_signed
: 0;
(detail::enum_extra_apply(ed, extra), ...);
m_ptr = detail::enum_create(&ed);
}
NB_INLINE enum_ &value(const char *name, T value, const char *doc = nullptr) {
detail::enum_append(m_ptr, name, (int64_t) value, doc);
return *this;
}
NB_INLINE enum_ &export_values() { detail::enum_export(m_ptr); return *this; }
template <typename Func, typename... Extra>
NB_INLINE enum_ &def(const char *name_, Func &&f, const Extra &... extra) {
cpp_function_def<T>((detail::forward_t<Func>) f, scope(*this),
name(name_), is_method(), extra...);
return *this;
}
template <typename Func, typename... Extra>
NB_INLINE enum_ &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 enum_ &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<T>((detail::forward_t<Getter>) getter,
is_method(), is_getter(),
rv_policy::reference_internal,
detail::filter_getter(extra)...);
if constexpr (!std::is_same_v<Setter, std::nullptr_t>)
set_p = cpp_function<T>((detail::forward_t<Setter>) setter,
is_method(), detail::filter_setter(extra)...);
detail::property_install(m_ptr, name_, get_p.ptr(), set_p.ptr());
return *this;
}
template <typename Getter, typename... Extra>
NB_INLINE enum_ &def_prop_ro(const char *name_, Getter &&getter,
const Extra &...extra) {
return def_prop_rw(name_, getter, nullptr, extra...);
}
};
template <typename Source, typename Target> void implicitly_convertible() {
using Caster = detail::make_caster<Source>;
static_assert(!std::is_enum_v<Target>, "implicitly_convertible(): 'Target' cannot be an enumeration.");
if constexpr (detail::is_base_caster_v<Caster>) {
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));
}
}
NAMESPACE_END(NB_NAMESPACE)