qzdoom/src/utility/dictionary.cpp

219 lines
5.2 KiB
C++
Raw Normal View History

#include "utility/dictionary.h"
#include "scripting/vm/vm.h"
#include "serializer.h"
#include <cassert>
//=====================================================================================
//
// DObject implementations for Dictionary and DictionaryIterator
//
//=====================================================================================
IMPLEMENT_CLASS(Dictionary, false, false);
IMPLEMENT_CLASS(DictionaryIterator, false, true);
IMPLEMENT_POINTERS_START(DictionaryIterator)
IMPLEMENT_POINTER(Dict)
IMPLEMENT_POINTERS_END
//=====================================================================================
//
// Dictionary functions
//
//=====================================================================================
void Dictionary::Serialize(FSerializer &arc)
{
Super::Serialize(arc);
constexpr char key[] { "dictionary" };
if (arc.isWriting())
{
// Pass this instance to serializer.
Dictionary *pointerToThis = this;
arc(key, pointerToThis);
}
else
{
// Receive new Dictionary, copy contents, clean up.
Dictionary *pointerToDeserializedDictionary;
arc(key, pointerToDeserializedDictionary);
Map.TransferFrom(pointerToDeserializedDictionary->Map);
delete pointerToDeserializedDictionary;
}
}
static Dictionary *DictCreate()
{
Dictionary *dict { Create<Dictionary>() };
return dict;
}
static void DictInsert(Dictionary *dict, const FString &key, const FString &value)
{
dict->Map.Insert(key, value);
}
static void DictAt(const Dictionary *dict, const FString &key, FString *result)
{
const FString *value = dict->Map.CheckKey(key);
*result = value ? *value : "";
}
static void DictToString(const Dictionary *dict, FString *result)
{
*result = DictionaryToString(*dict);
}
static void DictRemove(Dictionary *dict, const FString &key)
{
dict->Map.Remove(key);
}
//=====================================================================================
//
// Dictionary exports
//
//=====================================================================================
DEFINE_ACTION_FUNCTION_NATIVE(_Dictionary, Create, DictCreate)
{
ACTION_RETURN_POINTER(DictCreate());
}
DEFINE_ACTION_FUNCTION_NATIVE(_Dictionary, Insert, DictInsert)
{
PARAM_SELF_STRUCT_PROLOGUE(Dictionary);
PARAM_STRING(key);
PARAM_STRING(value);
DictInsert(self, key, value);
return 0;
}
DEFINE_ACTION_FUNCTION_NATIVE(_Dictionary, At, DictAt)
{
PARAM_SELF_STRUCT_PROLOGUE(Dictionary);
PARAM_STRING(key);
FString result;
DictAt(self, key, &result);
ACTION_RETURN_STRING(result);
}
DEFINE_ACTION_FUNCTION_NATIVE(_Dictionary, ToString, DictToString)
{
PARAM_SELF_STRUCT_PROLOGUE(Dictionary);
FString result;
DictToString(self, &result);
ACTION_RETURN_STRING(result);
}
DEFINE_ACTION_FUNCTION_NATIVE(_Dictionary, FromString, DictionaryFromString)
{
PARAM_PROLOGUE;
PARAM_STRING(string);
ACTION_RETURN_POINTER(DictionaryFromString(string));
}
DEFINE_ACTION_FUNCTION_NATIVE(_Dictionary, Remove, DictRemove)
{
PARAM_SELF_STRUCT_PROLOGUE(Dictionary);
PARAM_STRING(key);
DictRemove(self, key);
return 0;
}
2019-12-31 15:26:52 +00:00
//=====================================================================================
//
// DictionaryIterator functions
2019-12-31 15:26:52 +00:00
//
//=====================================================================================
DictionaryIterator::DictionaryIterator()
: Iterator(nullptr)
2019-12-31 15:26:52 +00:00
, Pair(nullptr)
, Dict(nullptr)
2019-12-31 15:26:52 +00:00
{
}
void DictionaryIterator::Serialize(FSerializer &arc)
2019-12-31 15:26:52 +00:00
{
if (arc.isWriting())
{
I_Error("Attempt to save pointer to unhandled type DictionaryIterator");
}
2019-12-31 15:26:52 +00:00
}
void DictionaryIterator::init(Dictionary *dict)
2019-12-31 15:26:52 +00:00
{
Iterator = std::make_unique<Dictionary::ConstIterator>(dict->Map);
Dict = dict;
GC::WriteBarrier(this, Dict);
2019-12-31 15:26:52 +00:00
}
static DictionaryIterator *DictIteratorCreate(Dictionary *dict)
2019-12-31 15:26:52 +00:00
{
DictionaryIterator *iterator = Create<DictionaryIterator>();
iterator->init(dict);
return iterator;
2019-12-31 15:26:52 +00:00
}
static int DictIteratorNext(DictionaryIterator *self)
2019-12-31 15:26:52 +00:00
{
assert(self->Iterator != nullptr);
const bool hasNext { self->Iterator->NextPair(self->Pair) };
return hasNext;
2019-12-31 15:26:52 +00:00
}
static void DictIteratorKey(const DictionaryIterator *self, FString *result)
{
*result = self->Pair ? self->Pair->Key : FString {};
}
static void DictIteratorValue(const DictionaryIterator *self, FString *result)
{
*result = self->Pair ? self->Pair->Value : FString {};
}
//=====================================================================================
//
// DictionaryIterator exports
//
//=====================================================================================
DEFINE_ACTION_FUNCTION_NATIVE(_DictionaryIterator, Create, DictIteratorCreate)
{
PARAM_PROLOGUE;
PARAM_POINTER(dict, Dictionary);
ACTION_RETURN_POINTER(DictIteratorCreate(dict));
}
DEFINE_ACTION_FUNCTION_NATIVE(_DictionaryIterator, Next, DictIteratorNext)
{
PARAM_SELF_STRUCT_PROLOGUE(DictionaryIterator);
ACTION_RETURN_BOOL(DictIteratorNext(self));
}
2019-12-31 15:26:52 +00:00
DEFINE_ACTION_FUNCTION_NATIVE(_DictionaryIterator, Key, DictIteratorKey)
{
PARAM_SELF_STRUCT_PROLOGUE(DictionaryIterator);
FString result;
DictIteratorKey(self, &result);
ACTION_RETURN_STRING(result);
}
DEFINE_ACTION_FUNCTION_NATIVE(_DictionaryIterator, Value, DictIteratorValue)
{
PARAM_SELF_STRUCT_PROLOGUE(DictionaryIterator);
FString result;
DictIteratorValue(self, &result);
ACTION_RETURN_STRING(result);
}