1 /**
2 * Global State Management
3 * 
4 * Copyright:
5 * (C) 2010 Jack Lloyd
6 * (C) 2014-2015 Etienne Cimon
7 *
8 * License:
9 * Botan is released under the Simplified BSD License (see LICENSE.md)
10 */
11 module botan.libstate.global_state;
12 import botan.constants;
13 import botan.libstate.libstate;
14 import core.thread : Thread;
15 /// Thread-Local, no locks needed.
16 private Unique!LibraryState g_lib_state;
17 
18 /**
19 * Access the global library state
20 * Returns: reference to the global library state
21 */
22 LibraryState globalState()
23 {
24     if (!g_lib_state) { 
25 
26         g_lib_state = new LibraryState;
27         /* Lazy initialization. Botan still needs to be deinitialized later
28             on or memory might leak.
29         */
30         g_lib_state.initialize();
31     }
32     return *g_lib_state;
33 }
34 
35 /**
36 * Set the global state object
37 * Params:
38 *  new_state = the new global state to use
39 */
40 void setGlobalState(LibraryState new_state)
41 {
42     if (g_lib_state) destroy(g_lib_state);
43     g_lib_state = new_state;
44 }
45 
46 
47 /**
48 * Set the global state object unless it is already set
49 * Params:
50 *  new_state = the new global state to use
51 * Returns: true if the state parameter is now being used as the global
52 *            state, or false if one was already set, in which case the
53 *            parameter was deleted immediately
54 */
55 bool setGlobalStateUnlessSet(LibraryState new_state)
56 {
57     if (g_lib_state)
58     {
59         return false;
60     }
61     else
62     {
63         g_lib_state = new_state;
64         return true;
65     }
66 }
67 
68 /**
69 * Query if the library is currently initialized
70 * Returns: true iff the library is initialized
71 */
72 bool globalStateExists()
73 {
74     return (*g_lib_state) !is null;
75 }