LazyInit Class Reference 线程
#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
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 ; });THREADSAFE.
公共成员函数 |
|
template<typename FN > | |
MAXON_ATTRIBUTE_FORCE_INLINE auto | Init (FN &&fn) -> decltype(fn()) |
template<typename FN = decltype(Dummy)> | |
void | 重置 (FN &&fn= Dummy ) |
Bool | IsInitialized () const |
operator Bool () const |
Private Types |
|
enum |
STATE
: Int32 {
UNINITIALIZED , PENDING , INITIALIZED } |
私有成员函数 |
|
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 |
|
strong private |
Enumerator | |
---|---|
UNINITIALIZED | |
PENDING | |
INITIALIZED |
|
private |
|
static private |
|
static private |
|
static private |
|
static private |
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.
|
private |