From 8419375aee0eab342ecdd497ee0c3cf2da61ebe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Grisez?= Date: Fri, 13 Jan 2017 16:00:41 +0100 Subject: [PATCH] Make LiphoneConfig inherite from belle_sip_object --- coreapi/lpconfig.c | 54 ++++++++++++++++++++++++------------- coreapi/private.h | 1 + include/linphone/lpconfig.h | 5 ++++ 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/coreapi/lpconfig.c b/coreapi/lpconfig.c index 6dd616c3b..4ad080252 100644 --- a/coreapi/lpconfig.c +++ b/coreapi/lpconfig.c @@ -24,8 +24,9 @@ #define MAX_LEN 16384 -#include "linphone/core.h" +#include "private.h" #include "bctoolbox/vfs.h" +#include "belle-sip/object.h" #include #include @@ -81,7 +82,7 @@ typedef struct _LpSection{ } LpSection; struct _LpConfig{ - int refcnt; + belle_sip_object_t base; bctbx_vfs_file_t* pFile; char *filename; char *tmpfilename; @@ -91,6 +92,8 @@ struct _LpConfig{ bctbx_vfs_t* g_bctbx_vfs; }; +BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphoneConfig); +BELLE_SIP_DECLARE_VPTR(LinphoneConfig); char* lp_realpath(const char* file, char* name) { @@ -376,31 +379,30 @@ LpConfig * linphone_config_new(const char *filename){ return linphone_config_new_with_factory(filename, NULL); } -LpConfig * linphone_config_new_from_buffer(const char *buffer){ - LpConfig* conf = lp_new0(LpConfig,1); +static void _linphone_config_init_from_buffer(LinphoneConfig *conf, const char *buffer) { LpSection* current_section = NULL; char* ptr = ms_strdup(buffer); char* strtok_storage = NULL; char* line = strtok_r(ptr, "\n", &strtok_storage); - conf->refcnt=1; - while( line != NULL ){ current_section = linphone_config_parse_line(conf,line,current_section); line = strtok_r(NULL, "\n", &strtok_storage); } ms_free(ptr); +} +LpConfig * linphone_config_new_from_buffer(const char *buffer){ + LpConfig* conf = belle_sip_object_new(LinphoneConfig); + _linphone_config_init_from_buffer(conf, buffer); return conf; } -LpConfig *linphone_config_new_with_factory(const char *config_filename, const char *factory_config_filename) { - LpConfig *lpconfig=lp_new0(LpConfig,1); +static int _linphone_config_init_from_files(LinphoneConfig *lpconfig, const char *config_filename, const char *factory_config_filename) { lpconfig->g_bctbx_vfs = bctbx_vfs_get_default(); - lpconfig->refcnt=1; if (config_filename!=NULL){ if(ortp_file_exist(config_filename) == 0) { lpconfig->filename=lp_realpath(config_filename, NULL); @@ -447,11 +449,20 @@ LpConfig *linphone_config_new_with_factory(const char *config_filename, const ch if (factory_config_filename != NULL) { linphone_config_read_file(lpconfig, factory_config_filename); } - return lpconfig; + return 0; fail: - ms_free(lpconfig); - return NULL; + return -1; +} + +LpConfig *linphone_config_new_with_factory(const char *config_filename, const char *factory_config_filename) { + LpConfig *lpconfig=belle_sip_object_new(LinphoneConfig); + if (_linphone_config_init_from_files(lpconfig, config_filename, factory_config_filename) == 0) { + return lpconfig; + } else { + ms_free(lpconfig); + return NULL; + } } int linphone_config_read_file(LpConfig *lpconfig, const char *filename){ @@ -476,23 +487,19 @@ void lp_item_set_value(LpItem *item, const char *value){ } -static void _linphone_config_destroy(LpConfig *lpconfig){ +static void _linphone_config_uninit(LpConfig *lpconfig){ if (lpconfig->filename!=NULL) ortp_free(lpconfig->filename); if (lpconfig->tmpfilename) ortp_free(lpconfig->tmpfilename); bctbx_list_for_each(lpconfig->sections,(void (*)(void*))lp_section_destroy); bctbx_list_free(lpconfig->sections); - free(lpconfig); } LpConfig *linphone_config_ref(LpConfig *lpconfig){ - lpconfig->refcnt++; - return lpconfig; + return (LinphoneConfig *)belle_sip_object_ref(BELLE_SIP_OBJECT(lpconfig)); } void linphone_config_unref(LpConfig *lpconfig){ - lpconfig->refcnt--; - if (lpconfig->refcnt==0) - _linphone_config_destroy(lpconfig); + belle_sip_object_unref(BELLE_SIP_OBJECT(lpconfig)); } void linphone_config_destroy(LpConfig *lpconfig){ @@ -1099,3 +1106,12 @@ int linphone_config_has_entry(const LpConfig *lpconfig, const char *section, con return FALSE; } + +BELLE_SIP_INSTANCIATE_VPTR( + LinphoneConfig, + belle_sip_object_t, + _linphone_config_uninit, // uninit + NULL, // copy + NULL, // marshal + FALSE +); diff --git a/coreapi/private.h b/coreapi/private.h index 90a7c7333..e17f86e62 100644 --- a/coreapi/private.h +++ b/coreapi/private.h @@ -1595,6 +1595,7 @@ BELLE_SIP_TYPE_ID(LinphoneCoreCbs), BELLE_SIP_TYPE_ID(LinphoneFactory), BELLE_SIP_TYPE_ID(LinphoneAuthInfo), BELLE_SIP_TYPE_ID(LinphoneVcard), +BELLE_SIP_TYPE_ID(LinphoneConfig) BELLE_SIP_DECLARE_TYPES_END diff --git a/include/linphone/lpconfig.h b/include/linphone/lpconfig.h index 936b4c16f..ee9b97200 100644 --- a/include/linphone/lpconfig.h +++ b/include/linphone/lpconfig.h @@ -62,6 +62,11 @@ typedef struct _LpConfig LinphoneConfig; */ #define LpConfig LinphoneConfig +/** + * Safely downcast a belle_sip_object into LinphoneConfig + */ +#define LINPHONE_CONFIG(obj) BELLE_SIP_CAST(obj, LinphoneConfig); + #ifdef __cplusplus extern "C" { #endif