Skip to content

Commit

Permalink
Map::GetIfExists and Map::CheckValue
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoLuis0 authored and coelckers committed Jan 21, 2023
1 parent 0b9fdf2 commit decba68
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 16 deletions.
78 changes: 78 additions & 0 deletions src/common/scripting/core/maps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,59 @@ template<typename M> void MapGetString(M * self,expand_types_vm<typename M::KeyT
}
}

template<typename M> expand_types_vm<typename M::ValueType> MapGetIfExists(M * self,expand_types_vm<typename M::KeyType> key)
{
typename M::ValueType * v = self->CheckKey(key);
if (v) {
return *v;
}
else
{
return {};
}
}

template<typename M> void MapGetIfExistsString(M * self,expand_types_vm<typename M::KeyType> key, FString &out)
{
FString * v = self->CheckKey(key);
if (v) {
out = *v;
}
else
{
out = FString();
}
}

template<typename M> int MapCheckKey(M * self, expand_types_vm<typename M::KeyType> key)
{
return self->CheckKey(key) != nullptr;
}

template<typename M> expand_types_vm<typename M::ValueType> MapCheckValue(M * self,expand_types_vm<typename M::KeyType> key, int * exists)
{
typename M::ValueType * v = self->CheckKey(key);
if ((*exists = !!v)) {
return *v;
}
else
{
return {};
}
}

template<typename M> void MapCheckValueString(M * self,expand_types_vm<typename M::KeyType> key, int * exists, FString &out)
{
FString * v = self->CheckKey(key);
if ((*exists = !!v)) {
out = *v;
}
else
{
out = FString();
}
}


//==========================================================================
//
Expand Down Expand Up @@ -345,6 +393,19 @@ template<typename I> void MapIteratorSetValue(I * self, expand_types_vm<typename
PARAM_SELF_STRUCT_PROLOGUE( name ); \
PARAM_KEY( key ); \
ACTION_RETURN_VALUE( MapGet(self, key) ); \
} \
DEFINE_ACTION_FUNCTION_NATIVE( name, GetIfExists, MapGetIfExists< name >) \
{ \
PARAM_SELF_STRUCT_PROLOGUE( name ); \
PARAM_KEY( key ); \
ACTION_RETURN_VALUE( MapGetIfExists(self, key) ); \
} \
DEFINE_ACTION_FUNCTION_NATIVE( name, CheckValue, MapCheckValue< name >) \
{ \
PARAM_SELF_STRUCT_PROLOGUE( name ); \
PARAM_KEY( key ); \
PARAM_OUTPOINTER( exists, int); \
ACTION_RETURN_VALUE( MapCheckValue(self, key, exists) ); \
}

#define DEF_MAP_X_S( name, key_type, PARAM_KEY ) \
Expand All @@ -356,6 +417,23 @@ template<typename I> void MapIteratorSetValue(I * self, expand_types_vm<typename
FString out; \
MapGetString(self, key, out); \
ACTION_RETURN_STRING( out ); \
} \
DEFINE_ACTION_FUNCTION_NATIVE( name, GetIfExists, MapGetIfExistsString< name >) \
{ \
PARAM_SELF_STRUCT_PROLOGUE( name ); \
PARAM_KEY( key ); \
FString out; \
MapGetIfExistsString(self, key, out); \
ACTION_RETURN_STRING( out ); \
} \
DEFINE_ACTION_FUNCTION_NATIVE( name, CheckValue, MapCheckValueString< name >) \
{ \
PARAM_SELF_STRUCT_PROLOGUE( name ); \
PARAM_KEY( key ); \
PARAM_OUTPOINTER( exists, int); \
FString out; \
MapCheckValueString(self, key, exists, out); \
ACTION_RETURN_STRING( out ); \
}

#define COMMA ,
Expand Down
96 changes: 80 additions & 16 deletions wadsrc/static/zscript/engine/maps.zs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ struct Map_I32_I8 native

native int Get(int key);
native bool CheckKey(int key);
native void Insert(int key,int value);

native version("4.11") int GetIfExists(int key);
native version("4.11") int CheckValue(int key, out bool exists);

native void Insert(int key, int value);
native void InsertNew(int key);
native void Remove(int key);
}
Expand Down Expand Up @@ -37,7 +41,11 @@ struct Map_I32_I16 native

native int Get(int key);
native bool CheckKey(int key);
native void Insert(int key,int value);

native version("4.11") int GetIfExists(int key);
native version("4.11") int CheckValue(int key, out bool exists);

native void Insert(int key, int value);
native void InsertNew(int key);
native void Remove(int key);
}
Expand Down Expand Up @@ -65,7 +73,11 @@ struct Map_I32_I32 native

native int Get(int key);
native bool CheckKey(int key);
native void Insert(int key,int value);

native version("4.11") int GetIfExists(int key);
native version("4.11") int CheckValue(int key, out bool exists);

native void Insert(int key, int value);
native void InsertNew(int key);
native void Remove(int key);
}
Expand Down Expand Up @@ -93,7 +105,11 @@ struct Map_I32_F32 native

