blocxx
|
This class can be used to store a global pointer. More...
#include <GlobalPtr.hpp>
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 |
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.
T | The type of the pointer. |
FactoryT | To 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.
BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::BLOCXX_SAFE_BOOL_IMPL | ( | GlobalPtr< T, FactoryT > | , |
T * | , | ||
GlobalPtr< T, FactoryT >::m_p | , | ||
get()! | = 0 |
||
) |
T* BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::get | ( | ) | const [inline] |
Gets the current value of the pointer.
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.
T& BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::operator* | ( | ) | const [inline] |
Definition at line 127 of file GlobalPtr.hpp.
T* BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::operator-> | ( | ) | const [inline] |
Definition at line 131 of file GlobalPtr.hpp.
T* BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::set | ( | T * | newP | ) | [inline] |
Sets the pointer.
newP | the new pointer. |
Definition at line 118 of file GlobalPtr.hpp.
References BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::m_p.
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().
T* BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::m_p [mutable] |
Definition at line 138 of file GlobalPtr.hpp.
Referenced by BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::get(), and BLOCXX_NAMESPACE::GlobalPtr< T, FactoryT >::set().