-
Notifications
You must be signed in to change notification settings - Fork 153
/
nb_cast.h
510 lines (427 loc) · 17.5 KB
/
nb_cast.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
/*
nanobind/nb_cast.h: Type caster interface and essential type casters
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.
*/
#define NB_TYPE_CASTER(Value_, descr) \
using Value = Value_; \
static constexpr auto Name = descr; \
template <typename T_> using Cast = movable_cast_t<T_>; \
static handle from_cpp(Value *p, rv_policy policy, cleanup_list *list) { \
if (!p) \
return none().release(); \
return from_cpp(*p, policy, list); \
} \
explicit operator Value*() { return &value; } \
explicit operator Value&() { return (Value &) value; } \
explicit operator Value&&() { return (Value &&) value; } \
Value value;
#define NB_MAKE_OPAQUE(...) \
namespace nanobind::detail { \
template <> class type_caster<__VA_ARGS__> \
: public type_caster_base<__VA_ARGS__> { }; }
NAMESPACE_BEGIN(NB_NAMESPACE)
NAMESPACE_BEGIN(detail)
enum cast_flags : uint8_t {
// Enable implicit conversions (impl. assumes this is 1, don't reorder..)
convert = (1 << 0),
// Passed to the 'self' argument in a constructor call (__init__)
construct = (1 << 1)
};
/**
* Type casters expose a member 'Cast<T>' which users of a type caster must
* query to determine what the caster actually can (and prefers) to produce.
* The convenience alias ``cast_t<T>`` defined below performs this query for a
* given type ``T``.
*
* Often ``cast_t<T>`` is simply equal to ``T`` or ``T&``. More significant
* deviations are also possible, which could be due to one of the following
* two reasons:
*
* 1. Efficiency: most STL type casters create a local copy (``value`` member)
* of the value being cast. The caller should move this value to its
* intended destination instead of making further copies along the way.
* Consequently, ``cast_t<std::vector<T>>`` yields ``cast_t<std::vector<T>>
* &&`` to enable such behavior.
*
* 2. STL pairs may contain references, and such pairs aren't
* default-constructible. The STL pair caster therefore cannot create a local
* copy and must construct the pair on the fly, which in turns means that it
* cannot return references. Therefore, ``cast_t<const std::pair<T1, T2>&>``
* yields ``std::pair<T1, T2>``.
*/
/// Ask a type caster what flavors of a type it can actually produce -- may be different from 'T'
template <typename T> using cast_t = typename make_caster<T>::template Cast<T>;
/// This is a default choice for the 'Cast' type alias described above. It
/// prefers to return rvalue references to allow the caller to move the object.
template <typename T>
using movable_cast_t =
std::conditional_t<is_pointer_v<T>, intrinsic_t<T> *,
std::conditional_t<std::is_lvalue_reference_v<T>,
intrinsic_t<T> &, intrinsic_t<T> &&>>;
/// This version is more careful about what the caller actually requested and
/// only moves when this was explicitly requested. It is the default for the
/// base type caster (i.e., types bound via ``nanobind::class_<..>``)
template <typename T>
using precise_cast_t =
std::conditional_t<is_pointer_v<T>, intrinsic_t<T> *,
std::conditional_t<std::is_rvalue_reference_v<T>,
intrinsic_t<T> &&, intrinsic_t<T> &>>;
template <typename T>
struct type_caster<T, enable_if_t<std::is_arithmetic_v<T> && !is_std_char_v<T>>> {
public:
NB_INLINE bool from_python(handle src, uint8_t flags, cleanup_list *) noexcept {
if constexpr (std::is_floating_point_v<T>) {
if constexpr (sizeof(T) == 8)
return detail::load_f64(src.ptr(), flags, &value);
else
return detail::load_f32(src.ptr(), flags, &value);
} else {
if constexpr (std::is_signed_v<T>) {
if constexpr (sizeof(T) == 8)
return detail::load_i64(src.ptr(), flags, (int64_t *) &value);
else if constexpr (sizeof(T) == 4)
return detail::load_i32(src.ptr(), flags, (int32_t *) &value);
else if constexpr (sizeof(T) == 2)
return detail::load_i16(src.ptr(), flags, (int16_t *) &value);
else
return detail::load_i8(src.ptr(), flags, (int8_t *) &value);
} else {
if constexpr (sizeof(T) == 8)
return detail::load_u64(src.ptr(), flags, (uint64_t *) &value);
else if constexpr (sizeof(T) == 4)
return detail::load_u32(src.ptr(), flags, (uint32_t *) &value);
else if constexpr (sizeof(T) == 2)
return detail::load_u16(src.ptr(), flags, (uint16_t *) &value);
else
return detail::load_u8(src.ptr(), flags, (uint8_t *) &value);
}
}
}
NB_INLINE static handle from_cpp(T src, rv_policy, cleanup_list *) noexcept {
if constexpr (std::is_floating_point_v<T>) {
return PyFloat_FromDouble((double) src);
} else {
if constexpr (std::is_signed_v<T>) {
if constexpr (sizeof(T) <= sizeof(long))
return PyLong_FromLong((long) src);
else
return PyLong_FromLongLong((long long) src);
} else {
if constexpr (sizeof(T) <= sizeof(unsigned long))
return PyLong_FromUnsignedLong((unsigned long) src);
else
return PyLong_FromUnsignedLongLong((unsigned long long) src);
}
}
}
NB_TYPE_CASTER(T, const_name<std::is_integral_v<T>>("int", "float"));
};
template <> struct type_caster<void_type> {
static constexpr auto Name = const_name("None");
};
template <> struct type_caster<void> {
template <typename T_> using Cast = void *;
using Value = void*;
static constexpr auto Name = const_name("capsule");
explicit operator void *() { return value; }
Value value;
bool from_python(handle src, uint8_t, cleanup_list *) noexcept {
if (src.is_none()) {
value = nullptr;
return true;
} else {
value = PyCapsule_GetPointer(src.ptr(), "nb_handle");
if (!value) {
PyErr_Clear();
return false;
}
return true;
}
}
static handle from_cpp(void *ptr, rv_policy, cleanup_list *) noexcept {
if (ptr)
return PyCapsule_New(ptr, "nb_handle", nullptr);
else
return none().release();
}
};
template <> struct type_caster<std::nullptr_t> {
bool from_python(handle src, uint8_t, cleanup_list *) noexcept {
if (src.is_none())
return true;
return false;
}
static handle from_cpp(std::nullptr_t, rv_policy, cleanup_list *) noexcept {
return none().release();
}
NB_TYPE_CASTER(std::nullptr_t, const_name("None"));
};
template <> struct type_caster<bool> {
bool from_python(handle src, uint8_t, cleanup_list *) noexcept {
if (src.ptr() == Py_True) {
value = true;
return true;
} else if (src.ptr() == Py_False) {
value = false;
return true;
} else {
return false;
}
}
static handle from_cpp(bool src, rv_policy, cleanup_list *) noexcept {
return handle(src ? Py_True : Py_False).inc_ref();
}
NB_TYPE_CASTER(bool, const_name("bool"));
};
template <> struct type_caster<char> {
using Value = const char *;
Value value;
static constexpr auto Name = const_name("str");
template <typename T_>
using Cast = std::conditional_t<is_pointer_v<T_>, const char *, char>;
bool from_python(handle src, uint8_t, cleanup_list *) noexcept {
value = PyUnicode_AsUTF8AndSize(src.ptr(), nullptr);
if (!value) {
PyErr_Clear();
return false;
}
return true;
}
static handle from_cpp(const char *value, rv_policy,
cleanup_list *) noexcept {
return PyUnicode_FromString(value);
}
static handle from_cpp(char value, rv_policy, cleanup_list *) noexcept {
return PyUnicode_FromStringAndSize(&value, 1);
}
explicit operator const char *() { return value; }
explicit operator char() {
if (value && value[0] && value[1] == '\0')
return value[0];
else
throw next_overload();
}
};
template <typename T> struct type_caster<pointer_and_handle<T>> {
using Caster = detail::make_caster<T>;
using T2 = pointer_and_handle<T>;
NB_TYPE_CASTER(T2, Caster::Name)
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
Caster c;
if (!c.from_python(src, flags, cleanup))
return false;
value.h = src;
value.p = c.operator T*();
return true;
}
};
template <typename T, typename X> struct type_caster<typed<T, X>> {
using Caster = detail::make_caster<T>;
using T2 = typed<T, X>;
NB_TYPE_CASTER(T2, X::Name)
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
Caster c;
if (!c.from_python(src, flags, cleanup))
return false;
value = T2{ (T &&) c.value };
return true;
}
static handle from_cpp(const T2 &src, rv_policy policy,
cleanup_list *cleanup) noexcept {
return Caster::from_cpp(src.value, policy, cleanup);
}
};
template <typename T>
struct type_caster<T, enable_if_t<std::is_base_of_v<detail::api_tag, T>>> {
public:
NB_TYPE_CASTER(T, T::Name)
type_caster() : value(nullptr, ::nanobind::detail::steal_t()) { }
bool from_python(handle src, uint8_t, cleanup_list *) noexcept {
if (!isinstance<T>(src))
return false;
if constexpr (std::is_base_of_v<object, T>)
value = borrow<T>(src);
else
value = src;
return true;
}
static handle from_cpp(const handle &src, rv_policy,
cleanup_list *) noexcept {
return src.inc_ref();
}
};
template <typename T> NB_INLINE rv_policy infer_policy(rv_policy policy) {
if constexpr (is_pointer_v<T>) {
if (policy == rv_policy::automatic)
policy = rv_policy::take_ownership;
else if (policy == rv_policy::automatic_reference)
policy = rv_policy::reference;
} else if constexpr (std::is_lvalue_reference_v<T>) {
if (policy == rv_policy::automatic ||
policy == rv_policy::automatic_reference)
policy = rv_policy::copy;
} else {
if (policy == rv_policy::automatic ||
policy == rv_policy::automatic_reference ||
policy == rv_policy::reference ||
policy == rv_policy::reference_internal)
policy = rv_policy::move;
}
return policy;
}
template <typename T, typename SFINAE = int> struct type_hook : std::false_type { };
template <typename Type_> struct type_caster_base : type_caster_base_tag {
using Type = Type_;
static constexpr auto Name = const_name<Type>();
template <typename T> using Cast = precise_cast_t<T>;
NB_INLINE bool from_python(handle src, uint8_t flags,
cleanup_list *cleanup) noexcept {
return nb_type_get(&typeid(Type), src.ptr(), flags, cleanup,
(void **) &value);
}
template <typename T>
NB_INLINE static handle from_cpp(T &&value, rv_policy policy,
cleanup_list *cleanup) noexcept {
Type *ptr;
if constexpr (is_pointer_v<T>)
ptr = (Type *) value;
else
ptr = (Type *) &value;
policy = infer_policy<T>(policy);
const std::type_info *type = &typeid(Type);
constexpr bool has_type_hook =
!std::is_base_of_v<std::false_type, type_hook<Type>>;
if constexpr (has_type_hook)
type = type_hook<Type>::get(ptr);
if constexpr (!std::is_polymorphic_v<Type>) {
return nb_type_put(type, ptr, policy, cleanup);
} else {
const std::type_info *type_p =
(!has_type_hook && ptr) ? &typeid(*ptr) : nullptr;
return nb_type_put_p(type, type_p, ptr, policy, cleanup);
}
}
operator Type*() { return value; }
operator Type&() {
raise_next_overload_if_null(value);
return *value;
}
operator Type&&() {
raise_next_overload_if_null(value);
return (Type &&) *value;
}
private:
Type *value;
};
template <typename Type, typename SFINAE>
struct type_caster : type_caster_base<Type> { };
NAMESPACE_END(detail)
template <typename T, typename Derived>
bool try_cast(const detail::api<Derived> &value, T &out, bool convert = true) noexcept {
using Caster = detail::make_caster<T>;
static_assert(!std::is_same_v<const char *, T>,
"nanobind::try_cast(): cannot return a reference to a temporary.");
Caster caster;
if (caster.from_python(value.derived().ptr(),
convert ? (uint8_t) detail::cast_flags::convert
: (uint8_t) 0, nullptr)) {
out = caster.operator detail::cast_t<T>();
return true;
}
return false;
}
template <typename T, typename Derived>
T cast(const detail::api<Derived> &value, bool convert = true) {
if constexpr (std::is_same_v<T, void>) {
return;
} else {
using Caster = detail::make_caster<T>;
static_assert(
!(std::is_reference_v<T> || std::is_pointer_v<T>) ||
detail::is_base_caster_v<Caster> ||
std::is_same_v<const char *, T>,
"nanobind::cast(): cannot return a reference to a temporary.");
Caster caster;
if (!caster.from_python(value.derived().ptr(),
convert ? (uint8_t) detail::cast_flags::convert
: (uint8_t) 0, nullptr))
detail::raise_cast_error();
// GCC misses that from_python will return or ensure orderly initialization
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
return caster.operator detail::cast_t<T>();
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
}
}
template <typename T>
object cast(T &&value, rv_policy policy = rv_policy::automatic_reference) {
handle h = detail::make_caster<T>::from_cpp((detail::forward_t<T>) value,
policy, nullptr);
if (!h.is_valid())
detail::raise_cast_error();
return steal(h);
}
template <typename T> object find(const T &value) noexcept {
return steal(detail::make_caster<T>::from_cpp(value, rv_policy::none, nullptr));
}
template <rv_policy policy = rv_policy::automatic, typename... Args>
tuple make_tuple(Args &&...args) {
tuple result = steal<tuple>(PyTuple_New((Py_ssize_t) sizeof...(Args)));
size_t nargs = 0;
PyObject *o = result.ptr();
(NB_TUPLE_SET_ITEM(o, nargs++,
detail::make_caster<Args>::from_cpp(
(detail::forward_t<Args>) args, policy, nullptr)
.ptr()),
...);
detail::tuple_check(o, sizeof...(Args));
return result;
}
template <typename T> arg_v arg::operator=(T &&value) const {
return arg_v(*this, cast((detail::forward_t<T>) value));
}
template <typename Impl> template <typename T>
detail::accessor<Impl>& detail::accessor<Impl>::operator=(T &&value) {
object result = cast((detail::forward_t<T>) value);
Impl::set(m_base, m_key, result.ptr());
return *this;
}
template <typename T> void list::append(T &&value) {
object o = nanobind::cast((detail::forward_t<T>) value);
if (PyList_Append(m_ptr, o.ptr()))
detail::raise_python_error();
}
template <typename T> bool dict::contains(T&& key) const {
object o = nanobind::cast((detail::forward_t<T>) key);
int rv = PyDict_Contains(m_ptr, o.ptr());
if (rv == -1)
detail::raise_python_error();
return rv == 1;
}
template <typename T> bool set::contains(T&& key) const {
object o = nanobind::cast((detail::forward_t<T>) key);
int rv = PySet_Contains(m_ptr, o.ptr());
if (rv == -1)
detail::raise_python_error();
return rv == 1;
}
template <typename T> void set::add(T&& key) {
object o = nanobind::cast((detail::forward_t<T>) key);
int rv = PySet_Add(m_ptr, o.ptr());
if (rv == -1)
detail::raise_python_error();
}
template <typename T> bool mapping::contains(T&& key) const {
object o = nanobind::cast((detail::forward_t<T>) key);
int rv = PyMapping_HasKey(m_ptr, o.ptr());
if (rv == -1)
detail::raise_python_error();
return rv == 1;
}
NAMESPACE_END(NB_NAMESPACE)