1 #include "evas_common.h"
2 #include "evas_private.h"
5 * @defgroup Evas_Object_Data_Group Object Data Functions
7 * Functions that retrieve and set data associated attached to an evas
12 * Set an attached data pointer to an object with a given string key.
13 * @param obj The object to attach the data pointer to
14 * @param key The string key for the data to access it
15 * @param data The ponter to the data to be attached
17 * This attaches the pointer @p data to the object @p obj given the string
18 * @p key. This pointer will stay "hooked" to the object until a new pointer
19 * with the same string key is attached with evas_object_data_set() or it is
20 * deleted with evas_object_data_del(). On deletion of the object @p obj, the
21 * pointers will not be accessible from the object anymore.
23 * You can find the pointer attached under a string key using
24 * evas_object_data_get(). It is the job of the calling application to free
25 * any data pointed to by @p data when it is no longer required.
27 * If @p data is NULL, the old value stored at @p key will be removed but no
28 * new value will be stored. This is synonymous with calling
29 * evas_object_data_del() with @p obj and @p key.
35 * extern Evas_Object *obj;
37 * my_data = malloc(500);
38 * evas_object_data_set(obj, "name_of_data", my_data);
39 * printf("The data that was attached was %p\n", evas_object_data_get(obj, "name_of_data"));
41 * @ingroup Evas_Object_Data_Group
44 evas_object_data_set(Evas_Object *obj, const char *key, const void *data)
48 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
53 evas_object_data_del(obj, key);
54 if (data == NULL) return;
55 node = malloc(sizeof(Evas_Data_Node) + strlen(key) + 1);
56 node->key = (char *)node + sizeof(Evas_Data_Node);
57 strcpy(node->key, key);
58 node->data = (void *)data;
59 obj->data.elements = evas_list_prepend(obj->data.elements, node);
63 * Return an attached data pointer by its given string key.
64 * @param obj The object to which the data was attached
65 * @param key The string key the data was stored under
66 * @return The data pointer stored, or NULL if none was stored
68 * This function will return the data pointer attached to the object @p obj
69 * stored using the string key @p key. If the object is valid and data was
70 * stored under the given key, the pointer that was stored will be reuturned.
71 * If this is not the case, NULL will be returned, signifying an invalid object
72 * or non-existent key. It is possible a NULL pointer was stored given that
73 * key, but this situation is non-sensical and thus can be considered an error
74 * as well. NULL pointers are never stored as this is the return value if an
81 * extern Evas_Object *obj;
83 * my_data = evas_object_data_get(obj, "name_of_my_data");
84 * if (my_data) printf("Data stored was %p\n", my_data);
85 * else printf("No data was stored on the object\n");
87 * @ingroup Evas_Object_Data_Group
90 evas_object_data_get(const Evas_Object *obj, const char *key)
94 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
97 if (!key) return NULL;
99 for (l = obj->data.elements; l; l = l->next)
101 Evas_Data_Node *node;
104 if (!strcmp(node->key, key))
107 lst = obj->data.elements;
108 lst = evas_list_promote_list(lst, l);
109 ((Evas_Object *)obj)->data.elements = lst;
117 * Delete at attached data pointer from an object.
118 * @param obj The object to delete the data pointer from
119 * @param key The string key the data was stored under
120 * @return The original data pointer stored at @p key on @p obj
122 * This will remove thee stored data pointer from @p obj stored under @p key,
123 * and return the original pointer stored under @p key, if any, nor NULL if
124 * nothing was stored under that key.
130 * extern Evas_Object *obj;
132 * my_data = evas_object_data_del(obj, "name_of_my_data");
134 * @ingroup Evas_Object_Data_Group
137 evas_object_data_del(Evas_Object *obj, const char *key)
141 MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
144 if (!key) return NULL;
145 for (l = obj->data.elements; l; l = l->next)
147 Evas_Data_Node *node;
150 if (!strcmp(node->key, key))
155 obj->data.elements = evas_list_remove_list(obj->data.elements, l);