summaryrefslogtreecommitdiff
path: root/src/lib/ecore/efl_model_composite_boolean.c
blob: ef9ec433a5f9d6b0cc8fabe1fd6814ddcb2134a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Efl_Core.h>

#include "efl_model_composite_boolean_children.eo.h"

typedef struct _Efl_Model_Composite_Boolean_Data Efl_Model_Composite_Boolean_Data;
typedef struct _Efl_Model_Composite_Boolean_Children_Data Efl_Model_Composite_Boolean_Children_Data;
typedef struct _Efl_Model_Composite_Boolean_Value Efl_Model_Composite_Boolean_Value;

struct _Efl_Model_Composite_Boolean_Data
{
   Eina_Hash *values;
};

struct _Efl_Model_Composite_Boolean_Value
{
   Eina_Stringshare *property;

   // This is not the best for supporting sparse bitfield with random insertion
   // but will do for now (Would be best to have a tree of fixed size array
   // or something along that line).
   unsigned char *buffer;
   unsigned int   buffer_count;

   Eina_Bool      default_value;
};

struct _Efl_Model_Composite_Boolean_Children_Data
{
   Efl_Model_Composite_Boolean_Data *parent;
   unsigned int                      index;
};

/**************** efl_mmodel_composite_boolean_children **************/

static void
_efl_model_composite_boolean_children_index_set(Eo *obj EINA_UNUSED,
                                                Efl_Model_Composite_Boolean_Children_Data *pd,
                                                unsigned int index)
{
   if (pd->parent) // This is set during finalize
     return ;

   pd->index = index;
}

static unsigned int
_efl_model_composite_boolean_children_index_get(const Eo *obj EINA_UNUSED,
                                                Efl_Model_Composite_Boolean_Children_Data *pd)
{
   return pd->index;
}

static Efl_Object *
_efl_model_composite_boolean_children_efl_object_finalize(Eo *obj,
                                                          Efl_Model_Composite_Boolean_Children_Data *pd)
{
   Eo *parent;

   parent = efl_parent_get(obj);
   if (!parent) return NULL;
   pd->parent = efl_data_scope_get(parent, EFL_MODEL_COMPOSITE_BOOLEAN_CLASS);

   return obj;
}

static Eina_Iterator *
_efl_model_composite_boolean_children_efl_model_properties_get(const Eo *obj,
                                                               Efl_Model_Composite_Boolean_Children_Data *pd)
{
   Eina_Iterator *its;
   Eina_Iterator *itr;

   its = efl_model_properties_get(efl_super(obj, EFL_MODEL_COMPOSITE_BOOLEAN_CHILDREN_CLASS));
   itr = eina_hash_iterator_key_new(pd->parent->values);

   if (!its) return itr;
   if (!itr) return its;

   return eina_multi_iterator_new(its, itr);
}

static Eina_Value *
_efl_model_composite_boolean_children_efl_model_property_get(const Eo *obj,
                                                             Efl_Model_Composite_Boolean_Children_Data *pd,
                                                             const char *property)
{
   Efl_Model_Composite_Boolean_Value *v;
   Eina_Stringshare *s;
   Eina_Bool flag;

   if (property == NULL) return NULL;
   if (strcmp(property, "child.index") == 0)
     return eina_value_uint_new(pd->index);

   s = eina_stringshare_add(property);
   v = eina_hash_find(pd->parent->values, s);
   eina_stringshare_del(s);

   if (!v)
     return efl_model_property_get(efl_super(obj, EFL_MODEL_COMPOSITE_BOOLEAN_CHILDREN_CLASS),
                                   property);

   // As an optimization we do optimistically allocate the boolean array
   // Better would be to have a sparse boolean array
   if ((pd->index >> 3) >= v->buffer_count)
     flag = v->default_value;
   else
     flag = v->buffer[pd->index >> 3] & (1 << pd->index & 0x7);

   return eina_value_uchar_new(!!flag);
}

static Eina_Future *
_efl_model_composite_boolean_children_efl_model_property_set(Eo *obj,
                                                             Efl_Model_Composite_Boolean_Children_Data *pd,
                                                             const char *property, Eina_Value *value)
{
   Efl_Model_Composite_Boolean_Value *v;
   Eina_Stringshare *s;
   Eina_Value b = EINA_VALUE_EMPTY;
   Eina_Bool flag;

   if (!property)
     return eina_future_rejected(efl_loop_future_scheduler_get(obj),
                                 EFL_MODEL_ERROR_UNKNOWN);
   if (strcmp(property, "child.index") == 0)
     return eina_future_rejected(efl_loop_future_scheduler_get(obj),
                                 EFL_MODEL_ERROR_READ_ONLY);

   s = eina_stringshare_add(property);
   v = eina_hash_find(pd->parent->values, s);
   eina_stringshare_del(s);

   if (!v)
     return efl_model_property_set(efl_super(obj, EFL_MODEL_COMPOSITE_BOOLEAN_CHILDREN_CLASS),
                                   property, value);

   eina_value_setup(&b, EINA_VALUE_TYPE_UCHAR);
   if (!eina_value_convert(value, &b))
     return eina_future_rejected(efl_loop_future_scheduler_get(obj),
                                 EFL_MODEL_ERROR_UNKNOWN);
   if (!eina_value_get(value, &flag))
     return eina_future_rejected(efl_loop_future_scheduler_get(obj),
                                 EFL_MODEL_ERROR_UNKNOWN);

   eina_value_flush(&b);

   // We are optimistically allocating the boolean buffer now.
   // Aligning it on 64bits
   if (v->buffer_count < (((pd->index) >> 3) | 0x7) + 1)
     {
        unsigned int rcount = (((pd->index | 0xF) >> 3) | 0x7) + 1;
        unsigned char *tmp;

        tmp = realloc(v->buffer, rcount);
        if (!tmp) return eina_future_rejected(efl_loop_future_scheduler_get(obj), ENOMEM);
        v->buffer = tmp;
        memset(v->buffer + v->buffer_count, 0, rcount - v->buffer_count);
        v->buffer_count = rcount;
     }

   // It is assumed that during slice get the buffer is properly sized
   if (flag)
     v->buffer[pd->index >> 3] |= 1 << (pd->index & 0x7);
   else
     v->buffer[pd->index >> 3] &= ~(1 << (pd->index & 0x7));

   return eina_future_resolved(efl_loop_future_scheduler_get(obj),
                               eina_value_uchar_init(!!flag));
}

