xref: /aosp_15_r20/external/pytorch/torch/csrc/profiler/python/pybind.h (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
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::detail {
10 // Strong typedefs don't make much sense in Python since everything is duck
11 // typed. So instead we simply extract the underlying value and let the caller
12 // handle correctness.
13 template <typename T>
14 struct strong_pointer_type_caster {
15   template <typename T_>
caststrong_pointer_type_caster16   static handle cast(
17       T_&& src,
18       return_value_policy /*policy*/,
19       handle /*parent*/) {
20     const auto* ptr = reinterpret_cast<const void*>(src.value_of());
21     return ptr ? handle(THPUtils_packUInt64(reinterpret_cast<intptr_t>(ptr)))
22                : none();
23   }
24 
loadstrong_pointer_type_caster25   bool load(handle /*src*/, bool /*convert*/) {
26     return false;
27   }
28 
29   PYBIND11_TYPE_CASTER(T, _("strong_pointer"));
30 };
31 
32 template <typename T>
33 struct strong_uint_type_caster {
34   template <typename T_>
caststrong_uint_type_caster35   static handle cast(
36       T_&& src,
37       return_value_policy /*policy*/,
38       handle /*parent*/) {
39     return handle(THPUtils_packUInt64(src.value_of()));
40   }
41 
loadstrong_uint_type_caster42   bool load(handle /*src*/, bool /*convert*/) {
43     return false;
44   }
45 
46   PYBIND11_TYPE_CASTER(T, _("strong_uint"));
47 };
48 } // namespace pybind11::detail
49