61 lines
2.0 KiB
C++
61 lines
2.0 KiB
C++
|
|
#ifndef PORYGONLANG_USERDATASTORAGE_HPP
|
|
#define PORYGONLANG_USERDATASTORAGE_HPP
|
|
|
|
#include <unordered_map>
|
|
#include <mutex>
|
|
#include "UserData.hpp"
|
|
#include "RetrievedUserData.hpp"
|
|
|
|
namespace Porygon::UserData {
|
|
class UserDataStorage {
|
|
private:
|
|
class _internalDataStorage {
|
|
public:
|
|
std::unordered_map<uint32_t, std::unique_ptr<UserData>> _userData;
|
|
|
|
_internalDataStorage()
|
|
: _userData(std::unordered_map<uint32_t, std::unique_ptr<UserData>>(0))
|
|
{
|
|
}
|
|
|
|
~_internalDataStorage(){
|
|
_userData.clear();
|
|
}
|
|
|
|
_internalDataStorage( const UserDataStorage& ) = delete; // non construction-copyable
|
|
_internalDataStorage& operator=( const UserDataStorage& ) = delete; // non copyable
|
|
|
|
};
|
|
|
|
static _internalDataStorage _internal;
|
|
static std::mutex _userDataMutex;
|
|
public:
|
|
static void RegisterType(uint32_t i, UserData *ud) {
|
|
std::lock_guard<std::mutex> guard(_userDataMutex);
|
|
UserDataStorage::_internal._userData.insert({i, std::unique_ptr<UserData>(ud)});
|
|
}
|
|
|
|
static void RemoveType(uint32_t key){
|
|
_internal._userData.erase(key);
|
|
}
|
|
|
|
inline static bool HasUserDataType(uint32_t i) {
|
|
return UserDataStorage::_internal._userData.find(i) != UserDataStorage::_internal._userData.end();
|
|
}
|
|
|
|
inline static RetrievedUserData* GetUserDataType(uint32_t i){
|
|
if (UserDataStorage::_internal._userData.count(i))
|
|
return new RetrievedUserData(UserDataStorage::_internal._userData.at(i).get());
|
|
return new RetrievedUserData(i);
|
|
}
|
|
inline static UserData* GetUserDataTypeRaw(uint32_t i){
|
|
if (UserDataStorage::_internal._userData.count(i))
|
|
return UserDataStorage::_internal._userData.at(i).get();
|
|
return nullptr;
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif //PORYGONLANG_USERDATASTORAGE_HPP
|