Lua: Fix type confusion between signed and unsigned
[lsnes.git] / include / library / globalwrap.hpp
blobac712d52f47ac1718f149ee6abf387cb93d760aa
1 #ifndef _library__globalwrap__hpp__included__
2 #define _library__globalwrap__hpp__included__
4 #include <iostream>
6 /**
7 * Wrapper for glboal/module-local objects accessable in global ctor context.
8 */
9 template<class T>
10 class globalwrap
12 public:
13 /**
14 * Ctor, forces the object to be constructed (to avoid races).
16 globalwrap() throw(std::bad_alloc)
18 (*this)();
20 /**
21 * Get the wrapped object.
23 * returns: The wrapped object.
24 * throws std::bad_alloc: Not enough memory.
26 T& operator()() throw(std::bad_alloc)
28 if(!storage) {
29 if(!state) //State initializes to 0.
30 state = 1;
31 else if(state == 1)
32 std::cerr << "Warning: Recursive use of globalwrap<T>." << std::endl;
33 storage = new T();
34 state = 2;
36 return *storage;
38 private:
39 T* storage;
40 unsigned state;
43 #endif