EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / lib / eina_accessor.c
1 /* EINA - EFL data type library
2  * Copyright (C) 2002-2008 Cedric Bail
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library;
16  * if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include <stdlib.h>
24
25 #include "eina_config.h"
26 #include "eina_private.h"
27
28 /* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
29 #include "eina_safety_checks.h"
30 #include "eina_accessor.h"
31
32 /*============================================================================*
33 *                                  Local                                     *
34 *============================================================================*/
35
36 /**
37  * @cond LOCAL
38  */
39
40 static const char EINA_MAGIC_ACCESSOR_STR[] = "Eina Accessor";
41
42 #define EINA_MAGIC_CHECK_ACCESSOR(d)                            \
43    do {                                                          \
44         if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_ACCESSOR)) {              \
45              EINA_MAGIC_FAIL(d, EINA_MAGIC_ACCESSOR); }                  \
46      } while(0)
47
48 /**
49  * @endcond
50  */
51
52 /*============================================================================*
53 *                                 Global                                     *
54 *============================================================================*/
55
56 /**
57  * @internal
58  * @brief Initialize the accessor module.
59  *
60  * @return #EINA_TRUE on success, #EINA_FALSE on failure.
61  *
62  * This function sets up the accessor module of Eina. It is called by
63  * eina_init().
64  *
65  * @see eina_init()
66  */
67 Eina_Bool
68 eina_accessor_init(void)
69 {
70    return eina_magic_string_set(EINA_MAGIC_ACCESSOR, EINA_MAGIC_ACCESSOR_STR);
71 }
72
73 /**
74  * @internal
75  * @brief Shut down the accessor module.
76  *
77  * @return #EINA_TRUE on success, #EINA_FALSE on failure.
78  *
79  * This function shuts down the accessor module set up by
80  * eina_accessor_init(). It is called by eina_shutdown().
81  *
82  * @see eina_shutdown()
83  */
84 Eina_Bool
85 eina_accessor_shutdown(void)
86 {
87    return EINA_TRUE;
88 }
89
90 /*============================================================================*
91 *                                   API                                      *
92 *============================================================================*/
93
94
95 EAPI void
96 eina_accessor_free(Eina_Accessor *accessor)
97 {
98    if (!accessor)
99      return;
100
101    EINA_MAGIC_CHECK_ACCESSOR(accessor);
102    EINA_SAFETY_ON_NULL_RETURN(accessor->free);
103    accessor->free(accessor);
104 }
105
106 EAPI void *
107 eina_accessor_container_get(Eina_Accessor *accessor)
108 {
109    EINA_MAGIC_CHECK_ACCESSOR(accessor);
110    EINA_SAFETY_ON_NULL_RETURN_VAL(accessor,                NULL);
111    EINA_SAFETY_ON_NULL_RETURN_VAL(accessor->get_container, NULL);
112    return accessor->get_container(accessor);
113 }
114
115 EAPI Eina_Bool
116 eina_accessor_data_get(Eina_Accessor *accessor,
117                        unsigned int position,
118                        void **data)
119 {
120    EINA_MAGIC_CHECK_ACCESSOR(accessor);
121    EINA_SAFETY_ON_NULL_RETURN_VAL(accessor,         EINA_FALSE);
122    EINA_SAFETY_ON_NULL_RETURN_VAL(accessor->get_at, EINA_FALSE);
123    EINA_SAFETY_ON_NULL_RETURN_VAL(data,             EINA_FALSE);
124    return accessor->get_at(accessor, position, data);
125 }
126
127 EAPI void
128 eina_accessor_over(Eina_Accessor *accessor,
129                    Eina_Each_Cb cb,
130                    unsigned int start,
131                    unsigned int end,
132                    const void *fdata)
133 {
134    const void *container;
135    void *data;
136    unsigned int i;
137
138    if (!accessor) return ;
139
140    EINA_MAGIC_CHECK_ACCESSOR(accessor);
141    EINA_SAFETY_ON_NULL_RETURN(accessor->get_container);
142    EINA_SAFETY_ON_NULL_RETURN(accessor->get_at);
143    EINA_SAFETY_ON_NULL_RETURN(cb);
144    EINA_SAFETY_ON_FALSE_RETURN(start < end);
145
146    if (!eina_accessor_lock(accessor))
147       return ;
148
149    container = accessor->get_container(accessor);
150    for (i = start; i < end && accessor->get_at(accessor, i, &data) == EINA_TRUE;
151         ++i)
152       if (cb(container, data, (void *)fdata) != EINA_TRUE)
153          goto on_exit;
154
155  on_exit:
156    (void) eina_accessor_unlock(accessor);
157 }
158
159 EAPI Eina_Bool
160 eina_accessor_lock(Eina_Accessor *accessor)
161 {
162    EINA_MAGIC_CHECK_ACCESSOR(accessor);
163    EINA_SAFETY_ON_NULL_RETURN_VAL(accessor, EINA_FALSE);
164
165    if (accessor->lock)
166       return accessor->lock(accessor);
167    return EINA_TRUE;
168 }
169
170 EAPI Eina_Bool
171 eina_accessor_unlock(Eina_Accessor *accessor)
172 {
173    EINA_MAGIC_CHECK_ACCESSOR(accessor);
174    EINA_SAFETY_ON_NULL_RETURN_VAL(accessor, EINA_FALSE);
175
176    if (accessor->unlock)
177       return accessor->unlock(accessor);
178    return EINA_TRUE;
179 }