LazyInit Class Reference

#include <lazyinit.h>

Detailed Description

Thread-safe helper class for single-threaded lazy initialization.

Typical usage case is a method which initializes data on first call, for example

class Sample
{
public:
MyObject* GetObject()
{
_state.Init(
[this]() -> Bool
{
// Your init code goes here.
// Return true if initialization was successful and false if it failed.
// Using type Result<void> instead of Bool for the return value is supported as well.
return true;
});
return _object;
}
private:
LazyInit _state;
MyObject* _object;
SomeMoreData _xyz;
};
AtomicInt32 _state
Definition: lazyinit.h:173
maxon::Bool Bool
Definition: ge_sys_math.h:55
Definition: object.h:105
Note
The code of the lambda must be single threaded, using multithreaded code will lead to a deadlock! For potentially multithreaded initialization use LazyInitThreaded. Performance note: Declaring a global LazyInit as a static member of a class will degrade its performance because the compiler will guard its access with a slow and unnecessary mutex. To avoid this move the global state outside of the class.

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
 

Member Enumeration Documentation

◆ STATE

enum STATE : Int32
strongprivate
Enumerator
UNINITIALIZED 
PENDING 
INITIALIZED 

Member Function Documentation

◆ MAXON_ENUM_LIST_CLASS()

enum maxon::LazyInit::STATE Int32 MAXON_ENUM_LIST_CLASS ( STATE  )
private

◆ Dummy()

static void Dummy ( )
staticprivate

◆ ToBool() [1/3]

static Bool ToBool ( Bool  v)
staticprivate

◆ ToBool() [2/3]

static Bool ToBool ( ResultMem  v)
staticprivate

◆ ToBool() [3/3]

static Bool ToBool ( const Result< void > &  v)
staticprivate

◆ Init()

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.

Note
The code of the lambda must be single threaded! Otherwise use LazyInitThreaded (and review your code thoroughly). THREADSAFE.
Parameters
[in]fnMethod (usually a lambda) to initialize something, must return a Bool or a Result<void>.
Returns
True/OK if initialization was successful or object has already been initialized, otherwise result of failed initialization.

◆ Reset()

void Reset ( FN &&  fn = Dummy)

Resets an object by calling the specified method. Does nothing if the object has already been reset. THREADSAFE.

Parameters
[in]fnOptional method (usually a lambda) to reset something.

◆ IsInitialized()

Bool IsInitialized ( ) const

Returns if the object already has been initialized. THREADSAFE.

Returns
True if initialization was successful.

◆ operator Bool()

operator Bool ( ) const
explicit

Returns if the object already has been initialized. THREADSAFE.

Returns
True if initialization was successful.

◆ IsPendingInitialization()

Bool IsPendingInitialization ( ) const

Returns if the object is currently being initialized (for debugging) THREADSAFE.

Returns
True if the tinitialization is pending.

Member Data Documentation

◆ _state

AtomicInt32 _state
private