aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eina/src/examples/eina_model_01.c
blob: 28895aa666793bbdb6c21e4951842f20f0c5b0fc (plain)
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
/*
 * Compile with:
 * gcc -o eina_model_01 eina_model_01.c `pkg-config --cflags --libs eina`
 */

/*
 * This example demonstrates the usage of Eina Model by implementing
 * Bank Account Class, which is inherited from Base Class;
 * and Credit Card Class, which is inherited from Bank Account Class.
 *
 * Base Class(Eina_Model_Type) --> Bank Account Class --> Credit Card Class
 *
 * Bank Account Class implements "bank_account_data_set()" and "print()" methods;
 * Credit Card Class inherits these two and implements "credit_card_data_set()"
 *
 *
 *  Bank Account Class::print() calls for "_bank_account_data_print"
 *  Credit Card Class ::print() is reloaded with "_credit_card_data_print()"
 *  which calls for parent function "_bank_account_data_print"
 *
 */


#include <Eina.h>
#include <eina_safety_checks.h>

/*
 * Defining type for new model type
 * Model will have two methods
 */
typedef struct _Bank_Account_Type
{
   Eina_Model_Type parent_class;
   void (*bank_account_data_set)(Eina_Model *, const char *name, const char *number);
   void (*print)(Eina_Model *);
} Bank_Account_Type;

/*
 * Defining type for Bank Account private data
 */
typedef struct _Bank_Account_Data
{
   char name[30];
   char number[30];
} Bank_Account_Data;

/*
 * Defining type for Credit Card model type, which will be inherited from Bank Account model type
 * Model will have two parent's methods and additional one
 */
typedef struct _Credit_Card_Type
{
   Bank_Account_Type parent_class;
   void (*credit_card_data_set)(Eina_Model *, const char *, const char *, int) ;
} Credit_Card_Type;

/*
 * Defining type for Credit Card private data
 */
typedef struct _Credit_Card_Data
{
   char number[30];
   char expiry_date[30];
   int pin;
} Credit_Card_Data;

static Bank_Account_Type _BANK_ACCOUNT_TYPE;
static Credit_Card_Type _CREDIT_CARD_TYPE;
static Eina_Model_Type *BANK_ACCOUNT_TYPE = (Eina_Model_Type *) &_BANK_ACCOUNT_TYPE;
static Eina_Model_Type *CREDIT_CARD_TYPE = (Eina_Model_Type *) &_CREDIT_CARD_TYPE;


/*
 * Defining method for for Bank Account data
 */
static void
_bank_account_data_set(Eina_Model *mdl, const char *name, const char *number)
{
   Bank_Account_Data *bdata = eina_model_type_private_data_get(mdl, BANK_ACCOUNT_TYPE);

   if (!bdata)
      printf("ERROR\n");

   if (name != NULL)
     {
        strncpy(bdata->name, name, sizeof(bdata->name));
        bdata->name[sizeof(bdata->number) - 1] = '\0';
     }

   if (number != NULL)
     {
        strncpy(bdata->number, number, sizeof(bdata->number));
        bdata->number[sizeof(bdata->number) - 1] = '\0';
     }

   printf("%s :: %s %p\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl);
}


static void
_credit_card_data_set(Eina_Model *mdl, const char *number, const char *expiry_date, int pin)
{
   Credit_Card_Data *cdata = eina_model_type_private_data_get(mdl, CREDIT_CARD_TYPE);

   if (!cdata)
      printf("ERROR\n");

   if (number != NULL)
     {
        strncpy(cdata->number, number, sizeof(cdata->number));
        cdata->number[sizeof(cdata->number) - 1] = '\0';
     }

   if (expiry_date != NULL)
     {
        strncpy(cdata->expiry_date, expiry_date, sizeof(cdata->expiry_date));
        cdata->expiry_date[sizeof(cdata->expiry_date) - 1] = '\0';
     }

   cdata->pin = pin;
   printf("%s :: %s %p\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl);
}

static void
_bank_account_data_print(Eina_Model *mdl)
{
   const Bank_Account_Data *bdata = eina_model_type_private_data_get(mdl, BANK_ACCOUNT_TYPE);

   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
         , bdata->name, bdata->name, bdata->number, bdata->number);
}

static void
_credit_card_data_print(Eina_Model *mdl)
{
   void (*pf)(Eina_Model *);
   const Eina_Model_Type *ptype = eina_model_type_parent_get(eina_model_type_get(mdl));
   //const Eina_Model_Type *ptype = eina_model_type_get(mdl);

   pf = eina_model_type_method_resolve(ptype, mdl, Bank_Account_Type, print);
   if (pf)
      pf(mdl);
   else
      printf("ERROR: %d", __LINE__);
   const Credit_Card_Data *cdata = eina_model_type_private_data_get(mdl, CREDIT_CARD_TYPE);
   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
         , cdata->number, cdata->number, cdata->expiry_date, cdata->expiry_date, cdata->pin, &cdata->pin);
}

