Support for length unary operator
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -20,7 +20,8 @@ namespace Porygon::UserData {
|
||||
_iterable(iterable)
|
||||
{}
|
||||
|
||||
static shared_ptr<UserDataCollectionType> CreateIndexable(const shared_ptr<const ScriptType>& keyType, const shared_ptr<const ScriptType>& valueType){
|
||||
static shared_ptr<UserDataCollectionType> CreateIndexable(const shared_ptr<const ScriptType>& keyType,
|
||||
const shared_ptr<const ScriptType>& valueType){
|
||||
return make_shared<UserDataCollectionType>(keyType, valueType, true, true);
|
||||
}
|
||||
|
||||
@@ -65,6 +66,10 @@ namespace Porygon::UserData {
|
||||
return _valueType;
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] bool IsCountable() const override {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,9 @@ UserDataCollectionType* CreateCollectionType(const ScriptType* keyType, const Sc
|
||||
UserDataCollectionValue* CreateCollectionValue(UserDataCollectionType* type, void* obj,
|
||||
const EvalValue* (*get)(void*, const EvalValue*),
|
||||
void (*set)(void*, const EvalValue*, const EvalValue*),
|
||||
Iterator* (*getIterator)(void*)){
|
||||
return new UserDataCollectionValue(shared_ptr<UserDataCollectionType>(type), new UserDataCollectionHelper(obj, get, set, getIterator));
|
||||
Iterator* (*getIterator)(void*), size_t (getSize)(void*)){
|
||||
return new UserDataCollectionValue(shared_ptr<UserDataCollectionType>(type),
|
||||
new UserDataCollectionHelper(obj, get, set, getIterator, getSize));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "UserDataCollectionType.hpp"
|
||||
#include "../../Evaluator/EvalValues/EvalValue.hpp"
|
||||
#include "../../Utilities/Random.hpp"
|
||||
#include "../../Evaluator/EvalValues/NumericEvalValue.hpp"
|
||||
|
||||
using namespace Porygon::Evaluation;
|
||||
|
||||
@@ -15,12 +16,14 @@ namespace Porygon::UserData {
|
||||
const EvalValue* (*_get)(void*, const EvalValue*);
|
||||
void (*_set)(void*, const EvalValue* , const EvalValue*);
|
||||
Iterator* (*_getIterator)(void*);
|
||||
size_t (*_getLength)(void*);
|
||||
public:
|
||||
UserDataCollectionHelper(void* parentObject,
|
||||
const EvalValue* (*get)(void*, const EvalValue*),
|
||||
void (*set)(void*, const EvalValue*, const EvalValue*),
|
||||
Iterator* (*getIterator)(void*))
|
||||
: _parentObject(parentObject), _get(get), _set(set), _getIterator(getIterator){}
|
||||
Iterator* (*getIterator)(void*), size_t (*getLength)(void*))
|
||||
: _parentObject(parentObject), _get(get), _set(set), _getIterator(getIterator),
|
||||
_getLength(getLength){}
|
||||
|
||||
const EvalValue* Get(const EvalValue* key) const{
|
||||
return _get(_parentObject, key);
|
||||
@@ -33,6 +36,10 @@ namespace Porygon::UserData {
|
||||
[[nodiscard]] Iterator* GetIterator() const{
|
||||
return _getIterator(_parentObject);
|
||||
}
|
||||
|
||||
[[nodiscard]] size_t GetLength() const{
|
||||
return _getLength(_parentObject);
|
||||
}
|
||||
};
|
||||
|
||||
class UserDataCollectionValue : public Evaluation::EvalValue{
|
||||
@@ -46,7 +53,7 @@ namespace Porygon::UserData {
|
||||
}
|
||||
public:
|
||||
|
||||
UserDataCollectionValue(shared_ptr<const ScriptType> type, const UserDataCollectionHelper* helper)
|
||||
UserDataCollectionValue(const shared_ptr<const ScriptType>& type, const UserDataCollectionHelper* helper)
|
||||
: _type(dynamic_pointer_cast<const UserDataCollectionType>(type)), _helper(helper), _hash(Utilities::Random::Get())
|
||||
{
|
||||
}
|
||||
@@ -85,6 +92,13 @@ namespace Porygon::UserData {
|
||||
_helper->Set(key, value);
|
||||
delete value;
|
||||
}
|
||||
|
||||
[[nodiscard]] EvalValue *UnaryOperation(Binder::BoundUnaryOperation operation) const override {
|
||||
if (operation == Binder::BoundUnaryOperation::Count){
|
||||
return new NumericEvalValue(static_cast<int64_t>(_helper->GetLength()));
|
||||
}
|
||||
return EvalValue::UnaryOperation(operation);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -96,6 +96,8 @@ Porygon::Utilities::HashedString* Convert(const char* k){
|
||||
auto val = ((T_USERDATA*)obj)->fieldName; \
|
||||
auto size = val.size(); \
|
||||
return new Porygon::UserData::UserDataCollectionRangeIterator(0, size); \
|
||||
}, [](void* obj) -> size_t{ \
|
||||
return ((T_USERDATA*)obj)->fieldName.size(); \
|
||||
} \
|
||||
) \
|
||||
) \
|
||||
|
||||
Reference in New Issue
Block a user