1 | #pragma once |
---|---|
2 | |
3 | #include <pybind11/pybind11.h> |
4 | |
5 | #include <c10/util/strong_type.h> |
6 | #include <torch/csrc/utils/pybind.h> |
7 | #include <torch/csrc/utils/python_numbers.h> |
8 | |
9 | namespace pybind11 { |
10 | namespace detail { |
11 | // Strong typedefs don't make much sense in Python since everything is duck |
12 | // typed. So instead we simply extract the underlying value and let the caller |
13 | // handle correctness. |
14 | template <typename T> |
15 | struct strong_pointer_type_caster { |
16 | template <typename T_> |
17 | static handle cast( |
18 | T_&& src, |
19 | return_value_policy /*policy*/, |
20 | handle /*parent*/) { |
21 | const auto* ptr = reinterpret_cast<const void*>(src.value_of()); |
22 | return ptr ? handle(THPUtils_packUInt64(reinterpret_cast<intptr_t>(ptr))) |
23 | : none(); |
24 | } |
25 | |
26 | bool load(handle /*src*/, bool /*convert*/) { |
27 | return false; |
28 | } |
29 | |
30 | PYBIND11_TYPE_CASTER(T, _("strong_pointer")); |
31 | }; |
32 | |
33 | template <typename T> |
34 | struct strong_uint_type_caster { |
35 | template <typename T_> |
36 | static handle cast( |
37 | T_&& src, |
38 | return_value_policy /*policy*/, |
39 | handle /*parent*/) { |
40 | return handle(THPUtils_packUInt64(src.value_of())); |
41 | } |
42 | |
43 | bool load(handle /*src*/, bool /*convert*/) { |
44 | return false; |
45 | } |
46 | |
47 | PYBIND11_TYPE_CASTER(T, _("strong_uint")); |
48 | }; |
49 | } // namespace detail |
50 | } // namespace pybind11 |
51 |