Botan  1.10.9
global_state.cpp
Go to the documentation of this file.
1 /*
2 * Global State Management
3 * (C) 2010 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/global_state.h>
9 #include <botan/libstate.h>
10 
11 namespace Botan {
12 
13 /*
14 * @todo There should probably be a lock to avoid racy manipulation
15 * of the state among different threads
16 */
17 
18 namespace Global_State_Management {
19 
20 /*
21 * Botan's global state
22 */
23 namespace {
24 
25 Library_State* global_lib_state = 0;
26 
27 }
28 
29 /*
30 * Access the global state object
31 */
33  {
34  /* Lazy initialization. Botan still needs to be deinitialized later
35  on or memory might leak.
36  */
37  if(!global_lib_state)
38  {
39  global_lib_state = new Library_State;
40  global_lib_state->initialize(true);
41  }
42 
43  return (*global_lib_state);
44  }
45 
46 /*
47 * Set a new global state object
48 */
50  {
51  delete swap_global_state(new_state);
52  }
53 
54 /*
55 * Set a new global state object unless one already existed
56 */
58  {
59  if(global_lib_state)
60  {
61  delete new_state;
62  return false;
63  }
64  else
65  {
66  delete swap_global_state(new_state);
67  return true;
68  }
69  }
70 
71 /*
72 * Swap two global state objects
73 */
75  {
76  Library_State* old_state = global_lib_state;
77  global_lib_state = new_state;
78  return old_state;
79  }
80 
81 /*
82 * Query if library is initialized
83 */
85  {
86  return (global_lib_state != 0);
87  }
88 
89 }
90 
91 }
void set_global_state(Library_State *new_state)
Library_State & global_state()
void initialize(bool thread_safe)
Definition: libstate.cpp:197
Library_State * swap_global_state(Library_State *new_state)
bool set_global_state_unless_set(Library_State *new_state)