diff options
Diffstat (limited to 'libraries/eina/src/examples/eina_model_01.c')
-rw-r--r-- | libraries/eina/src/examples/eina_model_01.c | 235 |
1 files changed, 0 insertions, 235 deletions
diff --git a/libraries/eina/src/examples/eina_model_01.c b/libraries/eina/src/examples/eina_model_01.c deleted file mode 100644 index 28895aa..0000000 --- a/libraries/eina/src/examples/eina_model_01.c +++ /dev/null | |||
@@ -1,235 +0,0 @@ | |||
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 | } | ||