Actually call on_reset callback
[lsnes.git] / include / library / stateobject.hpp
blob796feccb5e529bad7ae227dfcdea356ad93ab8cf
1 #ifndef _library__stateobject__hpp__included__
2 #define _library__stateobject__hpp__included__
4 #include <stdexcept>
6 namespace stateobject
8 //Internal methods.
9 void* _get(void* obj, void* (*create)());
10 void* _get_soft(void* obj);
11 void _clear(void* obj, void (*destroy)(void* obj));
13 template<typename T, typename U> class type
15 public:
16 /**
17 * Get state object, creating it if needed.
19 * Parameter obj: The object to get the state for.
20 * Returns: The object state.
22 static U& get(T* obj)
24 return *reinterpret_cast<U*>(_get(obj, []() -> void* { return new U; }));
26 /**
27 * Get state object, but don't create if it does not already exist.
29 * Parameter obj: The object to get the state for.
30 * Returns: The object state, or NULL if no existing state.
32 static U* get_soft(T* obj) throw() { return reinterpret_cast<U*>(_get_soft(obj)); }
33 /**
34 * Clear state object.
36 static void clear(T* obj) throw()
38 return _clear(obj, [](void* obj) -> void { delete reinterpret_cast<U*>(obj); });
44 #endif