MuldeR's Utilities for Qt
MUtilities
|
Lazy initialization template class. More...
#include <Lazy.h>
Public Member Functions | |
T & | operator* (void) |
Protected Member Functions | |
virtual T * | create ()=0 |
Lazy initialization template class.
In order to create your own "lazy" initializer, inherit from the Lazy<T>
class an implement the create() function. The lazy-initialized value 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 concrete create() function. The return value of create() 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 concrete create() function may be invoked more than once (concurrently and by different threads). In that case, all but one return value of create() are discarded, and all threads eventually receive the same value/object.