ajwillia-ms pushed a commit to branch master. http://git.enlightenment.org/tools/examples.git/commit/?id=866bfc8a49411d755fbb827344b8f79a9bd0be05
commit 866bfc8a49411d755fbb827344b8f79a9bd0be05 Author: Andy Williams <[email protected]> Date: Thu Nov 16 08:53:54 2017 +0000 eina: Add struct value reference --- reference/c/eina/src/eina_value.c | 71 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/reference/c/eina/src/eina_value.c b/reference/c/eina/src/eina_value.c index 79c8650..0fe7a79 100644 --- a/reference/c/eina/src/eina_value.c +++ b/reference/c/eina/src/eina_value.c @@ -70,6 +70,74 @@ _value_convert() 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) + }; + + 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); + + 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) { @@ -80,6 +148,9 @@ efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED) printf("\n"); _value_convert(); + printf("\n"); + + _value_struct(); efl_exit(0); } --
