From 44afa5b023619ae26e6633d6eeb0103e2807e4f7 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 13 Mar 2023 16:09:22 +0800 Subject: [PATCH] up --- src/cffi.h | 4 ++-- src/obj.h | 21 +++++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/cffi.h b/src/cffi.h index 0cffcd99..444e7577 100644 --- a/src/cffi.h +++ b/src/cffi.h @@ -431,8 +431,8 @@ struct pointer { }; template -std::enable_if_t, T> -py_cast(VM* vm, const PyVar& var){ +T py_pointer_cast(VM* vm, const PyVar& var){ + static_assert(std::is_pointer_v); Pointer& p = CAST(Pointer&, var); const TypeInfo* type = _type_db.get::baseT>(); const int level = pointer::level; diff --git a/src/obj.h b/src/obj.h index 3a73226c..ebb65d93 100644 --- a/src/obj.h +++ b/src/obj.h @@ -178,26 +178,31 @@ template struct is_py_class> : s template void _check_py_class(VM* vm, const PyVar& var); +template +T py_pointer_cast(VM* vm, const PyVar& var); + template -std::enable_if_t, __T> -py_cast(VM* vm, const PyVar& obj) { +__T py_cast(VM* vm, const PyVar& obj) { + if constexpr(std::is_pointer_v<__T>){ + return py_pointer_cast<__T>(vm, obj); + } using T = std::decay_t<__T>; if constexpr(is_py_class::value){ _check_py_class(vm, obj); return OBJ_GET(T, obj); - }else{ - throw std::runtime_error("bad py_cast() call"); } + throw std::runtime_error("bad py_cast() call"); } template -std::enable_if_t, __T> -_py_cast(VM* vm, const PyVar& obj) { +__T _py_cast(VM* vm, const PyVar& obj) { + if constexpr(std::is_pointer_v<__T>){ + return py_pointer_cast<__T>(vm, obj); + } using T = std::decay_t<__T>; if constexpr(is_py_class::value){ return OBJ_GET(T, obj); - }else{ - throw std::runtime_error("bad py_cast() call"); } + throw std::runtime_error("bad py_cast() call"); } #define VAR(x) py_var(vm, x)