Skip to content

Commit

Permalink
IdManager funcs added: idm::import, idm::withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekotekina committed Aug 24, 2015
1 parent 8b3c250 commit 94d1488
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions rpcs3/Emu/IdManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T> u32 import(const std::shared_ptr<T>& ptr)
{
extern std::mutex g_id_mutex;
extern std::unordered_map<u32, ID_data_t> g_id_map;
extern u32 g_cur_id;

std::lock_guard<std::mutex> 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<typename T> std::shared_ptr<T> get(u32 id)
{
Expand Down Expand Up @@ -169,6 +190,28 @@ namespace idm
return true;
}

// remove ID created with type T and return the object
template<typename T> std::shared_ptr<T> withdraw(u32 id)
{
extern std::mutex g_id_mutex;
extern std::unordered_map<u32, ID_data_t> g_id_map;

std::lock_guard<std::mutex> 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<T>(found->second.data);

g_id_map.erase(found);

return ptr;
}

template<typename T> u32 get_count()
{
extern std::mutex g_id_mutex;
Expand Down

0 comments on commit 94d1488

Please sign in to comment.