From e126ddd50a0c0bcff4323c3d2b9f6e2a99c6a931 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Thu, 8 Apr 2021 18:31:34 +0700 Subject: [PATCH] Add convenience get/setters for `Collection`s --- sources/Data/Collections/AssociativeArray.uc | 328 +++++++++++++++++++ sources/Data/Collections/DynamicArray.uc | 317 ++++++++++++++++++ 2 files changed, 645 insertions(+) diff --git a/sources/Data/Collections/AssociativeArray.uc b/sources/Data/Collections/AssociativeArray.uc index cefab76..f4ef8f0 100644 --- a/sources/Data/Collections/AssociativeArray.uc +++ b/sources/Data/Collections/AssociativeArray.uc @@ -543,6 +543,334 @@ protected function AcediaObject GetByText(MutableText key) return result; } +/** + * Returns `bool` item at key `key`. If key is invalid or + * stores a non-`bool` value, returns `defaultValue`. + * + * Referred value must be stored as `BoolBox` or `BoolRef` + * (or one of their sub-classes) for this method to work. + * + * @param key Key of a `bool` item that `AssociativeArray` + * has to return. + * @param defaultValue Value to return if there is either no item recorded + * at `key` or it has a wrong type. + * @return `bool` value at `key` in the caller `AssociativeArray`. + * `defaultValue` if passed `key` is invalid or non-`bool` value + * is stored with it. + */ +public final function bool GetBool(AcediaObject key, optional bool defaultValue) +{ + local AcediaObject result; + local BoolBox asBox; + local BoolRef asRef; + result = GetItem(key); + if (result == none) { + return defaultValue; + } + asBox = BoolBox(result); + if (asBox != none) { + return asBox.Get(); + } + asRef = BoolRef(result); + if (asRef != none) { + return asRef.Get(); + } + return defaultValue; +} + +/** + * Changes `AssociativeArray`'s value at key `key` to `value` that will be + * recorded as either `BoolBox` or `BoolRef`, depending of `asRef` + * optional parameter. + * + * Inserted value will always be recorded as a managed value, i.e. it will be + * automatically deallocated when overwritten, removed or + * caller `AssociativeArray` is deallocated. + * + * @param key Key, at which to change the value. If `DynamicArray` is + * not long enough to hold it, it will be automatically expanded. + * If passed key is negative - method will do nothing. + * @param value Value to be set at a given key. + * @param asRef Given `bool` value will be recorded as immutable `BoolBox` + * by default (`asRef == false`). Setting this parameter to `true` will + * make this method record it as a mutable `BoolRef`. + * @return Reference to the caller `AssociativeArray` to allow for + * method chaining. + */ +public final function AssociativeArray SetBool( + AcediaObject key, + bool value, + optional bool asRef) +{ + if (asRef) { + SetItem(key, _.ref.bool(value), true); + } + else { + SetItem(key, _.box.bool(value), true); + } + return self; +} + +/** + * Returns `byte` item at key `key`. If key is invalid or + * stores a non-`byte` value, returns `defaultValue`. + * + * Referred value must be stored as `ByteBox` or `ByteBox` + * (or one of their sub-classes) for this method to work. + * + * @param key Key of a `byte` item that `AssociativeArray` + * has to return. + * @param defaultValue Value to return if there is either no item recorded + * at `key` or it has a wrong type. + * @return `byte` value at `key` in the caller `AssociativeArray`. + * `defaultValue` if passed `key` is invalid or non-`byte` value + * is stored with it. + */ +public final function byte GetByte(AcediaObject key, optional byte defaultValue) +{ + local AcediaObject result; + local ByteBox asBox; + local ByteRef asRef; + result = GetItem(key); + if (result == none) { + return defaultValue; + } + asBox = ByteBox(result); + if (asBox != none) { + return asBox.Get(); + } + asRef = ByteRef(result); + if (asRef != none) { + return asRef.Get(); + } + return defaultValue; +} + +/** + * Changes `AssociativeArray`'s value at key `key` to `value` that will be + * recorded as either `ByteBox` or `ByteBox`, depending of `asRef` + * optional parameter. + * + * Inserted value will always be recorded as a managed value, i.e. it will be + * automatically deallocated when overwritten, removed or + * caller `AssociativeArray` is deallocated. + * + * @param key Key, at which to change the value. If `DynamicArray` is + * not long enough to hold it, it will be automatically expanded. + * If passed key is negative - method will do nothing. + * @param value Value to be set at a given key. + * @param asRef Given `byte` value will be recorded as immutable `ByteBox` + * by default (`asRef == false`). Setting this parameter to `true` will + * make this method record it as a mutable `ByteBox`. + * @return Reference to the caller `AssociativeArray` to allow for + * method chaining. + */ +public final function AssociativeArray SetByte( + AcediaObject key, + byte value, + optional bool asRef) +{ + if (asRef) { + SetItem(key, _.ref.byte(value), true); + } + else { + SetItem(key, _.box.byte(value), true); + } + return self; +} + +/** + * Returns `int` item at key `key`. If key is invalid or + * stores a non-`int` value, returns `defaultValue`. + * + * Referred value must be stored as `IntBox` or `IntRef` + * (or one of their sub-classes) for this method to work. + * + * @param key Key of a `int` item that `AssociativeArray` + * has to return. + * @param defaultValue Value to return if there is either no item recorded + * at `key` or it has a wrong type. + * @return `int` value at `key` in the caller `AssociativeArray`. + * `defaultValue` if passed `key` is invalid or non-`int` value + * is stored with it. + */ +public final function int GetInt(AcediaObject key, optional int defaultValue) +{ + local AcediaObject result; + local IntBox asBox; + local IntRef asRef; + result = GetItem(key); + if (result == none) { + return defaultValue; + } + asBox = IntBox(result); + if (asBox != none) { + return asBox.Get(); + } + asRef = IntRef(result); + if (asRef != none) { + return asRef.Get(); + } + return defaultValue; +} + +/** + * Changes `AssociativeArray`'s value at key `key` to `value` that will be + * recorded as either `IntBox` or `IntRef`, depending of `asRef` + * optional parameter. + * + * Inserted value will always be recorded as a managed value, i.e. it will be + * automatically deallocated when overwritten, removed or + * caller `AssociativeArray` is deallocated. + * + * @param key Key, at which to change the value. If `DynamicArray` is + * not long enough to hold it, it will be automatically expanded. + * If passed key is negative - method will do nothing. + * @param value Value to be set at a given key. + * @param asRef Given `int` value will be recorded as immutable `IntBox` + * by default (`asRef == false`). Setting this parameter to `true` will + * make this method record it as a mutable `IntRef`. + * @return Reference to the caller `AssociativeArray` to allow for + * method chaining. + */ +public final function AssociativeArray SetInt( + AcediaObject key, + int value, + optional bool asRef) +{ + if (asRef) { + SetItem(key, _.ref.int(value), true); + } + else { + SetItem(key, _.box.int(value), true); + } + return self; +} + +/** + * Returns `float` item at key `key`. If key is invalid or + * stores a non-`float` value, returns `defaultValue`. + * + * Referred value must be stored as `FloatBox` or `FloatRef` + * (or one of their sub-classes) for this method to work. + * + * @param key Key of a `float` item that `AssociativeArray` + * has to return. + * @param defaultValue Value to return if there is either no item recorded + * at `key` or it has a wrong type. + * @return `float` value at `key` in the caller `AssociativeArray`. + * `defaultValue` if passed `key` is invalid or non-`float` value + * is stored with it. + */ +public final function float GetFloat( + AcediaObject key, + optional float defaultValue) +{ + local AcediaObject result; + local FloatBox asBox; + local FloatRef asRef; + result = GetItem(key); + if (result == none) { + return defaultValue; + } + asBox = FloatBox(result); + if (asBox != none) { + return asBox.Get(); + } + asRef = FloatRef(result); + if (asRef != none) { + return asRef.Get(); + } + return defaultValue; +} + +/** + * Changes `AssociativeArray`'s value at key `key` to `value` that will be + * recorded as either `FloatBox` or `FloatRef`, depending of `asRef` + * optional parameter. + * + * Inserted value will always be recorded as a managed value, i.e. it will be + * automatically deallocated when overwritten, removed or + * caller `AssociativeArray` is deallocated. + * + * @param key Key, at which to change the value. If `DynamicArray` is + * not long enough to hold it, it will be automatically expanded. + * If passed key is negative - method will do nothing. + * @param value Value to be set at a given key. + * @param asRef Given `float` value will be recorded as immutable `FloatBox` + * by default (`asRef == false`). Setting this parameter to `true` will + * make this method record it as a mutable `FloatRef`. + * @return Reference to the caller `AssociativeArray` to allow for + * method chaining. + */ +public final function AssociativeArray SetFloat( + AcediaObject key, + float value, + optional bool asRef) +{ + if (asRef) { + SetItem(key, _.ref.float(value), true); + } + else { + SetItem(key, _.box.float(value), true); + } + return self; +} + +/** + * Returns `Text` item stored at key `key`. If key is invalid or + * stores a non-`Text` value, returns `none`. + * + * Referred value must be stored as `Text` (or one of it's sub-classes, + * such as `MutableText`) for this method to work. + * + * @param key Key of a `Text` item that `AssociativeArray` + * has to return. + * @return `Text` value recorded with `key` in the caller `AssociativeArray`. + * `none` if passed `key` is invalid or non-`Text` value + * is stored with it. + */ +public final function Text GetText(AcediaObject key) +{ + return Text(GetItem(key)); +} + +/** + * Returns `AssociativeArray` item stored at key `key`. If key is invalid or + * stores a non-`AssociativeArray` value, returns `none`. + * + * Referred value must be stored as `AssociativeArray` + * (or one of it's sub-classes) for this method to work. + * + * @param key Key of an `AssociativeArray` item that caller `AssociativeArray` + * has to return. + * @return `AssociativeArray` value recorded with `key` in the caller + * `AssociativeArray`. `none` if passed `key` is invalid or + * non-`AssociativeArray` value is stored with it. + */ +public final function AssociativeArray GetAssociativeArray(AcediaObject key) +{ + return AssociativeArray(GetItem(key)); +} + +/** + * Returns `DynamicArray` item stored at key `key`. If key is invalid or + * stores a non-`DynamicArray` value, returns `none`. + * + * Referred value must be stored as `DynamicArray` + * (or one of it's sub-classes) for this method to work. + * + * @param key Key of a `DynamicArray` item that caller `AssociativeArray` + * has to return. + * @return `AssociativeArray` value recorded with `key` in the caller + * `AssociativeArray`. `none` if passed `key` is invalid or + * non-`AssociativeArray` value is stored with it. + */ +public final function DynamicArray GetDynamicArray(AcediaObject key) +{ + return DynamicArray(GetItem(key)); +} + defaultproperties { iteratorClass = class'AssociativeArrayIterator' diff --git a/sources/Data/Collections/DynamicArray.uc b/sources/Data/Collections/DynamicArray.uc index 7808b1d..dbe2f15 100644 --- a/sources/Data/Collections/DynamicArray.uc +++ b/sources/Data/Collections/DynamicArray.uc @@ -489,6 +489,323 @@ protected function AcediaObject GetByText(MutableText key) return GetItem(index); } +/** + * Returns `bool` item at `index`. If index is invalid or + * stores a non-`bool` value, returns `defaultValue`. + * + * Referred value must be stored as `BoolBox` or `BoolRef` + * (or one of their sub-classes) for this method to work. + * + * @param index Index of a `bool` item that `DynamicArray` + * has to return. + * @param defaultValue Value to return if there is either no item recorded + * at `index` or it has a wrong type. + * @return `bool` value at `index` in the caller `DynamicArray`. + * `defaultValue` if passed `index` is invalid or non-`bool` value + * is stored there. + */ +public final function bool GetBool(int index, optional bool defaultValue) +{ + local AcediaObject result; + local BoolBox asBox; + local BoolRef asRef; + result = GetItem(index); + if (result == none) { + return defaultValue; + } + asBox = BoolBox(result); + if (asBox != none) { + return asBox.Get(); + } + asRef = BoolRef(result); + if (asRef != none) { + return asRef.Get(); + } + return defaultValue; +} + +/** + * Changes `DynamicArray`'s value at `index` to `value` that will be recorded + * as either `BoolBox` or `BoolRef`, depending of `asRef` optional parameter. + * + * Inserted value will always be recorded as a managed value, i.e. it will be + * automatically deallocated when overwritten, removed or caller `DynamicArray` + * is deallocated. + * + * @param index Index, at which to change the value. If `DynamicArray` is + * not long enough to hold it, it will be automatically expanded. + * If passed index is negative - method will do nothing. + * @param value Value to be set at a given index. + * @param asRef Given `bool` value will be recorded as immutable `BoolBox` + * by default (`asRef == false`). Setting this parameter to `true` will + * make this method record it as a mutable `BoolRef`. + * @return Reference to the caller `DynamicArray` to allow for method chaining. + */ +public final function DynamicArray SetBool( + int index, + bool value, + optional bool asRef) +{ + if (asRef) { + SetItem(index, _.ref.bool(value), true); + } + else { + SetItem(index, _.box.bool(value), true); + } + return self; +} + +/** + * Returns `byte` item at `index`. If index is invalid or + * stores a non-`byte` value, returns `defaultValue`. + * + * Referred value must be stored as `ByteBox` or `ByteRef` + * (or one of their sub-classes) for this method to work. + * + * @param index Index of a `byte` item that `DynamicArray` + * has to return. + * @param defaultValue Value to return if there is either no item recorded + * at `index` or it has a wrong type. + * @return `byte` value at `index` in the caller `DynamicArray`. + * `defaultValue` if passed `index` is invalid or non-`byte` value + * is stored there. + */ +public final function byte GetByte(int index, optional byte defaultValue) +{ + local AcediaObject result; + local ByteBox asBox; + local ByteRef asRef; + result = GetItem(index); + if (result == none) { + return defaultValue; + } + asBox = ByteBox(result); + if (asBox != none) { + return asBox.Get(); + } + asRef = ByteRef(result); + if (asRef != none) { + return asRef.Get(); + } + return defaultValue; +} + +/** + * Changes `DynamicArray`'s value at `index` to `value` that will be recorded + * as either `ByteBox` or `ByteRef`, depending of `asRef` optional parameter. + * + * Inserted value will always be recorded as a managed value, i.e. it will be + * automatically deallocated when overwritten, removed or caller `DynamicArray` + * is deallocated. + * + * @param index Index, at which to change the value. If `DynamicArray` is + * not long enough to hold it, it will be automatically expanded. + * If passed index is negative - method will do nothing. + * @param value Value to be set at a given index. + * @param asRef Given `byte` value will be recorded as immutable `ByteBox` + * by default (`asRef == false`). Setting this parameter to `true` will + * make this method record it as a mutable `ByteRef`. + * @return Reference to the caller `DynamicArray` to allow for method chaining. + */ +public final function DynamicArray SetByte( + int index, + byte value, + optional bool asRef) +{ + if (asRef) { + SetItem(index, _.ref.byte(value), true); + } + else { + SetItem(index, _.box.byte(value), true); + } + return self; +} + +/** + * Returns `int` item at `index`. If index is invalid or + * stores a non-`int` value, returns `defaultValue`. + * + * Referred value must be stored as `IntBox` or `IntRef` + * (or one of their sub-classes) for this method to work. + * + * @param index Index of a `int` item that `DynamicArray` + * has to return. + * @param defaultValue Value to return if there is either no item recorded + * at `index` or it has a wrong type. + * @return `int` value at `index` in the caller `DynamicArray`. + * `defaultValue` if passed `index` is invalid or non-`int` value + * is stored there. + */ +public final function int GetInt(int index, optional int defaultValue) +{ + local AcediaObject result; + local IntBox asBox; + local IntRef asRef; + result = GetItem(index); + if (result == none) { + return defaultValue; + } + asBox = IntBox(result); + if (asBox != none) { + return asBox.Get(); + } + asRef = IntRef(result); + if (asRef != none) { + return asRef.Get(); + } + return defaultValue; +} + +/** + * Changes `DynamicArray`'s value at `index` to `value` that will be recorded + * as either `IntBox` or `IntRef`, depending of `asRef` optional parameter. + * + * Inserted value will always be recorded as a managed value, i.e. it will be + * automatically deallocated when overwritten, removed or caller `DynamicArray` + * is deallocated. + * + * @param index Index, at which to change the value. If `DynamicArray` is + * not long enough to hold it, it will be automatically expanded. + * If passed index is negative - method will do nothing. + * @param value Value to be set at a given index. + * @param asRef Given `int` value will be recorded as immutable `IntBox` + * by default (`asRef == false`). Setting this parameter to `true` will + * make this method record it as a mutable `IntRef`. + * @return Reference to the caller `DynamicArray` to allow for method chaining. + */ +public final function DynamicArray SetInt( + int index, + int value, + optional bool asRef) +{ + if (asRef) { + SetItem(index, _.ref.int(value), true); + } + else { + SetItem(index, _.box.int(value), true); + } + return self; +} + +/** + * Returns `float` item at `index`. If index is invalid or + * stores a non-`int` value, returns `defaultValue`. + * + * Referred value must be stored as `FloatBox` or `FloatRef` + * (or one of their sub-classes) for this method to work. + * + * @param index Index of a `float` item that `DynamicArray` + * has to return. + * @param defaultValue Value to return if there is either no item recorded + * at `index` or it has a wrong type. + * @return `float` value at `index` in the caller `DynamicArray`. + * `defaultValue` if passed `index` is invalid or non-`float` value + * is stored there. + */ +public final function float GetFloat(int index, optional float defaultValue) +{ + local AcediaObject result; + local FloatBox asBox; + local FloatRef asRef; + result = GetItem(index); + if (result == none) { + return defaultValue; + } + asBox = FloatBox(result); + if (asBox != none) { + return asBox.Get(); + } + asRef = FloatRef(result); + if (asRef != none) { + return asRef.Get(); + } + return defaultValue; +} + +/** + * Changes `DynamicArray`'s value at `index` to `value` that will be recorded + * as either `FloatBox` or `FloatRef`, depending of `asRef` optional parameter. + * + * Inserted value will always be recorded as a managed value, i.e. it will be + * automatically deallocated when overwritten, removed or caller `DynamicArray` + * is deallocated. + * + * @param index Index, at which to change the value. If `DynamicArray` is + * not long enough to hold it, it will be automatically expanded. + * If passed index is negative - method will do nothing. + * @param value Value to be set at a given index. + * @param asRef Given `float` value will be recorded as immutable `FloatBox` + * by default (`asRef == false`). Setting this parameter to `true` will + * make this method record it as a mutable `FloatRef`. + * @return Reference to the caller `DynamicArray` to allow for method chaining. + */ +public final function DynamicArray SetFloat( + int index, + float value, + optional bool asRef) +{ + if (asRef) { + SetItem(index, _.ref.float(value), true); + } + else { + SetItem(index, _.box.float(value), true); + } + return self; +} + +/** + * Returns `Text` item at `index`. If index is invalid or + * stores a non-`Text` value, returns `none`. + * + * Referred value must be stored as `Text` (or one of it's sub-classes, + * such as `MutableText`) for this method to work. + * + * @param index Index of a `Text` item that `DynamicArray` has to return. + * @return `Text` value at `index` in the caller `DynamicArray`. + * `none` if passed `index` is invalid or non-`Text` value + * is stored there. + */ +public final function Text GetText(int index) +{ + return Text(GetItem(index)); +} + +/** + * Returns `AssociativeArray` item at `index`. If index is invalid or + * stores a non-`AssociativeArray` value, returns `none`. + * + * Referred value must be stored as `AssociativeArray` + * (or one of it's sub-classes) for this method to work. + * + * @param index Index of an `AssociativeArray` item that `DynamicArray` + * has to return. + * @return `AssociativeArray` value at `index` in the caller `DynamicArray`. + * `none` if passed `index` is invalid or non-`AssociativeArray` value + * is stored there. + */ +public final function AssociativeArray GetAssociativeArray(int index) +{ + return AssociativeArray(GetItem(index)); +} + +/** + * Returns `DynamicArray` item at `index`. If index is invalid or + * stores a non-`DynamicArray` value, returns `none`. + * + * Referred value must be stored as `Text` (or one of it's sub-classes, + * such as `MutableText`) for this method to work. + * + * @param index Index of a `DynamicArray` item that caller `DynamicArray` + * has to return. + * @return `DynamicArray` value at `index` in the caller `DynamicArray`. + * `none` if passed `index` is invalid or non-`DynamicArray` value + * is stored there. + */ +public final function DynamicArray GetDynamicArray(int index) +{ + return DynamicArray(GetItem(index)); +} + defaultproperties { iteratorClass = class'DynamicArrayIterator'