diff --git a/src/c-wrapper/internal/c-tools.h b/src/c-wrapper/internal/c-tools.h index 23aed93d0..5a71264ec 100644 --- a/src/c-wrapper/internal/c-tools.h +++ b/src/c-wrapper/internal/c-tools.h @@ -32,12 +32,6 @@ // Internal. // ============================================================================= -#ifdef DEBUG - #define CHECK_CPP_OBJECT_AT_RUNTIME(CPP_OBJECT) checkCppObjectAtRuntime(CPP_OBJECT) -#else - #define CHECK_CPP_OBJECT_AT_RUNTIME(CPP_OBJECT) -#endif - LINPHONE_BEGIN_NAMESPACE template @@ -128,13 +122,34 @@ public: // Get c/cpp ptr helpers. // --------------------------------------------------------------------------- + #ifdef DEBUG + #define L_INTERNAL_WRAPPER_CONSTEXPR + #else + #define L_INTERNAL_WRAPPER_CONSTEXPR constexpr + #endif + template< typename CType, typename CppType = typename CTypeMetaInfo::cppType, typename = typename std::enable_if::value, CppType>::type > - static constexpr std::shared_ptr getCppPtrFromC (CType *cObject) { - return reinterpret_cast *>(cObject)->cppPtr; + static L_INTERNAL_WRAPPER_CONSTEXPR std::shared_ptr getCppPtrFromC (CType *cObject) { + #ifdef DEBUG + typedef typename CTypeMetaInfo::cppType BaseType; + typedef CppType DerivedType; + + std::shared_ptr cppObject = reinterpret_cast *>(cObject)->cppPtr; + if (!cppObject) + fatal("Cpp Object is null."); + + std::shared_ptr derivedCppObject = std::static_pointer_cast(cppObject); + if (!derivedCppObject) + fatal("Invalid derived cpp object."); + + return derivedCppObject; + #else + return reinterpret_cast *>(cObject)->cppPtr; + #endif } template< @@ -142,8 +157,12 @@ public: typename CppType = typename CTypeMetaInfo::cppType, typename = typename std::enable_if::value, CppType>::type > - static constexpr std::shared_ptr getCppPtrFromC (const CType *cObject) { - return reinterpret_cast *>(cObject)->cppPtr; + static L_INTERNAL_WRAPPER_CONSTEXPR std::shared_ptr getCppPtrFromC (const CType *cObject) { + #ifdef DEBUG + return getCppPtrFromC(const_cast(cObject)); + #else + return reinterpret_cast *>(cObject)->cppPtr; + #endif } template< @@ -151,8 +170,23 @@ public: typename CppType = typename CTypeMetaInfo::cppType, typename = typename std::enable_if::value, CppType>::type > - static constexpr CppType *getCppPtrFromC (CType *cObject) { - return reinterpret_cast *>(cObject)->cppPtr; + static L_INTERNAL_WRAPPER_CONSTEXPR CppType *getCppPtrFromC (CType *cObject) { + #ifdef DEBUG + typedef typename CTypeMetaInfo::cppType BaseType; + typedef CppType DerivedType; + + BaseType *cppObject = reinterpret_cast *>(cObject)->cppPtr; + if (!cppObject) + fatal("Cpp Object is null."); + + DerivedType *derivedCppObject = dynamic_cast(cppObject); + if (!derivedCppObject) + fatal("Invalid derived cpp object."); + + return derivedCppObject; + #else + return reinterpret_cast *>(cObject)->cppPtr; + #endif } template< @@ -160,10 +194,16 @@ public: typename CppType = typename CTypeMetaInfo::cppType, typename = typename std::enable_if::value, CppType>::type > - static constexpr const CppType *getCppPtrFromC (const CType *cObject) { - return reinterpret_cast *>(cObject)->cppPtr; + static L_INTERNAL_WRAPPER_CONSTEXPR const CppType *getCppPtrFromC (const CType *cObject) { + #ifdef DEBUG + return getCppPtrFromC(const_cast(cObject)); + #else + return reinterpret_cast *>(cObject)->cppPtr; + #endif } + #undef L_INTERNAL_WRAPPER_CONSTEXPR + // --------------------------------------------------------------------------- // Set c/cpp ptr helpers. // ---------------------------------------------------------------------------