export dictionary iterator

This commit is contained in:
Alexander Kromm 2019-12-31 22:26:52 +07:00 committed by Christoph Oelckers
parent 5ae0ae885d
commit 60026ba4f2
3 changed files with 109 additions and 2 deletions

View file

@ -11,7 +11,6 @@
DEFINE_ACTION_FUNCTION(_Dictionary, Create)
{
PARAM_PROLOGUE;
ACTION_RETURN_POINTER(new Dictionary);
}
@ -81,3 +80,64 @@ DEFINE_ACTION_FUNCTION_NATIVE(_Dictionary, Remove, DictRemove)
DictRemove(self, key);
return 0;
}
//=====================================================================================
//
// DictionaryIterator exports
//
//=====================================================================================
DictionaryIterator::DictionaryIterator(const Dictionary &dict)
: Iterator(dict)
, Pair(nullptr)
{
}
static DictionaryIterator *DictIteratorCreate(const Dictionary *dict)
{
return new DictionaryIterator(*dict);
}
DEFINE_ACTION_FUNCTION_NATIVE(_DictionaryIterator, Create, DictIteratorCreate)
{
PARAM_PROLOGUE;
PARAM_POINTER(dict, Dictionary);
ACTION_RETURN_POINTER(DictIteratorCreate(dict));
}
static int DictIteratorNext(DictionaryIterator *self)
{
return self->Iterator.NextPair(self->Pair);
}
DEFINE_ACTION_FUNCTION_NATIVE(_DictionaryIterator, Next, DictIteratorNext)
{
PARAM_SELF_STRUCT_PROLOGUE(DictionaryIterator);
ACTION_RETURN_BOOL(DictIteratorNext(self));
}
static void DictIteratorKey(const DictionaryIterator *self, FString *result)
{
*result = self->Pair ? self->Pair->Key : FString {};
}
DEFINE_ACTION_FUNCTION_NATIVE(_DictionaryIterator, Key, DictIteratorKey)
{
PARAM_SELF_STRUCT_PROLOGUE(DictionaryIterator);
FString result;
DictIteratorKey(self, &result);
ACTION_RETURN_STRING(result);
}
static void DictIteratorValue(const DictionaryIterator *self, FString *result)
{
*result = self->Pair ? self->Pair->Value : FString {};
}
DEFINE_ACTION_FUNCTION_NATIVE(_DictionaryIterator, Value, DictIteratorValue)
{
PARAM_SELF_STRUCT_PROLOGUE(DictionaryIterator);
FString result;
DictIteratorValue(self, &result);
ACTION_RETURN_STRING(result);
}

View file

@ -4,3 +4,11 @@
#include "zstring.h"
using Dictionary = TMap<FString, FString>;
struct DictionaryIterator
{
explicit DictionaryIterator(const Dictionary &dict);
Dictionary::ConstIterator Iterator;
Dictionary::ConstPair *Pair;
};

View file

@ -1,11 +1,50 @@
struct Dictionary native
{
native static Dictionary Create();
native static Dictionary FromString(String s);
native void Insert(String key, String value);
native void Remove(String key);
/**
* Returns the value for the specified key.
*/
native String At(String key) const;
/**
* Deserializes a dictionary from a string.
*
* @param s serialized string, must be either empty or returned from ToString().
*/
native static Dictionary FromString(String s);
/**
* Serializes a dictionary to a string.
*/
native String ToString() const;
}
struct DictionaryIterator native
{
native static DictionaryIterator Create(Dictionary dict);
/**
* Returns false if there are no more entries in the dictionary.
* Otherwise, returns true.
*
* While it returns true, get key and value for the current entry
* with Key() and Value() functions.
*/
native bool Next();
/**
* Returns the key for the current dictionary entry.
* Do not call this function before calling Next().
*/
native String Key() const;
/**
* Returns the value for the current dictionary entry.
* Do not call this function before calling Next().
*/
native String Value() const;
}