MuldeR's Utilities for Qt
MUtilities
|
Lazy initialization template class. More...
#include <Lazy.h>
Public Member Functions | |
Lazy (std::function< T *(void)> &&initializer) | |
T & | operator* (void) |
T * | operator-> (void) |
bool | initialized () |
Protected Member Functions | |
__forceinline T * | getValue () |
Lazy initialization template class.
The lazy-initialized value of type T can be obtained from a Lazy<T>
instance by using the operator*()
. Initialization of the value happens when the operator*()
is called for the very first time, by invoking the initializer
lambda-function that was passed to the constructor. The return value of the initializer
lambda-function is then stored internally, so that any subsequent call to the operator*()
immediately returns the previously created value.
Note on thread-saftey: This class is thread-safe in the sense that all calls to operator*()
on the same Lazy<T>
instance, regardless from which thread, are guaranteed to return the exactly same value/object. The first thread trying to access the value will invoke the initializer
lambda-function; concurrent threads may need to busy-wait until the initialization is completed. The initializer
lambda-function is invoked at most once.