#include <lazyinit.h>
Thread-safe helper class for single-threaded lazy initialization.
Typical usage case is a method which initializes data on first call, for example
THREADSAFE.
Public Member Functions | |
template<typename FN > | |
MAXON_ATTRIBUTE_FORCE_INLINE auto | Init (FN &&fn) -> decltype(fn()) |
template<typename FN = decltype(Dummy)> | |
void | Reset (FN &&fn=Dummy) |
Bool | IsInitialized () const |
operator Bool () const | |
Bool | IsPendingInitialization () const |
Private Types | |
enum class | STATE : Int32 { UNINITIALIZED , PENDING , INITIALIZED } |
Private Member Functions | |
enum maxon::LazyInit::STATE Int32 | MAXON_ENUM_LIST_CLASS (STATE) |
Static Private Member Functions | |
static void | Dummy () |
static Bool | ToBool (Bool v) |
static Bool | ToBool (ResultMem v) |
static Bool | ToBool (const Result< void > &v) |
Private Attributes | |
AtomicInt32 | _state |
|
private |
|
staticprivate |
MAXON_ATTRIBUTE_FORCE_INLINE auto Init | ( | FN && | fn | ) | -> decltype(fn()) |
Initializes an object by calling the specified method (and does nothing if the object has already been initialized). The method #fn will be executed on the current thread and must be executing quickly. It is not allowed to perform multithreaded code as part of the initialization because this could deadlock and waiting threads would be busy-idling.
[in] | fn | Method (usually a lambda) to initialize something, must return a Bool or a Result<void>. |
void Reset | ( | FN && | fn = Dummy | ) |
Resets an object by calling the specified method. Does nothing if the object has already been reset. THREADSAFE.
[in] | fn | Optional method (usually a lambda) to reset something. |
Bool IsInitialized | ( | ) | const |
Returns if the object already has been initialized. THREADSAFE.
|
explicit |
Returns if the object already has been initialized. THREADSAFE.
Bool IsPendingInitialization | ( | ) | const |
Returns if the object is currently being initialized (for debugging) THREADSAFE.
|
private |