#define BANK_ACCOUNT(x) ((Bank_Account_Type *) x)
#define CREDIT_CARD(x) ((Credit_Card_Type *) x)

void
bank_account_data_set(Eina_Model *mdl, const char *name, char *number)
{

   EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(mdl, BANK_ACCOUNT_TYPE));

   void (*pf)(Eina_Model *, const char *, const char *);
   pf = eina_model_method_resolve(mdl, Bank_Account_Type, bank_account_data_set);
   if (pf)
      pf(mdl, name, number);
   else
      printf("ERROR %d\n", __LINE__);
}

void
data_print(Eina_Model *mdl)
{
   EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(mdl, BANK_ACCOUNT_TYPE));

   void (*pf)(Eina_Model *);
   pf = eina_model_method_resolve(mdl, Bank_Account_Type, print);
   if (pf)
      pf(mdl);
   else
      printf("ERROR %d\n", __LINE__);
}

void
credit_card_data_set(Eina_Model *mdl, const char *number, const char *expiry_date, int pin)
{
   EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(mdl, CREDIT_CARD_TYPE));

   void (*pf)(Eina_Model *, const char *, const char *, int);
   pf = eina_model_method_resolve(mdl, Credit_Card_Type, credit_card_data_set);
   if (pf)
      pf(mdl, number, expiry_date, pin);
   else
      printf("ERROR %d\n", __LINE__);
}

int main(void)
{
   Eina_Model *b, *cc;

   eina_init();

   memset(&_BANK_ACCOUNT_TYPE, 0, sizeof(_BANK_ACCOUNT_TYPE));
   memset(&_CREDIT_CARD_TYPE, 0, sizeof(_CREDIT_CARD_TYPE));

   BANK_ACCOUNT_TYPE->version = EINA_MODEL_TYPE_VERSION;
   BANK_ACCOUNT_TYPE->type_size = sizeof(Bank_Account_Type);
   BANK_ACCOUNT_TYPE->private_size = sizeof(Bank_Account_Data);
   BANK_ACCOUNT_TYPE->name = "Bank_Account_Model";
   BANK_ACCOUNT_TYPE->parent = EINA_MODEL_TYPE_GENERIC;
   BANK_ACCOUNT(BANK_ACCOUNT_TYPE)->bank_account_data_set = _bank_account_data_set;
   BANK_ACCOUNT(BANK_ACCOUNT_TYPE)->print = _bank_account_data_print;

   CREDIT_CARD_TYPE->version = EINA_MODEL_TYPE_VERSION;
   CREDIT_CARD_TYPE->type_size = sizeof(Credit_Card_Type);
   CREDIT_CARD_TYPE->private_size = sizeof(Credit_Card_Data);
   CREDIT_CARD_TYPE->name = "Credit_Card_Model";
   CREDIT_CARD_TYPE->parent = BANK_ACCOUNT_TYPE;
   CREDIT_CARD(CREDIT_CARD_TYPE)->credit_card_data_set = _credit_card_data_set;
   BANK_ACCOUNT(CREDIT_CARD_TYPE)->print = _credit_card_data_print;

   b = eina_model_new(BANK_ACCOUNT_TYPE);   //creating object of bank class
   cc = eina_model_new(CREDIT_CARD_TYPE);   //creating object of credit card class

   bank_account_data_set(b, "Bill Clark", "8569214756");
   bank_account_data_set(cc, "John Smith", "3154789");

   credit_card_data_set(cc, "5803 6589 4786 3279 9173", "01/01/2015", 1234);

   data_print(b);
   data_print(cc);

   eina_model_unref(b);
   eina_model_unref(cc);

   eina_shutdown();

   return 0;
}