lsnes rr2-β23
[lsnes.git] / include / core / instance-map.hpp
bloba370c962713d9fb60e5d791e20008e5d53644b0d
1 #ifndef _instance_map__hpp__included__
2 #define _instance_map__hpp__included__
4 #include <map>
6 class emulator_instance;
8 template<typename T> class instance_map
10 public:
11 /**
12 * Destroy a instance map.
14 ~instance_map()
16 for(auto i : instances)
17 delete i.second;
18 instances.clear();
20 /**
21 * Does this instance exist?
23 bool exists(emulator_instance& inst)
25 return instances.count(&inst);
27 /**
28 * Lookup a instance. Returns NULL if none.
30 T* lookup(emulator_instance& inst)
32 if(!instances.count(&inst))
33 return NULL;
34 return instances[&inst];
36 /**
37 * Create a new instance.
39 template<typename... U> T* create(emulator_instance& inst, U... args)
41 T* out = NULL;
42 try {
43 out = new T(inst, args...);
44 instances[&inst] = out;
45 return out;
46 } catch(...) {
47 if(out) delete out;
48 throw;
51 /**
52 * Erase a instance.
54 void destroy(emulator_instance& inst)
56 if(instances.count(&inst)) {
57 delete instances[&inst];
58 instances.erase(&inst);
61 /**
62 * Remove a instance.
64 void remove(emulator_instance& inst)
66 if(instances.count(&inst)) {
67 instances.erase(&inst);
70 private:
71 std::map<emulator_instance*, T*> instances;
74 #endif