Commit fd1385fd authored by Ronan's avatar Ronan
Browse files

feat(c-wrapper): provide a L_REGISTER_SUBTYPE macro to register derived cpp object

parent 27bd8962
......@@ -31,22 +31,22 @@ LINPHONE_BEGIN_NAMESPACE
namespace Utils {
template<typename T>
static constexpr T *getPtr (const std::shared_ptr<T> &object) {
LINPHONE_PUBLIC constexpr T *getPtr (const std::shared_ptr<T> &object) {
return object.get();
}
template<typename T>
static constexpr T *getPtr (T *object) {
LINPHONE_PUBLIC constexpr T *getPtr (T *object) {
return object;
}
template<typename T>
static constexpr const T *getPtr (const std::shared_ptr<const T> &object) {
LINPHONE_PUBLIC constexpr const T *getPtr (const std::shared_ptr<const T> &object) {
return object.get();
}
template<typename T>
static constexpr const T *getPtr (const T *object) {
LINPHONE_PUBLIC constexpr const T *getPtr (const T *object) {
return object;
}
......
......@@ -37,4 +37,6 @@ L_REGISTER_TYPE(EventLog, EventLog);
L_REGISTER_TYPE(MediaSessionParams, CallParams);
L_REGISTER_TYPE(Participant, Participant);
L_REGISTER_SUBTYPE(ChatRoom, ClientGroupChatRoom);
#endif // ifndef _C_WRAPPER_H_
......@@ -83,7 +83,6 @@ public:
// Get c/cpp ptr helpers.
// ---------------------------------------------------------------------------
// Get Object.
template<
typename CType,
typename CppType = typename CTypeToCppType<CType>::type,
......@@ -102,7 +101,6 @@ public:
return reinterpret_cast<const WrappedObject<CppType> *>(cObject)->cppPtr;
}
// Get ClonableObject.
template<
typename CType,
typename CppType = typename CTypeToCppType<CType>::type,
......@@ -121,25 +119,24 @@ public:
return reinterpret_cast<const WrappedClonableObject<CppType> *>(cObject)->cppPtr;
}
// Set Object.
// ---------------------------------------------------------------------------
// Set c/cpp ptr helpers.
// ---------------------------------------------------------------------------
template<
typename CppType,
typename = typename std::enable_if<std::is_base_of<Object, CppType>::value, CppType>::type
>
static inline void setCppPtrFromC (void *cObject, const std::shared_ptr<CppType> &cppObject) {
L_ASSERT(cObject);
static_cast<WrappedObject<CppType> *>(cObject)->cppPtr = cppObject;
cppObject->setProperty("LinphonePrivate::Wrapper::cBackPtr", cObject);
}
// Set ClonableObject.
template<
typename CppType,
typename = typename std::enable_if<std::is_base_of<ClonableObject, CppType>::value, CppType>::type
>
static inline void setCppPtrFromC (void *cObject, const CppType *cppObject) {
L_ASSERT(cObject);
CppType **cppObjectAddr = &static_cast<WrappedClonableObject<CppType> *>(cObject)->cppPtr;
if (*cppObjectAddr == cppObject)
return;
......@@ -170,6 +167,14 @@ public:
return cObject;
}
template<
typename CppType,
typename = typename std::enable_if<std::is_base_of<Object, CppType>::value, CppType>::type
>
static inline typename CppTypeToCType<CppType>::type *getCBackPtr (CppType *cppObject) {
return getCBackPtr(std::static_pointer_cast<CppType>(cppObject->shared_from_this()));
}
template<
typename CppType,
typename = typename std::enable_if<std::is_base_of<ClonableObject, CppType>::value, CppType>::type
......@@ -317,12 +322,28 @@ LINPHONE_END_NAMESPACE
}; \
template<> \
struct CObjectInitializer<CPP_TYPE> { \
static Linphone ## C_TYPE *init () { \
static inline Linphone ## C_TYPE *init () { \
return _linphone_ ## C_TYPE ## _init(); \
} \
}; \
LINPHONE_END_NAMESPACE
#define L_REGISTER_SUBTYPE(CPP_TYPE, CPP_SUBTYPE) \
LINPHONE_BEGIN_NAMESPACE \
class CPP_SUBTYPE; \
template<> \
struct CppTypeToCType<CPP_SUBTYPE> { \
enum { defined = true }; \
typedef CppTypeToCType<CPP_TYPE>::type type; \
}; \
template<> \
struct CObjectInitializer<CPP_SUBTYPE> { \
static inline typename CppTypeToCType<CPP_TYPE>::type *init () { \
return CObjectInitializer<CPP_TYPE>::init(); \
} \
}; \
LINPHONE_END_NAMESPACE
#define L_ASSERT_C_TYPE(C_TYPE) \
static_assert(LINPHONE_NAMESPACE::CTypeToCppType<Linphone ## C_TYPE>::defined, "Type is not defined."); \
......
......@@ -31,8 +31,6 @@
#include "modifier/cpim-chat-message-modifier.h"
#include "chat-room.h"
#define GET_BACK_PTR(object) L_GET_C_BACK_PTR(static_pointer_cast<ChatMessage>(object->shared_from_this()))
// =============================================================================
LINPHONE_BEGIN_NAMESPACE
......@@ -53,7 +51,7 @@ ChatMessage::ChatMessage (const std::shared_ptr<ChatRoom> &room) : Object(*new C
ChatMessage::ChatMessage (ChatMessagePrivate &p) : Object(p) {}
LinphoneChatMessage * ChatMessage::getBackPtr() {
return GET_BACK_PTR(this);
return L_GET_C_BACK_PTR(this);
}
shared_ptr<ChatRoom> ChatMessage::getChatRoom () const {
......
......@@ -30,8 +30,6 @@
#include "chat-message.h"
#include "chat-room.h"
#define GET_BACK_PTR(object) L_GET_C_BACK_PTR(static_pointer_cast<ChatRoom>(object->shared_from_this()))
// =============================================================================
using namespace std;
......@@ -105,7 +103,7 @@ void ChatRoomPrivate::release () {
linphone_chat_message_deactivate(message);
core = nullptr;
linphone_chat_room_unref(GET_BACK_PTR(q));
linphone_chat_room_unref(L_GET_C_BACK_PTR(q));
}
void ChatRoomPrivate::sendImdn (const string &content, LinphoneReason reason) {
......@@ -136,7 +134,7 @@ void ChatRoomPrivate::sendImdn (const string &content, LinphoneReason reason) {
LinphoneImEncryptionEngineCbs *imeeCbs = linphone_im_encryption_engine_get_callbacks(imee);
LinphoneImEncryptionEngineCbsOutgoingMessageCb cbProcessOutgoingMessage = linphone_im_encryption_engine_cbs_get_process_outgoing_message(imeeCbs);
if (cbProcessOutgoingMessage) {
retval = cbProcessOutgoingMessage(imee, GET_BACK_PTR(q), msg);
retval = cbProcessOutgoingMessage(imee, L_GET_C_BACK_PTR(q), msg);
}
}
......@@ -222,7 +220,7 @@ void ChatRoomPrivate::sendIsComposingNotification () {
LinphoneImEncryptionEngineCbs *imeeCbs = linphone_im_encryption_engine_get_callbacks(imee);
LinphoneImEncryptionEngineCbsOutgoingMessageCb cbProcessOutgoingMessage = linphone_im_encryption_engine_cbs_get_process_outgoing_message(imeeCbs);
if (cbProcessOutgoingMessage) {
retval = cbProcessOutgoingMessage(imee, GET_BACK_PTR(q), msg);
retval = cbProcessOutgoingMessage(imee, L_GET_C_BACK_PTR(q), msg);
}
}
......@@ -427,7 +425,7 @@ LinphoneReason ChatRoomPrivate::messageReceived (SalOp *op, const SalMessage *sa
LinphoneImEncryptionEngineCbs *imeeCbs = linphone_im_encryption_engine_get_callbacks(imee);
LinphoneImEncryptionEngineCbsIncomingMessageCb cbProcessIncomingMessage = linphone_im_encryption_engine_cbs_get_process_incoming_message(imeeCbs);
if (cbProcessIncomingMessage) {
retval = cbProcessIncomingMessage(imee, GET_BACK_PTR(q), msg);
retval = cbProcessIncomingMessage(imee, L_GET_C_BACK_PTR(q), msg);
if (retval == 0) {
linphone_chat_message_set_is_secured(msg, TRUE);
} else if (retval > 0) {
......@@ -506,7 +504,7 @@ void ChatRoomPrivate::chatMessageReceived (LinphoneChatMessage *msg) {
if (!ContentType::isImdn(linphone_chat_message_get_content_type(msg)) && !ContentType::isImIsComposing(linphone_chat_message_get_content_type(msg))) {
notifyChatMessageReceived(msg);
remoteIsComposing = false;
linphone_core_notify_is_composing_received(core, GET_BACK_PTR(q));
linphone_core_notify_is_composing_received(core, L_GET_C_BACK_PTR(q));
linphone_chat_message_send_delivery_notification(msg, LinphoneReasonNone);
}
}
......@@ -524,7 +522,7 @@ void ChatRoomPrivate::isComposingReceived (const string &text) {
void ChatRoomPrivate::notifyChatMessageReceived (LinphoneChatMessage *msg) {
L_Q(ChatRoom);
LinphoneChatRoom *cr = GET_BACK_PTR(q);
LinphoneChatRoom *cr = L_GET_C_BACK_PTR(q);
if (linphone_chat_message_get_text(msg)) {
/* Legacy API */
linphone_core_notify_text_message_received(core, cr, linphone_chat_message_get_from_address(msg), linphone_chat_message_get_text(msg));
......@@ -538,7 +536,7 @@ void ChatRoomPrivate::notifyChatMessageReceived (LinphoneChatMessage *msg) {
void ChatRoomPrivate::notifyStateChanged () {
L_Q(ChatRoom);
LinphoneChatRoom *cr = GET_BACK_PTR(q);
LinphoneChatRoom *cr = L_GET_C_BACK_PTR(q);
LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
LinphoneChatRoomCbsStateChangedCb cb = linphone_chat_room_cbs_get_state_changed(cbs);
if (cb)
......@@ -547,7 +545,7 @@ void ChatRoomPrivate::notifyStateChanged () {
void ChatRoomPrivate::notifyUndecryptableMessageReceived (LinphoneChatMessage *msg) {
L_Q(ChatRoom);
LinphoneChatRoom *cr = GET_BACK_PTR(q);
LinphoneChatRoom *cr = L_GET_C_BACK_PTR(q);
LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
LinphoneChatRoomCbsUndecryptableMessageReceivedCb cb = linphone_chat_room_cbs_get_undecryptable_message_received(cbs);
if (cb)
......@@ -565,7 +563,7 @@ void ChatRoomPrivate::onIsComposingStateChanged (bool isComposing) {
void ChatRoomPrivate::onIsRemoteComposingStateChanged (bool isComposing) {
L_Q(ChatRoom);
remoteIsComposing = isComposing;
linphone_core_notify_is_composing_received(core, GET_BACK_PTR(q));
linphone_core_notify_is_composing_received(core, L_GET_C_BACK_PTR(q));
}
void ChatRoomPrivate::onIsComposingRefreshNeeded () {
......@@ -593,7 +591,7 @@ void ChatRoom::compose () {
LinphoneChatMessage *ChatRoom::createFileTransferMessage (const LinphoneContent *initialContent) {
L_D(ChatRoom);
LinphoneChatMessage *msg = createMessage("");
linphone_chat_message_set_chat_room(msg, GET_BACK_PTR(this));
linphone_chat_message_set_chat_room(msg, L_GET_C_BACK_PTR(this));
linphone_chat_message_set_text(msg, NULL);
linphone_chat_message_set_file_transfer_information(msg, linphone_content_copy(initialContent));
linphone_chat_message_set_outgoing(msg);
......@@ -612,7 +610,7 @@ LinphoneChatMessage *ChatRoom::createFileTransferMessage (const LinphoneContent
LinphoneChatMessage *ChatRoom::createMessage (const string &message) {
shared_ptr<ChatMessage> chatMessage = make_shared<ChatMessage>(static_pointer_cast<ChatRoom>(shared_from_this()));
LinphoneChatMessage *msg = chatMessage->getBackPtr();
linphone_chat_message_set_chat_room(msg, GET_BACK_PTR(this));
linphone_chat_message_set_chat_room(msg, L_GET_C_BACK_PTR(this));
linphone_chat_message_set_state(msg, LinphoneChatMessageStateIdle);
linphone_chat_message_set_text(msg, message.empty() ? nullptr : ms_strdup(message.c_str()));
linphone_chat_message_set_content_type(msg, ms_strdup("text/plain"));
......@@ -843,7 +841,7 @@ void ChatRoom::sendMessage (LinphoneChatMessage *msg) {
LinphoneImEncryptionEngineCbs *imeeCbs = linphone_im_encryption_engine_get_callbacks(imee);
LinphoneImEncryptionEngineCbsOutgoingMessageCb cbProcessOutgoingMessage = linphone_im_encryption_engine_cbs_get_process_outgoing_message(imeeCbs);
if (cbProcessOutgoingMessage) {
retval = cbProcessOutgoingMessage(imee, GET_BACK_PTR(this), msg);
retval = cbProcessOutgoingMessage(imee, L_GET_C_BACK_PTR(this), msg);
if (retval == 0) {
linphone_chat_message_set_is_secured(msg, TRUE);
}
......
......@@ -102,7 +102,7 @@ void ClientGroupChatRoom::onParticipantAdded (const Address &addr) {
}
participant = make_shared<Participant>(addr);
participants.push_back(participant);
LinphoneChatRoom *cr = L_GET_C_BACK_PTR(static_pointer_cast<ChatRoom>(this->shared_from_this()));
LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
LinphoneChatRoomCbsParticipantAddedCb cb = linphone_chat_room_cbs_get_participant_added(cbs);
if (cb)
......@@ -115,7 +115,7 @@ void ClientGroupChatRoom::onParticipantRemoved (const Address &addr) {
lWarning() << "Participant " << participant << " removed but not in the list of participants!";
return;
}
LinphoneChatRoom *cr = L_GET_C_BACK_PTR(static_pointer_cast<ChatRoom>(this->shared_from_this()));
LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
LinphoneChatRoomCbsParticipantRemovedCb cb = linphone_chat_room_cbs_get_participant_removed(cbs);
if (cb)
......@@ -130,7 +130,7 @@ void ClientGroupChatRoom::onParticipantSetAdmin (const Address &addr, bool isAdm
return;
}
participant->setAdmin(isAdmin);
LinphoneChatRoom *cr = L_GET_C_BACK_PTR(static_pointer_cast<ChatRoom>(this->shared_from_this()));
LinphoneChatRoom *cr = L_GET_C_BACK_PTR(this);
LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(cr);
LinphoneChatRoomCbsParticipantAdminStatusChangedCb cb = linphone_chat_room_cbs_get_participant_admin_status_changed(cbs);
if (cb)
......
......@@ -24,8 +24,6 @@
#include "c-wrapper/c-wrapper.h"
#include "logger/logger.h"
#define GET_BACK_PTR(object) L_GET_C_BACK_PTR(static_pointer_cast<ChatRoom>(object->shared_from_this()))
// =============================================================================
using namespace std;
......@@ -65,7 +63,7 @@ void RealTimeTextChatRoomPrivate::realtimeTextReceived (uint32_t character, Linp
receivedRttCharacters.push_back(cmc);
remoteIsComposing = true;
linphone_core_notify_is_composing_received(core, GET_BACK_PTR(q));
linphone_core_notify_is_composing_received(core, L_GET_C_BACK_PTR(q));
if ((character == new_line) || (character == crlf) || (character == lf)) {
/* End of message */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment