diff --git a/c-eo-intro/src/eo_intro_main.c b/c-eo-intro/src/eo_intro_main.c index 2d8eb0cc..e3d6d20d 100644 --- a/c-eo-intro/src/eo_intro_main.c +++ b/c-eo-intro/src/eo_intro_main.c @@ -8,45 +8,6 @@ #include Eo *_root, *_child1, *_child2; -Eo *_root_ref, *_child1_ref, *_child2_ref; - -// Retrieves the name of an object's parent, handling special cases like not -// having any parent, or the object having been already destroyed. -static const char* -_parent_name_get(Eo *obj) -{ - // Object has been destroyed - if (efl_ref_count(obj) == 0) return "-"; - // Object has no parent - if (efl_parent_get(obj) == NULL) return "none"; - // Otherwise, return parent's name - return efl_name_get(efl_parent_get(obj)); -} - -// Prints status of all our objects in a pretty table -static void -_status_print() -{ - printf("Object: %6s %6s %6s\n", "ROOT", "CHILD1", "CHILD2"); - printf("Refcount: %6d %6d %6d\n", - efl_ref_count(_root_ref), - efl_ref_count(_child1_ref), - efl_ref_count(_child2_ref)); - printf("Parent: %6s %6s %6s\n\n", - _parent_name_get(_root_ref), - _parent_name_get(_child1_ref), - _parent_name_get(_child2_ref)); -} - -// Gets called whenever an object is deleted -static void -_obj_del_cb(void *data EINA_UNUSED, const Efl_Event *event) -{ - Eo *obj = event->object; - - printf("Object named \"%s\" is about to be deleted\n", efl_name_get(obj)); - _status_print(); -} // Create our test hierarchy static void @@ -55,26 +16,14 @@ _obj_create() // First create a root element _root = efl_add(EFL_MODEL_ITEM_CLASS, NULL, efl_name_set(efl_added, "Root")); - // Add a weak reference so we can keep track of its state - efl_wref_add(_root, &_root_ref); - // Register a callback for DELETION events - efl_event_callback_add(_root, EFL_EVENT_DEL, _obj_del_cb, NULL); // Create the first child element _child1 = efl_add(EFL_MODEL_ITEM_CLASS, _root, efl_name_set(efl_added, "Child1")); - // Add a weak reference so we can keep track of its state - efl_wref_add(_child1, &_child1_ref); - // Register a callback for DELETION events - efl_event_callback_add(_child1, EFL_EVENT_DEL, _obj_del_cb, NULL); // Create the second child element, this time, with an extra reference _child2 = efl_add_ref(EFL_MODEL_ITEM_CLASS, _root, efl_name_set(efl_added, "Child2")); - // Add a weak reference so we can keep track of its state - efl_wref_add(_child2, &_child2_ref); - // Register a callback for DELETION events - efl_event_callback_add(_child2, EFL_EVENT_DEL, _obj_del_cb, NULL); } // Destroy the test hierarchy @@ -84,8 +33,6 @@ _obj_destroy() // Destroy the root element printf ("Deleting root...\n"); efl_unref(_root); - printf ("After deleting root:\n"); - _status_print(); // Destroy the child2 element, for which we were keeping an extra reference printf ("Deleting Child2...\n"); @@ -98,15 +45,9 @@ efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED) // Create all objects _obj_create(); - printf ("Initial state:\n"); - _status_print(); - // Destroy all objects _obj_destroy(); - printf ("Final state:\n"); - _status_print(); - // Exit efl_exit(0); } diff --git a/c-eo-refcount/meson.build b/c-eo-refcount/meson.build new file mode 100644 index 00000000..dff8ea96 --- /dev/null +++ b/c-eo-refcount/meson.build @@ -0,0 +1,13 @@ +project( + 'efl-example-eo-refcount', 'c', + version : '0.0.1', + default_options: [ 'c_std=gnu99', 'warning_level=2' ], + meson_version : '>= 0.38.0') + +eina = dependency('eina', version : '>=1.20.99') +efl = dependency('efl', version : '>=1.20.99') +elm = dependency('elementary', version : '>=1.20.99') + +inc = include_directories('.') +subdir('src') + diff --git a/c-eo-refcount/src/eo_refcount_main.c b/c-eo-refcount/src/eo_refcount_main.c new file mode 100644 index 00000000..2d8eb0cc --- /dev/null +++ b/c-eo-refcount/src/eo_refcount_main.c @@ -0,0 +1,114 @@ +#define EFL_EO_API_SUPPORT 1 +#define EFL_BETA_API_SUPPORT 1 + +#include +#include +#include + +#include + +Eo *_root, *_child1, *_child2; +Eo *_root_ref, *_child1_ref, *_child2_ref; + +// Retrieves the name of an object's parent, handling special cases like not +// having any parent, or the object having been already destroyed. +static const char* +_parent_name_get(Eo *obj) +{ + // Object has been destroyed + if (efl_ref_count(obj) == 0) return "-"; + // Object has no parent + if (efl_parent_get(obj) == NULL) return "none"; + // Otherwise, return parent's name + return efl_name_get(efl_parent_get(obj)); +} + +// Prints status of all our objects in a pretty table +static void +_status_print() +{ + printf("Object: %6s %6s %6s\n", "ROOT", "CHILD1", "CHILD2"); + printf("Refcount: %6d %6d %6d\n", + efl_ref_count(_root_ref), + efl_ref_count(_child1_ref), + efl_ref_count(_child2_ref)); + printf("Parent: %6s %6s %6s\n\n", + _parent_name_get(_root_ref), + _parent_name_get(_child1_ref), + _parent_name_get(_child2_ref)); +} + +// Gets called whenever an object is deleted +static void +_obj_del_cb(void *data EINA_UNUSED, const Efl_Event *event) +{ + Eo *obj = event->object; + + printf("Object named \"%s\" is about to be deleted\n", efl_name_get(obj)); + _status_print(); +} + +// Create our test hierarchy +static void +_obj_create() +{ + // First create a root element + _root = efl_add(EFL_MODEL_ITEM_CLASS, NULL, + efl_name_set(efl_added, "Root")); + // Add a weak reference so we can keep track of its state + efl_wref_add(_root, &_root_ref); + // Register a callback for DELETION events + efl_event_callback_add(_root, EFL_EVENT_DEL, _obj_del_cb, NULL); + + // Create the first child element + _child1 = efl_add(EFL_MODEL_ITEM_CLASS, _root, + efl_name_set(efl_added, "Child1")); + // Add a weak reference so we can keep track of its state + efl_wref_add(_child1, &_child1_ref); + // Register a callback for DELETION events + efl_event_callback_add(_child1, EFL_EVENT_DEL, _obj_del_cb, NULL); + + // Create the second child element, this time, with an extra reference + _child2 = efl_add_ref(EFL_MODEL_ITEM_CLASS, _root, + efl_name_set(efl_added, "Child2")); + // Add a weak reference so we can keep track of its state + efl_wref_add(_child2, &_child2_ref); + // Register a callback for DELETION events + efl_event_callback_add(_child2, EFL_EVENT_DEL, _obj_del_cb, NULL); +} + +// Destroy the test hierarchy +static void +_obj_destroy() +{ + // Destroy the root element + printf ("Deleting root...\n"); + efl_unref(_root); + printf ("After deleting root:\n"); + _status_print(); + + // Destroy the child2 element, for which we were keeping an extra reference + printf ("Deleting Child2...\n"); + efl_unref(_child2); +} + +EAPI_MAIN void +efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED) +{ + // Create all objects + _obj_create(); + + printf ("Initial state:\n"); + _status_print(); + + // Destroy all objects + _obj_destroy(); + + printf ("Final state:\n"); + _status_print(); + + // Exit + efl_exit(0); +} +EFL_MAIN() + diff --git a/c-eo-refcount/src/meson.build b/c-eo-refcount/src/meson.build new file mode 100644 index 00000000..efea9ab9 --- /dev/null +++ b/c-eo-refcount/src/meson.build @@ -0,0 +1,12 @@ +src = files([ + 'eo_refcount_main.c', +]) + +deps = [eina, efl, elm] + +executable('efl_example_eo_refcount', src, + dependencies : deps, + include_directories : inc, + install : true +) +