ecore: remove Efl.Container_Model_Item.

This also enable to create a tree of Container_Model instead of just one level.

Reviewed-by: Vitor Sousa da Silva <vitorsousa@expertisesolutions.com.br>
Differential Revision: https://phab.enlightenment.org/D8046
This commit is contained in:
Cedric BAIL 2019-02-21 17:58:47 -08:00
parent c3ed0791a6
commit 3971106c1a
8 changed files with 164 additions and 381 deletions

View File

@ -45,7 +45,6 @@ ecore_eolian_files_public = \
lib/ecore/efl_loop_model.eo \
lib/ecore/efl_generic_model.eo \
lib/ecore/efl_container_model.eo \
lib/ecore/efl_container_model_item.eo \
lib/ecore/efl_boolean_model.eo \
lib/ecore/efl_select_model.eo \
lib/ecore/efl_composite_model.eo \
@ -135,8 +134,6 @@ lib/ecore/efl_io_buffered_stream.c \
lib/ecore/efl_loop_model.c \
lib/ecore/efl_generic_model.c \
lib/ecore/efl_container_model.c \
lib/ecore/efl_container_model_item.c \
lib/ecore/efl_container_model_private.h \
lib/ecore/efl_composite_model.c \
lib/ecore/efl_boolean_model.c \
lib/ecore/efl_select_model.c \

View File

@ -116,7 +116,6 @@ EAPI Eo *efl_main_loop_get(void);
#include "efl_loop_model.eo.h"
#include "efl_generic_model.eo.h"
#include "efl_container_model.eo.h"
#include "efl_container_model_item.eo.h"
#include "efl_composite_model.eo.h"
#include "efl_boolean_model.eo.h"
#include "efl_select_model.eo.h"

View File

