From 8e17324a5eb3a2cb1fc9f665580461a72938f556 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Mon, 8 Mar 2021 01:25:43 +0700 Subject: [PATCH] Refactor static constructors There wasn't actually any need for `InitializeStatic()` method. In fact it could lead to problems. Now calling `StaticCosntructor()` is allowed instead. --- docs/introduction/ObjectsActors.md | 5 +---- sources/Global.uc | 2 +- sources/Types/AcediaActor.uc | 34 +++++++++++++----------------- sources/Types/AcediaObject.uc | 21 +++--------------- sources/Types/Tests/TEST_Base.uc | 2 +- 5 files changed, 21 insertions(+), 43 deletions(-) diff --git a/docs/introduction/ObjectsActors.md b/docs/introduction/ObjectsActors.md index ab1b67f..e7f306c 100644 --- a/docs/introduction/ObjectsActors.md +++ b/docs/introduction/ObjectsActors.md @@ -21,10 +21,7 @@ Implementing and enforcing (de)allocation allowed us to introduce constructors t To make use of them one can simply overload protected methods `Constructor()` and `Finalizer()`. Parametrized constructors are not supported. -Static constructors are also supported (and can be used by overloading `StaticConstructor()`), - they are methods that take care of some initialization work before any instance of the given class is created. They are called only once, at the earliest of the following two events: - -1. An instance of relevant class (or it's child class) is created; -2. Public `InitializeStatic()` method was called to force it early. +Static constructors are also supported (and can be used by overloading `StaticConstructor()`), - they are methods that take care of some initialization work before any instance of the given class is created. They are called only once, at the latest when an instance of relevant class (or it's child class) is created. `StaticConstructor()` is allowed to be called early to initialize static members of a certain class before creating it's instances. ### General collections diff --git a/sources/Global.uc b/sources/Global.uc index a0dfa61..da1c9e1 100644 --- a/sources/Global.uc +++ b/sources/Global.uc @@ -65,5 +65,5 @@ protected function Initialize() color = ColorAPI(memory.Allocate(class'ColorAPI')); users = UserAPI(memory.Allocate(class'UserAPI')); json = JSONAPI(memory.Allocate(class'JSONAPI')); - json.InitializeStatic(); + json.StaticConstructor(); } \ No newline at end of file diff --git a/sources/Types/AcediaActor.uc b/sources/Types/AcediaActor.uc index df43dec..126314d 100644 --- a/sources/Types/AcediaActor.uc +++ b/sources/Types/AcediaActor.uc @@ -68,9 +68,7 @@ var private bool _hashCodeWasCached; var private TextCache _textCache; // Formatted `strings` declared in this array will be converted into `Text`s -// available via `T()` method when static constructor is called -// (either when first object of this class is created or -// `InitializeStatic()` method is called) +// available via `T()` method when static constructor is called. var protected const array stringConstants; /** @@ -116,6 +114,10 @@ public function _constructor() default._ = class'Global'.static.GetInstance(); _ = default._; } + if (!default._staticConstructorWasCalled) { + StaticConstructor(); + default._staticConstructorWasCalled = true; + } _hashCodeWasCached = false; Constructor(); } @@ -151,19 +153,6 @@ protected final static function bool StaticConstructorGuard() return true; } -/** - * Method that can cause early static constructor call for a caller class. - * - * Normally static constructor is called the first time an instance of a class - * (or it's child class) is created, but this method can be used to cause this - * initialization early. - */ -public final static function InitializeStatic() -{ - if (StaticConstructorGuard()) return; - StaticConstructor(); -} - /** * When using proper methods for creating actors (`MemoryAPI`), * this method is guaranteed to be called after actor is spawned, @@ -185,7 +174,7 @@ protected function Constructor(){} * |___________________________________________________________________________ * otherwise behavior of constructors should be considered undefined. */ -protected static function StaticConstructor() +public static function StaticConstructor() { local int i; local array stringConstantsCopy; @@ -323,9 +312,16 @@ public final function int GetLifeVersion() * * You can define array `stringConstants` (of `string`s) in `defaultproperties` * that will statically be converted into `Text` objects first time an object - * of that class is created or (`InitializeStatic()` method is called). + * of that class is created or `StaticConstructor()` method is called manually. + * + * Provided that returned values are not deallocated, they always refer to + * the same `Text` object for any fixed `index`. + * Otherwise new `Text` object can be allocated. * - * `Text` instances + * @param index Index for which to return `Text` instance. + * @return `Text` instance containing the data in a `stringConstants[index]`. + * `none` if either `index < 0` or `index >= stringConstants.length`, + * otherwise guaranteed to be not `none`. */ public static final function Text T(int index) { diff --git a/sources/Types/AcediaObject.uc b/sources/Types/AcediaObject.uc index 0c8567d..55a08b8 100644 --- a/sources/Types/AcediaObject.uc +++ b/sources/Types/AcediaObject.uc @@ -69,9 +69,7 @@ var private bool _hashCodeWasCached; var private TextCache _textCache; // Formatted `strings` declared in this array will be converted into `Text`s -// available via `T()` method when static constructor is called -// (either when first object of this class is created or -// `InitializeStatic()` method is called) +// available via `T()` method when static constructor is called. var protected const array stringConstants; /** @@ -156,19 +154,6 @@ protected final static function bool StaticConstructorGuard() return true; } -/** - * Method that can cause early static constructor call for a caller class. - * - * Normally static constructor is called the first time an instance of a class - * (or it's child class) is created, but this method can be used to cause this - * initialization early. - */ -public final static function InitializeStatic() -{ - if (default._staticConstructorWasCalled) return; - StaticConstructor(); -} - /** * When using proper methods for creating objects (`MemoryAPI`), * this method is guaranteed to be called after object is allocated, @@ -190,7 +175,7 @@ protected function Constructor(){} * |___________________________________________________________________________ * otherwise behavior of constructors should be considered undefined. */ -protected static function StaticConstructor() +public static function StaticConstructor() { local int i; local array stringConstantsCopy; @@ -342,7 +327,7 @@ public final function int GetLifeVersion() * * You can define array `stringConstants` (of `string`s) in `defaultproperties` * that will statically be converted into `Text` objects first time an object - * of that class is created or (`InitializeStatic()` method is called). + * of that class is created or `StaticConstructor()` method is called manually. * * Provided that returned values are not deallocated, they always refer to * the same `Text` object for any fixed `index`. diff --git a/sources/Types/Tests/TEST_Base.uc b/sources/Types/Tests/TEST_Base.uc index 60c256a..689b30d 100644 --- a/sources/Types/Tests/TEST_Base.uc +++ b/sources/Types/Tests/TEST_Base.uc @@ -24,7 +24,7 @@ class TEST_Base extends TestCase protected static function TESTS() { // Use test case itself to test `Text`-returning methods - InitializeStatic(); + StaticConstructor(); Test_QuickText(); Test_Constants(); }