summaryrefslogtreecommitdiff
path: root/src/lib/efl/interfaces/efl_model_composite_boolean.c
blob: 3a2944614a70d077c50daf2b465a6924349d772c (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "Eina.h"
#include "Efl.h"

#include "interfaces/efl_model_composite_boolean_children.eo.h"

typedef struct _Efl_Model_Hash_Value
{
   unsigned char* buffer;
   unsigned int bits_count;
   Eina_Bool default_value : 1;
} Efl_Model_Hash_Value;

typedef struct _Efl_Model_Composite_Boolean_Data
{
   Efl_Model* composite_model;
   Eina_Array* empty_properties;
   Eina_Hash* values; // [property_name, Efl_Model_Hash_Value*]
} Efl_Model_Composite_Boolean_Data;

typedef struct _Efl_Model_Composite_Boolean_Children_Data
{
   Efl_Model_Composite_Boolean_Data* parent_pd;
   Efl_Model* composite_children;
   Eina_Array* properties_names;
   unsigned int index;
} Efl_Model_Composite_Boolean_Children_Data;

typedef struct _Efl_Model_Iterator_Slice
{
   Eina_Iterator vtable;
   Eina_Iterator* composite_iterator;
   Efl_Model* parent;
   Efl_Model_Composite_Boolean_Data* parent_pd;
   Eina_Promise_Owner* promise;
   unsigned int index;
} Efl_Model_Iterator_Slice;

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

static const Eina_Array*
_efl_model_composite_boolean_children_efl_model_properties_get(Eo *obj EINA_UNUSED,
  Efl_Model_Composite_Boolean_Children_Data *pd)
{
   Eina_Array const* composite_properties;
   unsigned int i;
  
   if(pd->properties_names) return pd->properties_names;
   else
     {
        composite_properties = efl_model_properties_get(pd->parent_pd->composite_model);
        unsigned int composite_count = eina_array_count_get(composite_properties),
          self_count = eina_hash_population(pd->parent_pd->values);
        pd->properties_names = eina_array_new(composite_count + self_count);
        for(i = 0; i != composite_count; ++i)
          {
             const char* name = strdup(eina_array_data_get(composite_properties, i));
             eina_array_push(pd->properties_names, name);
          }
        return pd->properties_names;
     }
}

static Eina_Promise*
_efl_model_composite_boolean_children_efl_model_property_get(Eo *obj EINA_UNUSED,
  Efl_Model_Composite_Boolean_Children_Data *pd, const char *property)
{
   Efl_Model_Hash_Value* value = eina_hash_find(pd->parent_pd->values, property);
   if(value)
     {
        Eina_Promise_Owner* promise = eina_promise_add();
        Eina_Value* eina_value = eina_value_new(EINA_VALUE_TYPE_UCHAR);
        if(value->bits_count <= pd->index)
          {
            unsigned char f = value->default_value;
            eina_value_set(eina_value, f);
          }
        else
          {
            eina_value_set(eina_value, (value->buffer[pd->index / 8] >> (pd->index % 8))&1);
          }
        eina_promise_owner_value_set(promise, eina_value, (Eina_Promise_Free_Cb)eina_value_free);
        return eina_promise_owner_promise_get(promise);
     }
   else if(pd->composite_children)
     return efl_model_property_get(pd->composite_children, property);
   else
     {
        Eina_Promise_Owner* promise = eina_promise_add();
        eina_promise_owner_error_set(promise, EFL_MODEL_ERROR_NOT_FOUND);
        return eina_promise_owner_promise_get(promise);
     }
}

static void
_efl_model_composite_boolean_children_efl_model_property_set(Eo *obj EINA_UNUSED,
  Efl_Model_Composite_Boolean_Children_Data *pd, const char *property, const Eina_Value *value, Eina_Promise_Owner *promise)
{
   
}

static Eina_Promise*
_efl_model_composite_boolean_children_efl_model_children_slice_get(Eo *obj EINA_UNUSED,
  Efl_Model_Composite_Boolean_Children_Data *pd, unsigned int start, unsigned int count)
{
   return efl_model_children_slice_get(pd->composite_children, start, count);
}

static Eina_Promise*
_efl_model_composite_boolean_children_efl_model_children_count_get(Eo *obj  EINA_UNUSED,
  Efl_Model_Composite_Boolean_Children_Data *pd)
{
   return efl_model_children_count_get(pd->composite_children);
}

static Eo_Base*
_efl_model_composite_boolean_children_efl_model_child_add(Eo *obj  EINA_UNUSED,
  Efl_Model_Composite_Boolean_Children_Data *pd)
{
   return efl_model_child_add(pd->composite_children);
}

static void
_efl_model_composite_boolean_children_efl_model_child_del(Eo *obj EINA_UNUSED,
  Efl_Model_Composite_Boolean_Children_Data *pd, Eo_Base *child)
{
   return efl_model_child_del(pd->composite_children, child);
}

/**************** efl_mmodel_iterator_slice **************/

static Eina_Bool
efl_model_iterator_slice_next(Efl_Model_Iterator_Slice* iterator, void** data)
{
  Efl_Model* child;
  if(eina_iterator_next(iterator->composite_iterator, (void**)&child))
    {
       Efl_Model* model = eo_add(EFL_MODEL_COMPOSITE_BOOLEAN_CHILDREN_CLASS, iterator->parent);
       Efl_Model_Composite_Boolean_Children_Data* pd =
         eo_data_scope_get(model, EFL_MODEL_COMPOSITE_BOOLEAN_CHILDREN_CLASS);
       pd->parent_pd = iterator->parent_pd;
       pd->composite_children = eo_ref(child);
       pd->index = iterator->index++;
       eo_data_ref(iterator->parent, EFL_MODEL_COMPOSITE_BOOLEAN_CLASS);
       eo_data_unref(model, pd);
       *data = model;
       return EINA_TRUE;
    }
  else
    return EINA_FALSE;
}

static void*
efl_model_iterator_slice_container(Efl_Model_Iterator_Slice* iterator)
{
  return iterator->composite_iterator;
}

static void
efl_model_iterator_slice_free(Efl_Model_Iterator_Slice* iterator)
{
  eina_iterator_free(iterator->composite_iterator);
  free(iterator);
}

static Eina_Bool
efl_model_iterator_slice_lock(Efl_Model_Iterator_Slice* iterator EINA_UNUSED)
{
  return EINA_FALSE;
}

static void
efl_model_iterator_slice_setup(Efl_Model_Iterator_Slice* iterator,
   Efl_Model* parent, Efl_Model_Composite_Boolean_Data* parent_pd, Eina_Promise_Owner* promise)
{
   iterator->vtable.version = EINA_ITERATOR_VERSION;
   iterator->vtable.next = FUNC_ITERATOR_NEXT(efl_model_iterator_slice_next);
   iterator->vtable.get_container = FUNC_ITERATOR_GET_CONTAINER(efl_model_iterator_slice_container);
   iterator->vtable.free = FUNC_ITERATOR_FREE(efl_model_iterator_slice_free);
   iterator->vtable.lock = FUNC_ITERATOR_LOCK(efl_model_iterator_slice_lock);
   EINA_MAGIC_SET(&iterator->vtable, EINA_MAGIC_ITERATOR);
   iterator->parent = eo_ref(parent);
   iterator->parent_pd = parent_pd;
   iterator->promise = promise;
}

static void
_efl_model_composite_boolean_slice_then_cb(void* data, void* value)
{
   Efl_Model_Iterator_Slice* slice_iterator = data;
   Eina_Iterator* iterator = value;

   slice_iterator->composite_iterator = eina_iterator_clone(iterator);
   if(slice_iiterator->composite_iterator)
     eina_promise_owner_value_set(slice_iterator->promise, slice_iterator, (Eina_Promise_Free_Cb)&eina_iterator_free);
   else
     {
        eina_promise_owner_error_set(slice_iterator->promise, EFL_MODEL_ERROR_NOT_SUPPORTED);
        free(slice_iterator);
     }
}

static void
_efl_model_composite_boolean_slice_error_cb(void* data, Eina_Error error)
{
   Efl_Model_Iterator_Slice* slice_iterator = data;
   eina_promise_owner_error_set(slice_iterator->promise, error);
   free(slice_iterator);
}

static void efl_model_hash_value_free(void* p)
{
   Efl_Model_Hash_Value* value = p;
   if(value)
     {
        free(value->buffer);
        free(value);
     }
}

/**************** efl_mmodel_composite_boolean **************/

static const Eina_Array *
_efl_model_composite_boolean_efl_model_properties_get(Eo *obj EINA_UNUSED, Efl_Model_Composite_Boolean_Data *pd)
{
  if(pd->composite_model)
    return efl_model_properties_get(pd->composite_model);
  else if(pd->empty_properties)
    return pd->empty_properties;
  else
    return pd->empty_properties = eina_array_new(1);
    
}

static void
_efl_model_composite_boolean_property_add(Eo *obj EINA_UNUSED, Efl_Model_Composite_Boolean_Data *pd,
   const char *name, Eina_Bool initial_value)
{
   if(!pd->values)
     pd->values = eina_hash_string_small_new(&efl_model_hash_value_free);
   Efl_Model_Hash_Value* value = calloc(sizeof(Efl_Model_Hash_Value), 1);
   value->default_value = initial_value;
   eina_hash_add(pd->values, name, value);
}

static Eina_Promise *
_efl_model_composite_boolean_efl_model_property_get(Eo *obj EINA_UNUSED, Efl_Model_Composite_Boolean_Data *pd EINA_UNUSED, const char *property EINA_UNUSED)
{
   if(pd->composite_model)
     return efl_model_property_get(pd->composite_model, property);
   else
     {
        Eina_Promise_Owner* promise = eina_promise_add();
        eina_promise_owner_error_set(promise, EFL_MODEL_ERROR_NOT_FOUND);
        return eina_promise_owner_promise_get(promise);
     }
}

static void
_efl_model_composite_boolean_efl_model_property_set(Eo *obj EINA_UNUSED, Efl_Model_Composite_Boolean_Data *pd EINA_UNUSED, const char *property EINA_UNUSED, const Eina_Value *value EINA_UNUSED, Eina_Promise_Owner *promise EINA_UNUSED)
{
  
}

static Eina_Promise*
_efl_model_composite_boolean_efl_model_children_slice_get(Eo *obj, Efl_Model_Composite_Boolean_Data *pd, unsigned int start, unsigned int count)
{
   Eina_Promise_Owner* promise = eina_promise_add();
   if(pd->composite_model)
     {
        Eina_Promise* composite_promise = efl_model_children_slice_get(pd->composite_model, start, count);
        Efl_Model_Iterator_Slice* iterator = calloc(sizeof(*iterator), 1);
        efl_model_iterator_slice_setup(iterator, obj, pd, promise);
        eina_promise_then(composite_promise, &_efl_model_composite_boolean_slice_then_cb,
                          &_efl_model_composite_boolean_slice_error_cb, iterator);
     }
   else
     {
        eina_promise_owner_error_set(promise, EFL_MODEL_ERROR_NOT_SUPPORTED);
     }
  return eina_promise_owner_promise_get(promise);
}

static Eina_Promise*
_efl_model_composite_boolean_efl_model_children_count_get(Eo *obj EINA_UNUSED, Efl_Model_Composite_Boolean_Data *pd)
{
  if(pd->composite_model)
    return efl_model_children_count_get(pd->composite_model);
  else
    {
      Eina_Promise_Owner* promise = eina_promise_add();
      unsigned int* count = malloc(sizeof(unsigned int));
      *count = eina_hash_population(pd->values);
      eina_promise_owner_value_set(promise, count, free);
      return eina_promise_owner_promise_get(promise);
    }
}

static Eo_Base*
_efl_model_composite_boolean_efl_model_child_add(Eo *obj EINA_UNUSED, Efl_Model_Composite_Boolean_Data *pd)
{
  if(pd->composite_model)
    return efl_model_child_add(pd->composite_model);
  else
    {
       eina_error_set(EFL_MODEL_ERROR_NOT_SUPPORTED);
       return NULL;
    }
}

static void
_efl_model_composite_boolean_efl_model_child_del(Eo *obj EINA_UNUSED, Efl_Model_Composite_Boolean_Data *pd, Eo_Base *child)
{
  if(pd->composite_model)
    return efl_model_child_del(pd->composite_model, child);
  else
    {
       eina_error_set(EFL_MODEL_ERROR_NOT_SUPPORTED);
    }
}
                                                                       
#include "interfaces/efl_model_composite_boolean.eo.c"
#include "interfaces/efl_model_composite_boolean_children.eo.c"