You.i Engine
CYILazy< YI_CLASS > Class Template Reference

Detailed Description

template<class YI_CLASS>
class CYILazy< YI_CLASS >

CYILazy is a thread-safe lazy initializer of a class. The instance pointer remains null until CYILazy::Get() is called, which will trigger a thread-safe initialization of the instance. Also, a CYILazy::TryGet() is available, which will return the actual value while not instantiating, providing a convenient way to test for nullptr in the event that the user does not want the instantiation to occur.

Usage example:

// If used for the first time, this line will instantiate and lock the mutex.
lazyMutex1.Get()->Lock();
std::mutex *pMutex = lazyMutex2.TryGet();
if (pMutex)
{
pMutex->lock();
// ...
pMutex->unlock();
}
Note
It is always advisable to reuse the pointer returned from CYILazy::Get() CYILazy::TryGet() from within your function scope because there is a the cost of the cache-miss due to the volatile data store of the atomic pointer, which will happened everytime CYILazy::TryGet() is invoked, as long as it is not stored because the pointer itself is not thread-safe as it is not declared as volatile. Please refer to the usage exmple above.

#include <utility/YiLazy.h>

Inheritance diagram for CYILazy< YI_CLASS >:

Public Member Functions

 CYILazy ()
 
 ~CYILazy ()
 
const YI_CLASS * Get () const
 
YI_CLASS * Get ()
 
const YI_CLASS * TryGet () const
 
YI_CLASS * TryGet ()
 

Constructor & Destructor Documentation

◆ CYILazy()

template<class YI_CLASS >
CYILazy< YI_CLASS >::CYILazy ( )
inline

Default constructor.

◆ ~CYILazy()

template<class YI_CLASS >
CYILazy< YI_CLASS >::~CYILazy ( )
inline

Destructor.

Member Function Documentation

◆ Get() [1/2]

template<class YI_CLASS >
const YI_CLASS * CYILazy< YI_CLASS >::Get ( ) const
inline

This getter returns the pointer of the instance. The instance will also be instantiated if not instantiated.

Note
This function is thread-safe.
Warning
More than one instances could be allocated if more than one thread calls Get() in parallel when the internal instance is null. Only one thread's created instance will be retained while the other instances will be deallocated.

◆ Get() [2/2]

template<class YI_CLASS >
YI_CLASS * CYILazy< YI_CLASS >::Get ( )
inline

This getter returns the pointer of the instance. The instance will also be instantiated if not instantiated.

Note
This function is thread-safe.
Warning
More than one instances could be allocated if more than one thread calls Get() in parallel when the internal instance is null. Only one thread's created instance will be retained while the other instances will be deallocated.

◆ TryGet() [1/2]

template<class YI_CLASS >
const YI_CLASS * CYILazy< YI_CLASS >::TryGet ( ) const
inline

This getter returns the pointer of the instance. The instance will not be instantiated and may return nullptr.

◆ TryGet() [2/2]

template<class YI_CLASS >
YI_CLASS * CYILazy< YI_CLASS >::TryGet ( )
inline

This getter returns the pointer of the instance. The instance will not be instantiated and may return nullptr.


The documentation for this class was generated from the following files: