Skip to content

Commit

Permalink
Implement Static Function Pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoLuis0 committed Oct 7, 2023
1 parent 6055ff0 commit 82ade1e
Show file tree
Hide file tree
Showing 23 changed files with 876 additions and 51 deletions.
3 changes: 3 additions & 0 deletions src/common/engine/namedef.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ xx(Vector)
xx(Map)
xx(MapIterator)
xx(Array)
xx(Function)
xx(Include)
xx(Sound)
xx(State)
Expand Down Expand Up @@ -190,6 +191,7 @@ xx(SetNull)
xx(Key)
xx(Index)
xx(Find)
xx(Call)

// color channels
xx(a)
Expand Down Expand Up @@ -269,6 +271,7 @@ xx(BuiltinRandom2)
xx(BuiltinFRandom)
xx(BuiltinNameToClass)
xx(BuiltinClassCast)
xx(BuiltinFunctionPtrCast)

xx(ScreenJobRunner)
xx(Action)
1 change: 1 addition & 0 deletions src/common/engine/sc_man_scanner.re
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ std2:
'map' { RET(TK_Map); }
'mapiterator' { RET(TK_MapIterator); }
'array' { RET(TK_Array); }
'function' { RET(ParseVersion >= MakeVersion(4, 11, 0)? TK_FunctionType : TK_Identifier); }
'in' { RET(TK_In); }
'sizeof' { RET(TK_SizeOf); }
'alignof' { RET(TK_AlignOf); }
Expand Down
1 change: 1 addition & 0 deletions src/common/engine/sc_man_tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ xx(TK_Vector3, "'vector3'")
xx(TK_Map, "'map'")
xx(TK_MapIterator, "'mapiterator'")
xx(TK_Array, "'array'")
xx(TK_FunctionType, "'function'")
xx(TK_In, "'in'")
xx(TK_SizeOf, "'sizeof'")
xx(TK_AlignOf, "'alignof'")
Expand Down
78 changes: 78 additions & 0 deletions src/common/engine/serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,84 @@ FSerializer &Serialize(FSerializer &arc, const char *key, NumericValue &value, N
return arc;
}

//==========================================================================
//
// PFunctionPointer
//
//==========================================================================

void SerializeFunctionPointer(FSerializer &arc, const char *key, FunctionPointerValue *&p)
{
if (arc.isWriting())
{
if(p)
{
arc.BeginObject(key);
arc("Class",p->ClassName);
arc("Function",p->FunctionName);
arc.EndObject();
}
else
{
arc.WriteKey(key);
arc.w->Null();
}
}
else
{
assert(p);
auto v = arc.r->FindKey(key);
if(!v || v->IsNull())
{
p = nullptr;
}
else if(v->IsObject())
{
arc.r->mObjects.Push(FJSONObject(v)); // BeginObject

const char * cstr;
arc.StringPtr("Class", cstr);

if(!cstr)
{
arc.StringPtr("Function", cstr);
if(!cstr)
{
Printf(TEXTCOLOR_RED "Function Pointer missing Class and Function Fields in Object\n");
}
else
{
Printf(TEXTCOLOR_RED "Function Pointer missing Class Field in Object\n");
}
arc.mErrors++;
arc.EndObject();
p = nullptr;
return;
}

p->ClassName = FString(cstr);
arc.StringPtr("Function", cstr);

if(!cstr)
{
Printf(TEXTCOLOR_RED "Function Pointer missing Function Field in Object\n");
arc.mErrors++;
arc.EndObject();
p = nullptr;
return;
}
p->FunctionName = FString(cstr);
arc.EndObject();
}
else
{
Printf(TEXTCOLOR_RED "Function Pointer is not an Object\n");
arc.mErrors++;
p = nullptr;
}
}
}

#include "renderstyle.h"
FSerializer& Serialize(FSerializer& arc, const char* key, FRenderStyle& style, FRenderStyle* def)
{
Expand Down
8 changes: 8 additions & 0 deletions src/common/engine/serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ struct NumericValue
}
};

struct FunctionPointerValue
{
FString ClassName;
FString FunctionName;
};


class FSerializer
{
Expand Down Expand Up @@ -235,6 +241,8 @@ FSerializer &Serialize(FSerializer &arc, const char *key, FString &sid, FString
FSerializer &Serialize(FSerializer &arc, const char *key, NumericValue &sid, NumericValue *def);
FSerializer &Serialize(FSerializer &arc, const char *key, struct ModelOverride &sid, struct ModelOverride *def);

void SerializeFunctionPointer(FSerializer &arc, const char *key, FunctionPointerValue *&p);

template <typename T/*, typename = std::enable_if_t<std::is_base_of_v<DObject, T>>*/>
FSerializer &Serialize(FSerializer &arc, const char *key, T *&value, T **)
{
Expand Down
Loading

0 comments on commit 82ade1e

Please sign in to comment.