File tree 2 files changed +49
-31
lines changed
2 files changed +49
-31
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ nanobind/stl/optional.h: type caster for std::optional<...>
3
+
4
+ Copyright (c) 2022 Yoshiki Matsuda and Wenzel Jakob
5
+
6
+ All rights reserved. Use of this source code is governed by a
7
+ BSD-style license that can be found in the LICENSE file.
8
+ */
9
+
10
+ #pragma once
11
+
12
+ #include < nanobind/nanobind.h>
13
+
14
+ NAMESPACE_BEGIN (NB_NAMESPACE)
15
+ NAMESPACE_BEGIN(detail)
16
+
17
+ template <typename Optional, typename T = typename Optional::value_type>
18
+ struct optional_caster {
19
+ using Caster = make_caster<T>;
20
+
21
+ NB_TYPE_CASTER (Optional, optional_name (Caster::Name))
22
+
23
+ bool from_python (handle src, uint8_t flags, cleanup_list* cleanup) noexcept {
24
+ if (src.is_none ())
25
+ // default-constructed value is already empty
26
+ return true ;
27
+
28
+ Caster caster;
29
+ if (!caster.from_python (src, flags_for_local_caster<T>(flags), cleanup) ||
30
+ !caster.template can_cast <T>())
31
+ return false ;
32
+
33
+ value.emplace (caster.operator cast_t <T>());
34
+
35
+ return true ;
36
+ }
37
+
38
+ template <typename T_>
39
+ static handle from_cpp (T_ &&value, rv_policy policy, cleanup_list *cleanup) noexcept {
40
+ if (!value)
41
+ return none ().release ();
42
+
43
+ return Caster::from_cpp (forward_like_<T_>(*value), policy, cleanup);
44
+ }
45
+ };
46
+
47
+ NAMESPACE_END (detail)
48
+ NAMESPACE_END(NB_NAMESPACE)
Original file line number Diff line number Diff line change 9
9
10
10
#pragma once
11
11
12
- #include < nanobind/nanobind.h >
12
+ #include " detail/nb_optional.h "
13
13
#include < optional>
14
14
15
15
NAMESPACE_BEGIN (NB_NAMESPACE)
@@ -18,36 +18,6 @@ NAMESPACE_BEGIN(detail)
18
18
template <typename T> struct remove_opt_mono<std::optional<T>>
19
19
: remove_opt_mono<T> { };
20
20
21
- template <typename Optional, typename T = typename Optional::value_type>
22
- struct optional_caster {
23
- using Caster = make_caster<T>;
24
-
25
- NB_TYPE_CASTER (Optional, optional_name(Caster::Name))
26
-
27
- bool from_python (handle src, uint8_t flags, cleanup_list* cleanup) noexcept {
28
- if (src.is_none ())
29
- // default-constructed value is already empty
30
- return true ;
31
-
32
- Caster caster;
33
- if (!caster.from_python (src, flags_for_local_caster<T>(flags), cleanup) ||
34
- !caster.template can_cast <T>())
35
- return false ;
36
-
37
- value.emplace (caster.operator cast_t <T>());
38
-
39
- return true ;
40
- }
41
-
42
- template <typename T_>
43
- static handle from_cpp (T_ &&value, rv_policy policy, cleanup_list *cleanup) noexcept {
44
- if (!value)
45
- return none ().release ();
46
-
47
- return Caster::from_cpp (forward_like_<T_>(*value), policy, cleanup);
48
- }
49
- };
50
-
51
21
template <typename T>
52
22
struct type_caster <std::optional<T>> : optional_caster<std::optional<T>> {};
53
23
You can’t perform that action at this time.
0 commit comments