From 300058b0630ede9b92dbfc69596e41b98cdd77d1 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 13 Mar 2023 16:36:48 +0800 Subject: [PATCH] Update obj.h --- src/obj.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/obj.h b/src/obj.h index ebb65d93..e37dc1c0 100644 --- a/src/obj.h +++ b/src/obj.h @@ -181,28 +181,31 @@ void _check_py_class(VM* vm, const PyVar& var); template T py_pointer_cast(VM* vm, const PyVar& var); +struct Discarded {}; + template __T py_cast(VM* vm, const PyVar& obj) { + using T = std::decay_t<__T>; 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){ + }else if constexpr(is_py_class::value){ _check_py_class(vm, obj); return OBJ_GET(T, obj); + }else{ + return Discarded(); } - throw std::runtime_error("bad py_cast() call"); } + template __T _py_cast(VM* vm, const PyVar& obj) { + using T = std::decay_t<__T>; 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){ + }else if constexpr(is_py_class::value){ return OBJ_GET(T, obj); + }else{ + return Discarded(); } - throw std::runtime_error("bad py_cast() call"); } #define VAR(x) py_var(vm, x)