move around - flatter.
[profile/ivi/evas.git] / src / lib / canvas / evas_data.c
1 #include "evas_common.h"
2 #include "evas_private.h"
3
4 /**
5  * @defgroup Evas_Object_Data_Group Object Data Functions
6  *
7  * Functions that retrieve and set data associated attached to an evas
8  * object.
9  */
10
11 /**
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
16  *
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.
22  *
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.
26  *
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.
30  *
31  * Example:
32  *
33  * @code
34  * int *my_data;
35  * extern Evas_Object *obj;
36  *
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"));
40  * @endcode
41  * @ingroup Evas_Object_Data_Group
42  */
43 EAPI void
44 evas_object_data_set(Evas_Object *obj, const char *key, const void *data)
45 {
46    Evas_Data_Node *node;
47
48    MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
49    return;
50    MAGIC_CHECK_END();
51    if (!key) return;
52
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);
60 }
61
62 /**
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
67  *
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
75  * error occurs.
76  *
77  * Example:
78  *
79  * @code
80  * int *my_data;
81  * extern Evas_Object *obj;
82  *
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");
86  * @endcode
87  * @ingroup Evas_Object_Data_Group
88  */
89 EAPI void *
90 evas_object_data_get(const Evas_Object *obj, const char *key)
91 {
92    Evas_List *l;
93
94    MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
95    return NULL;
96    MAGIC_CHECK_END();
97    if (!key) return NULL;
98
99    for (l = obj->data.elements; l; l = l->next)
100      {
101         Evas_Data_Node *node;
102
103         node = l->data;
104         if (!strcmp(node->key, key))
105           {
106              Evas_List *lst;
107              lst = obj->data.elements;
108              lst = evas_list_promote_list(lst, l);
109              ((Evas_Object *)obj)->data.elements = lst;
110              return node->data;
111           }
112      }
113    return NULL;
114 }
115
116 /**
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
121  *
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.
125  *
126  * Example:
127  *
128  * @code
129  * int *my_data;
130  * extern Evas_Object *obj;
131  *
132  * my_data = evas_object_data_del(obj, "name_of_my_data");
133  * @endcode
134  * @ingroup Evas_Object_Data_Group
135  */
136 EAPI void *
137 evas_object_data_del(Evas_Object *obj, const char *key)
138 {
139    Evas_List *l;
140
141    MAGIC_CHECK(obj, Evas_Object, MAGIC_OBJ);
142    return NULL;
143    MAGIC_CHECK_END();
144    if (!key) return NULL;
145    for (l = obj->data.elements; l; l = l->next)
146      {
147         Evas_Data_Node *node;
148
149         node = l->data;
150         if (!strcmp(node->key, key))
151           {
152              void *data;
153
154              data = node->data;
155              obj->data.elements = evas_list_remove_list(obj->data.elements, l);
156              free(node);
157              return data;
158           }
159      }
160    return NULL;
161 }