diff options
Diffstat (limited to 'libraries/eina/src/examples')
-rw-r--r-- | libraries/eina/src/examples/eina_inarray_01.c | 52 | ||||
-rw-r--r-- | libraries/eina/src/examples/eina_model_01.c | 235 | ||||
-rw-r--r-- | libraries/eina/src/examples/eina_model_02.c | 61 | ||||
-rw-r--r-- | libraries/eina/src/examples/eina_model_03.c | 236 | ||||
-rw-r--r-- | libraries/eina/src/examples/eina_model_04_animal.c | 76 | ||||
-rw-r--r-- | libraries/eina/src/examples/eina_model_04_child.c | 81 | ||||
-rw-r--r-- | libraries/eina/src/examples/eina_model_04_human.c | 157 | ||||
-rw-r--r-- | libraries/eina/src/examples/eina_model_04_main.c | 110 | ||||
-rw-r--r-- | libraries/eina/src/examples/eina_model_04_parrot.c | 95 | ||||
-rw-r--r-- | libraries/eina/src/examples/eina_model_04_whistler.c | 59 | ||||
-rw-r--r-- | libraries/eina/src/examples/eina_value_01.c | 53 | ||||
-rw-r--r-- | libraries/eina/src/examples/eina_value_02.c | 100 | ||||
-rw-r--r-- | libraries/eina/src/examples/eina_value_03.c | 178 |
13 files changed, 1493 insertions, 0 deletions
diff --git a/libraries/eina/src/examples/eina_inarray_01.c b/libraries/eina/src/examples/eina_inarray_01.c new file mode 100644 index 0000000..b9ab4e7 --- /dev/null +++ b/libraries/eina/src/examples/eina_inarray_01.c | |||
@@ -0,0 +1,52 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_inarray_01.c -o eina_inarray_01 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <Eina.h> | ||
5 | |||
6 | int | ||
7 | cmp(const void *a, const void *b) | ||
8 | { | ||
9 | return *(int*)a > *(int*)b; | ||
10 | } | ||
11 | |||
12 | int main(int argc, char **argv) | ||
13 | { | ||
14 | Eina_Inarray *iarr; | ||
15 | char ch, *ch2; | ||
16 | int a, *b; | ||
17 | |||
18 | eina_init(); | ||
19 | iarr = eina_inarray_new(sizeof(char), 0); | ||
20 | |||
21 | ch = 'a'; | ||
22 | eina_inarray_append(iarr, &ch); | ||
23 | ch = 'b'; | ||
24 | eina_inarray_append(iarr, &ch); | ||
25 | ch = 'c'; | ||
26 | eina_inarray_append(iarr, &ch); | ||
27 | ch = 'd'; | ||
28 | eina_inarray_append(iarr, &ch); | ||
29 | |||
30 | printf("Inline array of chars:\n"); | ||
31 | EINA_INARRAY_FOREACH(iarr, ch2) | ||
32 | printf("char: %c(pointer: %p)\n", *ch2, ch2); | ||
33 | |||
34 | eina_inarray_flush(iarr); | ||
35 | eina_inarray_setup(iarr, sizeof(int), 4); | ||
36 | |||
37 | a = 97; | ||
38 | eina_inarray_append(iarr, &a); | ||
39 | a = 98; | ||
40 | eina_inarray_append(iarr, &a); | ||
41 | a = 100; | ||
42 | eina_inarray_append(iarr, &a); | ||
43 | a = 99; | ||
44 | eina_inarray_insert_sorted(iarr, &a, cmp); | ||
45 | |||
46 | printf("Inline array of integers with %d elements:\n", eina_inarray_count(iarr)); | ||
47 | EINA_INARRAY_FOREACH(iarr, b) | ||
48 | printf("int: %d(pointer: %p)\n", *b, b); | ||
49 | |||
50 | eina_inarray_free(iarr); | ||
51 | eina_shutdown(); | ||
52 | } | ||
diff --git a/libraries/eina/src/examples/eina_model_01.c b/libraries/eina/src/examples/eina_model_01.c new file mode 100644 index 0000000..28895aa --- /dev/null +++ b/libraries/eina/src/examples/eina_model_01.c | |||
@@ -0,0 +1,235 @@ | |||
1 | /* | ||
2 | * Compile with: | ||
3 | * gcc -o eina_model_01 eina_model_01.c `pkg-config --cflags --libs eina` | ||
4 | */ | ||
5 | |||
6 | /* | ||
7 | * This example demonstrates the usage of Eina Model by implementing | ||
8 | * Bank Account Class, which is inherited from Base Class; | ||
9 | * and Credit Card Class, which is inherited from Bank Account Class. | ||
10 | * | ||
11 | * Base Class(Eina_Model_Type) --> Bank Account Class --> Credit Card Class | ||
12 | * | ||
13 | * Bank Account Class implements "bank_account_data_set()" and "print()" methods; | ||
14 | * Credit Card Class inherits these two and implements "credit_card_data_set()" | ||
15 | * | ||
16 | * | ||
17 | * Bank Account Class::print() calls for "_bank_account_data_print" | ||
18 | * Credit Card Class ::print() is reloaded with "_credit_card_data_print()" | ||
19 | * which calls for parent function "_bank_account_data_print" | ||
20 | * | ||
21 | */ | ||
22 | |||
23 | |||
24 | #include <Eina.h> | ||
25 | #include <eina_safety_checks.h> | ||
26 | |||
27 | /* | ||
28 | * Defining type for new model type | ||
29 | * Model will have two methods | ||
30 | */ | ||
31 | typedef struct _Bank_Account_Type | ||
32 | { | ||
33 | Eina_Model_Type parent_class; | ||
34 | void (*bank_account_data_set)(Eina_Model *, const char *name, const char *number); | ||
35 | void (*print)(Eina_Model *); | ||
36 | } Bank_Account_Type; | ||
37 | |||
38 | /* | ||
39 | * Defining type for Bank Account private data | ||
40 | */ | ||
41 | typedef struct _Bank_Account_Data | ||
42 | { | ||
43 | char name[30]; | ||
44 | char number[30]; | ||
45 | } Bank_Account_Data; | ||
46 | |||
47 | /* | ||
48 | * Defining type for Credit Card model type, which will be inherited from Bank Account model type | ||
49 | * Model will have two parent's methods and additional one | ||
50 | */ | ||
51 | typedef struct _Credit_Card_Type | ||
52 | { | ||
53 | Bank_Account_Type parent_class; | ||
54 | void (*credit_card_data_set)(Eina_Model *, const char *, const char *, int) ; | ||
55 | } Credit_Card_Type; | ||
56 | |||
57 | /* | ||
58 | * Defining type for Credit Card private data | ||
59 | */ | ||
60 | typedef struct _Credit_Card_Data | ||
61 | { | ||
62 | char number[30]; | ||
63 | char expiry_date[30]; | ||
64 | int pin; | ||
65 | } Credit_Card_Data; | ||
66 | |||
67 | static Bank_Account_Type _BANK_ACCOUNT_TYPE; | ||
68 | static Credit_Card_Type _CREDIT_CARD_TYPE; | ||
69 | static Eina_Model_Type *BANK_ACCOUNT_TYPE = (Eina_Model_Type *) &_BANK_ACCOUNT_TYPE; | ||
70 | static Eina_Model_Type *CREDIT_CARD_TYPE = (Eina_Model_Type *) &_CREDIT_CARD_TYPE; | ||
71 | |||
72 | |||
73 | /* | ||
74 | * Defining method for for Bank Account data | ||
75 | */ | ||
76 | static void | ||
77 | _bank_account_data_set(Eina_Model *mdl, const char *name, const char *number) | ||
78 | { | ||
79 | Bank_Account_Data *bdata = eina_model_type_private_data_get(mdl, BANK_ACCOUNT_TYPE); | ||
80 | |||
81 | if (!bdata) | ||
82 | printf("ERROR\n"); | ||
83 | |||
84 | if (name != NULL) | ||
85 | { | ||
86 | strncpy(bdata->name, name, sizeof(bdata->name)); | ||
87 | bdata->name[sizeof(bdata->number) - 1] = '\0'; | ||
88 | } | ||
89 | |||
90 | if (number != NULL) | ||
91 | { | ||
92 | strncpy(bdata->number, number, sizeof(bdata->number)); | ||
93 | bdata->number[sizeof(bdata->number) - 1] = '\0'; | ||
94 | } | ||
95 | |||
96 | printf("%s :: %s %p\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl); | ||
97 | } | ||
98 | |||
99 | |||
100 | static void | ||
101 | _credit_card_data_set(Eina_Model *mdl, const char *number, const char *expiry_date, int pin) | ||
102 | { | ||
103 | Credit_Card_Data *cdata = eina_model_type_private_data_get(mdl, CREDIT_CARD_TYPE); | ||
104 | |||
105 | if (!cdata) | ||
106 | printf("ERROR\n"); | ||
107 | |||
108 | if (number != NULL) | ||
109 | { | ||
110 | strncpy(cdata->number, number, sizeof(cdata->number)); | ||
111 | cdata->number[sizeof(cdata->number) - 1] = '\0'; | ||
112 | } | ||
113 | |||
114 | if (expiry_date != NULL) | ||
115 | { | ||
116 | strncpy(cdata->expiry_date, expiry_date, sizeof(cdata->expiry_date)); | ||
117 | cdata->expiry_date[sizeof(cdata->expiry_date) - 1] = '\0'; | ||
118 | } | ||
119 | |||
120 | cdata->pin = pin; | ||
121 | printf("%s :: %s %p\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl); | ||
122 | } | ||
123 | |||
124 | static void | ||
125 | _bank_account_data_print(Eina_Model *mdl) | ||
126 | { | ||
127 | const Bank_Account_Data *bdata = eina_model_type_private_data_get(mdl, BANK_ACCOUNT_TYPE); | ||
128 | |||
129 | printf("\n%s :: %s %p \n\tName: %s(%p)\n\tAccount: %s(%p)\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl | ||
130 | , bdata->name, bdata->name, bdata->number, bdata->number); | ||
131 | } | ||
132 | |||
133 | static void | ||
134 | _credit_card_data_print(Eina_Model *mdl) | ||
135 | { | ||
136 | void (*pf)(Eina_Model *); | ||
137 | const Eina_Model_Type *ptype = eina_model_type_parent_get(eina_model_type_get(mdl)); | ||
138 | //const Eina_Model_Type *ptype = eina_model_type_get(mdl); | ||
139 | |||
140 | pf = eina_model_type_method_resolve(ptype, mdl, Bank_Account_Type, print); | ||
141 | if (pf) | ||
142 | pf(mdl); | ||
143 | else | ||
144 | printf("ERROR: %d", __LINE__); | ||
145 | const Credit_Card_Data *cdata = eina_model_type_private_data_get(mdl, CREDIT_CARD_TYPE); | ||
146 | printf("%s :: %s %p \n\tNumber: %s(%p)\n\tCC Expiry Date: %s(%p)\n\tCC PIN: %d(%p)\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl | ||
147 | , cdata->number, cdata->number, cdata->expiry_date, cdata->expiry_date, cdata->pin, &cdata->pin); | ||
148 | } | ||
149 | |||
150 | #define BANK_ACCOUNT(x) ((Bank_Account_Type *) x) | ||
151 | #define CREDIT_CARD(x) ((Credit_Card_Type *) x) | ||
152 | |||
153 | void | ||
154 | bank_account_data_set(Eina_Model *mdl, const char *name, char *number) | ||
155 | { | ||
156 | |||
157 | EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(mdl, BANK_ACCOUNT_TYPE)); | ||
158 | |||
159 | void (*pf)(Eina_Model *, const char *, const char *); | ||
160 | pf = eina_model_method_resolve(mdl, Bank_Account_Type, bank_account_data_set); | ||
161 | if (pf) | ||
162 | pf(mdl, name, number); | ||
163 | else | ||
164 | printf("ERROR %d\n", __LINE__); | ||
165 | } | ||
166 | |||
167 | void | ||
168 | data_print(Eina_Model *mdl) | ||
169 | { | ||
170 | EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(mdl, BANK_ACCOUNT_TYPE)); | ||
171 | |||
172 | void (*pf)(Eina_Model *); | ||
173 | pf = eina_model_method_resolve(mdl, Bank_Account_Type, print); | ||
174 | if (pf) | ||
175 | pf(mdl); | ||
176 | else | ||
177 | printf("ERROR %d\n", __LINE__); | ||
178 | } | ||
179 | |||
180 | void | ||
181 | credit_card_data_set(Eina_Model *mdl, const char *number, const char *expiry_date, int pin) | ||
182 | { | ||
183 | EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(mdl, CREDIT_CARD_TYPE)); | ||
184 | |||
185 | void (*pf)(Eina_Model *, const char *, const char *, int); | ||
186 | pf = eina_model_method_resolve(mdl, Credit_Card_Type, credit_card_data_set); | ||
187 | if (pf) | ||
188 | pf(mdl, number, expiry_date, pin); | ||
189 | else | ||
190 | printf("ERROR %d\n", __LINE__); | ||
191 | } | ||
192 | |||
193 | int main(void) | ||
194 | { | ||
195 | Eina_Model *b, *cc; | ||
196 | |||
197 | eina_init(); | ||
198 | |||
199 | memset(&_BANK_ACCOUNT_TYPE, 0, sizeof(_BANK_ACCOUNT_TYPE)); | ||
200 | memset(&_CREDIT_CARD_TYPE, 0, sizeof(_CREDIT_CARD_TYPE)); | ||
201 | |||
202 | BANK_ACCOUNT_TYPE->version = EINA_MODEL_TYPE_VERSION; | ||
203 | BANK_ACCOUNT_TYPE->type_size = sizeof(Bank_Account_Type); | ||
204 | BANK_ACCOUNT_TYPE->private_size = sizeof(Bank_Account_Data); | ||
205 | BANK_ACCOUNT_TYPE->name = "Bank_Account_Model"; | ||
206 | BANK_ACCOUNT_TYPE->parent = EINA_MODEL_TYPE_GENERIC; | ||
207 | BANK_ACCOUNT(BANK_ACCOUNT_TYPE)->bank_account_data_set = _bank_account_data_set; | ||
208 | BANK_ACCOUNT(BANK_ACCOUNT_TYPE)->print = _bank_account_data_print; | ||
209 | |||
210 | CREDIT_CARD_TYPE->version = EINA_MODEL_TYPE_VERSION; | ||
211 | CREDIT_CARD_TYPE->type_size = sizeof(Credit_Card_Type); | ||
212 | CREDIT_CARD_TYPE->private_size = sizeof(Credit_Card_Data); | ||
213 | CREDIT_CARD_TYPE->name = "Credit_Card_Model"; | ||
214 | CREDIT_CARD_TYPE->parent = BANK_ACCOUNT_TYPE; | ||
215 | CREDIT_CARD(CREDIT_CARD_TYPE)->credit_card_data_set = _credit_card_data_set; | ||
216 | BANK_ACCOUNT(CREDIT_CARD_TYPE)->print = _credit_card_data_print; | ||
217 | |||
218 | b = eina_model_new(BANK_ACCOUNT_TYPE); //creating object of bank class | ||
219 | cc = eina_model_new(CREDIT_CARD_TYPE); //creating object of credit card class | ||
220 | |||
221 | bank_account_data_set(b, "Bill Clark", "8569214756"); | ||
222 | bank_account_data_set(cc, "John Smith", "3154789"); | ||
223 | |||
224 | credit_card_data_set(cc, "5803 6589 4786 3279 9173", "01/01/2015", 1234); | ||
225 | |||
226 | data_print(b); | ||
227 | data_print(cc); | ||
228 | |||
229 | eina_model_unref(b); | ||
230 | eina_model_unref(cc); | ||
231 | |||
232 | eina_shutdown(); | ||
233 | |||
234 | return 0; | ||
235 | } | ||
diff --git a/libraries/eina/src/examples/eina_model_02.c b/libraries/eina/src/examples/eina_model_02.c new file mode 100644 index 0000000..a2cb693 --- /dev/null +++ b/libraries/eina/src/examples/eina_model_02.c | |||
@@ -0,0 +1,61 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_model_02.c -o eina_model_02 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <Eina.h> | ||
5 | |||
6 | static void _cb_on_deleted(void *data, Eina_Model *model, const Eina_Model_Event_Description *desc, void *event_info); | ||
7 | |||
8 | int main(void) | ||
9 | { | ||
10 | Eina_Model *m; | ||
11 | char *s; | ||
12 | int i; | ||
13 | |||
14 | eina_init(); | ||
15 | |||
16 | m = eina_model_new(EINA_MODEL_TYPE_GENERIC); | ||
17 | |||
18 | eina_model_event_callback_add(m, "deleted", _cb_on_deleted, NULL); | ||
19 | |||
20 | //Adding properties to model | ||
21 | for (i = 0; i < 5; i++) | ||
22 | { | ||
23 | Eina_Value val; | ||
24 | char name[2] = {'a'+ i, 0}; | ||
25 | eina_value_setup(&val, EINA_VALUE_TYPE_INT); | ||
26 | eina_value_set(&val, i); | ||
27 | eina_model_property_set(m, name, &val); | ||
28 | eina_value_flush(&val); | ||
29 | } | ||
30 | |||
31 | //Adding children to model | ||
32 | for (i = 0; i < 5; i++) | ||
33 | { | ||
34 | Eina_Value val; | ||
35 | Eina_Model *c = eina_model_new(EINA_MODEL_TYPE_GENERIC); | ||
36 | eina_value_setup(&val, EINA_VALUE_TYPE_INT); | ||
37 | eina_value_set(&val, i); | ||
38 | eina_model_property_set(c, "x", &val); | ||
39 | |||
40 | eina_model_event_callback_add(c, "deleted", _cb_on_deleted, NULL); | ||
41 | |||
42 | eina_model_child_append(m, c); | ||
43 | //Now that the child has been appended to a model, it's parent will manage it's lifecycle | ||
44 | eina_model_unref(c); | ||
45 | eina_value_flush(&val); | ||
46 | } | ||
47 | |||
48 | s = eina_model_to_string(m); | ||
49 | printf("model as string:\n%s\n", s); | ||
50 | |||
51 | free(s); | ||
52 | eina_model_unref(m); | ||
53 | eina_shutdown(); | ||
54 | |||
55 | return 0; | ||
56 | } | ||
57 | |||
58 | static void _cb_on_deleted(void *data, Eina_Model *model, const Eina_Model_Event_Description *desc, void *event_info) | ||
59 | { | ||
60 | printf("deleted %p\n", model); | ||
61 | } | ||
diff --git a/libraries/eina/src/examples/eina_model_03.c b/libraries/eina/src/examples/eina_model_03.c new file mode 100644 index 0000000..a5e2562 --- /dev/null +++ b/libraries/eina/src/examples/eina_model_03.c | |||
@@ -0,0 +1,236 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_model_03.c -o eina_model_03 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <Eina.h> | ||
5 | #include <string.h> | ||
6 | #include <stdio.h> | ||
7 | #include <errno.h> | ||
8 | |||
9 | static Eina_Model_Type *ADDRESS_BOOK_TYPE; | ||
10 | static Eina_Model_Type *PERSON_TYPE; | ||
11 | |||
12 | static void address_book_init(void); | ||
13 | |||
14 | int main(void) | ||
15 | { | ||
16 | Eina_Model *address_book; | ||
17 | Eina_Value val; | ||
18 | int i, count; | ||
19 | char *s; | ||
20 | |||
21 | eina_init(); | ||
22 | |||
23 | address_book_init(); | ||
24 | |||
25 | address_book = eina_model_new(ADDRESS_BOOK_TYPE); | ||
26 | |||
27 | eina_value_setup(&val, EINA_VALUE_TYPE_STRING); | ||
28 | eina_value_set(&val, "addr_book.txt"); | ||
29 | eina_model_property_set(address_book, "filename", &val); | ||
30 | eina_value_flush(&val); | ||
31 | |||
32 | eina_model_load(address_book); | ||
33 | s = eina_model_to_string(address_book); | ||
34 | printf("model as string:\n%s\n\n", s); | ||
35 | free(s); | ||
36 | |||
37 | count = eina_model_child_count(address_book); | ||
38 | printf("Address Book with %d entries:\n", count); | ||
39 | for (i = 0; i < count; i++) | ||
40 | { | ||
41 | Eina_Model *person = eina_model_child_get(address_book, i); | ||
42 | Eina_Value nameval, emailval; | ||
43 | const char *name, *email; | ||
44 | |||
45 | eina_model_property_get(person, "name", &nameval); | ||
46 | eina_model_property_get(person, "email", &emailval); | ||
47 | |||
48 | eina_value_get(&nameval, &name); | ||
49 | eina_value_get(&emailval, &email); | ||
50 | |||
51 | printf("%02d \"%s\" <%s>\n", i, name, email); | ||
52 | |||
53 | // We don't need property values anymore | ||
54 | eina_value_flush(&nameval); | ||
55 | eina_value_flush(&emailval); | ||
56 | |||
57 | // We don't need our reference to person anymore | ||
58 | eina_model_unref(person); | ||
59 | } | ||
60 | |||
61 | eina_model_unref(address_book); | ||
62 | eina_shutdown(); | ||
63 | |||
64 | return 0; | ||
65 | } | ||
66 | |||
67 | // Structure Descriptions are just used internally in the type constructors: | ||
68 | static Eina_Value_Struct_Desc *ADDRESS_BOOK_DESC; | ||
69 | static Eina_Value_Struct_Desc *PERSON_DESC; | ||
70 | |||
71 | static Eina_Bool | ||
72 | _person_constructor(Eina_Model *model) | ||
73 | { | ||
74 | // call parent type constructor, like "super" in other languages: | ||
75 | if (!eina_model_type_constructor(EINA_MODEL_TYPE_STRUCT, model)) | ||
76 | return EINA_FALSE; | ||
77 | |||
78 | // Do specific setup of our internal structure, letting it know about | ||
79 | // our description | ||
80 | return eina_model_struct_set(model, PERSON_DESC, NULL); | ||
81 | } | ||
82 | |||
83 | static Eina_Bool | ||
84 | _address_book_constructor(Eina_Model *model) | ||
85 | { | ||
86 | // call parent type constructor, like "super" in other languages: | ||
87 | if (!eina_model_type_constructor(EINA_MODEL_TYPE_STRUCT, model)) | ||
88 | return EINA_FALSE; | ||
89 | |||
90 | // Do specific setup of our internal structure, letting it know about | ||
91 | // our description | ||
92 | return eina_model_struct_set(model, ADDRESS_BOOK_DESC, NULL); | ||
93 | } | ||
94 | |||
95 | static Eina_Bool | ||
96 | _address_book_load(Eina_Model *model) | ||
97 | { | ||
98 | const char *filename; | ||
99 | Eina_Value val; | ||
100 | char buf[256]; | ||
101 | FILE *f; | ||
102 | |||
103 | // We retrieve filename from property of same name: | ||
104 | eina_model_property_get(model, "filename", &val); | ||
105 | eina_value_get(&val, &filename); | ||
106 | |||
107 | EINA_SAFETY_ON_NULL_RETURN_VAL(filename, EINA_FALSE); | ||
108 | |||
109 | f = fopen(filename, "r"); | ||
110 | |||
111 | // Now that we have used filename, we must free its memory holder: | ||
112 | eina_value_flush(&val); | ||
113 | |||
114 | EINA_SAFETY_ON_NULL_RETURN_VAL(f, EINA_FALSE); | ||
115 | |||
116 | while (fgets(buf, sizeof(buf), f)) | ||
117 | { | ||
118 | Eina_Model *person; | ||
119 | char *name, *email; | ||
120 | |||
121 | if (strlen(buf) <= 1) | ||
122 | continue; | ||
123 | |||
124 | name = strtok(buf, "\t"); | ||
125 | email = strtok(NULL, "\n"); | ||
126 | |||
127 | if ((!name) || (!email)) continue; | ||
128 | |||
129 | // Create person | ||
130 | person = eina_model_new(PERSON_TYPE); | ||
131 | |||
132 | // Setup value type as string, as our properties are strings: | ||
133 | eina_value_setup(&val, EINA_VALUE_TYPE_STRING); | ||
134 | |||
135 | // Set string properties: | ||
136 | eina_value_set(&val, name); | ||
137 | eina_model_property_set(person, "name", &val); | ||
138 | |||
139 | eina_value_set(&val, email); | ||
140 | eina_model_property_set(person, "email", &val); | ||
141 | |||
142 | // Flush value, free string | ||
143 | eina_value_flush(&val); | ||
144 | |||
145 | // Add person to the end of model children | ||
146 | eina_model_child_append(model, person); | ||
147 | |||
148 | // Model already holds its reference to person, we release ours | ||
149 | eina_model_unref(person); | ||
150 | } | ||
151 | |||
152 | fclose(f); | ||
153 | return EINA_TRUE; | ||
154 | } | ||
155 | |||
156 | static void | ||
157 | address_book_init(void) | ||
158 | { | ||
159 | // Declare type for internal struct, this is just used to easily | ||
160 | // create Eina_Value_Struct_Member array for Eina_Value_Struct_Desc. | ||
161 | // | ||
162 | // We don't need this structure outside address_book_init() | ||
163 | // as it is managed automatically by Eina_Value_Struct, used by | ||
164 | // Eina_Model_Struct! Handy! :-) | ||
165 | typedef struct _Person Person; | ||
166 | struct _Person | ||
167 | { | ||
168 | const char *name; | ||
169 | const char *email; | ||
170 | }; | ||
171 | static Eina_Value_Struct_Member person_members[] = { | ||
172 | // no eina_value_type as they are not constant initializers, see below. | ||
173 | EINA_VALUE_STRUCT_MEMBER(NULL, Person, name), | ||
174 | EINA_VALUE_STRUCT_MEMBER(NULL, Person, email) | ||
175 | }; | ||
176 | // Values that cannot be set on static declarations since they are not | ||
177 | // constant initializers. It is a nitpick from C that we need to deal with | ||
178 | // here and on all our other declarations. | ||
179 | person_members[0].type = EINA_VALUE_TYPE_STRING; | ||
180 | person_members[1].type = EINA_VALUE_TYPE_STRING; | ||
181 | |||
182 | static Eina_Value_Struct_Desc person_desc = { | ||
183 | EINA_VALUE_STRUCT_DESC_VERSION, | ||
184 | NULL, // no special operations | ||
185 | person_members, | ||
186 | EINA_C_ARRAY_LENGTH(person_members), | ||
187 | sizeof(Person) | ||
188 | }; | ||
189 | static Eina_Model_Type person_type = EINA_MODEL_TYPE_INIT_NOPRIVATE | ||
190 | ("Person_Type", | ||
191 | Eina_Model_Type, | ||
192 | NULL, // no type as EINA_MODEL_TYPE_STRUCT is not constant initializer! | ||
193 | NULL, // no extra interfaces | ||
194 | NULL // no extra events); | ||
195 | ); | ||
196 | person_type.parent = EINA_MODEL_TYPE_STRUCT; | ||
197 | // Set our overloaded methods: | ||
198 | person_type.constructor = _person_constructor; | ||
199 | |||
200 | typedef struct _Address_Book Address_Book; | ||
201 | struct _Address_Book | ||
202 | { | ||
203 | const char *filename; | ||
204 | }; | ||
205 | static Eina_Value_Struct_Member address_book_members[] = { | ||
206 | // no eina_value_type as they are not constant initializers, see below. | ||
207 | EINA_VALUE_STRUCT_MEMBER(NULL, Address_Book, filename) | ||
208 | }; | ||
209 | address_book_members[0].type = EINA_VALUE_TYPE_STRING; | ||
210 | static Eina_Value_Struct_Desc address_book_desc = { | ||
211 | EINA_VALUE_STRUCT_DESC_VERSION, | ||
212 | NULL, // no special operations | ||
213 | address_book_members, | ||
214 | EINA_C_ARRAY_LENGTH(address_book_members), | ||
215 | sizeof(Address_Book) | ||
216 | }; | ||
217 | static Eina_Model_Type address_book_type = EINA_MODEL_TYPE_INIT_NOPRIVATE | ||
218 | ("Address_Book_Type", | ||
219 | Eina_Model_Type, | ||
220 | NULL, // no type as EINA_MODEL_TYPE_STRUCT is not constant initializer! | ||
221 | NULL, // no extra interfaces | ||
222 | NULL // no extra events); | ||
223 | ); | ||
224 | address_book_type.parent = EINA_MODEL_TYPE_STRUCT; | ||
225 | // Set our overloaded methods: | ||
226 | address_book_type.constructor = _address_book_constructor; | ||
227 | address_book_type.load = _address_book_load; | ||
228 | |||
229 | // Expose the configured pointers to public usage: | ||
230 | // NOTE: they are static, so they live after this function returns! | ||
231 | PERSON_TYPE = &person_type; | ||
232 | PERSON_DESC = &person_desc; | ||
233 | |||
234 | ADDRESS_BOOK_TYPE = &address_book_type; | ||
235 | ADDRESS_BOOK_DESC = &address_book_desc; | ||
236 | } | ||
diff --git a/libraries/eina/src/examples/eina_model_04_animal.c b/libraries/eina/src/examples/eina_model_04_animal.c new file mode 100644 index 0000000..bc9f06b --- /dev/null +++ b/libraries/eina/src/examples/eina_model_04_animal.c | |||
@@ -0,0 +1,76 @@ | |||
1 | /* | ||
2 | * animal.c | ||
3 | */ | ||
4 | |||
5 | #include "eina_model_04_animal.h" | ||
6 | |||
7 | static Eina_Bool initialized = EINA_FALSE; | ||
8 | |||
9 | static void | ||
10 | _animal_eat(Eina_Model *m) | ||
11 | { | ||
12 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
13 | __func__); | ||
14 | printf("\t\t Eat Animal\n"); | ||
15 | } | ||
16 | |||
17 | static void | ||
18 | _animal_breathe(Eina_Model *m) | ||
19 | { | ||
20 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
21 | __func__); | ||
22 | printf("\t\t Breathe Animal\n"); | ||
23 | } | ||
24 | |||
25 | const char *ANIMAL_MODEL_TYPE_NAME = NULL; | ||
26 | static Animal_Type _ANIMAL_TYPE; | ||
27 | |||
28 | const Eina_Model_Type * const ANIMAL_TYPE = (Eina_Model_Type *) &_ANIMAL_TYPE; | ||
29 | |||
30 | void | ||
31 | animal_init(void) | ||
32 | { | ||
33 | Eina_Model_Type *type; | ||
34 | |||
35 | if (initialized) return; | ||
36 | initialized = EINA_TRUE; | ||
37 | |||
38 | ANIMAL_MODEL_TYPE_NAME = "Animal_Model_Type"; | ||
39 | |||
40 | type = (Eina_Model_Type *)&_ANIMAL_TYPE; | ||
41 | type->version = EINA_MODEL_TYPE_VERSION; | ||
42 | type->name = ANIMAL_MODEL_TYPE_NAME; | ||
43 | type->private_size = 0; | ||
44 | |||
45 | eina_model_type_subclass_setup(type, EINA_MODEL_TYPE_GENERIC); | ||
46 | |||
47 | /* define extra methods */ | ||
48 | |||
49 | type->type_size = sizeof(Animal_Type); | ||
50 | ANIMAL_TYPE(type)->breathe = _animal_breathe; | ||
51 | ANIMAL_TYPE(type)->eat = _animal_eat; | ||
52 | } | ||
53 | |||
54 | void | ||
55 | animal_breathe(Eina_Model *m) | ||
56 | { | ||
57 | EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, ANIMAL_TYPE)); | ||
58 | |||
59 | void (*pf)(Eina_Model *m); | ||
60 | pf = eina_model_method_resolve(m, Animal_Type, breathe); | ||
61 | EINA_SAFETY_ON_NULL_RETURN(pf); | ||
62 | printf("%s() \t", __func__); | ||
63 | pf(m); | ||
64 | } | ||
65 | |||
66 | void | ||
67 | animal_eat(Eina_Model *m) | ||
68 | { | ||
69 | EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, ANIMAL_TYPE)); | ||
70 | |||
71 | void (*pf)(Eina_Model *m); | ||
72 | pf = eina_model_method_resolve(m, Animal_Type, eat); | ||
73 | EINA_SAFETY_ON_NULL_RETURN(pf); | ||
74 | printf("%s() \t", __func__); | ||
75 | pf(m); | ||
76 | } | ||
diff --git a/libraries/eina/src/examples/eina_model_04_child.c b/libraries/eina/src/examples/eina_model_04_child.c new file mode 100644 index 0000000..59b8aa5 --- /dev/null +++ b/libraries/eina/src/examples/eina_model_04_child.c | |||
@@ -0,0 +1,81 @@ | |||
1 | /* | ||
2 | * child.c | ||
3 | */ | ||
4 | |||
5 | #include "eina_model_04_child.h" | ||
6 | #include "eina_model_04_whistler.h" | ||
7 | |||
8 | static Eina_Bool initialized = EINA_FALSE; | ||
9 | |||
10 | static void | ||
11 | _child_cry(Eina_Model *m) | ||
12 | { | ||
13 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
14 | __func__); | ||
15 | printf("\t\t Cry Child\n"); | ||
16 | } | ||
17 | |||
18 | static void | ||
19 | _child_dive(Eina_Model *m) | ||
20 | { | ||
21 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
22 | __func__); | ||
23 | printf("\t\t Dive Child\n"); | ||
24 | } | ||
25 | |||
26 | const char *CHILD_MODEL_TYPE_NAME = NULL; | ||
27 | |||
28 | static Child_Type _CHILD_TYPE; | ||
29 | const Eina_Model_Type * const CHILD_TYPE = (Eina_Model_Type *) &_CHILD_TYPE; | ||
30 | |||
31 | static const Diver_Interface _DIVER_INTERFACE; | ||
32 | static const Eina_Model_Interface * const DIVER_INTERFACE = | ||
33 | (Eina_Model_Interface *) &_DIVER_INTERFACE; | ||
34 | |||
35 | static const Eina_Model_Interface * CLASS_INTERFACE_ARRAY[] = | ||
36 | { &_DIVER_INTERFACE.base_interface, NULL }; //this array is for model | ||
37 | |||
38 | void | ||
39 | child_init() | ||
40 | { | ||
41 | Eina_Model_Type *type; | ||
42 | |||
43 | if (initialized) return; | ||
44 | initialized = EINA_TRUE; | ||
45 | |||
46 | human_init(); | ||
47 | |||
48 | //overriding Diver Interface | ||
49 | Eina_Model_Interface * iface = (Eina_Model_Interface *) &_DIVER_INTERFACE; | ||
50 | iface->version = EINA_MODEL_INTERFACE_VERSION; | ||
51 | iface->interface_size = sizeof(Diver_Interface); | ||
52 | iface->name = DIVER_INTERFACE_NAME; | ||
53 | DIVER_INTERFACE(iface)->dive = _child_dive; | ||
54 | |||
55 | //creating instance of Child type | ||
56 | CHILD_MODEL_TYPE_NAME = "Child_Model_Type"; | ||
57 | |||
58 | type = (Eina_Model_Type *) &_CHILD_TYPE; | ||
59 | type->version = EINA_MODEL_TYPE_VERSION; | ||
60 | type->name = CHILD_MODEL_TYPE_NAME; | ||
61 | |||
62 | eina_model_type_subclass_setup(type, HUMAN_TYPE); | ||
63 | |||
64 | type->type_size = sizeof(Child_Type); | ||
65 | type->interfaces = CLASS_INTERFACE_ARRAY; | ||
66 | |||
67 | CHILD_TYPE(type)->cry = _child_cry; | ||
68 | } | ||
69 | |||
70 | //call for implemented Child Class function | ||
71 | void | ||
72 | child_cry(Eina_Model *m) | ||
73 | { | ||
74 | EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, CHILD_TYPE)); | ||
75 | |||
76 | void (*pf)(Eina_Model *m); | ||
77 | pf = eina_model_method_resolve(m, Child_Type, cry); | ||
78 | EINA_SAFETY_ON_NULL_RETURN(pf); | ||
79 | printf("%s() \t\t", __func__); | ||
80 | pf(m); | ||
81 | } | ||
diff --git a/libraries/eina/src/examples/eina_model_04_human.c b/libraries/eina/src/examples/eina_model_04_human.c new file mode 100644 index 0000000..d9a10ab --- /dev/null +++ b/libraries/eina/src/examples/eina_model_04_human.c | |||
@@ -0,0 +1,157 @@ | |||
1 | /* | ||
2 | * human.c | ||
3 | * | ||
4 | */ | ||
5 | |||
6 | #include "eina_model_04_human.h" | ||
7 | #include "eina_model_04_whistler.h" | ||
8 | |||
9 | static Eina_Bool initialized = EINA_FALSE; | ||
10 | |||
11 | static void | ||
12 | _human_eat(Eina_Model *m) | ||
13 | { | ||
14 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
15 | __func__); | ||
16 | printf("\t\t Salad\n"); | ||
17 | } | ||
18 | |||
19 | static void | ||
20 | _human_walk(Eina_Model *m) | ||
21 | { | ||
22 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
23 | __func__); | ||
24 | printf("\t\t Walk\n"); | ||
25 | } | ||
26 | |||
27 | static void | ||
28 | _human_whistle(Eina_Model *m) | ||
29 | { | ||
30 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
31 | __func__); | ||
32 | printf("\t\t Whistle Human\n"); | ||
33 | } | ||
34 | |||
35 | static void | ||
36 | _human_swim(Eina_Model *m) | ||
37 | { | ||
38 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
39 | __func__); | ||
40 | printf("\t\t Swim Human\n"); | ||
41 | } | ||
42 | |||
43 | static void | ||
44 | _human_dive(Eina_Model *m) | ||
45 | { | ||
46 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
47 | __func__); | ||
48 | printf("\t\t Dive Human\n"); | ||
49 | } | ||
50 | /* | ||
51 | * defining Human Model Instance | ||
52 | * defining Whistler Interface instance | ||
53 | * defining Swimmer Interface instance | ||
54 | * defining Diver Interface instance | ||
55 | */ | ||
56 | |||
57 | const char *HUMAN_MODEL_TYPE_NAME = NULL; | ||
58 | |||
59 | static Human_Type _HUMAN_TYPE; | ||
60 | const Eina_Model_Type * const HUMAN_TYPE = (Eina_Model_Type *) &_HUMAN_TYPE; | ||
61 | |||
62 | static const Whistler_Interface _WHISTLER_INTERFACE; | ||
63 | static const Eina_Model_Interface * const WHISTLER_INTERFACE = | ||
64 | (Eina_Model_Interface *) &_WHISTLER_INTERFACE; | ||
65 | |||
66 | static const Swimmer_Interface _SWIMMER_INTERFACE; | ||
67 | static const Eina_Model_Interface * const SWIMMER_INTERFACE = | ||
68 | (Eina_Model_Interface *) &_SWIMMER_INTERFACE; | ||
69 | |||
70 | static const Diver_Interface _DIVER_INTERFACE; | ||
71 | static const Eina_Model_Interface * const DIVER_INTERFACE = | ||
72 | (Eina_Model_Interface *) &_DIVER_INTERFACE; | ||
73 | |||
74 | /* | ||
75 | * defining parent interfaces for Diver Interface instance | ||
76 | * defining Interfaces for Human Model instance | ||
77 | */ | ||
78 | static const Eina_Model_Interface * PARENT_INTERFACES_ARRAY[] = | ||
79 | { &_SWIMMER_INTERFACE.base_interface, NULL }; //this array is for model | ||
80 | static const Eina_Model_Interface * MODEL_INTERFACES_ARRAY[] = | ||
81 | { &_WHISTLER_INTERFACE.base_interface, &_DIVER_INTERFACE.base_interface, | ||
82 | NULL }; //this array is for model | ||
83 | |||
84 | void | ||
85 | human_init() | ||
86 | { | ||
87 | Eina_Model_Type *type; | ||
88 | |||
89 | if (initialized) return; | ||
90 | initialized = EINA_TRUE; | ||
91 | |||
92 | animal_init(); | ||
93 | |||
94 | /* | ||
95 | * Initializing Whistler Interface Instance | ||
96 | */ | ||
97 | Eina_Model_Interface *iface = (Eina_Model_Interface *) &_WHISTLER_INTERFACE; | ||
98 | iface->version = EINA_MODEL_INTERFACE_VERSION; | ||
99 | iface->interface_size = sizeof(Whistler_Interface); | ||
100 | iface->name = WHISTLER_INTERFACE_NAME; | ||
101 | WHISTLER_INTERFACE(iface)->whistle = _human_whistle; | ||
102 | |||
103 | /* | ||
104 | * Initializing Swimmer Interface Instance | ||
105 | */ | ||
106 | iface = (Eina_Model_Interface *) &_SWIMMER_INTERFACE; | ||
107 | iface->version = EINA_MODEL_INTERFACE_VERSION; | ||
108 | iface->interface_size = sizeof(Swimmer_Interface); | ||
109 | iface->name = SWIMMER_INTERFACE_NAME; | ||
110 | SWIMMER_INTERFACE(iface)->swim = _human_swim; | ||
111 | |||
112 | /* | ||
113 | * Initializing Diver Interface Instance | ||
114 | * Diver_Interface is inherited from Swimmer | ||
115 | */ | ||
116 | iface = (Eina_Model_Interface *) &_DIVER_INTERFACE; | ||
117 | iface->version = EINA_MODEL_INTERFACE_VERSION; | ||
118 | iface->interface_size = sizeof(Diver_Interface); | ||
119 | iface->name = DIVER_INTERFACE_NAME; | ||
120 | iface->interfaces = PARENT_INTERFACES_ARRAY; | ||
121 | DIVER_INTERFACE(iface)->dive = _human_dive; | ||
122 | |||
123 | /* | ||
124 | * Initializing instance of Human Model | ||
125 | */ | ||
126 | |||
127 | HUMAN_MODEL_TYPE_NAME = "Human_Model_Type"; | ||
128 | |||
129 | type = (Eina_Model_Type *) &_HUMAN_TYPE; | ||
130 | type->version = EINA_MODEL_TYPE_VERSION; | ||
131 | type->name = HUMAN_MODEL_TYPE_NAME; | ||
132 | type->private_size = 0; | ||
133 | |||
134 | eina_model_type_subclass_setup(type, ANIMAL_TYPE); | ||
135 | |||
136 | type->type_size = sizeof(Human_Type); | ||
137 | type->interfaces = MODEL_INTERFACES_ARRAY; | ||
138 | |||
139 | ANIMAL_TYPE(type)->eat = _human_eat; | ||
140 | HUMAN_TYPE(type)->walk =_human_walk; | ||
141 | } | ||
142 | |||
143 | |||
144 | /* | ||
145 | * call for implemented Human Class function | ||
146 | */ | ||
147 | void | ||
148 | human_walk(Eina_Model *m) | ||
149 | { | ||
150 | EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, HUMAN_TYPE)); | ||
151 | |||
152 | void (*pf)(Eina_Model *m); | ||
153 | pf = eina_model_method_resolve(m, Human_Type, walk); | ||
154 | EINA_SAFETY_ON_NULL_RETURN(pf); | ||
155 | printf("%s() \t", __func__); | ||
156 | pf(m); | ||
157 | } | ||
diff --git a/libraries/eina/src/examples/eina_model_04_main.c b/libraries/eina/src/examples/eina_model_04_main.c new file mode 100644 index 0000000..0e419ec --- /dev/null +++ b/libraries/eina/src/examples/eina_model_04_main.c | |||
@@ -0,0 +1,110 @@ | |||
1 | /* | ||
2 | * main_animal.c | ||
3 | * compile with: gcc eina_model_04_*.c -o eina_model_04 `pkg-config --cflags --libs eina` | ||
4 | */ | ||
5 | |||
6 | /* | ||
7 | * This example demonstrates the extended usage of Eina Model. | ||
8 | * Class inheritance and interface implementation | ||
9 | * | ||
10 | * Animal Class is inherited from BaseClass and implements | ||
11 | * "_breathe_animal()" and "_eat_animal()" methods. | ||
12 | * | ||
13 | * Human Class is inherited from Animal class. | ||
14 | * Parrot Class is inherited from Animal class. | ||
15 | * | ||
16 | * Child Class is inherited from Human class. | ||
17 | * | ||
18 | * Human Class and Parrot Class implement Whistler Interface. | ||
19 | * Human Class implements Diver Interface. Diver Interface inherited from Swimmer Interface | ||
20 | * | ||
21 | * | ||
22 | * Animal Class (inherited from Base Class) | ||
23 | * + _breathe_animal() | ||
24 | * + _eat_animal() | ||
25 | * / -------/ \-------------\ | ||
26 | * / \ | ||
27 | * Human Class Parrot Class | ||
28 | * inherits inherits | ||
29 | * + animal_breathe() + animal_breathe() | ||
30 | * overrides overrides | ||
31 | * + animal_eat(); + animal_eat(); | ||
32 | * implements implements | ||
33 | * + human_walk(); + parrot_fly(); | ||
34 | * | ||
35 | * implements Whistler, Swimmer, implements Whistler, | ||
36 | * Diver Interfaces: + whistler_whistle() | ||
37 | * + whistler_whistle() | ||
38 | * + swimmer_swim() | ||
39 | * + diver_dive() | ||
40 | * | ||
41 | * ---------------------------------------------------------- | ||
42 | * | Swim_Interface | | ||
43 | * | + swim() | | ||
44 | * | | | | ||
45 | * | | | | ||
46 | * | Dive Intarface (inherited from Swim Interface) | | ||
47 | * | + dive() | | ||
48 | * --------------------------------------------------------- | ||
49 | * | | ||
50 | * | | ||
51 | * Child Class | ||
52 | * + inherits all parent's methods | ||
53 | * + implements cry_child() | ||
54 | * + overrides dive() interface method | ||
55 | */ | ||
56 | |||
57 | #include <Eina.h> | ||
58 | #include "eina_model_04_human.h" | ||
59 | #include "eina_model_04_parrot.h" | ||
60 | #include "eina_model_04_child.h" | ||
61 | #include "eina_model_04_whistler.h" | ||
62 | |||
63 | int | ||
64 | main() | ||
65 | { | ||
66 | Eina_Model *h, *p, *c; | ||
67 | |||
68 | eina_init(); | ||
69 | |||
70 | human_init(); | ||
71 | parrot_init(); | ||
72 | child_init(); | ||
73 | |||
74 | h = eina_model_new(HUMAN_TYPE); | ||
75 | p = eina_model_new(PARROT_TYPE); | ||
76 | c = eina_model_new(CHILD_TYPE); | ||
77 | |||
78 | animal_breathe(p); | ||
79 | animal_eat(p); | ||
80 | parrot_fly(p); | ||
81 | whistler_whistle(p); | ||
82 | |||
83 | printf("\n"); | ||
84 | animal_breathe(h); | ||
85 | animal_eat(h); | ||
86 | human_walk(h); | ||
87 | whistler_whistle(h); | ||
88 | swimmer_swim(h); | ||
89 | diver_dive(h); | ||
90 | |||
91 | printf("\n"); | ||
92 | animal_breathe(c); | ||
93 | animal_eat(c); | ||
94 | human_walk(c); | ||
95 | whistler_whistle(c); | ||
96 | swimmer_swim(c); | ||
97 | diver_dive(c); | ||
98 | child_cry(c); | ||
99 | |||
100 | eina_model_unref(c); | ||
101 | eina_model_unref(h); | ||
102 | eina_model_unref(p); | ||
103 | |||
104 | eina_shutdown(); | ||
105 | |||
106 | return 0; | ||
107 | } | ||
108 | |||
109 | |||
110 | |||
diff --git a/libraries/eina/src/examples/eina_model_04_parrot.c b/libraries/eina/src/examples/eina_model_04_parrot.c new file mode 100644 index 0000000..ac619ee --- /dev/null +++ b/libraries/eina/src/examples/eina_model_04_parrot.c | |||
@@ -0,0 +1,95 @@ | |||
1 | /* | ||
2 | * parrot.c | ||
3 | */ | ||
4 | |||
5 | #include "eina_model_04_parrot.h" | ||
6 | #include "eina_model_04_whistler.h" | ||
7 | |||
8 | static Eina_Bool initialized = EINA_FALSE; | ||
9 | |||
10 | static void | ||
11 | _parrot_fly(Eina_Model *m) | ||
12 | { | ||
13 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
14 | __func__); | ||
15 | printf("\t\t Fly Parrot\n"); | ||
16 | } | ||
17 | |||
18 | static void | ||
19 | _parrot_eat(Eina_Model *m) | ||
20 | { | ||
21 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
22 | __func__); | ||
23 | printf("\t\t Grain \n"); | ||
24 | } | ||
25 | |||
26 | static void | ||
27 | _parrot_whistle(Eina_Model *m) | ||
28 | { | ||
29 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
30 | __func__); | ||
31 | printf("\t\t Whistle Parrot\n"); | ||
32 | } | ||
33 | |||
34 | /* | ||
35 | * defining Parrot Model Instance | ||
36 | * defining Whistler Interface instance | ||
37 | */ | ||
38 | const char *PARROT_MODEL_TYPE_NAME = NULL; | ||
39 | |||
40 | static Parrot_Type _PARROT_TYPE; | ||
41 | const Eina_Model_Type * const PARROT_TYPE = (Eina_Model_Type *) &_PARROT_TYPE; | ||
42 | |||
43 | static const Whistler_Interface _WHISTLER_INTERFACE; | ||
44 | static const Eina_Model_Interface * const WHISTLER_INTERFACE = | ||
45 | (Eina_Model_Interface *) &_WHISTLER_INTERFACE; | ||
46 | |||
47 | static const Eina_Model_Interface * MODEL_INTERFACES_ARRAY[] = | ||
48 | { &_WHISTLER_INTERFACE.base_interface, NULL }; //this array is for model | ||
49 | |||
50 | void | ||
51 | parrot_init() | ||
52 | { | ||
53 | Eina_Model_Type *type; | ||
54 | if (initialized) return; | ||
55 | initialized = EINA_TRUE; | ||
56 | |||
57 | animal_init(); | ||
58 | /* | ||
59 | *overriding Whistler Interface (creating instance of Whistler Interface) | ||
60 | */ | ||
61 | Eina_Model_Interface *iface = (Eina_Model_Interface *) &_WHISTLER_INTERFACE; | ||
62 | iface->version = EINA_MODEL_INTERFACE_VERSION; | ||
63 | iface->interface_size = sizeof(Whistler_Interface); | ||
64 | iface->name = WHISTLER_INTERFACE_NAME; | ||
65 | WHISTLER_INTERFACE(iface)->whistle = _parrot_whistle; | ||
66 | |||
67 | PARROT_MODEL_TYPE_NAME = "Parrot_Model_Type"; | ||
68 | |||
69 | type = (Eina_Model_Type *)&_PARROT_TYPE; | ||
70 | type->version = EINA_MODEL_TYPE_VERSION; | ||
71 | type->name = PARROT_MODEL_TYPE_NAME; | ||
72 | type->private_size = 0; | ||
73 | |||
74 | eina_model_type_subclass_setup(type, ANIMAL_TYPE); | ||
75 | |||
76 | type->type_size = sizeof(Parrot_Type); | ||
77 | type->interfaces = MODEL_INTERFACES_ARRAY; | ||
78 | |||
79 | ANIMAL_TYPE(type)->eat = _parrot_eat; | ||
80 | PARROT_TYPE(type)->fly = _parrot_fly; | ||
81 | } | ||
82 | |||
83 | |||
84 | void | ||
85 | parrot_fly(Eina_Model *m) | ||
86 | { | ||
87 | EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, PARROT_TYPE)); | ||
88 | |||
89 | void (*pf)(Eina_Model *m); | ||
90 | pf = eina_model_method_resolve(m, Parrot_Type, fly); | ||
91 | EINA_SAFETY_ON_NULL_RETURN(pf); | ||
92 | printf("%s() \t", __func__); | ||
93 | pf(m); | ||
94 | } | ||
95 | |||
diff --git a/libraries/eina/src/examples/eina_model_04_whistler.c b/libraries/eina/src/examples/eina_model_04_whistler.c new file mode 100644 index 0000000..ed9832f --- /dev/null +++ b/libraries/eina/src/examples/eina_model_04_whistler.c | |||
@@ -0,0 +1,59 @@ | |||
1 | /* | ||
2 | * whistler.c | ||
3 | * | ||
4 | */ | ||
5 | |||
6 | #include "eina_model_04_whistler.h" | ||
7 | |||
8 | void | ||
9 | whistler_whistle(Eina_Model *m) | ||
10 | { | ||
11 | const Eina_Model_Interface *iface = NULL; | ||
12 | iface = eina_model_interface_get(m, WHISTLER_INTERFACE_NAME); | ||
13 | |||
14 | EINA_SAFETY_ON_NULL_RETURN(iface); | ||
15 | |||
16 | void (*pf)(Eina_Model *); | ||
17 | |||
18 | pf = eina_model_interface_method_resolve(iface, m, Whistler_Interface, whistle); | ||
19 | EINA_SAFETY_ON_NULL_RETURN(pf); | ||
20 | printf("%s() \t", __func__); | ||
21 | pf(m); | ||
22 | } | ||
23 | /* | ||
24 | * call for overridden Swimmer Interface function | ||
25 | */ | ||
26 | void | ||
27 | swimmer_swim(Eina_Model *m) | ||
28 | { | ||
29 | const Eina_Model_Interface *iface = NULL; | ||
30 | iface = eina_model_interface_get(m, SWIMMER_INTERFACE_NAME); | ||
31 | |||
32 | EINA_SAFETY_ON_NULL_RETURN(iface); | ||
33 | |||
34 | void (*pf)(Eina_Model *); | ||
35 | |||
36 | pf = eina_model_interface_method_resolve(iface, m, Swimmer_Interface, swim); | ||
37 | EINA_SAFETY_ON_NULL_RETURN(pf); | ||
38 | printf("%s() \t", __func__); | ||
39 | pf(m); | ||
40 | } | ||
41 | |||
42 | /* | ||
43 | * call for overridden Diver Interface function | ||
44 | */ | ||
45 | void | ||
46 | diver_dive(Eina_Model *m) | ||
47 | { | ||
48 | const Eina_Model_Interface *iface = NULL; | ||
49 | iface = eina_model_interface_get(m, DIVER_INTERFACE_NAME); | ||
50 | |||
51 | EINA_SAFETY_ON_NULL_RETURN(iface); | ||
52 | |||
53 | void (*pf)(Eina_Model *); | ||
54 | |||
55 | pf = eina_model_interface_method_resolve(iface, m, Diver_Interface, dive); | ||
56 | EINA_SAFETY_ON_NULL_RETURN(pf); | ||
57 | printf("%s() \t", __func__); | ||
58 | pf(m); | ||
59 | } | ||
diff --git a/libraries/eina/src/examples/eina_value_01.c b/libraries/eina/src/examples/eina_value_01.c new file mode 100644 index 0000000..8a20828 --- /dev/null +++ b/libraries/eina/src/examples/eina_value_01.c | |||
@@ -0,0 +1,53 @@ | |||
1 | //Compile with: | ||
2 | //gcc eina_value_01.c -o eina_value_01 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <Eina.h> | ||
5 | |||
6 | int main(int argc, char **argv) | ||
7 | { | ||
8 | Eina_Value v; | ||
9 | int i; | ||
10 | char *newstr; | ||
11 | |||
12 | eina_init(); | ||
13 | |||
14 | eina_value_setup(&v, EINA_VALUE_TYPE_INT); | ||
15 | eina_value_set(&v, 123); | ||
16 | eina_value_get(&v, &i); | ||
17 | printf("v=%d\n", i); | ||
18 | |||
19 | newstr = eina_value_to_string(&v); | ||
20 | printf("v as string: %s\n", newstr); | ||
21 | free(newstr); // it was allocated by eina_value_to_string() | ||
22 | eina_value_flush(&v); // destroy v contents, will not use anymore | ||
23 | |||
24 | const char *s; | ||
25 | eina_value_setup(&v, EINA_VALUE_TYPE_STRING); | ||
26 | eina_value_set(&v, "My string"); | ||
27 | eina_value_get(&v, &s); | ||
28 | printf("v=%s (pointer: %p)\n", s, s); | ||
29 | |||
30 | newstr = eina_value_to_string(&v); | ||
31 | printf("v as string: %s (pointer: %p)\n", newstr, newstr); | ||
32 | free(newstr); // it was allocated by eina_value_to_string() | ||
33 | eina_value_flush(&v); // destroy v contents, string 's' is not valid anymore! | ||
34 | |||
35 | Eina_Value otherv; | ||
36 | eina_value_setup(&otherv, EINA_VALUE_TYPE_STRING); | ||
37 | eina_value_setup(&v, EINA_VALUE_TYPE_INT); | ||
38 | |||
39 | // convert from int to string: | ||
40 | eina_value_set(&v, 123); | ||
41 | eina_value_convert(&v, &otherv); | ||
42 | eina_value_get(&otherv, &s); | ||
43 | printf("otherv=%s\n", s); | ||
44 | |||
45 | // and the other way around! | ||
46 | eina_value_set(&otherv, "33"); | ||
47 | eina_value_convert(&otherv, &v); | ||
48 | eina_value_get(&v, &i); | ||
49 | printf("v=%d\n", i); | ||
50 | |||
51 | eina_value_flush(&otherv); | ||
52 | eina_value_flush(&v); | ||
53 | } | ||
diff --git a/libraries/eina/src/examples/eina_value_02.c b/libraries/eina/src/examples/eina_value_02.c new file mode 100644 index 0000000..9c659ec --- /dev/null +++ b/libraries/eina/src/examples/eina_value_02.c | |||
@@ -0,0 +1,100 @@ | |||
1 | //Compile with: | ||
2 | //gcc eina_value_02.c -o eina_value_02 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <Eina.h> | ||
5 | |||
6 | static Eina_Value_Struct_Desc *V1_DESC = NULL; | ||
7 | static Eina_Value_Struct_Desc *V2_DESC = NULL; | ||
8 | |||
9 | void value_init(void) | ||
10 | { | ||
11 | typedef struct _My_Struct_V1 { | ||
12 | int param1; | ||
13 | char param2; | ||
14 | } My_Struct_V1; | ||
15 | |||
16 | |||
17 | static Eina_Value_Struct_Member v1_members[] = { | ||
18 | // no eina_value_type as they are not constant initializers, see below. | ||
19 | EINA_VALUE_STRUCT_MEMBER(NULL, My_Struct_V1, param1), | ||
20 | EINA_VALUE_STRUCT_MEMBER(NULL, My_Struct_V1, param2) | ||
21 | }; | ||
22 | v1_members[0].type = EINA_VALUE_TYPE_INT; | ||
23 | v1_members[1].type = EINA_VALUE_TYPE_CHAR; | ||
24 | static Eina_Value_Struct_Desc v1_desc = { | ||
25 | EINA_VALUE_STRUCT_DESC_VERSION, | ||
26 | NULL, // no special operations | ||
27 | v1_members, | ||
28 | EINA_C_ARRAY_LENGTH(v1_members), | ||
29 | sizeof(My_Struct_V1) | ||
30 | }; | ||
31 | V1_DESC = &v1_desc; | ||
32 | |||
33 | typedef struct _My_Struct_V2 { | ||
34 | int param1; | ||
35 | char param2; | ||
36 | int param3; | ||
37 | } My_Struct_V2; | ||
38 | static Eina_Value_Struct_Member v2_members[] = { | ||
39 | // no eina_value_type as they are not constant initializers, see below. | ||
40 | EINA_VALUE_STRUCT_MEMBER(NULL, My_Struct_V2, param1), | ||
41 | EINA_VALUE_STRUCT_MEMBER(NULL, My_Struct_V2, param2), | ||
42 | EINA_VALUE_STRUCT_MEMBER(NULL, My_Struct_V2, param3) | ||
43 | }; | ||
44 | v2_members[0].type = EINA_VALUE_TYPE_INT; | ||
45 | v2_members[1].type = EINA_VALUE_TYPE_CHAR; | ||
46 | v2_members[2].type = EINA_VALUE_TYPE_INT; | ||
47 | static Eina_Value_Struct_Desc v2_desc = { | ||
48 | EINA_VALUE_STRUCT_DESC_VERSION, | ||
49 | NULL, // no special operations | ||
50 | v2_members, | ||
51 | EINA_C_ARRAY_LENGTH(v2_members), | ||
52 | sizeof(My_Struct_V2) | ||
53 | }; | ||
54 | V2_DESC = &v2_desc; | ||
55 | } | ||
56 | |||
57 | void rand_init(Eina_Value *v) | ||
58 | { | ||
59 | if (v->type != EINA_VALUE_TYPE_STRUCT) | ||
60 | return; | ||
61 | |||
62 | eina_value_struct_set(v, "param1", rand()); | ||
63 | eina_value_struct_set(v, "param2", rand() % 256); | ||
64 | eina_value_struct_set(v, "param3", rand()); | ||
65 | } | ||
66 | |||
67 | void my_struct_use(Eina_Value *params) | ||
68 | { | ||
69 | int p1, p3; | ||
70 | char p2; | ||
71 | |||
72 | eina_value_struct_get(params, "param1", &p1); | ||
73 | eina_value_struct_get(params, "param2", &p2); | ||
74 | printf("param1: %d\nparam2: %c\n", p1, p2); | ||
75 | |||
76 | if (eina_value_struct_get(params, "param3", &p3)) | ||
77 | printf("param3: %d\n", p3); | ||
78 | } | ||
79 | |||
80 | int main(int argc, char **argv) | ||
81 | { | ||
82 | Eina_Value *v1, *v2; | ||
83 | |||
84 | eina_init(); | ||
85 | value_init(); | ||
86 | srand(time(NULL)); | ||
87 | |||
88 | v1 = eina_value_struct_new(V1_DESC); | ||
89 | v2 = eina_value_struct_new(V2_DESC); | ||
90 | |||
91 | rand_init(v1); | ||
92 | my_struct_use(v1); | ||
93 | |||
94 | rand_init(v2); | ||
95 | my_struct_use(v2); | ||
96 | |||
97 | eina_value_free(v1); | ||
98 | eina_value_free(v2); | ||
99 | eina_shutdown(); | ||
100 | } | ||
diff --git a/libraries/eina/src/examples/eina_value_03.c b/libraries/eina/src/examples/eina_value_03.c new file mode 100644 index 0000000..85f42b3 --- /dev/null +++ b/libraries/eina/src/examples/eina_value_03.c | |||
@@ -0,0 +1,178 @@ | |||
1 | //Compile with: | ||
2 | //gcc eina_value_03.c -o eina_value_03 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <Eina.h> | ||
5 | #include <sys/time.h> | ||
6 | |||
7 | static Eina_Bool | ||
8 | _tz_setup(const Eina_Value_Type *type, void *mem) | ||
9 | { | ||
10 | memset(mem, 0, type->value_size); | ||
11 | return EINA_TRUE; | ||
12 | } | ||
13 | |||
14 | static Eina_Bool | ||
15 | _tz_flush(const Eina_Value_Type *type, void *mem) | ||
16 | { | ||
17 | return EINA_TRUE; | ||
18 | } | ||
19 | |||
20 | static Eina_Bool | ||
21 | _tz_copy(const Eina_Value_Type *type, const void *src, void * dst) | ||
22 | { | ||
23 | struct timezone *tzsrc = src; | ||
24 | struct timezone *tzdst = dst; | ||
25 | *tzdst = *tzsrc; | ||
26 | return EINA_TRUE; | ||
27 | } | ||
28 | |||
29 | static Eina_Bool | ||
30 | _tz_compare(const Eina_Value_Type *type, const void *a, const void *b) | ||
31 | { | ||
32 | struct timezone tza = *(struct timezone*)a; | ||
33 | struct timezone tzb = *(struct timezone*)b; | ||
34 | |||
35 | if (tza.tz_minuteswest < tzb.tz_minuteswest) | ||
36 | return -1; | ||
37 | else if (tza.tz_minuteswest > tzb.tz_minuteswest) | ||
38 | return 1; | ||
39 | return 0; | ||
40 | } | ||
41 | |||
42 | static Eina_Bool | ||
43 | _tz_pset(const Eina_Value_Type *type, void *mem, const void *ptr) | ||
44 | { | ||
45 | *(struct timezone*)mem = *(struct timezone*)ptr; | ||
46 | return EINA_TRUE; | ||
47 | } | ||
48 | |||
49 | static Eina_Bool | ||
50 | _tz_vset(const Eina_Value_Type *type, void *mem, va_list args) | ||
51 | { | ||
52 | const struct timezone tz = va_arg(args, struct timezone); | ||
53 | return _tz_pset(type, mem, &tz); | ||
54 | } | ||
55 | |||
56 | static Eina_Bool | ||
57 | _tz_pget(const Eina_Value_Type *type, const void *mem, void *ptr) | ||
58 | { | ||
59 | memcpy(ptr, mem, type->value_size); | ||
60 | return EINA_TRUE; | ||
61 | } | ||
62 | |||
63 | static Eina_Bool | ||
64 | _tz_convert_to(const Eina_Value_Type *type, const Eina_Value_Type *convert, const void *type_mem, void *convert_mem) | ||
65 | { | ||
66 | struct timezone v = *(struct timezone*)type_mem; | ||
67 | |||
68 | eina_error_set(0); | ||
69 | |||
70 | if (convert == EINA_VALUE_TYPE_UCHAR) | ||
71 | { | ||
72 | unsigned char other_mem = v.tz_minuteswest; | ||
73 | return eina_value_type_pset(convert, convert_mem, &other_mem); | ||
74 | } | ||
75 | else if (convert == EINA_VALUE_TYPE_USHORT) | ||
76 | { | ||
77 | unsigned short other_mem = v.tz_minuteswest; | ||
78 | return eina_value_type_pset(convert, convert_mem, &other_mem); | ||
79 | } | ||
80 | else if (convert == EINA_VALUE_TYPE_UINT) | ||
81 | { | ||
82 | unsigned int other_mem = v.tz_minuteswest; | ||
83 | return eina_value_type_pset(convert, convert_mem, &other_mem); | ||
84 | } | ||
85 | else if ((convert == EINA_VALUE_TYPE_ULONG) || (convert == EINA_VALUE_TYPE_TIMESTAMP)) | ||
86 | { | ||
87 | unsigned long other_mem = v.tz_minuteswest; | ||
88 | return eina_value_type_pset(convert, convert_mem, &other_mem); | ||
89 | } | ||
90 | else if (convert == EINA_VALUE_TYPE_UINT64) | ||
91 | { | ||
92 | uint64_t other_mem = v.tz_minuteswest; | ||
93 | return eina_value_type_pset(convert, convert_mem, &other_mem); | ||
94 | } | ||
95 | else if (convert == EINA_VALUE_TYPE_CHAR) | ||
96 | { | ||
97 | char other_mem = v.tz_minuteswest; | ||
98 | return eina_value_type_pset(convert, convert_mem, &other_mem); | ||
99 | } | ||
100 | else if (convert == EINA_VALUE_TYPE_SHORT) | ||
101 | { | ||
102 | short other_mem = v.tz_minuteswest; | ||
103 | return eina_value_type_pset(convert, convert_mem, &other_mem); | ||
104 | } | ||
105 | else if (convert == EINA_VALUE_TYPE_INT) | ||
106 | { | ||
107 | int other_mem = v.tz_minuteswest; | ||
108 | return eina_value_type_pset(convert, convert_mem, &other_mem); | ||
109 | } | ||
110 | else if (convert == EINA_VALUE_TYPE_LONG) | ||
111 | { | ||
112 | long other_mem = v.tz_minuteswest; | ||
113 | return eina_value_type_pset(convert, convert_mem, &other_mem); | ||
114 | } | ||
115 | else if (convert == EINA_VALUE_TYPE_INT64) | ||
116 | { | ||
117 | int64_t other_mem = v.tz_minuteswest; | ||
118 | return eina_value_type_pset(convert, convert_mem, &other_mem); | ||
119 | } | ||
120 | else if (convert == EINA_VALUE_TYPE_FLOAT) | ||
121 | return eina_value_type_pset(convert, convert_mem, &v.tz_minuteswest); | ||
122 | else if (convert == EINA_VALUE_TYPE_DOUBLE) | ||
123 | return eina_value_type_pset(convert, convert_mem, &v.tz_minuteswest); | ||
124 | else if (convert == EINA_VALUE_TYPE_STRINGSHARE || | ||
125 | convert == EINA_VALUE_TYPE_STRING) | ||
126 | { | ||
127 | const char *other_mem; | ||
128 | char buf[64]; | ||
129 | snprintf(buf, sizeof(buf), "%d", v.tz_minuteswest); | ||
130 | other_mem = buf; /* required due &buf == buf */ | ||
131 | return eina_value_type_pset(convert, convert_mem, &other_mem); | ||
132 | } | ||
133 | |||
134 | eina_error_set(EINA_ERROR_VALUE_FAILED); | ||
135 | return EINA_FALSE; | ||
136 | } | ||
137 | |||
138 | static Eina_Value_Type TZ_TYPE = { | ||
139 | EINA_VALUE_TYPE_VERSION, | ||
140 | sizeof(struct timezone), | ||
141 | "struct timezone", | ||
142 | _tz_setup, | ||
143 | _tz_flush, | ||
144 | _tz_copy, | ||
145 | _tz_compare, | ||
146 | _tz_convert_to, | ||
147 | NULL, //No convert from | ||
148 | _tz_vset, | ||
149 | _tz_pset, | ||
150 | _tz_pget | ||
151 | }; | ||
152 | |||
153 | int main(int argc, char **argv) | ||
154 | { | ||
155 | Eina_Value vtv, vtz; | ||
156 | struct timeval tv; | ||
157 | struct timezone tz; | ||
158 | char *s; | ||
159 | |||
160 | eina_init(); | ||
161 | |||
162 | eina_value_setup(&vtv, EINA_VALUE_TYPE_TIMEVAL); | ||
163 | eina_value_setup(&vtz, &TZ_TYPE); | ||
164 | |||
165 | gettimeofday(&tv, &tz); | ||
166 | eina_value_set(&vtv, tv); | ||
167 | eina_value_set(&vtz, tz); | ||
168 | |||
169 | s = eina_value_to_string(&vtv); | ||
170 | printf("time: %s\n", s); | ||
171 | free(s); | ||
172 | s = eina_value_to_string(&vtz); | ||
173 | printf("timezone: %s\n", s); | ||
174 | free(s); | ||
175 | |||
176 | eina_value_flush(&vtz); | ||
177 | eina_value_flush(&vtv); | ||
178 | } | ||