From 94d148825944e6b6b595e200098e64508ebd87ce Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Sat, 8 Aug 2015 00:28:09 +0300 Subject: [PATCH] IdManager funcs added: idm::import, idm::withdraw --- rpcs3/Emu/IdManager.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/rpcs3/Emu/IdManager.h b/rpcs3/Emu/IdManager.h index 0ff98956e0..28f1058248 100644 --- a/rpcs3/Emu/IdManager.h +++ b/rpcs3/Emu/IdManager.h @@ -108,6 +108,27 @@ namespace idm throw EXCEPTION("Out of IDs"); } + // add new ID for an existing object provided (don't use for initial object creation) + template u32 import(const std::shared_ptr& ptr) + { + extern std::mutex g_id_mutex; + extern std::unordered_map g_id_map; + extern u32 g_cur_id; + + std::lock_guard lock(g_id_mutex); + + if (const u32 id = g_cur_id & 0x7fffffff) + { + g_id_map.emplace(id, ID_data_t(ptr)); + + g_cur_id = id + 1; + + return id; + } + + throw EXCEPTION("Out of IDs"); + } + // get ID of specified type template std::shared_ptr get(u32 id) { @@ -169,6 +190,28 @@ namespace idm return true; } + // remove ID created with type T and return the object + template std::shared_ptr withdraw(u32 id) + { + extern std::mutex g_id_mutex; + extern std::unordered_map g_id_map; + + std::lock_guard lock(g_id_mutex); + + const auto found = g_id_map.find(id); + + if (found == g_id_map.end() || found->second.info != typeid(T)) + { + return nullptr; + } + + auto ptr = std::static_pointer_cast(found->second.data); + + g_id_map.erase(found); + + return ptr; + } + template u32 get_count() { extern std::mutex g_id_mutex;