2
2
#pragma once
3
3
4
4
#include < pybind11/pybind11.h>
5
+ #include < pybind11/stl.h>
5
6
6
7
#include < wpi/StringMap.h>
7
8
@@ -10,60 +11,9 @@ namespace pybind11
10
11
namespace detail
11
12
{
12
13
13
-
14
14
template <typename Value>
15
- struct type_caster <wpi::StringMap<Value>> {
16
- using Type = wpi::StringMap<Value>;
17
- using Key = std::string_view;
18
-
19
- using key_conv = make_caster<Key>;
20
- using value_conv = make_caster<Value>;
21
-
22
- bool load (handle src, bool convert) {
23
- if (!isinstance<dict>(src)) {
24
- return false ;
25
- }
26
- auto d = reinterpret_borrow<dict>(src);
27
- value.clear ();
28
- for (auto it : d) {
29
- key_conv kconv;
30
- value_conv vconv;
31
- if (!kconv.load (it.first .ptr (), convert) || !vconv.load (it.second .ptr (), convert)) {
32
- return false ;
33
- }
34
- // this is different from pybind11/stl.h map_caster
35
- value.try_emplace (cast_op<Key &&>(std::move (kconv)), cast_op<Value &&>(std::move (vconv)));
36
- }
37
- return true ;
38
- }
39
-
40
- template <typename T>
41
- static handle cast (T &&src, return_value_policy policy, handle parent) {
42
- dict d;
43
- return_value_policy policy_key = policy;
44
- return_value_policy policy_value = policy;
45
- if (!std::is_lvalue_reference<T>::value) {
46
- policy_key = return_value_policy_override<Key>::policy (policy_key);
47
- policy_value = return_value_policy_override<Value>::policy (policy_value);
48
- }
49
- for (auto &&kv : src) {
50
- // getKey/getValue is different from pybind11/stl.h map_caster
51
- auto key = reinterpret_steal<object>(
52
- key_conv::cast (forward_like<T>(kv.getKey ()), policy_key, parent));
53
- auto value = reinterpret_steal<object>(
54
- value_conv::cast (forward_like<T>(kv.getValue ()), policy_value, parent));
55
- if (!key || !value) {
56
- return handle ();
57
- }
58
- d[std::move (key)] = std::move (value);
59
- }
60
- return d.release ();
61
- }
62
-
63
- PYBIND11_TYPE_CASTER (Type,
64
- const_name (" Dict[" ) + key_conv::name + const_name(" , " ) + value_conv::name
65
- + const_name(" ]" ));
66
- };
15
+ struct type_caster <wpi::StringMap<Value>>
16
+ : map_caster<wpi::StringMap<Value>, std::string, Value> { };
67
17
68
18
} // namespace detail
69
19
} // namespace pybind11
0 commit comments