From 289dc072edee7c19e38c597c1389b33d86a8704e Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Wed, 30 Aug 2017 14:12:34 +0200 Subject: [PATCH] feat(c-wrapper): provide a wrapper helper to access private data --- coreapi/chat.c | 34 +++++++++++++++-------------- src/CMakeLists.txt | 1 + src/c-wrapper/wrapper.h | 48 +++++++++++++++++++++++++++++++++++++++++ src/chat/chat-room-p.h | 12 ++++------- src/chat/chat-room.cpp | 8 +++---- src/chat/chat-room.h | 28 ------------------------ src/utils/general.h | 5 ++++- 7 files changed, 79 insertions(+), 57 deletions(-) create mode 100644 src/c-wrapper/wrapper.h diff --git a/coreapi/chat.c b/coreapi/chat.c index 530824512..b0b2ef27d 100644 --- a/coreapi/chat.c +++ b/coreapi/chat.c @@ -22,6 +22,10 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#include +#include +#include + #include "linphone/core.h" #include "private.h" #include "linphone/lpconfig.h" @@ -29,14 +33,12 @@ #include "ortp/b64.h" #include "linphone/wrapper_utils.h" -#include "chat/chat-room.h" +#include "c-wrapper/wrapper.h" #include "chat/chat-room-p.h" +#include "chat/chat-room.h" #include "utils/content-type.h" #include "utils/utils.h" -#include -#include -#include struct _LinphoneChatRoom{ belle_sip_object_t base; @@ -179,7 +181,7 @@ BELLE_SIP_INSTANCIATE_VPTR(LinphoneChatRoom, belle_sip_object_t, LinphoneChatRoom *_linphone_core_create_chat_room_base(LinphoneCore *lc, LinphoneAddress *addr){ LinphoneChatRoom *cr = belle_sip_object_new(LinphoneChatRoom); cr->cr = new LinphonePrivate::ChatRoom(lc, addr); - cr->cr->getPrivate()->setCBackPointer(cr); + L_GET_PRIVATE(cr->cr)->setCBackPointer(cr); return cr; } @@ -260,7 +262,7 @@ void linphone_chat_room_destroy(LinphoneChatRoom *cr) { } void linphone_chat_room_release(LinphoneChatRoom *cr) { - cr->cr->getPrivate()->release(); + L_GET_PRIVATE(cr->cr)->release(); } LinphoneChatRoom *linphone_chat_room_ref(LinphoneChatRoom *cr) { @@ -281,7 +283,7 @@ void linphone_chat_room_set_user_data(LinphoneChatRoom *cr, void *ud) { } void linphone_chat_room_remove_transient_message(LinphoneChatRoom *cr, LinphoneChatMessage *msg) { - cr->cr->getPrivate()->removeTransientMessage(msg); + L_GET_PRIVATE(cr->cr)->removeTransientMessage(msg); } void linphone_chat_message_update_state(LinphoneChatMessage *msg, LinphoneChatMessageState new_state) { @@ -289,7 +291,7 @@ void linphone_chat_message_update_state(LinphoneChatMessage *msg, LinphoneChatMe linphone_chat_message_store_state(msg); if (msg->state == LinphoneChatMessageStateDelivered || msg->state == LinphoneChatMessageStateNotDelivered) { - msg->chat_room->cr->getPrivate()->moveTransientMessageToWeakMessages(msg); + L_GET_PRIVATE(msg->chat_room->cr)->moveTransientMessageToWeakMessages(msg); } } @@ -346,7 +348,7 @@ void create_file_transfer_information_from_vnd_gsma_rcs_ft_http_xml(LinphoneChat file_url = xmlGetProp(cur, (const xmlChar *)"url"); } - if (!xmlStrcmp(cur->name, (const xmlChar *)"file-key")) { + if (!xmlStrcmp(cur->name, (const xmlChar *)"file-key")) { /* there is a key in the msg: file has been encrypted */ /* convert the key from base 64 */ xmlChar *keyb64 = xmlNodeListGetString(xmlMessageBody, cur->xmlChildrenNode, 1); @@ -354,7 +356,7 @@ void create_file_transfer_information_from_vnd_gsma_rcs_ft_http_xml(LinphoneChat uint8_t *keyBuffer = (uint8_t *)malloc(keyLength); /* decode the key into local key buffer */ b64::b64_decode((char *)keyb64, strlen((char *)keyb64), keyBuffer, keyLength); - linphone_content_set_key(msg->file_transfer_information, (char *)keyBuffer, keyLength); + linphone_content_set_key(msg->file_transfer_information, (char *)keyBuffer, keyLength); /* duplicate key value into the linphone content private structure */ xmlFree(keyb64); free(keyBuffer); @@ -380,7 +382,7 @@ int linphone_core_message_received(LinphoneCore *lc, SalOp *op, const SalMessage LinphoneAddress *addr = linphone_address_new(sal_msg->from); linphone_address_clean(addr); LinphoneChatRoom *cr = linphone_core_get_chat_room(lc, addr); - LinphoneReason reason = cr->cr->getPrivate()->messageReceived(op, sal_msg); + LinphoneReason reason = L_GET_PRIVATE(cr->cr)->messageReceived(op, sal_msg); linphone_address_unref(addr); return reason; } @@ -573,7 +575,7 @@ static char *linphone_chat_message_create_imdn_xml(LinphoneChatMessage *cm, Imdn void linphone_chat_message_send_imdn(LinphoneChatMessage *cm, ImdnType imdn_type, LinphoneReason reason) { char *content = linphone_chat_message_create_imdn_xml(cm, imdn_type, reason); if (content) { - linphone_chat_message_get_chat_room(cm)->cr->getPrivate()->sendImdn(content, reason); + L_GET_PRIVATE(linphone_chat_message_get_chat_room(cm)->cr)->sendImdn(content, reason); ms_free(content); } } @@ -597,7 +599,7 @@ void linphone_chat_message_send_display_notification(LinphoneChatMessage *cm) { } void linphone_core_real_time_text_received(LinphoneCore *lc, LinphoneChatRoom *cr, uint32_t character, LinphoneCall *call) { - cr->cr->getPrivate()->realtimeTextReceived(character, call); + L_GET_PRIVATE(cr->cr)->realtimeTextReceived(character, call); } uint32_t linphone_chat_room_get_char(const LinphoneChatRoom *cr) { @@ -652,11 +654,11 @@ LinphoneCall *linphone_chat_room_get_call(const LinphoneChatRoom *room) { } void linphone_chat_room_set_call(LinphoneChatRoom *cr, LinphoneCall *call) { - cr->cr->getPrivate()->setCall(call); + L_GET_PRIVATE(cr->cr)->setCall(call); } bctbx_list_t * linphone_chat_room_get_transient_messages(const LinphoneChatRoom *cr) { - std::list l = cr->cr->getPrivate()->getTransientMessages(); + std::list l = L_GET_PRIVATE(cr->cr)->getTransientMessages(); bctbx_list_t *result = nullptr; for (auto it = l.begin(); it != l.end(); it++) result = bctbx_list_append(result, *it); @@ -807,7 +809,7 @@ int linphone_chat_message_set_text(LinphoneChatMessage *msg, const char* text) { msg->message = ms_strdup(text); else msg->message = NULL; - + return 0; } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2b7c2031b..e1aa5947d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -24,6 +24,7 @@ set(LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES c-wrapper/api/c-event-log.h c-wrapper/c-tools.h c-wrapper/c-types.h + c-wrapper/wrapper.h chat/chat-room-p.h chat/chat-room.h content/content.h diff --git a/src/c-wrapper/wrapper.h b/src/c-wrapper/wrapper.h new file mode 100644 index 000000000..18f2e30e6 --- /dev/null +++ b/src/c-wrapper/wrapper.h @@ -0,0 +1,48 @@ +/* + * wrapper.h + * Copyright (C) 2017 Belledonne Communications SARL + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef _WRAPPER_H_ +#define _WRAPPER_H_ + +#include + +#include "object/object.h" + +// ============================================================================= + +LINPHONE_BEGIN_NAMESPACE + +class Wrapper { +public: + template + static decltype(std::declval().getPrivate()) getPrivate (T *object) { + return object->getPrivate(); + } + +private: + Wrapper (); + + L_DISABLE_COPY(Wrapper); +}; + +LINPHONE_END_NAMESPACE + +#define L_GET_PRIVATE(OBJECT) \ + LINPHONE_NAMESPACE::Wrapper::getPrivate(OBJECT) + +#endif // ifndef _WRAPPER_H_ diff --git a/src/chat/chat-room-p.h b/src/chat/chat-room-p.h index eae877b60..9e6de5c25 100644 --- a/src/chat/chat-room-p.h +++ b/src/chat/chat-room-p.h @@ -19,15 +19,11 @@ #ifndef _CHAT_ROOM_P_H_ #define _CHAT_ROOM_P_H_ -#include - -#include "object/object-p.h" - -#include "chat-room.h" - +// From coreapi. #include "private.h" -#include +#include "chat-room.h" +#include "object/object-p.h" // ============================================================================= @@ -122,7 +118,7 @@ public: std::list receivedRttCharacters; LinphoneChatMessage *pendingMessage = nullptr; -public: +private: L_DECLARE_PUBLIC(ChatRoom); }; diff --git a/src/chat/chat-room.cpp b/src/chat/chat-room.cpp index 764fcbf79..6d8373a6a 100644 --- a/src/chat/chat-room.cpp +++ b/src/chat/chat-room.cpp @@ -19,18 +19,18 @@ #include #include "chat-room-p.h" - -#include "chat-room.h" #include "logger/logger.h" #include "utils/content-type.h" #include "utils/utils.h" +#include "chat-room.h" + +// ============================================================================= + using namespace std; LINPHONE_BEGIN_NAMESPACE -// ============================================================================= - const string ChatRoomPrivate::imdnPrefix = "/imdn:imdn"; const string ChatRoomPrivate::isComposingPrefix = "/xsi:isComposing"; diff --git a/src/chat/chat-room.h b/src/chat/chat-room.h index 4cb9e5628..801896cf7 100644 --- a/src/chat/chat-room.h +++ b/src/chat/chat-room.h @@ -20,28 +20,10 @@ #define _CHAT_ROOM_H_ #include -#include #include "object/object.h" #include "linphone/types.h" -#include "sal/sal.h" - -#include "private.h" - -#include - -extern "C" { -LinphoneChatRoom *_linphone_core_create_chat_room_base (LinphoneCore *lc, LinphoneAddress *addr); -int linphone_core_message_received (LinphoneCore *lc, SalOp *op, const SalMessage *sal_msg); -void linphone_core_real_time_text_received (LinphoneCore *lc, LinphoneChatRoom *cr, uint32_t character, LinphoneCall *call); -bctbx_list_t *linphone_chat_room_get_transient_messages (const LinphoneChatRoom *cr); -void linphone_chat_room_remove_transient_message (LinphoneChatRoom *cr, LinphoneChatMessage *msg); -void linphone_chat_room_set_call (LinphoneChatRoom *cr, LinphoneCall *call); -void linphone_chat_room_release (LinphoneChatRoom *cr); -void linphone_chat_message_send_imdn (LinphoneChatMessage *cm, ImdnType imdn_type, LinphoneReason reason); -void linphone_chat_message_update_state (LinphoneChatMessage *msg, LinphoneChatMessageState new_state); -} // ============================================================================= @@ -50,16 +32,6 @@ LINPHONE_BEGIN_NAMESPACE class ChatRoomPrivate; class ChatRoom : public Object { - friend LinphoneChatRoom *::_linphone_core_create_chat_room_base (LinphoneCore *lc, LinphoneAddress *addr); - friend int ::linphone_core_message_received(LinphoneCore * lc, SalOp * op, const SalMessage * sal_msg); - friend void ::linphone_core_real_time_text_received(LinphoneCore * lc, LinphoneChatRoom * cr, uint32_t character, LinphoneCall * call); - friend bctbx_list_t *::linphone_chat_room_get_transient_messages (const LinphoneChatRoom *cr); - friend void ::linphone_chat_room_remove_transient_message(LinphoneChatRoom * cr, LinphoneChatMessage * msg); - friend void ::linphone_chat_room_set_call(LinphoneChatRoom * cr, LinphoneCall * call); - friend void ::linphone_chat_room_release(LinphoneChatRoom * cr); - friend void ::linphone_chat_message_send_imdn(LinphoneChatMessage * cm, ImdnType imdn_type, LinphoneReason reason); - friend void ::linphone_chat_message_update_state(LinphoneChatMessage * msg, LinphoneChatMessageState new_state); - public: ChatRoom (LinphoneCore *core, LinphoneAddress *peerAddress); virtual ~ChatRoom () = default; diff --git a/src/utils/general.h b/src/utils/general.h index fd1ebb9eb..6dcd120bd 100644 --- a/src/utils/general.h +++ b/src/utils/general.h @@ -63,6 +63,8 @@ void l_assert (const char *condition, const char *file, int line); #define L_ASSERT(CONDITION) ((CONDITION) ? static_cast(0) : l_assert(#CONDITION, __FILE__, __LINE__)) #endif +// Allows access to private internal data. +// Gives a control to C Wrapper. #define L_DECLARE_PRIVATE(CLASS) \ inline CLASS ## Private * getPrivate() { \ return reinterpret_cast(mPrivate); \ @@ -70,7 +72,8 @@ void l_assert (const char *condition, const char *file, int line); inline const CLASS ## Private *getPrivate() const { \ return reinterpret_cast(mPrivate); \ } \ - friend class CLASS ## Private; + friend class CLASS ## Private; \ + friend class Wrapper; class ClonableObject; class ClonableObjectPrivate;