#include "icl-state.h"
#include "icl-representation.h"
-void icl_representation_inc_ref_count(iotcon_representation_h val)
+iotcon_representation_h icl_representation_ref(iotcon_representation_h repr)
{
- RET_IF(NULL == val);
- RETM_IF(val->ref_count < 0, "Invalid Count(%d)", val->ref_count);
+ RETV_IF(NULL == repr, NULL);
+ RETV_IF(repr->ref_count <= 0, NULL);
- val->ref_count++;
-}
-
-
-static bool _icl_representation_dec_ref_count(iotcon_representation_h val)
-{
- RETV_IF(NULL == val, false);
- RETVM_IF(val->ref_count <= 0, false, "Invalid Count(%d)", val->ref_count);
-
- val->ref_count--;
- if (0 == val->ref_count)
- return true;
+ repr->ref_count++;
- return false;
+ return repr;
}
}
repr->visibility = (ICL_VISIBILITY_REPR | ICL_VISIBILITY_PROP);
- icl_representation_inc_ref_count(repr);
+ repr->ref_count = 1;
*ret_repr = repr;
{
RET_IF(NULL == repr);
- if (false == _icl_representation_dec_ref_count(repr))
+ repr->ref_count--;
+
+ if (0 != repr->ref_count)
return;
free(repr->uri_path);
{
RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- iotcon_resource_types_destroy(repr->res_types);
- repr->res_types = NULL;
-
if (types)
- repr->res_types = icl_resource_types_ref(types);
+ types = icl_resource_types_ref(types);
+
+ if (repr->res_types)
+ iotcon_resource_types_destroy(repr->res_types);
+
+ repr->res_types = types;
return IOTCON_ERROR_NONE;
}
int *ifaces)
{
RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == ifaces, IOTCON_ERROR_INVALID_PARAMETER);
*ifaces = repr->interfaces;
{
RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
- if (state == repr->state)
- return IOTCON_ERROR_NONE;
+ if (state)
+ state = icl_state_ref(state);
- if (repr->state) {
+ if (repr->state)
iotcon_state_destroy(repr->state);
- repr->state = NULL;
- }
-
- if (state)
- icl_state_inc_ref_count(state);
repr->state = state;
API int iotcon_representation_add_child(iotcon_representation_h parent,
iotcon_representation_h child)
{
+ iotcon_representation_h repr;
+
RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
- icl_representation_inc_ref_count(child);
- parent->children = g_list_append(parent->children, child);
+ repr = icl_representation_ref(child);
+
+ parent->children = g_list_append(parent->children, repr);
return IOTCON_ERROR_NONE;
}
parent->children = g_list_remove(parent->children, child);
+ iotcon_representation_destroy(child);
+
return IOTCON_ERROR_NONE;
}
unsigned int *count)
{
RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == parent->children, IOTCON_ERROR_INVALID_PARAMETER);
-
- *count = g_list_length(parent->children);
+ RETV_IF(NULL == count, IOTCON_ERROR_INVALID_PARAMETER);
+ if (NULL == parent->children)
+ *count = 0;
+ else
+ *count = g_list_length(parent->children);
return IOTCON_ERROR_NONE;
}
return IOTCON_ERROR_NONE;
}
-API int iotcon_state_foreach(iotcon_state_h state, iotcon_state_cb cb, void *user_data)
-{
- GHashTableIter iter;
- gpointer key;
-
- RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
-
- g_hash_table_iter_init(&iter, state->hash_table);
- while (g_hash_table_iter_next(&iter, &key, NULL)) {
- if (IOTCON_FUNC_STOP == cb(state, key, user_data))
- break;
- }
-
- return IOTCON_ERROR_NONE;
-}
-
-
-API int iotcon_state_get_keys_count(iotcon_state_h state, unsigned int *count)
-{
- RETV_IF(NULL == state, IOTCON_ERROR_INVALID_PARAMETER);
- RETV_IF(NULL == state->hash_table, IOTCON_ERROR_INVALID_PARAMETER);
-
- *count = g_hash_table_size(state->hash_table);
-
- return IOTCON_ERROR_NONE;
-}
-
-
-void icl_state_clone_foreach(char *key, iotcon_value_h src_val, iotcon_state_h dest_state)
-{
- FN_CALL;
- int type, ret;
- iotcon_value_h value, copied_val;
- iotcon_list_h child_list, copied_list;
- iotcon_state_h child_state;
- iotcon_state_h copied_state = NULL;
-
- type = src_val->type;
- switch (type) {
- case IOTCON_TYPE_INT:
- case IOTCON_TYPE_BOOL:
- case IOTCON_TYPE_DOUBLE:
- case IOTCON_TYPE_STR:
- case IOTCON_TYPE_NULL:
- copied_val = icl_value_clone(src_val);
- if (NULL == copied_val) {
- ERR("icl_value_clone() Fail");
- return;
- }
-
- icl_state_set_value(dest_state, key, copied_val);
- break;
- case IOTCON_TYPE_LIST:
- ret = icl_value_get_list(src_val, &child_list);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("icl_value_get_list() Fail(%d)", ret);
- return;
- }
-
- copied_list = icl_list_clone(child_list);
- if (NULL == copied_list) {
- ERR("icl_list_clone() Fail");
- return;
- }
-
- value = icl_value_create_list(copied_list);
- if (NULL == value) {
- ERR("icl_value_create_list() Fail");
- iotcon_list_destroy(copied_list);
- return;
- }
-
- icl_state_set_value(dest_state, key, value);
- break;
- case IOTCON_TYPE_STATE:
- ret = icl_value_get_state(src_val, &child_state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("icl_value_get_state() Fail(%d)", ret);
- return;
- }
-
- g_hash_table_foreach(child_state->hash_table, (GHFunc)icl_state_clone_foreach,
- copied_state);
-
- value = icl_value_create_state(copied_state);
- if (NULL == value) {
- ERR("icl_value_create_state(%p) Fail", copied_state);
- return;
- }
-
- icl_state_set_value(dest_state, key, value);
- break;
- default:
- ERR("Invalid type(%d)", type);
- return;
- }
-}
API int iotcon_representation_clone(const iotcon_representation_h src,
iotcon_representation_h *dest)
int ret;
GList *node;
iotcon_resource_types_h list;
- iotcon_state_h ori_state;
- iotcon_state_h cloned_state = NULL;
iotcon_representation_h cloned_repr, copied_repr;
RETV_IF(NULL == src, IOTCON_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == dest, IOTCON_ERROR_INVALID_PARAMETER);
ret = iotcon_representation_create(&cloned_repr);
if (IOTCON_ERROR_NONE != ret) {
cloned_repr->res_types = list;
}
- for (node = g_list_first(src->children); node; node = node->next) {
- ret = iotcon_representation_clone((iotcon_representation_h)node->data,
- &copied_repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_clone(child) Fail(%d)", ret);
- iotcon_representation_destroy(cloned_repr);
- return ret;
+ if (src->children) {
+ for (node = g_list_first(src->children); node; node = node->next) {
+ ret = iotcon_representation_clone((iotcon_representation_h)node->data,
+ &copied_repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_clone(child) Fail(%d)", ret);
+ iotcon_representation_destroy(cloned_repr);
+ return ret;
+ }
+ cloned_repr->children = g_list_append(cloned_repr->children, copied_repr);
}
- cloned_repr->children = g_list_append(cloned_repr->children, copied_repr);
}
- ori_state = src->state;
- if (ori_state->hash_table) {
- ret = iotcon_state_create(&cloned_state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_create() Fail");
- iotcon_representation_destroy(cloned_repr);
- return ret;
- }
- g_hash_table_foreach(ori_state->hash_table, (GHFunc)icl_state_clone_foreach,
- cloned_state);
- ret = iotcon_representation_set_state(cloned_repr, cloned_state);
+ if (src->state) {
+ ret = iotcon_state_clone(src->state, &cloned_repr->state);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_set_state() Fail");
- iotcon_state_destroy(cloned_state);
+ ERR("iotcon_state_clone() Fail(%d)", ret);
iotcon_representation_destroy(cloned_repr);
return ret;
}
- iotcon_state_destroy(cloned_state);
}
*dest = cloned_repr;