* add eina_config.h in all source files. Fix linking with Visual Studio
[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_types.h"
27 #include "eina_main.h"
28 #include "eina_error.h"
29 #include "eina_log.h"
30 #include "eina_hash.h"
31 #include "eina_stringshare.h"
32 #include "eina_list.h"
33 #include "eina_array.h"
34 #include "eina_counter.h"
35 #include "eina_benchmark.h"
36 #include "eina_magic.h"
37 #include "eina_rectangle.h"
38
39 /*============================================================================*
40  *                                  Local                                     *
41  *============================================================================*/
42
43 /**
44  * @cond LOCAL
45  */
46
47 static int _eina_main_count = 0;
48
49 /**
50  * @endcond
51  */
52
53 /*============================================================================*
54  *                                 Global                                     *
55  *============================================================================*/
56
57 /*============================================================================*
58  *                                   API                                      *
59  *============================================================================*/
60
61 /**
62  * @addtogroup Eina_Main_Group Main
63  *
64  * @brief These functions provide general initialisation and shut down
65  * functions.
66  *
67  * @{
68  */
69
70 /**
71  * @brief Initialize the Eina library.
72  *
73  * @return 1 or greater on success, 0 on error.
74  *
75  * This function sets up all the eina modules. It returns 0 on
76  * failure (that is, when one of the module fails to initialize),
77  * otherwise it returns the number of times it has already been
78  * called. The list of initialisation functions that are called are
79  * (in that order):
80  *
81  * @li eina_error_init()
82  * @li eina_hash_init()
83  * @li eina_stringshare_init()
84  * @li eina_list_init()
85  * @li eina_array_init()
86  * @li eina_counter_init()
87  * @li eina_benchmark_init()
88  * @li eina_magic_string_init()
89  * @li eina_rectangle_init()
90  *
91  * When Eina is not used anymore, call eina_shutdown() to shut down
92  * the Eina library.
93  */
94 EAPI int
95 eina_init(void)
96 {
97    if (_eina_main_count) goto finish_init;
98
99    if (!eina_error_init())
100      {
101         fprintf(stderr, "Could not initialize eina error module.\n");
102         return 0;
103      }
104
105    if (!eina_log_init())
106      {
107         fprintf(stderr, "Could not initialize eina log module.\n");
108         goto log_init_error;
109      }
110
111    if (!eina_hash_init())
112      {
113         EINA_ERROR_PERR("Could not initialize eina hash module.\n");
114         goto hash_init_error;
115      }
116    if (!eina_stringshare_init())
117      {
118         EINA_ERROR_PERR("Could not initialize eina stringshare module.\n");
119         goto stringshare_init_error;
120      }
121    if (!eina_list_init())
122      {
123         EINA_ERROR_PERR("Could not initialize eina list module.\n");
124         goto list_init_error;
125      }
126    if (!eina_array_init())
127      {
128         EINA_ERROR_PERR("Could not initialize eina array module.\n");
129         goto array_init_error;
130      }
131    if (!eina_counter_init())
132      {
133         EINA_ERROR_PERR("Could not initialize eina counter module.\n");
134         goto counter_init_error;
135      }
136    if (!eina_benchmark_init())
137      {
138         EINA_ERROR_PERR("Could not initialize eina benchmark module.\n");
139         goto benchmark_init_error;
140      }
141    if (!eina_magic_string_init())
142      {
143         EINA_ERROR_PERR("Could not initialize eina magic string module.\n");
144         goto magic_string_init_error;
145      }
146    if (!eina_rectangle_init())
147      {
148         EINA_ERROR_PERR("Could not initialize eina rectangle module.\n");
149         goto rectangle_init_error;
150      }
151
152  finish_init:
153    return ++_eina_main_count;
154
155  rectangle_init_error:
156    eina_magic_string_shutdown();
157  magic_string_init_error:
158    eina_benchmark_shutdown();
159  benchmark_init_error:
160    eina_counter_shutdown();
161  counter_init_error:
162    eina_array_shutdown();
163  array_init_error:
164    eina_list_shutdown();
165  list_init_error:
166    eina_stringshare_shutdown();
167  stringshare_init_error:
168    eina_hash_shutdown();
169  hash_init_error:
170    eina_log_shutdown();
171  log_init_error:
172    eina_error_shutdown();
173
174    return 0;
175 }
176
177 /**
178  * @brief Shut down the Eina library.
179  *
180  * @return 0 when all the modules is completely shut down, 1 or
181  * greater otherwise.
182  *
183  * This function shuts down the Eina library. It returns 0 when it has
184  * been called the same number of times than eina_init(). In that case
185  * it shut down all the Eina modules. The list of shut down functions
186  * that are called are (in that order):
187  *
188  * @li eina_rectangle_init()
189  * @li eina_magic_string_init()
190  * @li eina_benchmark_init()
191  * @li eina_counter_init()
192  * @li eina_array_init()
193  * @li eina_list_init()
194  * @li eina_stringshare_init()
195  * @li eina_hash_init()
196  * @li eina_error_init()
197  *
198  * Once this function succeeds (that is, @c 0 is returned), you must
199  * not call any of the Eina function anymore. You must call
200  * eina_init() again to use the Eina functions again.
201  */
202 EAPI int
203 eina_shutdown(void)
204 {
205    if (_eina_main_count != 1) goto finish_shutdown;
206
207    eina_rectangle_shutdown();
208    eina_magic_string_shutdown();
209    eina_benchmark_shutdown();
210    eina_counter_shutdown();
211    eina_array_shutdown();
212    eina_list_shutdown();
213    eina_stringshare_shutdown();
214    eina_hash_shutdown();
215    eina_log_shutdown();
216    eina_error_shutdown();
217
218  finish_shutdown:
219    return --_eina_main_count;
220 }
221
222 /**
223  * @}
224  */