native double Get(int key);
native bool CheckKey(int key);
native void Insert(int key,double value);

native version("4.11") double GetIfExists(int key);
native version("4.11") double CheckValue(int key, out bool exists);

native void Insert(int key, double value);
native void InsertNew(int key);
native void Remove(int key);
}
Expand Down Expand Up @@ -121,7 +137,11 @@ struct Map_I32_F64 native

native double Get(int key);
native bool CheckKey(int key);
native void Insert(int key,double value);

native version("4.11") double GetIfExists(int key);
native version("4.11") double CheckValue(int key, out bool exists);

native void Insert(int key, double value);
native void InsertNew(int key);
native void Remove(int key);
}
Expand Down Expand Up @@ -149,7 +169,11 @@ struct Map_I32_Obj native

native Object Get(int key);
native bool CheckKey(int key);
native void Insert(int key,Object value);

native version("4.11") Object GetIfExists(int key);
native version("4.11") Object CheckValue(int key, out bool exists);

native void Insert(int key, Object value);
native void InsertNew(int key);
native void Remove(int key);
}
Expand Down Expand Up @@ -177,7 +201,11 @@ struct Map_I32_Ptr native

native voidptr Get(int key);
native bool CheckKey(int key);
native void Insert(int key,voidptr value);

native version("4.11") voidptr GetIfExists(int key);
native version("4.11") voidptr CheckValue(int key, out bool exists);

native void Insert(int key, voidptr value);
native void InsertNew(int key);
native void Remove(int key);
}
Expand All @@ -202,7 +230,11 @@ struct Map_I32_Str native

native String Get(int key);
native bool CheckKey(int key);
native void Insert(int key,String value);

native version("4.11") String GetIfExists(int key);
native version("4.11") String CheckValue(int key, out bool exists);

native void Insert(int key, String value);
native void InsertNew(int key);
native void Remove(int key);
}
Expand Down Expand Up @@ -232,7 +264,11 @@ struct Map_Str_I8 native

native int Get(String key);
native bool CheckKey(String key);
native void Insert(String key,int value);

native version("4.11") int GetIfExists(String key);
native version("4.11") int CheckValue(String key, out bool exists);

native void Insert(String key, int value);
native void InsertNew(String key);
native void Remove(String key);
}
Expand Down Expand Up @@ -260,7 +296,11 @@ struct Map_Str_I16 native

native int Get(String key);
native bool CheckKey(String key);
native void Insert(String key,int value);

native version("4.11") int GetIfExists(String key);
native version("4.11") int CheckValue(String key, out bool exists);

native void Insert(String key, int value);
native void InsertNew(String key);
native void Remove(String key);
}
Expand Down Expand Up @@ -288,7 +328,11 @@ struct Map_Str_I32 native

native int Get(String key);
native bool CheckKey(String key);
native void Insert(String key,int value);

native version("4.11") int GetIfExists(String key);
native version("4.11") int CheckValue(String key, out bool exists);

native void Insert(String key, int value);
native void InsertNew(String key);
native void Remove(String key);
}
Expand Down Expand Up @@ -316,7 +360,11 @@ struct Map_Str_F32 native

native double Get(String key);
native bool CheckKey(String key);
native void Insert(String key,double value);

native version("4.11") double GetIfExists(String key);
native version("4.11") double CheckValue(String key, out bool exists);

native void Insert(String key, double value);
native void InsertNew(String key);
native void Remove(String key);
}
Expand Down Expand Up @@ -344,7 +392,11 @@ struct Map_Str_F64 native

native double Get(String key);
native bool CheckKey(String key);
native void Insert(String key,double value);

native version("4.11") double GetIfExists(String key);
native version("4.11") double CheckValue(String key, out bool exists);

native void Insert(String key, double value);
native void InsertNew(String key);
native void Remove(String key);
}
Expand Down Expand Up @@ -372,7 +424,11 @@ struct Map_Str_Obj native

native Object Get(String key);
native bool CheckKey(String key);
native void Insert(String key,Object value);

native version("4.11") Object GetIfExists(String key);
native version("4.11") Object CheckValue(String key, out bool exists);

native void Insert(String key, Object value);
native void InsertNew(String key);
native void Remove(String key);
}
Expand Down Expand Up @@ -400,7 +456,11 @@ struct Map_Str_Ptr native

native voidptr Get(String key);
native bool CheckKey(String key);
native void Insert(String key,voidptr value);

native version("4.11") voidptr GetIfExists(String key);
native version("4.11") voidptr CheckValue(String key, out bool exists);

native void Insert(String key, voidptr value);
native void InsertNew(String key);
native void Remove(String key);
}
Expand Down Expand Up @@ -428,7 +488,11 @@ struct Map_Str_Str native

native String Get(String key);
native bool CheckKey(String key);
native void Insert(String key,String value);

native version("4.11") String GetIfExists(String key);
native version("4.11") String CheckValue(String key, out bool exists);

native void Insert(String key, String value);
native void InsertNew(String key);
native void Remove(String key);
}
Expand Down

0 comments on commit decba68

Please sign in to comment.