/**************** efl_model_composite_boolean **************/
typedef struct _Efl_Model_Slice_Request Efl_Model_Slice_Request;
struct _Efl_Model_Slice_Request
{
   Eo *parent;
   unsigned int start;
};

static Eina_Value
_efl_model_composite_boolean_then(void *data, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   Efl_Model_Slice_Request *req = data;
   unsigned int i, len;
   Eina_Value r = EINA_VALUE_EMPTY;
   Eo *target = NULL;

   if (eina_value_type_get(&v) != EINA_VALUE_TYPE_ARRAY)
     goto on_error;

   eina_value_array_setup(&r, EINA_VALUE_TYPE_OBJECT, 4);

   EINA_VALUE_ARRAY_FOREACH(&v, len, i, target)
     {
        Eo *composite;

        // It would have been nice if I could have just overriden the object
        // function, but this would allow only one composite model
        composite = efl_add(EFL_MODEL_COMPOSITE_BOOLEAN_CHILDREN_CLASS, req->parent,
                            efl_model_composite_boolean_children_index_set(efl_added, req->start + i),
                            efl_ui_view_model_set(efl_added, target));

        eina_value_array_append(&r, composite);
     }

 on_error:
   efl_unref(req->parent);
   free(req);

   return r;
}

static void
_boolean_value_free(void *data)
{
   Efl_Model_Composite_Boolean_Value *value = data;

   eina_stringshare_del(value->property);
   value->property = NULL;

   free(value->buffer);
   value->buffer = NULL;
   value->buffer_count = 0;
}

static Eo *
_efl_model_composite_boolean_efl_object_constructor(Eo *obj, Efl_Model_Composite_Boolean_Data *pd)
{
   obj = efl_constructor(efl_super(obj, EFL_MODEL_COMPOSITE_BOOLEAN_CLASS));

   if (!obj) return NULL;

   pd->values = eina_hash_stringshared_new(_boolean_value_free);

   return obj;
}

static void
_efl_model_composite_boolean_efl_object_destructor(Eo *obj, Efl_Model_Composite_Boolean_Data *pd)
{
   eina_hash_free(pd->values);

   efl_destructor(efl_super(obj, EFL_MODEL_COMPOSITE_BOOLEAN_CLASS));
}

static void
_efl_model_composite_boolean_boolean_add(Eo *obj EINA_UNUSED,
                                         Efl_Model_Composite_Boolean_Data *pd,
                                         const char *name, Eina_Bool default_value)
{
   Efl_Model_Composite_Boolean_Value *value;

   if (!name) return ;

   value = calloc(1, sizeof (Efl_Model_Composite_Boolean_Value));
   if (!value) return ;

   value->property = eina_stringshare_add(name);
   value->default_value = default_value;

   eina_hash_direct_add(pd->values, value->property, value);
}

static void
_efl_model_composite_boolean_boolean_del(Eo *obj EINA_UNUSED,
                                         Efl_Model_Composite_Boolean_Data *pd,
                                         const char *name)
{
   Eina_Stringshare *s;

   s = eina_stringshare_add(name);
   eina_hash_del(pd->values, s, NULL);
   eina_stringshare_del(s);
}

static Eina_Future *
_efl_model_composite_boolean_efl_model_children_slice_get(Eo *obj,
                                                          Efl_Model_Composite_Boolean_Data *pd EINA_UNUSED,
                                                          unsigned int start, unsigned int count)
{
   Efl_Model_Slice_Request *req;
   Eina_Future *f;

   f = efl_model_children_slice_get(efl_super(obj, EFL_MODEL_COMPOSITE_BOOLEAN_CLASS),
                                    start, count);

   req = malloc(sizeof (Efl_Model_Slice_Request));
   if (!req) return eina_future_rejected(efl_loop_future_scheduler_get(obj),
                                         ENOMEM);
   req->parent = efl_ref(obj);
   req->start = start;

   return efl_future_Eina_FutureXXX_then
     (obj, eina_future_then(f, _efl_model_composite_boolean_then, req, NULL));
}

#include "efl_model_composite_boolean.eo.c"
#include "efl_model_composite_boolean_children.eo.c"