-
Notifications
You must be signed in to change notification settings - Fork 153
/
nb_set.h
76 lines (55 loc) · 1.88 KB
/
nb_set.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
/*
nanobind/stl/detail/nb_set.h: base class of set casters
Copyright (c) 2022 Raymond Yun Fei and 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.
*/
#pragma once
#include <nanobind/nanobind.h>
NAMESPACE_BEGIN(NB_NAMESPACE)
NAMESPACE_BEGIN(detail)
template <typename Value_, typename Key> struct set_caster {
NB_TYPE_CASTER(Value_, const_name(NB_TYPING_SET "[") + make_caster<Key>::Name + const_name("]"));
using KeyCaster = make_caster<Key>;
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
value.clear();
PyObject* iter = obj_iter(src.ptr());
if (iter == nullptr) {
PyErr_Clear();
return false;
}
bool success = true;
KeyCaster key_caster;
PyObject *key;
while ((key = PyIter_Next(iter)) != nullptr) {
success &= key_caster.from_python(key, flags, cleanup);
Py_DECREF(key);
if (!success)
break;
value.emplace(key_caster.operator cast_t<Key>());
}
if (PyErr_Occurred()) {
PyErr_Clear();
success = false;
}
Py_DECREF(iter);
return success;
}
template <typename T>
static handle from_cpp(T &&src, rv_policy policy, cleanup_list *cleanup) {
object ret = steal(PySet_New(nullptr));
if (ret.is_valid()) {
for (auto& key : src) {
object k = steal(
KeyCaster::from_cpp(forward_like<T>(key), policy, cleanup));
if (!k.is_valid() || PySet_Add(ret.ptr(), k.ptr()) != 0) {
ret.reset();
break;
}
}
}
return ret.release();
}
};
NAMESPACE_END(detail)
NAMESPACE_END(NB_NAMESPACE)