-
Notifications
You must be signed in to change notification settings - Fork 202
/
implicit.cpp
75 lines (61 loc) · 2.48 KB
/
implicit.cpp
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
/*
src/implicit.cpp: functions for registering implicit conversions
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.
*/
#include <nanobind/trampoline.h>
#include "nb_internals.h"
NAMESPACE_BEGIN(NB_NAMESPACE)
NAMESPACE_BEGIN(detail)
void implicitly_convertible(const std::type_info *src,
const std::type_info *dst) noexcept {
nb_internals *internals_ = internals;
type_data *t = nb_type_c2p(internals_, dst);
check(t, "nanobind::detail::implicitly_convertible(src=%s, dst=%s): "
"destination type unknown!", type_name(src), type_name(dst));
lock_internals guard(internals_);
size_t size = 0;
if (t->flags & (uint32_t) type_flags::has_implicit_conversions) {
while (t->implicit.cpp && t->implicit.cpp[size])
size++;
} else {
t->implicit.cpp = nullptr;
t->implicit.py = nullptr;
t->flags |= (uint32_t) type_flags::has_implicit_conversions;
}
void **data = (void **) PyMem_Malloc(sizeof(void *) * (size + 2));
if (size)
memcpy(data, t->implicit.cpp, size * sizeof(void *));
data[size] = (void *) src;
data[size + 1] = nullptr;
PyMem_Free(t->implicit.cpp);
t->implicit.cpp = (decltype(t->implicit.cpp)) data;
}
void implicitly_convertible(bool (*predicate)(PyTypeObject *, PyObject *,
cleanup_list *),
const std::type_info *dst) noexcept {
nb_internals *internals_ = internals;
type_data *t = nb_type_c2p(internals_, dst);
check(t, "nanobind::detail::implicitly_convertible(src=<predicate>, dst=%s): "
"destination type unknown!", type_name(dst));
lock_internals guard(internals_);
size_t size = 0;
if (t->flags & (uint32_t) type_flags::has_implicit_conversions) {
while (t->implicit.py && t->implicit.py[size])
size++;
} else {
t->implicit.cpp = nullptr;
t->implicit.py = nullptr;
t->flags |= (uint32_t) type_flags::has_implicit_conversions;
}
void **data = (void **) PyMem_Malloc(sizeof(void *) * (size + 2));
if (size)
memcpy(data, t->implicit.py, size * sizeof(void *));
data[size] = (void *) predicate;
data[size + 1] = nullptr;
PyMem_Free(t->implicit.py);
t->implicit.py = (decltype(t->implicit.py)) data;
}
NAMESPACE_END(detail)
NAMESPACE_END(NB_NAMESPACE)