aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eina/src/examples/eina_model_04_human.c
blob: d9a10ab2a6e387c15668640f41a9c36c7962bd3a (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
/*
 * human.c
 *
 */

#include "eina_model_04_human.h"
#include "eina_model_04_whistler.h"

static Eina_Bool initialized = EINA_FALSE;

static void
_human_eat(Eina_Model *m)
{
   printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)),
         __func__);
   printf("\t\t Salad\n");
}

static void
_human_walk(Eina_Model *m)
{
   printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)),
         __func__);
   printf("\t\t Walk\n");
}

static void
_human_whistle(Eina_Model *m)
{
   printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)),
         __func__);
   printf("\t\t Whistle Human\n");
}

static void
_human_swim(Eina_Model *m)
{
   printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)),
         __func__);
   printf("\t\t Swim Human\n");
}

static void
_human_dive(Eina_Model *m)
{
   printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)),
         __func__);
   printf("\t\t Dive Human\n");
}
/*
 * defining Human Model Instance
 * defining Whistler Interface instance
 * defining Swimmer Interface instance
 * defining Diver Interface instance
 */

const char *HUMAN_MODEL_TYPE_NAME = NULL;

static Human_Type _HUMAN_TYPE;
const Eina_Model_Type * const HUMAN_TYPE = (Eina_Model_Type *) &_HUMAN_TYPE;

static const Whistler_Interface _WHISTLER_INTERFACE;
static const Eina_Model_Interface * const WHISTLER_INTERFACE =
   (Eina_Model_Interface *) &_WHISTLER_INTERFACE;

static const Swimmer_Interface _SWIMMER_INTERFACE;
static const Eina_Model_Interface * const SWIMMER_INTERFACE =
   (Eina_Model_Interface *) &_SWIMMER_INTERFACE;

static const Diver_Interface _DIVER_INTERFACE;
static const Eina_Model_Interface * const DIVER_INTERFACE =
   (Eina_Model_Interface *) &_DIVER_INTERFACE;

/*
 * defining parent interfaces for Diver Interface instance
 * defining Interfaces for Human Model instance
 */
static const Eina_Model_Interface * PARENT_INTERFACES_ARRAY[] =
   { &_SWIMMER_INTERFACE.base_interface, NULL }; //this array is for model
static const Eina_Model_Interface * MODEL_INTERFACES_ARRAY[] =
   { &_WHISTLER_INTERFACE.base_interface, &_DIVER_INTERFACE.base_interface,
      NULL }; //this array is for model

void
human_init()
{
   Eina_Model_Type *type;

   if (initialized) return;
   initialized = EINA_TRUE;

   animal_init();

   /*
    * Initializing Whistler Interface Instance
    */
   Eina_Model_Interface *iface = (Eina_Model_Interface *) &_WHISTLER_INTERFACE;
   iface->version = EINA_MODEL_INTERFACE_VERSION;
   iface->interface_size = sizeof(Whistler_Interface);
   iface->name = WHISTLER_INTERFACE_NAME;
   WHISTLER_INTERFACE(iface)->whistle = _human_whistle;

   /*
    * Initializing Swimmer Interface Instance
    */
   iface = (Eina_Model_Interface *) &_SWIMMER_INTERFACE;
   iface->version = EINA_MODEL_INTERFACE_VERSION;
   iface->interface_size = sizeof(Swimmer_Interface);
   iface->name = SWIMMER_INTERFACE_NAME;
   SWIMMER_INTERFACE(iface)->swim = _human_swim;

   /*
    * Initializing Diver Interface Instance
    * Diver_Interface is inherited from Swimmer
    */
   iface = (Eina_Model_Interface *) &_DIVER_INTERFACE;
   iface->version = EINA_MODEL_INTERFACE_VERSION;
   iface->interface_size = sizeof(Diver_Interface);
   iface->name = DIVER_INTERFACE_NAME;
   iface->interfaces = PARENT_INTERFACES_ARRAY;
   DIVER_INTERFACE(iface)->dive = _human_dive;

   /*
    * Initializing instance of Human Model
    */

   HUMAN_MODEL_TYPE_NAME = "Human_Model_Type";
   
   type = (Eina_Model_Type *) &_HUMAN_TYPE;
   type->version = EINA_MODEL_TYPE_VERSION;
   type->name = HUMAN_MODEL_TYPE_NAME;
   type->private_size = 0;

   eina_model_type_subclass_setup(type, ANIMAL_TYPE);

   type->type_size = sizeof(Human_Type);
   type->interfaces = MODEL_INTERFACES_ARRAY;

   ANIMAL_TYPE(type)->eat = _human_eat;
   HUMAN_TYPE(type)->walk =_human_walk;
}


/*
 * call for implemented Human Class function
 */
void
human_walk(Eina_Model *m)
{
   EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, HUMAN_TYPE));

   void (*pf)(Eina_Model *m);
   pf = eina_model_method_resolve(m, Human_Type, walk);
   EINA_SAFETY_ON_NULL_RETURN(pf);
   printf("%s()    \t", __func__);
   pf(m);
}