This repository has been archived on 2022-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
Acedia/sources/Core/Singleton.uc

96 lines
3.3 KiB
Ucode
Raw Normal View History

2020-02-16 15:41:27 +03:00
/**
* Singleton is an auxiliary class, meant to be used as a base for others,
* that allows for only one instance of it to exist.
* To make sure your child class properly works, either don't overload
* 'PreBeginPlay' or make sure to call it's parent's version.
* Copyright 2019 Anton Tarasenko
*------------------------------------------------------------------------------
* This file is part of Acedia.
*
* Acedia is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License, or
* (at your option) any later version.
*
* Acedia is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Acedia. If not, see <https://www.gnu.org/licenses/>.
*/
class Singleton extends AcediaActor
2020-02-16 15:41:27 +03:00
abstract;
// Default value of this variable will store one and only existing version
2020-02-16 15:41:27 +03:00
// of actor of this class.
var private Singleton activeInstance;
// Setting default value of this variable to 'true' prevents creation of
// a singleton, even if no instances of it exist.
// Only a default value is ever used.
var protected bool blockSpawning;
public final static function Singleton GetInstance()
{
if (default.activeInstance != none && default.activeInstance.bPendingDelete)
return none;
return default.activeInstance;
}
public final static function bool IsSingletonCreationBlocked()
{
return default.blockSpawning;
}
protected function OnCreated(){}
protected function OnDestroyed(){}
2020-02-16 15:41:27 +03:00
// Make sure only one instance of 'Singleton' exists at any point in time.
// Instead of overloading this function we suggest you overload a special
// event function `OnCreated()` that is called whenever a valid `Singleton`
// instance is spawned.
// If you absolutely must overload this function in any child class -
2020-02-16 15:41:27 +03:00
// first call this version of the method and then check if
// you are about to be deleted 'bDeleteMe == true':
// ____________________________________________________________________________
// | super.PreBeginPlay();
// | // ^^^ If singleton wasn't already created, - only after that call
// | // will instance, returned by 'GetInstance()', be set.
// | if (bDeleteMe)
// | return;
// |___________________________________________________________________________
event PreBeginPlay()
{
super.PreBeginPlay();
2020-02-16 15:41:27 +03:00
if (default.blockSpawning || GetInstance() != none)
{
Destroy();
}
else
{
default.activeInstance = self;
OnCreated();
}
}
// Make sure only one instance of 'Singleton' exists at any point in time.
// Instead of overloading this function we suggest you overload a special
// event function `OnDestroyed()` that is called whenever a valid `Singleton`
// instance is destroyed.
// If you absolutely must overload this function in any child class -
// first call this version of the method.
event Destroyed()
{
super.Destroyed();
if (self == GetInstance())
{
OnDestroyed();
2020-02-16 15:41:27 +03:00
}
}
defaultproperties
{
blockSpawning = false
}