Rework of memory handling in Evaluation
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2019-07-27 17:59:42 +02:00
parent 268f6b59fb
commit ccc6e297f2
32 changed files with 496 additions and 461 deletions

View File

@@ -4,38 +4,39 @@
#include <memory>
namespace Porygon::Evaluation {
EvaluationScope::EvaluationScope(map<Utilities::HashedString, shared_ptr<const EvalValue>> *scriptVariables)
EvaluationScope::EvaluationScope(map<Utilities::HashedString, EvalValuePointer> *scriptVariables)
: _scriptScope(scriptVariables),
_localScope(map<uint64_t, shared_ptr<const EvalValue>>()) {
_localScope(map<uint64_t, EvalValuePointer>()) {
}
void EvaluationScope::CreateVariable(const BoundVariableKey *key, const shared_ptr<const EvalValue>& value) {
void EvaluationScope::CreateVariable(const BoundVariableKey *key, EvalValuePointer value) {
if (key->GetScopeId() == 0) {
_scriptScope->at(*key->GetIdentifier()) = value;
_scriptScope->at(*key->GetIdentifier()).ClearAssign(value.Take());
} else {
auto insert = _localScope.insert({key->GetHash(), value});
auto val = value.Clone();
auto insert = _localScope.insert({key->GetHash(), val});
if (!insert.second) {
_localScope[key->GetHash()] = value;
_localScope[key->GetHash()].ClearAssign(value.Take());
}
}
}
void EvaluationScope::SetVariable(const BoundVariableKey *key, const shared_ptr<const EvalValue> &value) {
void EvaluationScope::SetVariable(const BoundVariableKey *key, EvalValuePointer value) {
if (key->GetScopeId() == 0) {
_scriptScope->at(*key->GetIdentifier()) = value;
_scriptScope->at(*key->GetIdentifier()).ClearAssign(value.Take());
} else {
_localScope[key->GetHash()] = value;
_localScope[key->GetHash()].ClearAssign(value.Take());
}
}
shared_ptr<const EvalValue> EvaluationScope::GetVariable(const BoundVariableKey *key) {
EvalValuePointer EvaluationScope::GetVariable(const BoundVariableKey *key) {
auto scopeId = key -> GetScopeId();
if (scopeId== 0) {
return _scriptScope->at(*key->GetIdentifier());
return _scriptScope->at(*key->GetIdentifier())->Clone();
} else if(scopeId == -2){
return StandardLibraries::StaticScope::GetVariable(*key->GetIdentifier());
return StandardLibraries::StaticScope::GetVariable(*key->GetIdentifier()).Clone();
} else {
return _localScope[key->GetHash()];
return _localScope[key->GetHash()]->Clone();
}
}
}

View File

@@ -5,22 +5,26 @@
#include <map>
#include <vector>
#include "../EvalValues/EvalValue.hpp"
#include "../EvalValuePointer.hpp"
using namespace Porygon::Binder;
namespace Porygon::Evaluation {
class EvaluationScope {
map<Utilities::HashedString, shared_ptr<const EvalValue>> *_scriptScope;
map<uint64_t, shared_ptr<const EvalValue>> _localScope;
map<Utilities::HashedString, EvalValuePointer> *_scriptScope;
map<uint64_t, EvalValuePointer> _localScope;
public:
explicit EvaluationScope(map<Utilities::HashedString, shared_ptr<const EvalValue>> *scriptVariables);
explicit EvaluationScope(map<Utilities::HashedString, EvalValuePointer> *scriptVariables);
~EvaluationScope() = default;
~EvaluationScope(){
_localScope.clear();
};
void CreateVariable(const BoundVariableKey *key, const shared_ptr<const EvalValue>&value);
void CreateVariable(const BoundVariableKey *key, EvalValuePointer value);
void SetVariable(const BoundVariableKey *key, const shared_ptr<const EvalValue> &value);
void SetVariable(const BoundVariableKey *key, EvalValuePointer value);
shared_ptr<const EvalValue> GetVariable(const BoundVariableKey *key);
EvalValuePointer GetVariable(const BoundVariableKey *key);
};
}