allow amalgamation of files, at least symbols don't clash anymore.
[profile/ivi/eina.git] / src / lib / eina_main.c
1 /* EINA - EFL data type library
2  * Copyright (C) 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 #include <stdio.h>
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "eina_config.h"
26 #include "eina_private.h"
27 #include "eina_types.h"
28 #include "eina_main.h"
29 #include "eina_error.h"
30 #include "eina_log.h"
31 #include "eina_hash.h"
32 #include "eina_stringshare.h"
33 #include "eina_list.h"
34 #include "eina_matrixsparse.h"
35 #include "eina_array.h"
36 #include "eina_counter.h"
37 #include "eina_benchmark.h"
38 #include "eina_magic.h"
39 #include "eina_rectangle.h"
40 #include "eina_safety_checks.h"
41
42 /*============================================================================*
43  *                                  Local                                     *
44  *============================================================================*/
45
46 /**
47  * @cond LOCAL
48  */
49
50 static int _eina_main_count = 0;
51 static int _eina_log_dom = -1;
52
53 #ifdef ERR
54 #undef ERR
55 #endif
56 #define ERR(...) EINA_LOG_DOM_ERR(_eina_log_dom, __VA_ARGS__)
57
58 #ifdef DBG
59 #undef DBG
60 #endif
61 #define DBG(...) EINA_LOG_DOM_DBG(_eina_log_dom, __VA_ARGS__)
62
63
64 /* place module init/shutdown functions here to avoid other modules
65  * calling them by mistake.
66  */
67 #define S(x) extern Eina_Bool eina_##x##_init(void); extern Eina_Bool eina_##x##_shutdown(void)
68 S(log);
69 S(error);
70 S(safety_checks);
71 S(magic_string);
72 S(iterator);
73 S(accessor);
74 S(array);
75 S(module);
76 S(mempool);
77 S(list);
78 S(stringshare);
79 S(matrixsparse);
80 S(convert);
81 S(counter);
82 S(benchmark);
83 S(rectangle);
84 #undef S
85
86 struct eina_desc_setup
87 {
88    const char *name;
89    Eina_Bool (*init)(void);
90    Eina_Bool (*shutdown)(void);
91 };
92
93 static const struct eina_desc_setup _eina_desc_setup[] = {
94 #define S(x) {#x, eina_##x##_init, eina_##x##_shutdown}
95   /* log is a special case as it needs printf */
96   S(error),
97   S(safety_checks),
98   S(magic_string),
99   S(iterator),
100   S(accessor),
101   S(array),
102   S(module),
103   S(mempool),
104   S(list),
105   S(stringshare),
106   S(matrixsparse),
107   S(convert),
108   S(counter),
109   S(benchmark),
110   S(rectangle)
111 #undef S
112 };
113 static const size_t _eina_desc_setup_len = sizeof(_eina_desc_setup) / sizeof(_eina_desc_setup[0]);
114
115 static void
116 _eina_shutdown_from_desc(const struct eina_desc_setup *itr)
117 {
118    for (itr--; itr >= _eina_desc_setup; itr--)
119      {
120         if (!itr->shutdown())
121           ERR("Problems shutting down eina module '%s', ignored.", itr->name);
122      }
123
124    eina_log_domain_unregister(_eina_log_dom);
125    _eina_log_dom = -1;
126    eina_log_shutdown();
127 }
128
129 /**
130  * @endcond
131  */
132
133 /*============================================================================*
134  *                                 Global                                     *
135  *============================================================================*/
136
137 /*============================================================================*
138  *                                   API                                      *
139  *============================================================================*/
140
141 /**
142  * @addtogroup Eina_Main_Group Main
143  *
144  * @brief These functions provide general initialisation and shut down
145  * functions.
146  *
147  * @{
148  */
149
150 /**
151  * @brief Initialize the Eina library.
152  *
153  * @return 1 or greater on success, 0 on error.
154  *
155  * This function sets up all the eina modules. It returns 0 on
156  * failure (that is, when one of the module fails to initialize),
157  * otherwise it returns the number of times it has already been
158  * called.
159  *
160  * When Eina is not used anymore, call eina_shutdown() to shut down
161  * the Eina library.
162  */
163 EAPI int
164 eina_init(void)
165 {
166    const struct eina_desc_setup *itr, *itr_end;
167
168    if (EINA_LIKELY(_eina_main_count > 0))
169      return ++_eina_main_count;
170
171    if (!eina_log_init())
172      {
173         fprintf(stderr, "Could not initialize eina logging system.\n");
174         return 0;
175      }
176    _eina_log_dom = eina_log_domain_register("eina", EINA_LOG_COLOR_DEFAULT);
177    if (_eina_log_dom < 0)
178      {
179         EINA_LOG_ERR("Could not register log domain: eina");
180         eina_log_shutdown();
181         return 0;
182      }
183
184    itr = _eina_desc_setup;
185    itr_end = itr + _eina_desc_setup_len;
186    for (; itr < itr_end; itr++)
187      {
188         if (!itr->init())
189           {
190              ERR("Could not initialize eina module '%s'.", itr->name);
191              _eina_shutdown_from_desc(itr);
192              return 0;
193           }
194      }
195
196    _eina_main_count = 1;
197    return 1;
198 }
199
200 /**
201  * @brief Shut down the Eina library.
202  *
203  * @return 0 when all the modules is completely shut down, 1 or
204  * greater otherwise.
205  *
206  * This function shuts down the Eina library. It returns 0 when it has
207  * been called the same number of times than eina_init(). In that case
208  * it shut down all the Eina modules.
209  *
210  * Once this function succeeds (that is, @c 0 is returned), you must
211  * not call any of the Eina function anymore. You must call
212  * eina_init() again to use the Eina functions again.
213  */
214 EAPI int
215 eina_shutdown(void)
216 {
217    _eina_main_count--;
218    if (EINA_UNLIKELY(_eina_main_count == 0))
219      _eina_shutdown_from_desc(_eina_desc_setup + _eina_desc_setup_len);
220    return _eina_main_count;
221 }
222
223 /**
224  * @}
225  */