Commit 0d8c3d56 authored by Guillaume BIENKOWSKI's avatar Guillaume BIENKOWSKI

Added a foreach() method for object and dict.

Unit test updated for testing it.
parent 7d7d89fd
......@@ -129,6 +129,11 @@ BELLESIP_EXPORT void belle_sip_dict_merge( const belle_sip_dict_t* src, belle_si
*/
BELLESIP_EXPORT int belle_sip_dict_haskey(belle_sip_dict_t* obj, const char* key);
/**
* Apply a function for all keys stored in the dictionary
*/
BELLESIP_EXPORT void belle_sip_dict_foreach(const belle_sip_dict_t* obj, void (*apply_func)(const char*, void*, void*), void* userdata);
BELLE_SIP_END_DECLS
#endif // DICT_H
......@@ -283,7 +283,10 @@ BELLESIP_EXPORT void belle_sip_object_data_clone( const belle_sip_object_t* src,
*/
BELLESIP_EXPORT void belle_sip_object_data_merge( const belle_sip_object_t* src, belle_sip_object_t* dst, belle_sip_data_clone clone_func);
/**
* Apply a function for each entry in the data store
*/
BELLESIP_EXPORT void belle_sip_object_data_foreach( const belle_sip_object_t* obj, void (*apply_func)(const char* key, void* data, void* userdata), void* userdata);
/**
* Returns a string describing the inheritance diagram and implemented interfaces of object obj.
**/
......
......@@ -45,7 +45,6 @@ static void belle_sip_dict_destroy( belle_sip_dict_t* obj)
{
}
void belle_sip_dict_set_int(belle_sip_dict_t* obj, const char* key, int value)
{
char tmp[30];
......@@ -120,6 +119,13 @@ int belle_sip_dict_haskey(belle_sip_dict_t* obj, const char* key)
}
void belle_sip_dict_foreach(const belle_sip_dict_t* obj, void (*apply_func)(const char*, void*, void*), void* userdata)
{
belle_sip_object_data_foreach(BELLE_SIP_OBJECT(obj),apply_func, userdata);
}
void belle_sip_dict_clear(belle_sip_dict_t* obj)
{
return belle_sip_object_data_clear(BELLE_SIP_OBJECT(obj));
......
......@@ -336,6 +336,25 @@ void belle_sip_object_data_merge( const belle_sip_object_t* src, belle_sip_objec
}
struct belle_sip_object_foreach_data {
void (*apply_func)(const char*, void*, void*);
void* userdata;
};
static void belle_sip_object_for_each_cb(void* data, void* userdata)
{
struct belle_sip_object_foreach_data* fd = (struct belle_sip_object_foreach_data*)userdata;
struct belle_sip_object_data* it = (struct belle_sip_object_data*)data;
if( it && fd->apply_func )
fd->apply_func(it->name, it->data, userdata);
}
void belle_sip_object_data_foreach( const belle_sip_object_t* obj, void (*apply_func)(const char* key, void* data, void* userdata), void* userdata)
{
struct belle_sip_object_foreach_data fd = { apply_func, userdata };
belle_sip_list_for_each2(obj->data_store, belle_sip_object_for_each_cb, &fd);
}
void *belle_sip_object_cast(belle_sip_object_t *obj, belle_sip_type_id_t id, const char *castname, const char *file, int fileno){
......
......@@ -34,6 +34,7 @@
#define INT_TO_VOIDPTR(i) ((void*)(intptr_t)(i))
#define VOIDPTR_TO_INT(p) ((int)(intptr_t)(p))
static int foreach_called = 0;
static int destroy_called = 0;
static int clone_called = 0;
......@@ -50,6 +51,14 @@ static void* test_object_data_string_clone(const char*name, void* data){
return data;
}
static void test_object_data_foreach_cb(const char*name, void*data, void*udata)
{
(void)name;
(void)data;
(void)udata;
foreach_called++;
}
static void test_object_data(void)
{
belle_sip_object_t* obj = belle_sip_object_new(belle_sip_object_t);
......@@ -129,6 +138,12 @@ static void test_object_data(void)
(const char*)belle_sip_object_data_get(cloned, "test_str"));
/*
* Foreach test
*/
belle_sip_object_data_foreach(obj, test_object_data_foreach_cb, NULL);
CU_ASSERT_EQUAL( foreach_called, 2 );
belle_sip_object_unref(obj);
belle_sip_object_unref(cloned);
......
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