Ensure memory is handled properly with userdata
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
3217fd1479
commit
2c313791d9
|
@ -8,7 +8,7 @@
|
||||||
#include "UserDataStorage.hpp"
|
#include "UserDataStorage.hpp"
|
||||||
|
|
||||||
class UserDataScriptType : public ScriptType{
|
class UserDataScriptType : public ScriptType{
|
||||||
UserData* _userData;
|
shared_ptr<UserData> _userData;
|
||||||
public:
|
public:
|
||||||
explicit UserDataScriptType(int id) : ScriptType(TypeClass::UserData){
|
explicit UserDataScriptType(int id) : ScriptType(TypeClass::UserData){
|
||||||
_userData = UserDataStorage::GetUserDataType(id);
|
_userData = UserDataStorage::GetUserDataType(id);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
|
|
||||||
#include "UserDataStorage.hpp"
|
#include "UserDataStorage.hpp"
|
||||||
|
|
||||||
std::unordered_map<int, UserData*> UserDataStorage::_userData = {};
|
UserDataStorage::_internalDataStorage UserDataStorage::_internal = UserDataStorage::_internalDataStorage();
|
||||||
|
|
|
@ -6,14 +6,19 @@
|
||||||
#include "UserData.hpp"
|
#include "UserData.hpp"
|
||||||
|
|
||||||
class UserDataStorage {
|
class UserDataStorage {
|
||||||
static std::unordered_map<int, UserData*> _userData;
|
class _internalDataStorage{
|
||||||
|
public:
|
||||||
|
std::unordered_map<int, shared_ptr<UserData>> _userData;
|
||||||
|
};
|
||||||
|
static _internalDataStorage _internal;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static void RegisterType(int i, UserData* ud){
|
static void RegisterType(int i, UserData* ud){
|
||||||
UserDataStorage::_userData.insert({i, ud});
|
UserDataStorage::_internal._userData.insert({i, shared_ptr<UserData>(ud)});
|
||||||
}
|
}
|
||||||
|
|
||||||
static UserData* GetUserDataType(int i){
|
static shared_ptr<UserData> GetUserDataType(int i){
|
||||||
return UserDataStorage::_userData[i];
|
return UserDataStorage::_internal._userData[i];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,18 +1,19 @@
|
||||||
|
|
||||||
|
|
||||||
#ifndef PORYGONLANG_USERDATAVALUE_HPP
|
#ifndef PORYGONLANG_USERDATAVALUE_HPP
|
||||||
#define PORYGONLANG_USERDATAVALUE_HPP
|
#define PORYGONLANG_USERDATAVALUE_HPP
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
#include "../Evaluator/EvalValues/EvalValue.hpp"
|
#include "../Evaluator/EvalValues/EvalValue.hpp"
|
||||||
#include "UserData.hpp"
|
#include "UserData.hpp"
|
||||||
#include "UserDataStorage.hpp"
|
#include "UserDataStorage.hpp"
|
||||||
|
|
||||||
class UserDataValue : public EvalValue{
|
class UserDataValue : public EvalValue{
|
||||||
UserData* _userData;
|
shared_ptr<UserData> _userData;
|
||||||
void* _obj;
|
void* _obj;
|
||||||
public:
|
public:
|
||||||
UserDataValue(UserData* userData, void* obj){
|
UserDataValue(shared_ptr<UserData> userData, void* obj){
|
||||||
_userData = userData;
|
_userData = std::move(userData);
|
||||||
_obj = obj;
|
_obj = obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,8 +56,10 @@ end
|
||||||
auto obj = new UserDataTestObject();
|
auto obj = new UserDataTestObject();
|
||||||
auto parameter = new UserDataValue(HashedString::ConstHash("testObject"), obj);
|
auto parameter = new UserDataValue(HashedString::ConstHash("testObject"), obj);
|
||||||
script->CallFunction("testFunc", {parameter});
|
script->CallFunction("testFunc", {parameter});
|
||||||
REQUIRE(obj->foo == 5000);
|
|
||||||
delete script;
|
delete script;
|
||||||
|
REQUIRE(obj->foo == 5000);
|
||||||
|
delete obj;
|
||||||
|
delete parameter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue