Implements iterator for user data collection
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -0,0 +1 @@
|
||||
#include "UserDataCollectionIterator.hpp"
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef PORYGONLANG_USERDATACOLLECTIONITERATOR_HPP
|
||||
#define PORYGONLANG_USERDATACOLLECTIONITERATOR_HPP
|
||||
|
||||
#include "../../Evaluator/Iterator/Iterator.hpp"
|
||||
|
||||
namespace Porygon::UserData{
|
||||
class UserDataCollectionIterator : public Evaluation::Iterator{
|
||||
const Evaluation::EvalValue* (*_getCurrent)();
|
||||
const Evaluation::EvalValue* (*_moveNext)();
|
||||
const Evaluation::EvalValue* (*_reset)();
|
||||
public:
|
||||
UserDataCollectionIterator(const Evaluation::EvalValue* (*getCurrent)(),
|
||||
const Evaluation::EvalValue* (*moveNext)(),
|
||||
const Evaluation::EvalValue* (*reset)()){
|
||||
_getCurrent = getCurrent;
|
||||
_moveNext = moveNext;
|
||||
_reset = reset;
|
||||
}
|
||||
|
||||
const Evaluation::EvalValue* GetCurrent() final{
|
||||
return _getCurrent();
|
||||
}
|
||||
bool MoveNext() final{
|
||||
_moveNext();
|
||||
}
|
||||
void Reset() final{
|
||||
_reset();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif //PORYGONLANG_USERDATACOLLECTIONITERATOR_HPP
|
||||
@@ -0,0 +1 @@
|
||||
#include "UserDataCollectionRangeIterator.hpp"
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef PORYGONLANG_USERDATACOLLECTIONRANGEITERATOR_HPP
|
||||
#define PORYGONLANG_USERDATACOLLECTIONRANGEITERATOR_HPP
|
||||
|
||||
#include "../../Evaluator/Iterator/Iterator.hpp"
|
||||
#include "../../Evaluator/EvalValues/NumericEvalValue.hpp"
|
||||
|
||||
namespace Porygon::UserData{
|
||||
class UserDataCollectionRangeIterator : public Evaluation::Iterator{
|
||||
size_t _start;
|
||||
size_t _end;
|
||||
|
||||
size_t _current;
|
||||
public:
|
||||
UserDataCollectionRangeIterator(size_t start, size_t end){
|
||||
_start = start;
|
||||
_end = end;
|
||||
_current = start;
|
||||
}
|
||||
|
||||
const Evaluation::EvalValue* GetCurrent() final{
|
||||
return new Evaluation::IntegerEvalValue(_current);
|
||||
}
|
||||
bool MoveNext() final{
|
||||
_current ++;
|
||||
return _current <= _end;
|
||||
}
|
||||
void Reset() final{
|
||||
_current = _start;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif //PORYGONLANG_USERDATACOLLECTIONRANGEITERATOR_HPP
|
||||
@@ -2,7 +2,7 @@
|
||||
#define PORYGONLANG_USERDATACOLLECTIONTYPE_HPP
|
||||
|
||||
#include <utility>
|
||||
#include "../ScriptTypes/ScriptType.hpp"
|
||||
#include "../../ScriptTypes/ScriptType.hpp"
|
||||
|
||||
namespace Porygon::UserData {
|
||||
class UserDataCollectionType : public ScriptType {
|
||||
@@ -4,8 +4,8 @@
|
||||
#include <utility>
|
||||
|
||||
#include "UserDataCollectionType.hpp"
|
||||
#include "../Evaluator/EvalValues/EvalValue.hpp"
|
||||
#include "../Utilities/Random.hpp"
|
||||
#include "../../Evaluator/EvalValues/EvalValue.hpp"
|
||||
#include "../../Utilities/Random.hpp"
|
||||
|
||||
using namespace Porygon::Evaluation;
|
||||
|
||||
@@ -14,12 +14,13 @@ namespace Porygon::UserData {
|
||||
void* _parentObject;
|
||||
const EvalValue* (*_get)(void*, const EvalValue*);
|
||||
void (*_set)(void*, const EvalValue* , const EvalValue*);
|
||||
|
||||
Iterator* (*_getIterator)(void*);
|
||||
public:
|
||||
UserDataCollectionHelper(void* parentObject,
|
||||
const EvalValue* (*get)(void*, const EvalValue*),
|
||||
void (*set)(void*, const EvalValue*, const EvalValue*))
|
||||
: _parentObject(parentObject), _get(get), _set(set){}
|
||||
void (*set)(void*, const EvalValue*, const EvalValue*),
|
||||
Iterator* (*getIterator)(void*))
|
||||
: _parentObject(parentObject), _get(get), _set(set), _getIterator(getIterator){}
|
||||
|
||||
const EvalValue* Get(const EvalValue* key) const{
|
||||
return _get(_parentObject, key);
|
||||
@@ -28,6 +29,10 @@ namespace Porygon::UserData {
|
||||
void Set(const EvalValue* key, const EvalValue* value) const{
|
||||
_set(_parentObject, key, value);
|
||||
}
|
||||
|
||||
[[nodiscard]] Iterator* GetIterator() const{
|
||||
return _getIterator(_parentObject);
|
||||
}
|
||||
};
|
||||
|
||||
class UserDataCollectionValue : public Evaluation::EvalValue{
|
||||
@@ -42,7 +47,7 @@ namespace Porygon::UserData {
|
||||
public:
|
||||
|
||||
UserDataCollectionValue(ScriptType* type, const UserDataCollectionHelper* helper)
|
||||
: _type((UserDataCollectionType*)type), _helper(helper), _hash(Utilities::Random::Get())
|
||||
: _type(dynamic_cast<UserDataCollectionType*>(type)), _helper(helper), _hash(Utilities::Random::Get())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -67,6 +72,11 @@ namespace Porygon::UserData {
|
||||
return _helper->Get(val);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
Iterator * GetKeyIterator() const final{
|
||||
return _helper->GetIterator();
|
||||
}
|
||||
|
||||
void SetIndexValue(const EvalValue *key, const EvalValue* value) const final{
|
||||
_helper->Set(key, value);
|
||||
delete value;
|
||||
@@ -85,7 +85,13 @@
|
||||
, [](void* obj, const EvalValue* key, const EvalValue* value){ \
|
||||
auto index = key->EvaluateInteger() - 1;\
|
||||
((T_USERDATA*)obj)->fieldName[index] = value->EvaluateInteger(); \
|
||||
}) \
|
||||
} \
|
||||
, [](void* obj) -> Porygon::Evaluation::Iterator* {\
|
||||
auto val = ((T_USERDATA*)obj)->fieldName; \
|
||||
auto size = val.size(); \
|
||||
return new Porygon::UserData::UserDataCollectionRangeIterator(0, size); \
|
||||
} \
|
||||
) \
|
||||
) \
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user