@ -8,59 +8,44 @@
#include <Ecore.h>
#include "ecore_internal.h"
#include "efl_composite_model_private.h"
#include "efl_container_model_private.h"
typedef struct _Efl_Container_Property_Data Efl_Container_Property_Data;
typedef struct _Efl_Container_Model_Data Efl_Container_Model_Data;
#define MY_CLASS EFL_CONTAINER_MODEL_CLASS
void *
_value_copy_alloc(void *v, const Eina_Value_Type *type)
struct _Efl_Container_Property_Data
{
if (!v)
return v;
Eina_Stringshare *name;
if (type == EINA_VALUE_TYPE_STRINGSHARE)
return (void*) eina_stringshare_ref(v);
else if (type == EINA_VALUE_TYPE_STRING)
return (void*) strdup(v);
else
{
void *ret = malloc(type->value_size);
memcpy(ret, v, type->value_size);
return ret;
}
}
const Eina_Value_Type *type;
Eina_Array *values;
};
void
_value_free(void *v, const Eina_Value_Type *type)
struct _Efl_Container_Model_Data
{
if (!v)
return;
Efl_Container_Model_Data *parent;
if (type == EINA_VALUE_TYPE_STRINGSHARE)
return eina_stringshare_del(v);
else
free(v);
}
Eina_Hash *properties; // All properties value for children
unsigned int children_count;
};
static void
_values_free(Eina_Array *values, const Eina_Value_Type *type)
_property_values_free(Eina_Array *values)
{
unsigned int i;
void *v;
Eina_Array_Iterator it;
EINA_ARRAY_ITER_NEXT(values, i, v, it)
{
_value_free(v, type);
}
Eina_Value *v;
while ((v = eina_array_pop(values)))
eina_value_free(v);
eina_array_free(values);
}
static void
_property_data_free_cb(void *data)
{
Child_Property_Data *cpd = data;
_values_free(cpd->values, cpd->type);
Efl_Container_Property_Data *cpd = data;
eina_stringshare_del(cpd->name);
_property_values_free(cpd->values);
free(cpd);
}
@ -68,33 +53,38 @@ static Efl_Object *
_efl_container_model_efl_object_constructor(Eo *obj,
Efl_Container_Model_Data *sd)
{
obj = efl_constructor(efl_super(obj, MY_CLASS));
if (!obj)
return NULL;
Eo *parent;
obj = efl_constructor(efl_super(obj, EFL_CONTAINER_MODEL_CLASS));
if (!obj) return NULL;
parent = efl_parent_get(obj);
if (efl_isa(parent, EFL_CONTAINER_MODEL_CLASS))
sd->parent = efl_data_scope_get(parent, EFL_CONTAINER_MODEL_CLASS);
sd->obj = obj;
sd->properties = eina_hash_stringshared_new(_property_data_free_cb);
return obj;
}
static Efl_Object *
_efl_container_model_efl_object_finalize(Eo *obj, Efl_Container_Model_Data *sd EINA_UNUSED)
{
if (!efl_ui_view_model_get(obj))
{
// Add a dummy object as source if there isn't any to allow using this class without source.
efl_ui_view_model_set(obj, efl_add(EFL_GENERIC_MODEL_CLASS, obj));
}
return efl_finalize(efl_super(obj, EFL_CONTAINER_MODEL_CLASS));
}
static void
_efl_container_model_efl_object_destructor(Eo *obj,
Efl_Container_Model_Data *sd)
{
Eina_Stringshare *key;
Eina_Iterator *it;
eina_list_free(sd->childrens);
it = eina_hash_iterator_key_new(sd->properties);
EINA_ITERATOR_FOREACH(it, key)
eina_stringshare_del(key);
eina_iterator_free(it);
eina_hash_free(sd->properties);
efl_destructor(efl_super(obj, MY_CLASS));
efl_destructor(efl_super(obj, EFL_CONTAINER_MODEL_CLASS));
}
static const Eina_Value_Type *
@ -102,7 +92,7 @@ _efl_container_model_child_property_value_type_get(Eo *obj EINA_UNUSED,
Efl_Container_Model_Data *sd,
const char *property)
{
Child_Property_Data *cpd;
Efl_Container_Property_Data *cpd;
Eina_Stringshare *key;
key = eina_stringshare_add(property);
@ -113,22 +103,6 @@ _efl_container_model_child_property_value_type_get(Eo *obj EINA_UNUSED,
return cpd->type;
}
static Eina_Iterator *
_efl_container_model_child_property_values_get(Eo *obj EINA_UNUSED,
Efl_Container_Model_Data *sd,
const char *property)
{
Child_Property_Data *cpd;
Eina_Stringshare *key;
key = eina_stringshare_add(property);
cpd = eina_hash_find(sd->properties, key);
eina_stringshare_del(key);
if (!cpd) return NULL;
return eina_array_iterator_new(cpd->values);
}
static Eina_Bool
_efl_container_model_child_property_add(Eo *obj,
Efl_Container_Model_Data *sd,
@ -137,9 +111,9 @@ _efl_container_model_child_property_add(Eo *obj,
Eina_Iterator *values)
{
Eina_Array *arr = NULL;
void *data = NULL;
Child_Property_Data *cpd = NULL;
unsigned int i, in_count, children_count;
void *original;
Efl_Container_Property_Data *cpd = NULL;
unsigned int i;
Eina_Error err = EFL_MODEL_ERROR_INCORRECT_VALUE;
name = eina_stringshare_add(name);
@ -154,15 +128,24 @@ _efl_container_model_child_property_add(Eo *obj,
arr = eina_array_new(4);
if (!arr) goto on_error;
EINA_ITERATOR_FOREACH(values, data)
EINA_ITERATOR_FOREACH(values, original)
{
void *new_data = _value_copy_alloc(data, type);
if ((data && !new_data) || !eina_array_push(arr, new_data))
Eina_Value *copy = eina_value_new(type);
Eina_Bool r;
if (type == EINA_VALUE_TYPE_STRINGSHARE ||
type == EINA_VALUE_TYPE_STRING)
r = eina_value_set(copy, original);
else
r = eina_value_pset(copy, original);
if (!r)
{
if (new_data)
_value_free(new_data, type);
goto on_error;
eina_value_free(copy);
copy = eina_value_error_new(EINA_ERROR_VALUE_FAILED);
}
eina_array_push(arr, copy);
}
eina_iterator_free(values);
@ -171,10 +154,11 @@ _efl_container_model_child_property_add(Eo *obj,
cpd = eina_hash_find(sd->properties, name);
if (!cpd)
{
cpd = calloc(1, sizeof(Child_Property_Data));
cpd = calloc(1, sizeof(Efl_Container_Property_Data));
if (!cpd)
goto on_error;
cpd->name = eina_stringshare_ref(name);
cpd->type = type;
cpd->values = arr;
@ -183,100 +167,147 @@ _efl_container_model_child_property_add(Eo *obj,
}
else
{
eina_stringshare_del(name);
_values_free(cpd->values, cpd->type);
_property_values_free(cpd->values);
cpd->type = type;
cpd->values = arr;
}
in_count = eina_array_count(arr);
children_count = eina_list_count(sd->childrens);
for (i = children_count; i < in_count; ++i)
for (i = sd->children_count; i < eina_array_count(arr); ++i)
{
Efl_Model_Children_Event cevt = { 0 };
Efl_Model *child;
child = efl_add(EFL_CONTAINER_MODEL_ITEM_CLASS, obj,
efl_container_model_item_define(efl_added, sd, i));
sd->childrens = eina_list_append(sd->childrens, child);
cevt.index = i;
efl_event_callback_call(obj, EFL_MODEL_EVENT_CHILD_ADDED, &cevt);
}
if (in_count > children_count)
efl_event_callback_call(obj, EFL_MODEL_EVENT_CHILDREN_COUNT_CHANGED, NULL);
if (eina_array_count(arr) > sd->children_count)
{
sd->children_count = eina_array_count(arr);
efl_event_callback_call(obj, EFL_MODEL_EVENT_CHILDREN_COUNT_CHANGED, NULL);
}
eina_stringshare_del(name);
return EINA_TRUE;
on_error:
eina_stringshare_del(name);
if (cpd) free(cpd);
if (arr)
_values_free(arr, type);
if (arr) _property_values_free(arr);
eina_error_set(err);
return EINA_FALSE;
}
static Eina_Iterator *
_efl_container_model_efl_model_properties_get(const Eo *obj EINA_UNUSED, Efl_Container_Model_Data *sd)
_efl_container_model_efl_model_properties_get(const Eo *obj EINA_UNUSED,
Efl_Container_Model_Data *sd)
{
return eina_hash_iterator_key_new(sd->properties);
EFL_COMPOSITE_MODEL_PROPERTIES_SUPER(props,
obj, EFL_CONTAINER_MODEL_CLASS,
eina_hash_iterator_key_new(sd->parent->properties));
return props;
}
static Eina_Future *
_efl_container_model_efl_model_property_set(Eo *obj,
Efl_Container_Model_Data *pd EINA_UNUSED,
const char *property EINA_UNUSED,
Eina_Value *value EINA_UNUSED)
Efl_Container_Model_Data *pd,
const char *property,
Eina_Value *value)
{
return efl_loop_future_rejected(obj,
EFL_MODEL_ERROR_NOT_FOUND);
if (pd->parent)
{
Efl_Container_Property_Data *cpd;
Eina_Stringshare *name;
unsigned int index;
Eina_Value r = EINA_VALUE_EMPTY;
name = eina_stringshare_add(property);
cpd = eina_hash_find(pd->parent->properties, name);
eina_stringshare_del(name);
// Check if this is a property of this object
if (!cpd) goto not_mine;
// Check if we can get the expected type for this property
if (!cpd->values)
return efl_loop_future_rejected(obj, EFL_MODEL_ERROR_INCORRECT_VALUE);
index = efl_composite_model_index_get(obj);
// Try converting the type before touching any data inside the Model
if (eina_value_type_get(value) != cpd->type)
{
eina_value_setup(&r, cpd->type);
if (!eina_value_convert(value, &r))
return efl_loop_future_rejected(obj, EFL_MODEL_ERROR_INCORRECT_VALUE);
}
else
{
eina_value_copy(&r, value);
}
// Expand the array to have enough space for this value if it wasn't already the case
while (index >= eina_array_count(cpd->values))
eina_array_push(cpd->values, eina_value_error_new(EFL_MODEL_ERROR_INCORRECT_VALUE));
// Clean the previous value and introduce the new one
eina_value_free(eina_array_data_get(cpd->values, index));
// Same type as the expected type, we can just replace the value
eina_array_data_set(cpd->values, index, eina_value_dup(&r));
eina_value_free(value);
return efl_loop_future_resolved(obj, r);
}
not_mine:
return efl_model_property_set(efl_super(obj, EFL_CONTAINER_MODEL_CLASS), property, value);
}
static Eina_Value *
_efl_container_model_efl_model_property_get(const Eo *obj EINA_UNUSED,
Efl_Container_Model_Data *pd EINA_UNUSED,
const char *property EINA_UNUSED)
_efl_container_model_efl_model_property_get(const Eo *obj,
Efl_Container_Model_Data *pd,
const char *property)
{
return eina_value_error_new(EFL_MODEL_ERROR_NOT_FOUND);
}
if (pd->parent)
{
Efl_Container_Property_Data *cpd;
Eina_Stringshare *name;
Eina_Value *copy;
unsigned int index;
static Eina_Future *
_efl_container_model_efl_model_children_slice_get(Eo *obj,
Efl_Container_Model_Data *sd,
unsigned int start,
unsigned int count)
{
Eina_Value v;
name = eina_stringshare_add(property);
cpd = eina_hash_find(pd->parent->properties, name);
eina_stringshare_del(name);
v = efl_model_list_value_get(sd->childrens, start, count);
return efl_loop_future_resolved(obj, v);
// Check if this is a property of this object
if (!cpd) goto not_mine;
// Check if we can get the expected type for this property
if (!cpd->values) return eina_value_error_new(EFL_MODEL_ERROR_INCORRECT_VALUE);
index = efl_composite_model_index_get(obj);
if (index >= eina_array_count(cpd->values))
return eina_value_error_new(EFL_MODEL_ERROR_INCORRECT_VALUE);
copy = eina_value_dup(eina_array_data_get(cpd->values, index));
return copy;
}
not_mine:
return efl_model_property_get(efl_super(obj, EFL_CONTAINER_MODEL_CLASS), property);
}
static unsigned int
_efl_container_model_efl_model_children_count_get(const Eo *obj EINA_UNUSED, Efl_Container_Model_Data *sd)
{
return eina_list_count(sd->childrens);
}
unsigned int pcount;
static Eo *
_efl_container_model_efl_model_child_add(Eo *obj EINA_UNUSED, Efl_Container_Model_Data *sd EINA_UNUSED)
{
EINA_LOG_WARN("child_add not supported by Efl.Model.Container");
eina_error_set(EFL_MODEL_ERROR_NOT_SUPPORTED);
pcount = efl_model_children_count_get(efl_super(obj, EFL_CONTAINER_MODEL_CLASS));
return NULL;
}
static void
_efl_container_model_efl_model_child_del(Eo *obj EINA_UNUSED, Efl_Container_Model_Data *sd EINA_UNUSED, Eo *child EINA_UNUSED)
{
EINA_LOG_WARN("child_del not supported by Efl.Model.Container");
eina_error_set(EFL_MODEL_ERROR_NOT_SUPPORTED);
return pcount > sd->children_count ? pcount : sd->children_count;
}
#include "efl_container_model.eo.c"

View File

@ -1,12 +1,12 @@
import eina_types;
class @beta Efl.Container_Model extends Efl.Loop_Model
class @beta Efl.Container_Model extends Efl.Composite_Model
{
[[
Class used to create data models from Eina containers.
Each container supplied represents a series of property values, each item
being the property value for a child object (@Efl.Container_Model_Item).
being the property value for a child object.
The data in the given containers are copied and stored internally.
@ -21,14 +21,6 @@ class @beta Efl.Container_Model extends Efl.Loop_Model
}
return: ptr(const(Eina.Value_Type)); [[Property type]]
}
child_property_values_get {
[[Gets the values for the given property.]]
params {
name: string; [[Property name]]
}
return: iterator<void_ptr> @owned @warn_unused;
[[The currently wrapped values]]
}
child_property_add {
[[Adds the given property to child objects and supply the values.
@ -48,12 +40,10 @@ class @beta Efl.Container_Model extends Efl.Loop_Model
}
implements {
Efl.Object.constructor;
Efl.Object.finalize;
Efl.Object.destructor;
Efl.Model.properties { get; }
Efl.Model.property { set; get; }
Efl.Model.child_add;
Efl.Model.child_del;
Efl.Model.children_slice_get;
Efl.Model.children_count { get; }
}
}

View File

@ -1,164 +0,0 @@
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <Efl.h>
#include <Ecore.h>
#include "efl_container_model_private.h"
#define MY_CLASS EFL_CONTAINER_MODEL_ITEM_CLASS
static void
_efl_container_model_item_define(Eo *obj EINA_UNUSED, Efl_Container_Model_Item_Data *sd, void *parent_data, unsigned int index)
{
sd->parent_data = parent_data;
sd->index = index;
}
EOLIAN static void
_efl_container_model_item_efl_object_invalidate(Eo *obj, Efl_Container_Model_Item_Data *sd)
{
efl_invalidate(efl_super(obj, MY_CLASS));
sd->parent_data = NULL;
sd->index = 0;
}
static Eina_Iterator *
_efl_container_model_item_efl_model_properties_get(const Eo *obj EINA_UNUSED, Efl_Container_Model_Item_Data *sd)
{
// FIXME: Not to sure here, shouldn't we extend a child of the parent actually ?
return efl_model_properties_get(sd->parent_data->obj);
}
static Efl_Object *
_efl_container_model_item_efl_object_finalize(Eo *obj, Efl_Container_Model_Item_Data *pd)
{
if (!pd->parent_data)
return NULL;
return obj;
}
static Eina_Future *
_efl_container_model_item_efl_model_property_set(Eo *obj,
Efl_Container_Model_Item_Data *sd,
const char *property, Eina_Value *value)
{
Eina_Stringshare *name;
Child_Property_Data *cpd;
Eina_Value v = EINA_VALUE_EMPTY;
void *data, *new_data;
name = eina_stringshare_add(property);
cpd = eina_hash_find(sd->parent_data->properties, name);
eina_stringshare_del(name);
if (!cpd || !cpd->values ||
sd->index >= eina_array_count_get(cpd->values))
return efl_loop_future_rejected(obj,
EFL_MODEL_ERROR_NOT_FOUND);
eina_value_setup(&v,cpd->type);
if (!eina_value_convert(value, &v))
return efl_loop_future_rejected(obj,
EFL_MODEL_ERROR_INCORRECT_VALUE);
// FIXME: This is trying to optimize and avoid the use of Eina_Value,
// but this put restriction on the type that can be stored in this container.
data = calloc(1, cpd->type->value_size);
if (!data || !eina_value_pget(&v, data))
goto on_error;
new_data = _value_copy_alloc(data, cpd->type);
_value_free(eina_array_data_get(cpd->values, sd->index), cpd->type);
eina_array_data_set(cpd->values, sd->index, new_data);
free(data);
return efl_loop_future_resolved(obj, v);
on_error:
eina_value_flush(&v);
free(data);
return efl_loop_future_rejected(obj,
EFL_MODEL_ERROR_UNKNOWN);
}
static Eina_Value *
_efl_container_model_item_efl_model_property_get(const Eo *obj EINA_UNUSED,
Efl_Container_Model_Item_Data *sd,
const char *property)
{
Eina_Stringshare *name;
Child_Property_Data *cpd;
Eina_Value *value;
void *data;
Eina_Bool r = EINA_FALSE;
name = eina_stringshare_add(property);
cpd = eina_hash_find(sd->parent_data->properties, name);
eina_stringshare_del(name);
if (!cpd || !cpd->values ||
sd->index >= eina_array_count_get(cpd->values))
return eina_value_error_new(EFL_MODEL_ERROR_NOT_FOUND);
data = eina_array_data_get(cpd->values, sd->index);
value = eina_value_new(cpd->type);
if (cpd->type == EINA_VALUE_TYPE_STRINGSHARE ||
cpd->type == EINA_VALUE_TYPE_STRING)
r = eina_value_set(value, data);
else
r = eina_value_pset(value, data);
if (!r)
{
eina_value_free(value);
value = eina_value_error_new(EFL_MODEL_ERROR_UNKNOWN);
}
return value;
}
EOLIAN static Eina_Future *
_efl_container_model_item_efl_model_children_slice_get(Eo *obj,
Efl_Container_Model_Item_Data *sd EINA_UNUSED,
unsigned int start EINA_UNUSED,
unsigned int count EINA_UNUSED)
{
return efl_loop_future_resolved(obj, EINA_VALUE_EMPTY);
}
EOLIAN static unsigned int
_efl_container_model_item_efl_model_children_count_get(const Eo *obj EINA_UNUSED,
Efl_Container_Model_Item_Data *sd EINA_UNUSED)
{
return 0;
}
EOLIAN static Eo *
_efl_container_model_item_efl_model_child_add(Eo *obj EINA_UNUSED,
Efl_Container_Model_Item_Data *sd EINA_UNUSED)
{
EINA_LOG_WARN("child_add not supported by Efl.Model.Container.Item");
eina_error_set(EFL_MODEL_ERROR_NOT_SUPPORTED);
return NULL;
}
EOLIAN static void
_efl_container_model_item_efl_model_child_del(Eo *obj EINA_UNUSED,
Efl_Container_Model_Item_Data *sd EINA_UNUSED,
Eo *child EINA_UNUSED)
{
EINA_LOG_WARN("child_del not supported by Efl.Model.Container.Item");
eina_error_set(EFL_MODEL_ERROR_NOT_SUPPORTED);
}
#include "efl_container_model_item.eo.c"

View File

@ -1,34 +0,0 @@
class @beta Efl.Container_Model_Item extends Efl.Object implements Efl.Model
{
[[
Used as a child of @Efl.Container_Model.
Provides the @Efl.Model API for elements of @Efl.Container_Model.
Should not be used in another context, so do not manually create objects
of this class.
]]
methods {
define {
[[Defines @Efl.Container_Model_Item internal data.]]
params {
parent_data: void_ptr; [[Pointer to the private data of the
@Efl.Container_Model parent object.]]
index: uint; [[Index of this item within the @Efl.Container_Model
children.]]
}
}
}
implements {
Efl.Object.finalize;
Efl.Model.properties { get; }
Efl.Model.property { set; get; }
Efl.Model.child_add;
Efl.Model.child_del;
Efl.Model.children_slice_get;
Efl.Model.children_count { get; }
Efl.Object.invalidate;
}
constructors {
.define;
}
}

View File

@ -1,33 +0,0 @@
#ifndef EFL_CONTAINER_MODEL_PRIVATE_H__
#define EFL_CONTAINER_MODEL_PRIVATE_H__
typedef struct _Child_Property_Data Child_Property_Data;
struct _Child_Property_Data
{
const Eina_Value_Type *type;
Eina_Array *values;
};
typedef struct _Efl_Container_Model_Data Efl_Container_Model_Data;
struct _Efl_Container_Model_Data
{
Eo *obj;
Eina_Hash *properties;
Eina_List *childrens;
};
typedef struct _Efl_Container_Model_Item_Data Efl_Container_Model_Item_Data;
struct _Efl_Container_Model_Item_Data
{
Efl_Container_Model_Data *parent_data;
unsigned int index;
};
void *_value_copy_alloc(void *v, const Eina_Value_Type *type);
void _value_free(void *v, const Eina_Value_Type *type);
#endif

View File

@ -70,7 +70,6 @@ pub_eo_files = [
'efl_loop_model.eo',
'efl_generic_model.eo',
'efl_container_model.eo',
'efl_container_model_item.eo',
'efl_boolean_model.eo',
'efl_select_model.eo',
'efl_composite_model.eo',
@ -150,8 +149,6 @@ ecore_src = [
'efl_loop_model.c',
'efl_generic_model.c',
'efl_container_model.c',
'efl_container_model_item.c',
'efl_container_model_private.h',
'efl_composite_model.c',
'efl_boolean_model.c',
'efl_select_model.c',