From 2f74df954f6e761719a5e6e31c6226af779420a0 Mon Sep 17 00:00:00 2001 From: Ghislain MARY Date: Fri, 15 Sep 2017 15:42:02 +0200 Subject: [PATCH] Improve conversion of lists in c-tools. --- src/c-wrapper/api/c-chat-room.cpp | 2 +- src/c-wrapper/c-tools.h | 50 +++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/c-wrapper/api/c-chat-room.cpp b/src/c-wrapper/api/c-chat-room.cpp index 5540611ce..4d22db9ab 100644 --- a/src/c-wrapper/api/c-chat-room.cpp +++ b/src/c-wrapper/api/c-chat-room.cpp @@ -236,7 +236,7 @@ LinphoneChatRoom * linphone_client_group_chat_room_new(LinphoneCore *lc, const b if (from.empty()) from = linphone_core_get_primary_contact(lc); LinphonePrivate::Address me(from); - std::list l = L_GET_CPP_LIST_OF_CPP_OBJ_FROM_C_LIST_OF_STRUCT_PTR(addresses, Address); + std::list l = L_GET_CPP_LIST_OF_CPP_OBJ_FROM_C_LIST_OF_STRUCT_PTR(addresses, Address, Address); LinphoneChatRoom *cr = _linphone_chat_room_init(); L_SET_CPP_PTR_FROM_C_STRUCT(cr, make_shared(lc, me, l)); return cr; diff --git a/src/c-wrapper/c-tools.h b/src/c-wrapper/c-tools.h index a5300446d..d97057a3a 100644 --- a/src/c-wrapper/c-tools.h +++ b/src/c-wrapper/c-tools.h @@ -68,7 +68,6 @@ public: typename CppType, typename CType, typename = typename std::enable_if::value, CppType>::type - //typename std::enable_if::value, CppType>::type = 0 > static inline std::shared_ptr getCppPtrFromC (CType *object) { L_ASSERT(object); @@ -79,7 +78,6 @@ public: typename CppType, typename CType, typename = typename std::enable_if::value, CppType>::type - //typename std::enable_if::value, CppType>::type = 0 > static inline std::shared_ptr getCppPtrFromC (const CType *object) { L_ASSERT(object); @@ -186,13 +184,29 @@ public: // --------------------------------------------------------------------------- template - static inline bctbx_list_t * getCListFromCppList (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); return result; } + template + static inline bctbx_list_t * getCListOfStructPtrFromCppListOfCppObj (const std::list> cppList, CType *(*cTypeAllocator)()) { + bctbx_list_t *result = nullptr; + for (const auto &value : cppList) + result = bctbx_list_append(result, getCBackPtr(value, cTypeAllocator)); + return result; + } + + template + static inline bctbx_list_t * getCListOfStructPtrFromCppListOfCppObj (const std::list cppList, CType *(*cTypeAllocator)()) { + bctbx_list_t *result = nullptr; + for (const auto &value : cppList) + result = bctbx_list_append(result, getCBackPtr(value, cTypeAllocator)); + return result; + } + template static inline std::list getCppListFromCList (const bctbx_list_t *cList) { std::list result; @@ -201,11 +215,27 @@ public: return result; } - template - static inline std::list getCppListOfCppObjFromCListOfStructPtr (const bctbx_list_t *cList) { - std::list result; + template< + typename CppType, + typename CType, + typename = typename std::enable_if::value, CppType>::type + > + static inline std::list> getCppListOfCppObjFromCListOfStructPtr (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(reinterpret_cast(bctbx_list_get_data(it)))); + return result; + } + + template< + typename CppType, + typename CType, + typename = typename std::enable_if::value, CppType>::type + > + static inline std::list getCppListOfCppObjFromCListOfStructPtr (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)))); return result; } @@ -333,7 +363,9 @@ LINPHONE_END_NAMESPACE LINPHONE_NAMESPACE::Wrapper::getCListFromCppList(LIST) #define L_GET_CPP_LIST_FROM_C_LIST(LIST, TYPE) \ LINPHONE_NAMESPACE::Wrapper::getCppListFromCList(LIST) -#define L_GET_CPP_LIST_OF_CPP_OBJ_FROM_C_LIST_OF_STRUCT_PTR(LIST, TYPE) \ - LINPHONE_NAMESPACE::Wrapper::getCppListOfCppObjFromCListOfStructPtr(LIST) +#define L_GET_C_LIST_OF_STRUCT_PTR_FROM_CPP_LIST_OF_CPP_OBJ(LIST, CPP_TYPE, C_TYPE, C_NAME) \ + LINPHONE_NAMESPACE::Wrapper::getCListOfStructPtrFromCppListOfCppObj(LIST, _linphone_ ## C_NAME ## _init) +#define L_GET_CPP_LIST_OF_CPP_OBJ_FROM_C_LIST_OF_STRUCT_PTR(LIST, CPP_TYPE, C_TYPE) \ + LINPHONE_NAMESPACE::Wrapper::getCppListOfCppObjFromCListOfStructPtr(LIST) #endif // ifndef _C_TOOLS_H_