IdManager cleaned

This commit is contained in:
Nekotekina 2015-03-15 12:20:29 +03:00
parent fae7ddc611
commit ff90ddbd2f

View file

@ -29,48 +29,24 @@ enum IDType
TYPE_OTHER, TYPE_OTHER,
}; };
class IDData class ID final
{
protected:
void* m_ptr;
std::function<void(void*)> m_destr;
public:
IDData(void* ptr, std::function<void(void*)> destr)
: m_ptr(ptr)
, m_destr(destr)
{
}
~IDData()
{
m_destr(m_ptr);
}
template<typename T> std::shared_ptr<T> get() const
{
return *(std::shared_ptr<T>*)m_ptr;
}
};
class ID
{ {
const std::type_info& m_info; const std::type_info& m_info;
IDData* m_data; std::shared_ptr<void> m_data;
IDType m_type; IDType m_type;
public: public:
template<typename T> template<typename T> ID(std::shared_ptr<T>& data, const IDType type)
ID(std::shared_ptr<T>& data, const IDType type)
: m_info(typeid(T)) : m_info(typeid(T))
, m_data(data)
, m_type(type) , m_type(type)
{ {
m_data = new IDData(new std::shared_ptr<T>(data), [](void *ptr) -> void { delete (std::shared_ptr<T>*)ptr; });
} }
ID() ID()
: m_info(typeid(nullptr_t)) : m_info(typeid(void))
, m_data(nullptr) , m_data(nullptr)
, m_type(TYPE_OTHER)
{ {
} }
@ -82,26 +58,19 @@ public:
, m_type(right.m_type) , m_type(right.m_type)
{ {
right.m_data = nullptr; right.m_data = nullptr;
right.m_type = TYPE_OTHER;
} }
ID& operator=(ID&& other) = delete; ID& operator=(ID&& other) = delete;
~ID()
{
if (m_data)
{
delete m_data;
}
}
const std::type_info& GetInfo() const const std::type_info& GetInfo() const
{ {
return m_info; return m_info;
} }
IDData* GetData() const template<typename T> std::shared_ptr<T> GetData() const
{ {
return m_data; return std::static_pointer_cast<T>(m_data);
} }
IDType GetType() const IDType GetType() const
@ -119,18 +88,9 @@ class IdManager
std::set<u32> m_types[TYPE_OTHER]; std::set<u32> m_types[TYPE_OTHER];
std::mutex m_mtx_main; std::mutex m_mtx_main;
u32 m_cur_id; u32 m_cur_id = s_first_id;
public: public:
IdManager() : m_cur_id(s_first_id)
{
}
~IdManager()
{
Clear();
}
template<typename T> bool CheckID(const u32 id) template<typename T> bool CheckID(const u32 id)
{ {
std::lock_guard<std::mutex> lock(m_mtx_main); std::lock_guard<std::mutex> lock(m_mtx_main);
@ -161,13 +121,6 @@ public:
return m_cur_id++; return m_cur_id++;
} }
ID& GetID(const u32 id)
{
std::lock_guard<std::mutex> lock(m_mtx_main);
return m_id_map[id];
}
template<typename T> bool GetIDData(const u32 id, std::shared_ptr<T>& result) template<typename T> bool GetIDData(const u32 id, std::shared_ptr<T>& result)
{ {
@ -180,7 +133,7 @@ public:
return false; return false;
} }
result = f->second.GetData()->get<T>(); result = f->second.GetData<T>();
return true; return true;
} }
@ -196,7 +149,7 @@ public:
return nullptr; return nullptr;
} }
return f->second.GetData()->get<T>(); return f->second.GetData<T>();
} }
bool HasID(const u32 id) bool HasID(const u32 id)
@ -206,6 +159,20 @@ public:
return m_id_map.find(id) != m_id_map.end(); return m_id_map.find(id) != m_id_map.end();
} }
IDType GetIDType(const u32 id)
{
std::lock_guard<std::mutex> lock(m_mtx_main);
auto item = m_id_map.find(id);
if (item == m_id_map.end())
{
return TYPE_OTHER;
}
return item->second.GetType();
}
template<typename T> bool RemoveID(const u32 id) template<typename T> bool RemoveID(const u32 id)
{ {
std::lock_guard<std::mutex> lock(m_mtx_main); std::lock_guard<std::mutex> lock(m_mtx_main);