blocxx
BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT > Class Template Reference

This class can be used to store a global pointer. More...

#include <GlobalPtr.hpp>

Classes

struct  InitPtr
 

Public Types

typedef T *GlobalPtr::* safe_bool
 

Public Member Functions

T * get () const
 Gets the current value of the pointer.
 
T * set (T *newP)
 Sets the pointer.
 
 operator safe_bool () const
 
bool operator! () const
 
T & operator* () const
 
T * operator-> () const
 

Public Attributes

T * m_p
 
OnceFlag m_onceFlag
 

Detailed Description

template<typename T, typename FactoryT = DefaultConstructorFactory<T>>
class BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >

This class can be used to store a global pointer.

The pointer is lazily constructed. The pointer is never deleted. The intended use is for objects which must always exist for the lifetime of a program, but which may need to be polymorphic and replaced to enable plugging in a mock object for testing.

GlobalPtr is a POD type, and thus must be initialized using POD initialization syntax: GlobalPtr<myType> myGlobalPtr = BLOCXX_GLOBAL_PTR_INIT;

The pointer will be lazily constructed. The first time get() is called, a new T (or derived) will be instantiated by a call to FactoryT::create(). The default type for FactoryT allocates a new instance of T using the default constructor. The initialization is done in a thread safe manner. The double checked locking pattern is used, which doesn't have a large overhead.

The object will never be deleted, so if the destructor needs to do meaningful work, this class shouldn't be used.

All calls to get() are thread safe. Calls to set() are not thread safe.

Parameters
TThe type of the pointer.
FactoryTTo create the T*, FactoryT::create() will be called with no parameters. The return type must be convertible to a T*. The default will return a new T using the default constructor.

Definition at line 83 of file GlobalPtr.hpp.

Member Typedef Documentation

◆ safe_bool

template<typename T, typename FactoryT = DefaultConstructorFactory<T>>
typedef T* GlobalPtr::* BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::safe_bool

Definition at line 125 of file GlobalPtr.hpp.

Member Function Documentation

◆ get()

template<typename T, typename FactoryT = DefaultConstructorFactory<T>>
T * BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::get ( ) const
inline

◆ operator safe_bool()

template<typename T, typename FactoryT = DefaultConstructorFactory<T>>
BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::operator safe_bool ( ) const
inline

Definition at line 125 of file GlobalPtr.hpp.

◆ operator!()

template<typename T, typename FactoryT = DefaultConstructorFactory<T>>
bool BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::operator! ( ) const
inline

Definition at line 125 of file GlobalPtr.hpp.

◆ operator*()

template<typename T, typename FactoryT = DefaultConstructorFactory<T>>
T & BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::operator* ( ) const
inline

Definition at line 127 of file GlobalPtr.hpp.

◆ operator->()

template<typename T, typename FactoryT = DefaultConstructorFactory<T>>
T * BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::operator-> ( ) const
inline

Definition at line 131 of file GlobalPtr.hpp.

◆ set()

template<typename T, typename FactoryT = DefaultConstructorFactory<T>>
T * BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::set ( T * newP)
inline

Sets the pointer.

Parameters
newPthe new pointer.
Returns
T* the previous value.

Definition at line 118 of file GlobalPtr.hpp.

Member Data Documentation

◆ m_onceFlag

template<typename T, typename FactoryT = DefaultConstructorFactory<T>>
OnceFlag BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::m_onceFlag
mutable

Definition at line 139 of file GlobalPtr.hpp.

◆ m_p

template<typename T, typename FactoryT = DefaultConstructorFactory<T>>
T* BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::m_p
mutable

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