summaryrefslogtreecommitdiff
path: root/reference/c/eina/src/eina_value.c
blob: cc37d2f314321f2c1d29f6e881c4fda98c2c3e5c (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
#define EFL_BETA_API_SUPPORT 1

#include <stdio.h>

#include <Eina.h>
#include <Efl_Core.h>

/*
 * Eina Value examples.
 *
 * These examples demonstrate how to work with eina_value data and methods.
 * Eina_Value is a way to represent and pass data of varying types and to
 * convert efficiently between them..
 * Eina_Value can even define structs for managing more complex requirements.
 */

static void
_value_int()
{
   Eina_Value int_val;
   char *str;
   int i;

   // Setting up an integer value type
   eina_value_setup(&int_val, EINA_VALUE_TYPE_INT);
   eina_value_set(&int_val, 123);
   eina_value_get(&int_val, &i);
   printf("int_val value is %d\n", i);

   // It can easily be converted it to a string
   str = eina_value_to_string(&int_val);
   printf("int_val to string is \"%s\"\n", str);
   free(str); // it was allocated by eina_value_to_string()
   eina_value_flush(&int_val);
}

static void
_value_string()
{
   Eina_Value str_val;
   const char *str;
   char *newstr;

   // Setting up an integer value type
   eina_value_setup(&str_val, EINA_VALUE_TYPE_STRING);
   eina_value_set(&str_val, "My string");
   eina_value_get(&str_val, &str);
   printf("str_val value is \"%s\" (pointer: %p)\n", str, str);

   // To string here will copy the data, so we still need to free it
   // Notice that the pointer is different on the returned string
   newstr = eina_value_to_string(&str_val);
   printf("str_val to string is \"%s\" (pointer: %p)\n", newstr, newstr);
   free(newstr); // it was allocated by eina_value_to_string()
   eina_value_flush(&str_val);
}

static void
_value_convert()
{
   Eina_Value str_val, int_val;
   int i;
   char *str;

   // set up string and int types to convert between
   eina_value_setup(&str_val, EINA_VALUE_TYPE_STRING);
   eina_value_setup(&int_val, EINA_VALUE_TYPE_INT);

   // convert from int to string:
   eina_value_set(&int_val, 123);
   eina_value_get(&int_val, &i);
   eina_value_convert(&int_val, &str_val);
   eina_value_get(&str_val, &str);
   printf("int_val was %d, converted to string is \"%s\"\n", i, str);

   // and the other way around!
   eina_value_set(&str_val, "33");
   eina_value_get(&str_val, &str);
   eina_value_convert(&str_val, &int_val);
   eina_value_get(&int_val, &i);
   printf("str_val was \"%s\", converted to int is %d\n", str, i);

   eina_value_flush(&str_val);
   eina_value_flush(&int_val);
}

static Eina_Value_Struct_Desc *_STRUCT_DESC = NULL;

static void
_value_struct_define()
{
   typedef struct _Struct {
     int num;
     char chr;
   } Struct;

   static Eina_Value_Struct_Member members[] = {
     // no eina_value_type as they are not constant initializers, see below.
     EINA_VALUE_STRUCT_MEMBER(NULL, Struct, num),
     EINA_VALUE_STRUCT_MEMBER(NULL, Struct, chr)
   };

   // set up the members and describe the struct parameters
   members[0].type = EINA_VALUE_TYPE_INT;
   members[1].type = EINA_VALUE_TYPE_CHAR;
   static Eina_Value_Struct_Desc desc = {
     EINA_VALUE_STRUCT_DESC_VERSION,
     NULL, // no special operations
     members,
     EINA_C_ARRAY_LENGTH(members),
     sizeof(Struct)
   };
   _STRUCT_DESC = &desc;
}

static void
_value_struct_rand(Eina_Value *val)
{
   eina_value_struct_set(val, "num", rand());
   eina_value_struct_set(val, "chr", 32 + rand() % 94);

   // this parameter is not defined but does not error
   eina_value_struct_set(val, "missing", rand());
}

static void
_value_struct_print(Eina_Value *struct_val)
{
   int num;
   char chr;

   eina_value_struct_get(struct_val, "num", &num);
   eina_value_struct_get(struct_val, "chr", &chr);

   printf("Struct content\n");
   printf("  num: %d\n", num);
   printf("  chr: %c\n", chr);

   // we can check if a struct contains a field before working with it
   if (eina_value_struct_get(struct_val, "missing", &num))
     printf(  "missing: %d\n", num);
}

static void
_value_struct()
{
   Eina_Value *struct_val;
   _value_struct_define();

   struct_val = eina_value_struct_new(_STRUCT_DESC);
   _value_struct_rand(struct_val);

   _value_struct_print(struct_val);
   eina_value_free(struct_val);
}

EAPI_MAIN void
efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
{
   _value_int();
   printf("\n");

   _value_string();
   printf("\n");

   _value_convert();
   printf("\n");

   _value_struct();

   efl_exit(0);
}
EFL_MAIN()