blocxx
Classes | Public Member Functions | Public Attributes

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

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

#include <GlobalPtr.hpp>

List of all members.

Classes

struct  InitPtr

Public Member Functions

T * get () const
 Gets the current value of the pointer.
T * set (T *newP)
 Sets the pointer.
 BLOCXX_SAFE_BOOL_IMPL (GlobalPtr, T *, GlobalPtr::m_p, get()!=0)
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 Function Documentation

template<typename T , typename FactoryT = DefaultConstructorFactory<T>>
BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::BLOCXX_SAFE_BOOL_IMPL ( GlobalPtr< T, FactoryT >  ,
T *  ,
GlobalPtr< T, FactoryT >::m_p  ,
get()!  = 0 
)
template<typename T , typename FactoryT = DefaultConstructorFactory<T>>
T* BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::get ( ) const [inline]

Gets the current value of the pointer.

Returns:
T*

Definition at line 105 of file GlobalPtr.hpp.

References BLOCXX_NAMESPACE::callOnce(), BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::m_onceFlag, and BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::m_p.

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

Definition at line 127 of file GlobalPtr.hpp.

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

Definition at line 131 of file GlobalPtr.hpp.

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.

References BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::m_p.


Member Data Documentation

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

Definition at line 139 of file GlobalPtr.hpp.

Referenced by BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::get().

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: