From e46b4630dae53cae4d8db6b668c4753350178eed Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Wed, 27 Sep 2017 12:14:35 +0200 Subject: [PATCH] feat(c-wrapper): deal with shared from this --- src/c-wrapper/internal/c-tools.h | 52 +++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/src/c-wrapper/internal/c-tools.h b/src/c-wrapper/internal/c-tools.h index 736187980..23aed93d0 100644 --- a/src/c-wrapper/internal/c-tools.h +++ b/src/c-wrapper/internal/c-tools.h @@ -32,6 +32,12 @@ // 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 @@ -45,7 +51,9 @@ struct CppTypeMetaInfo { template struct CTypeMetaInfo { - enum { defined = false }; + enum { + defined = false + }; typedef void cppType; }; @@ -94,6 +102,15 @@ private: CType *cppPtr; }; + // --------------------------------------------------------------------------- + // Runtime checker. + // --------------------------------------------------------------------------- + + static inline void fatal (const char *message) { + std::cout << "[FATAL C-WRAPPER]" << message << std::endl; + exit(1); + } + public: // --------------------------------------------------------------------------- // Get private data of cpp Object. @@ -103,7 +120,7 @@ public: typename CppType, typename = typename std::enable_if::value, CppType>::type > - static constexpr decltype (std::declval().getPrivate()) getPrivate (CppType *cppObject) { + static constexpr decltype(std::declval().getPrivate()) getPrivate (CppType *cppObject) { return cppObject->getPrivate(); } @@ -190,7 +207,7 @@ public: Variant variant = cppObject->getProperty("LinphonePrivate::Wrapper::cBackPtr"); void *value = variant.getValue(); if (value) - return reinterpret_cast(value); + return static_cast(value); RetType *cObject = CppTypeMetaInfo::init(); setCppPtrFromC(cObject, cppObject); @@ -202,7 +219,14 @@ public: typename = typename std::enable_if::value, CppType>::type > static inline typename CppTypeMetaInfo::cType *getCBackPtr (CppType *cppObject) { - return getCBackPtr(std::static_pointer_cast(cppObject->shared_from_this())); + try { + return getCBackPtr(std::static_pointer_cast(cppObject->shared_from_this())); + } catch (const std::bad_weak_ptr &e) { + fatal(e.what()); + } + + L_ASSERT(false); + return nullptr; } template< @@ -212,10 +236,10 @@ public: static inline typename CppTypeMetaInfo::cType *getCBackPtr (const CppType *cppObject) { typedef typename CppTypeMetaInfo::cType RetType; - Variant v = cppObject->getProperty("LinphonePrivate::Wrapper::cBackPtr"); - void *value = v.getValue(); + Variant variant = cppObject->getProperty("LinphonePrivate::Wrapper::cBackPtr"); + void *value = variant.getValue(); if (value) - return reinterpret_cast(value); + return static_cast(value); RetType *cObject = CppTypeMetaInfo::init(); setCppPtrFromC(cObject, cppObject); @@ -241,7 +265,7 @@ public: // --------------------------------------------------------------------------- template - static inline bctbx_list_t *getCListFromCppList (const std::list &cppList) { + static inline bctbx_list_t *getCListFromCppList (const std::list &cppList) { bctbx_list_t *result = nullptr; for (const auto &value : cppList) result = bctbx_list_append(result, value); @@ -249,7 +273,7 @@ public: } template - static inline std::list getCppListFromCList (const bctbx_list_t *cList) { + static inline std::list getCppListFromCList (const bctbx_list_t *cList) { std::list result; for (auto it = cList; it; it = bctbx_list_next(it)) result.push_back(static_cast(bctbx_list_get_data(it))); @@ -290,7 +314,7 @@ public: static inline std::list> getResolvedCppListFromCList (const bctbx_list_t *cList) { std::list> result; for (auto it = cList; it; it = bctbx_list_next(it)) - result.push_back(getCppPtrFromC(reinterpret_cast(bctbx_list_get_data(it)))); + result.push_back(getCppPtrFromC(static_cast(bctbx_list_get_data(it)))); return result; } @@ -302,7 +326,7 @@ public: static inline std::list getResolvedCppListFromCList (const bctbx_list_t *cList) { std::list result; for (auto it = cList; it; it = bctbx_list_next(it)) - result.push_back(*getCppPtrFromC(reinterpret_cast(bctbx_list_get_data(it)))); + result.push_back(*getCppPtrFromC(static_cast(bctbx_list_get_data(it)))); return result; } @@ -318,7 +342,7 @@ LINPHONE_END_NAMESPACE #define L_INTERNAL_DECLARE_C_OBJECT_FUNCTIONS(C_TYPE, CONSTRUCTOR, DESTRUCTOR) \ BELLE_SIP_DECLARE_VPTR_NO_EXPORT(Linphone ## C_TYPE); \ - Linphone ## C_TYPE *_linphone_ ## C_TYPE ## _init() { \ + Linphone ## C_TYPE *_linphone_ ## C_TYPE ## _init () { \ Linphone ## C_TYPE * object = belle_sip_object_new(Linphone ## C_TYPE); \ new(&object->cppPtr) std::shared_ptr(); \ CONSTRUCTOR(object); \ @@ -443,7 +467,7 @@ LINPHONE_END_NAMESPACE #define L_DECLARE_C_OBJECT_NEW_DEFAULT(C_TYPE, C_NAME) \ Linphone ## C_TYPE * linphone_ ## C_NAME ## _new() { \ - Linphone ## C_TYPE * object = _linphone_ ## C_TYPE ## _init(); \ + Linphone ## C_TYPE *object = _linphone_ ## C_TYPE ## _init(); \ object->cppPtr = std::make_shared(); \ return object; \ } @@ -457,7 +481,7 @@ LINPHONE_END_NAMESPACE #define L_C_TO_STRING(STR) ((STR) == NULL ? std::string() : (STR)) // Call the init function of wrapped C object. -#define L_INIT(C_TYPE) _linphone_ ## C_TYPE ## _init () +#define L_INIT(C_TYPE) _linphone_ ## C_TYPE ## _init() // Get/set the cpp-ptr of a wrapped C object. #define L_GET_CPP_PTR_FROM_C_OBJECT_1_ARGS(C_OBJECT) \