c++-gtk-utils
|
This is a smart pointer for managing the lifetime of objects allocated on freestore, with a thread safe reference count. More...
#include <c++-gtk-utils/shared_ptr.h>
Public Member Functions | |
SharedLockPtr (T *ptr=0) | |
SharedLockPtr (T *ptr, Cgu::SharedPtrAllocFail::Leave tag) | |
void | reset (T *ptr=0) |
void | reset (T *ptr, Cgu::SharedPtrAllocFail::Leave tag) |
SharedLockPtr (const SharedLockPtr &sh_ptr) | |
template<class U > | |
SharedLockPtr (const SharedLockPtr< U > &sh_ptr) | |
SharedLockPtr & | operator= (SharedLockPtr sh_ptr) |
template<class U > | |
SharedLockPtr & | operator= (const SharedLockPtr< U > &sh_ptr) |
T * | get () const |
T & | operator* () const |
T * | operator-> () const |
unsigned int | get_refcount () const |
~SharedLockPtr () | |
Friends | |
template<class U > | |
class | SharedLockPtr |
This is a smart pointer for managing the lifetime of objects allocated on freestore, with a thread safe reference count.
Class SharedLockPtr is a version of the shared pointer class which includes synchronization so that it can handle objects accessed in multiple threads (although the word Lock is in the title, by default it uses glib atomic functions to access the reference count rather than a mutex, so the overhead should be very small). Note that only the reference count is protected, so this is thread safe in the sense in which a raw pointer is thread safe. A shared pointer accessed in one thread referencing a particular object is thread safe as against another shared pointer accessing the same object in a different thread. It is thus suitable for use in different standard C++ containers which exist in different threads but which contain shared objects by reference. But:
As mentioned, by default glib atomic functions are used to provide thread-safe manipulation of the reference count. However, from version 1.2.0 the symbol CGU_SHARED_LOCK_PTR_USE_MUTEX can be defined so that the library uses mutexes instead, which might be useful for some debugging purposes. Note that if CGU_SHARED_LOCK_PTR_USE_MUTEX is to be defined, this is best done by textually amending the shared_ptr.h header file before the library is compiled. This will ensure that everything in the program and the library which includes the shared_ptr.h header is guaranteed to see the same definitions so that the C++ standard's one-definition-rule is complied with.
From version 1.2.12, the library provides ==, != and < comparison operators for SharedLockPtr, but only if the library is compiled with the --with-smart-ptr-comp option, or if the user code defines the symbol CGU_USE_SMART_PTR_COMPARISON before shared_ptr.h is first parsed. This is because, if user code has provided such operators for these smart pointers itself, a duplicated function definition would arise.
If the library is compiled with the --with-glib-memory-slices-no-compat configuration option, Cgu::SharedLockPtr constructs its reference counting internals using glib memory slices. Although it is safe in a multi-threaded program if glib < 2.32 is installed to construct a static SharedLockPtr object in global namespace (that is, prior to g_thread_init() being called) by means of the default constructor and/or a pointer argument of NULL, it is not safe if constructed with a non-NULL pointer value. If glib >= 2.32 is installed, global objects with memory slices are safe in all circumstances. (Having said that, it would be highly unusual to have global SharedLockPtr objects.)
|
inlineexplicit |
Constructor taking an unmanaged object.
ptr | The object which the SharedLockPtr is to manage (if any). |
std::bad_alloc | This constructor will not throw if the 'ptr' argument has a NULL value (the default), otherwise it might throw std::bad_alloc if memory is exhausted and the system throws in that case. If such an exception is thrown, this constructor is exception safe (it does not leak resources), but as well as cleaning itself up this constructor will also delete the managed object passed to it to avoid a memory leak. If such automatic deletion is not wanted in that case, use the version of this constructor taking a Cgu::SharedPtrAllocFail::Leave tag argument. |
|
inline |
Constructor taking an unmanaged object.
ptr | The object which the SharedLockPtr is to manage. |
tag | Passing the tag emumerator Cgu::SharedPtrAllocFail::leave causes this constructor not to delete the new managed object passed as the 'ptr' argument in the event of internal allocation in this method failing because of memory exhaustion (in that event, Cgu::SharedPtrError will be thrown). |
Cgu::SharedPtrError | This constructor might throw Cgu::SharedPtrError if memory is exhausted and the system would otherwise throw std::bad_alloc in that case. This constructor is exception safe (it does not leak resources), and if such an exception is thrown it will clean itself up, but it will not attempt to delete the new managed object passed to it. Access to the object passed to the 'ptr' argument can be obtained via the thrown Cgu::SharedPtrError object. |
Since 0.9.1
|
inline |
This copy constructor does not throw.
sh_ptr | The shared pointer to be copied. |
|
inline |
A version of the copy constructor which enables pointer type conversion (assuming the type passed is implicitly type convertible to the managed type, such as a derived type). This copy constructor does not throw.
sh_ptr | The shared pointer to be copied. |
|
inline |
The destructor does not throw unless the destructor of a managed object throws - that should never happen.
|
inline |
This method does not throw.
|
inline |
This method does not throw.
|
inline |
This method does not throw.
|
inline |
This method does not throw.
|
inline |
A version of the assignment operator which enables pointer type conversion (assuming the type passed is implicitly type convertible to the managed type, such as a derived type). This method does not throw unless the destructor of a managed object throws.
sh_ptr | the assignor. |
|
inline |
This method does not throw unless the destructor of a managed object throws.
sh_ptr | the assignor. |
|
inline |
Causes the SharedLockPtr to cease to manage its managed object (if any), deleting it if this is the last SharedLockPtr object managing it. The SharedLockPtr object will manage the new object passed (which must not be managed by any other SharedLockPtr object). This method is exception safe, but see the comments below on Cgu::SharedPtrError.
ptr | A new unmanaged object to manage (if no new object is to be managed, use the version of reset() taking a default value of NULL). |
tag | Passing the tag emumerator Cgu::SharedPtrAllocFail::leave causes this method not to delete the new managed object passed as the 'ptr' argument in the event of internal allocation in this method failing because of memory exhaustion (in that event, Cgu::SharedPtrError will be thrown). |
Cgu::SharedPtrError | This method might throw Cgu::SharedPtrError if memory is exhausted and the system would otherwise throw std::bad_alloc in that case. Note that if such an exception is thrown then this method will do nothing (it is strongly exception safe and will continue to manage the object it was managing prior to the call), and it will not attempt to delete the new managed object passed to it. Access to the object passed to the 'ptr' argument can be obtained via the thrown Cgu::SharedPtrError object. |
Since 0.9.1
|
inline |
Causes the SharedLockPtr to cease to manage its managed object (if any), deleting it if this is the last SharedLockPtr object managing it. If the argument passed is not NULL, the SharedLockPtr object will manage the new object passed (which must not be managed by any other SharedLockPtr object). This method is exception safe, but see the comments below on std::bad_alloc.
ptr | NULL (the default), or a new unmanaged object to manage. |
std::bad_alloc | This method will not throw if the 'ptr' argument has a NULL value (the default) and the destructor of a managed object does not throw, otherwise it might throw std::bad_alloc if memory is exhausted and the system throws in that case. Note that if such an exception is thrown then this method will do nothing (it is strongly exception safe and will continue to manage the object it was managing prior to the call), except that it will delete the new managed object passed to it to avoid a memory leak. If such automatic deletion in the event of such an exception is not wanted, use the reset() method taking a Cgu::SharedPtrAllocFail::Leave tag type as its second argument. |
Since 0.9.1