lsnes/include/library/globalwrap.hpp
Ilari Liusvaara 8d1889c116 Revert initializing globalwrap state
Leave globalwrap state as uninitialized, even if that causes a compile
warning as initializing breaks AVI dumping on Windows. All globalwraps
should have static storage duration anyway, and C++ can not have
uninitialized variables with static storage duration.
2021-02-15 23:31:14 +02:00

43 lines
765 B
C++

#ifndef _library__globalwrap__hpp__included__
#define _library__globalwrap__hpp__included__
#include <iostream>
/**
* Wrapper for glboal/module-local objects accessable in global ctor context.
*/
template<class T>
class globalwrap
{
public:
/**
* Ctor, forces the object to be constructed (to avoid races).
*/
globalwrap()
{
(*this)();
}
/**
* Get the wrapped object.
*
* returns: The wrapped object.
* throws std::bad_alloc: Not enough memory.
*/
T& operator()()
{
if(!storage) {
if(!state) //State initializes to 0.
state = 1;
else if(state == 1)
std::cerr << "Warning: Recursive use of globalwrap<T>." << std::endl;
storage = new T();
state = 2;
}
return *storage;
}
private:
T* storage;
unsigned state;
};
#endif