Revert "Remove Lua 5.1 support"
[lsnes.git] / src / library / stateobject.cpp
blob65e7298fe398bc421bc9ad3b1f1606a6ad35c170
1 #include "stateobject.hpp"
2 #include "threads.hpp"
3 #include <map>
5 namespace stateobject
7 threads::lock* lock;
8 std::map<void*, void*>* states;
10 void* _get(void* obj, void* (*create)())
12 if(!lock) lock = new threads::lock;
13 threads::alock h(*lock);
14 if(!states) states = new std::map<void*, void*>;
15 if(!states->count(obj)) {
16 (*states)[obj] = NULL;
17 try {
18 (*states)[obj] = create();
19 } catch(...) {
20 states->erase(obj);
21 throw;
24 return (*states)[obj];
27 void* _get_soft(void* obj)
29 if(!lock) return NULL;
30 threads::alock h(*lock);
31 if(!states) return NULL;
32 if(states->count(obj))
33 return (*states)[obj];
34 return NULL;
37 void _clear(void* obj, void (*destroy)(void* obj))
39 if(!lock) return;
40 threads::alock h(*lock);
41 if(!states) return;
42 if(!states->count(obj)) return;
43 destroy((*states)[obj]);
44 states->erase(obj);