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) |
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. Still, if the value has not been initialized yet and if multiple threads happen to call operator*()
at the same time, then the initializer
lambda-function may be invoked more than once (concurrently and by different threads). In that case, all but one return value of the initializer
lambda-function are discarded, and all threads eventually obtain the same value/object.