#include "tarray.h" #include "dobject.h" #include "zstring.h" #include "vm.h" #include "types.h" #include "v_draw.h" #include "maps.h" //========================================================================== // // MAP_GC_WRITE_BARRIER // //========================================================================== #define MAP_GC_WRITE_BARRIER(x) { \ TMapIterator it(*x);\ typename M::Pair * p;\ while(it.NextPair(p)){\ GC::WriteBarrier(p->Value);\ }\ } //========================================================================== // // MapCopy // //========================================================================== template void MapCopy(M * self, M * other) { if constexpr(std::is_same_v) { MAP_GC_WRITE_BARRIER(other); } *self = *other; // how does this handle self->info? TODO double check. self->info->rev++; } //========================================================================== // // MapMove // //========================================================================== template void MapMove(M * self, M * other) { if constexpr(std::is_same_v) { MAP_GC_WRITE_BARRIER(other); } self->TransferFrom(*other); self->info->rev++; other->info->rev++; } //========================================================================== // // MapSwap // //========================================================================== template void MapSwap(M * self, M * other) { if constexpr(std::is_same_v) { MAP_GC_WRITE_BARRIER(other); } self->Swap(*other); self->info->rev++; other->info->rev++; } //========================================================================== // // MapClear // //========================================================================== template void MapClear(M * self) { if constexpr(std::is_same_v) { MAP_GC_WRITE_BARRIER(self); } self->Clear(); self->info->rev++; } //========================================================================== // // // //========================================================================== template using expand_types_vm = std::conditional_t || std::is_same_v, uint32_t , /* expand 8/16-bit to 32-bit */ std::conditional_t , double , /* expand float to double */ std::conditional_t , const FString & , T>>>; /* change String to String ref */ template unsigned int MapCountUsed(M * self) { return self->CountUsed(); } template expand_types_vm MapGet(M * self,expand_types_vm key) { typename M::ValueType * v = self->CheckKey(key); if (v) { if constexpr(std::is_same_v) { return GC::ReadBarrier(*v); } else { return *v; } } else { typename M::ValueType n {}; self->Insert(key,n); self->info->rev++; // invalidate iterators return n; } } template void MapGetString(M * self,expand_types_vm key, FString &out) { FString * v = self->CheckKey(key); if (v) { out = *v; } else { out = FString(); self->Insert(key,out); self->info->rev++; // invalidate iterators } } template expand_types_vm MapGetIfExists(M * self,expand_types_vm key) { typename M::ValueType * v = self->CheckKey(key); if (v) { if constexpr(std::is_same_v) { return GC::ReadBarrier(*v); } else { return *v; } } else { return {}; } } template void MapGetIfExistsString(M * self,expand_types_vm key, FString &out) { FString * v = self->CheckKey(key); if (v) { out = *v; } else { out = FString(); } } template int MapCheckKey(M * self, expand_types_vm key) { return self->CheckKey(key) != nullptr; } template expand_types_vm MapCheckValue(M * self,expand_types_vm key, int &exists) { typename M::ValueType * v = self->CheckKey(key); if ((exists = !!v)) { if constexpr(std::is_same_v) { return GC::ReadBarrier(*v); } else { return *v; } } else { return {}; } } template void MapCheckValueString(M * self,expand_types_vm key, FString &out, int &exists) { FString * v = self->CheckKey(key); if ((exists = !!v)) { out = *v; } else { out = FString(); } } //========================================================================== // // MapInsert // //========================================================================== template void MapInsert(M * self, expand_types_vm key, expand_types_vm value) { if constexpr(std::is_same_v) { MAP_GC_WRITE_BARRIER(self); GC::WriteBarrier(value); } if constexpr(std::is_same_v) { self->Insert(key,static_cast(value)); } else { self->Insert(key, value); } self->info->rev++; // invalidate iterators } //========================================================================== // // // //========================================================================== template void MapInsertNew(M * self, expand_types_vm key) { if constexpr(std::is_same_v) { MAP_GC_WRITE_BARRIER(self); } self->Insert(key,{}); self->info->rev++; // invalidate iterators } template void MapRemove(M * self, expand_types_vm key) { self->Remove(key); self->info->rev++; // invalidate iterators } //========================================================================== // // // //========================================================================== template int MapIteratorInit(I * self, M * other) { return self->Init(*other); } template int MapIteratorReInit(I * self) { return self->ReInit(); } template int MapIteratorValid(I * self) { return self->Valid(); } template int MapIteratorNext(I * self) { return self->Next(); } template expand_types_vm MapIteratorGetKey(I * self) { return self->GetKey(); } template void MapIteratorGetKeyString(I * self, FString &out) { out = self->GetKey(); } template expand_types_vm MapIteratorGetValue(I * self) { if constexpr(std::is_same_v) { return GC::ReadBarrier(self->GetValue()); } else { return self->GetValue(); } } template void MapIteratorGetValueString(I * self, FString &out) { out = self->GetValue(); } template void MapIteratorSetValue(I * self, expand_types_vm value) { auto & val = self->GetValue(); if constexpr(std::is_same_v) { GC::WriteBarrier(val); GC::WriteBarrier(value); } if constexpr(std::is_same_v) { val = static_cast(value); } else { val = value; } } //========================================================================== // // // //========================================================================== template void SetValType(T & ret,U & val){ if constexpr(std::is_same_v) { ret[N].SetObject(val); } else if constexpr(std::is_same_v) { ret[N].SetPointer(val); } else if constexpr(std::is_same_v) { ret[N].SetInt(val); } else if constexpr(std::is_same_v) { ret[N].SetFloat(val); } } #define PARAM_VOIDPOINTER(X) PARAM_POINTER( X , void ) #define PARAM_OBJPOINTER(X) PARAM_OBJECT( X , DObject ) #define _DEF_MAP( name, key_type, value_type, PARAM_KEY, PARAM_VALUE ) \ typedef ZSMap name;\ DEFINE_ACTION_FUNCTION_NATIVE( name , Copy , MapCopy< name > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ PARAM_POINTER( other, name ); \ MapCopy(self , other); \ return 0; \ } \ DEFINE_ACTION_FUNCTION_NATIVE( name , Move , MapMove< name > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ PARAM_POINTER( other, name ); \ MapMove(self , other); \ return 0; \ } \ DEFINE_ACTION_FUNCTION_NATIVE( name , Swap , MapSwap< name > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ PARAM_POINTER( other, name ); \ MapSwap(self , other); \ return 0; \ } \ DEFINE_ACTION_FUNCTION_NATIVE( name , Clear , MapClear< name > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ MapClear(self); \ return 0; \ } \ DEFINE_ACTION_FUNCTION_NATIVE( name, CountUsed, MapCountUsed< name >) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ ACTION_RETURN_INT( MapCountUsed(self) ); \ } \ DEFINE_ACTION_FUNCTION_NATIVE( name, CheckKey, MapCheckKey< name >) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ PARAM_KEY( key ); \ ACTION_RETURN_BOOL( MapCheckKey(self, key) ); \ } \ DEFINE_ACTION_FUNCTION_NATIVE( name, Insert, MapInsert< name >) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ PARAM_KEY( key ); \ PARAM_VALUE( value ); \ MapInsert(self, key, value); \ return 0; \ } \ DEFINE_ACTION_FUNCTION_NATIVE( name, InsertNew, MapInsertNew< name >) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ PARAM_KEY( key ); \ MapInsertNew(self, key); \ return 0; \ } \ DEFINE_ACTION_FUNCTION_NATIVE( name, Remove, MapRemove< name >) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ PARAM_KEY( key ); \ MapRemove(self, key); \ return 0; \ } #define DEF_MAP_X_X( name, key_type, value_type, PARAM_KEY, PARAM_VALUE , ACTION_RETURN_VALUE ) \ _DEF_MAP( name , key_type , value_type , PARAM_KEY , PARAM_VALUE ) \ DEFINE_ACTION_FUNCTION_NATIVE( name, Get, MapGet< name >) \ { \ 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 ); \ int exists; \ expand_types_vm out; \ out = MapCheckValue(self, key, exists); \ if (numret > 1) ret[1].SetInt(exists); \ if (numret > 0) SetValType<0>(ret, out); \ return numret; \ } #define DEF_MAP_X_S( name, key_type, PARAM_KEY ) \ _DEF_MAP( name , key_type , FString , PARAM_KEY , PARAM_STRING ) \ DEFINE_ACTION_FUNCTION_NATIVE( name, Get, MapGetString< name >) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ PARAM_KEY( key ); \ 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 ); \ int exists; \ FString out; \ MapCheckValueString(self, key, out, exists); \ if (numret > 1) ret[1].SetInt(exists); \ if (numret > 0) ret[0].SetString(out); \ return numret; \ } #define COMMA , #define _DEF_MAP_IT( map_name , name, key_type, value_type, PARAM_VALUE ) \ typedef ZSMapIterator name; \ DEFINE_ACTION_FUNCTION_NATIVE( name , Init , MapIteratorInit< name COMMA map_name > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ PARAM_POINTER( other, map_name ); \ ACTION_RETURN_BOOL( MapIteratorInit(self , other) ); \ } \ DEFINE_ACTION_FUNCTION_NATIVE( name , ReInit , MapIteratorReInit< name > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ ACTION_RETURN_BOOL( MapIteratorReInit(self) ); \ } \ DEFINE_ACTION_FUNCTION_NATIVE( name , Valid , MapIteratorValid< name > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ ACTION_RETURN_BOOL( MapIteratorValid(self) ); \ } \ DEFINE_ACTION_FUNCTION_NATIVE( name , Next , MapIteratorNext< name > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ ACTION_RETURN_BOOL( MapIteratorNext(self) ); \ } \ DEFINE_ACTION_FUNCTION_NATIVE( name , SetValue , MapIteratorSetValue< name > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ PARAM_VALUE( value ); \ MapIteratorSetValue(self, value); \ return 0; \ } /* DEFINE_ACTION_FUNCTION_NATIVE( name , GetKey , MapIteratorGetKey< name > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ ACTION_RETURN_KEY( MapIteratorGetKey(self) ); \ } \ DEFINE_ACTION_FUNCTION_NATIVE( name , GetValue , MapIteratorGetValue< name > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ ACTION_RETURN_VALUE( MapIteratorGetValue(self) ); \ } \ */ #define DEF_MAP_IT_I_X( map_name , name , value_type , PARAM_VALUE , ACTION_RETURN_VALUE ) \ _DEF_MAP_IT( map_name , name , uint32_t , value_type , PARAM_VALUE ) \ DEFINE_ACTION_FUNCTION_NATIVE( name , GetKey , MapIteratorGetKey< name > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ ACTION_RETURN_INT( MapIteratorGetKey(self) ); \ } \ DEFINE_ACTION_FUNCTION_NATIVE( name , GetValue , MapIteratorGetValue< name > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ ACTION_RETURN_VALUE( MapIteratorGetValue(self) ); \ } #define DEF_MAP_IT_S_X( map_name , name , value_type , PARAM_VALUE , ACTION_RETURN_VALUE ) \ _DEF_MAP_IT( map_name , name , FString , value_type , PARAM_VALUE ) \ DEFINE_ACTION_FUNCTION_NATIVE( name , GetKey , MapIteratorGetKeyString< name > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ FString out; \ MapIteratorGetKeyString(self , out); \ ACTION_RETURN_STRING( out ); \ } \ DEFINE_ACTION_FUNCTION_NATIVE( name , GetValue , MapIteratorGetValue< name > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( name ); \ ACTION_RETURN_VALUE( MapIteratorGetValue(self) ); \ } #define DEF_MAP_IT_I_S() \ _DEF_MAP_IT( FMap_I32_Str , FMapIterator_I32_Str , uint32_t , FString , PARAM_STRING ) \ DEFINE_ACTION_FUNCTION_NATIVE( FMapIterator_I32_Str , GetKey , MapIteratorGetKey< FMapIterator_I32_Str > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( FMapIterator_I32_Str ); \ ACTION_RETURN_INT( MapIteratorGetKey(self) ); \ } \ DEFINE_ACTION_FUNCTION_NATIVE( FMapIterator_I32_Str , GetValue , MapIteratorGetValueString< FMapIterator_I32_Str > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( FMapIterator_I32_Str ); \ FString out; \ MapIteratorGetValueString(self , out); \ ACTION_RETURN_STRING( out ); \ } #define DEF_MAP_IT_S_S() \ _DEF_MAP_IT( FMap_Str_Str , FMapIterator_Str_Str , FString , FString , PARAM_STRING ) \ DEFINE_ACTION_FUNCTION_NATIVE( FMapIterator_Str_Str , GetKey , MapIteratorGetKeyString< FMapIterator_Str_Str > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( FMapIterator_Str_Str ); \ FString out; \ MapIteratorGetKeyString(self , out); \ ACTION_RETURN_STRING( out ); \ } \ DEFINE_ACTION_FUNCTION_NATIVE( FMapIterator_Str_Str , GetValue , MapIteratorGetValueString< FMapIterator_Str_Str > ) \ { \ PARAM_SELF_STRUCT_PROLOGUE( FMapIterator_Str_Str ); \ FString out; \ MapIteratorGetValueString(self , out); \ ACTION_RETURN_STRING( out ); \ } #define DEFINE_MAP_AND_IT_I_X( name , value_type , PARAM_VALUE , ACTION_RETURN_VALUE ) \ DEF_MAP_X_X( FMap_ ## name , uint32_t , value_type , PARAM_INT , PARAM_VALUE , ACTION_RETURN_VALUE ) \ DEF_MAP_IT_I_X( FMap_ ## name , FMapIterator_ ## name , value_type , PARAM_VALUE , ACTION_RETURN_VALUE ) #define DEFINE_MAP_AND_IT_S_X( name , value_type , PARAM_VALUE , ACTION_RETURN_VALUE ) \ DEF_MAP_X_X( FMap_ ## name , FString , value_type , PARAM_STRING , PARAM_VALUE , ACTION_RETURN_VALUE ) \ DEF_MAP_IT_S_X( FMap_ ## name , FMapIterator_ ## name , value_type , PARAM_VALUE , ACTION_RETURN_VALUE ) #define DEFINE_MAP_AND_IT_I_S() \ DEF_MAP_X_S( FMap_I32_Str , uint32_t , PARAM_INT ) \ DEF_MAP_IT_I_S() #define DEFINE_MAP_AND_IT_S_S() \ DEF_MAP_X_S( FMap_Str_Str , FString , PARAM_STRING ) \ DEF_MAP_IT_S_S() DEFINE_MAP_AND_IT_I_X(I32_I8 , uint8_t , PARAM_INT , ACTION_RETURN_INT); DEFINE_MAP_AND_IT_I_X(I32_I16 , uint16_t , PARAM_INT , ACTION_RETURN_INT); DEFINE_MAP_AND_IT_I_X(I32_I32 , uint32_t , PARAM_INT , ACTION_RETURN_INT); DEFINE_MAP_AND_IT_I_X(I32_F32 , float , PARAM_FLOAT , ACTION_RETURN_FLOAT); DEFINE_MAP_AND_IT_I_X(I32_F64 , double , PARAM_FLOAT , ACTION_RETURN_FLOAT); DEFINE_MAP_AND_IT_I_X(I32_Obj , DObject* , PARAM_OBJPOINTER , ACTION_RETURN_OBJECT); DEFINE_MAP_AND_IT_I_X(I32_Ptr , void* , PARAM_VOIDPOINTER , ACTION_RETURN_POINTER); DEFINE_MAP_AND_IT_I_S(); DEFINE_MAP_AND_IT_S_X(Str_I8 , uint8_t , PARAM_INT , ACTION_RETURN_INT); DEFINE_MAP_AND_IT_S_X(Str_I16 , uint16_t , PARAM_INT , ACTION_RETURN_INT); DEFINE_MAP_AND_IT_S_X(Str_I32 , uint32_t , PARAM_INT , ACTION_RETURN_INT); DEFINE_MAP_AND_IT_S_X(Str_F32 , float , PARAM_FLOAT , ACTION_RETURN_FLOAT); DEFINE_MAP_AND_IT_S_X(Str_F64 , double , PARAM_FLOAT , ACTION_RETURN_FLOAT); DEFINE_MAP_AND_IT_S_X(Str_Obj , DObject* , PARAM_OBJPOINTER , ACTION_RETURN_OBJECT); DEFINE_MAP_AND_IT_S_X(Str_Ptr , void* , PARAM_VOIDPOINTER , ACTION_RETURN_POINTER); DEFINE_MAP_AND_IT_S_S();