Does not inherit
#include rwsf/core/CountingPointer.h
A smart pointer class that is intended for use as a handle to a reference-counted body. Each time an instance of this class is bound to a body instance, it increments a reference count associated with that body. Each time it detaches from a body instance, it decrements the body's associated reference count; and if the reference count reaches zero, it deletes the body instance. The reference counting relieves clients of the burden of having to keep track of when it is safe to delete a body instance.
rwsf::CountingPointer is a non-intrusive smart pointer type, which maintains a parallel counter to the body. For a class that assumes the body maintains the count, see rwsf::RefCountingPointer.
T * PointerType;
Typedef to the underlying raw pointer type.
T & ReferenceType;
Typedef to the underlying reference type.
CountingPointer(PointerType pointer = 0, AtomicCounter * counterP = 0);
Constructs a pointer instance that manages references for the specified body instance. If pointer is null (the default case), an invalid CountingPointer instance is created, and counterP is ignored. If counterP is null (the default case), a new AtomicCounter instance will be created with an initial count of one.
If the CountingPointer fails to initialize, and counterP is null, the memory associated with pointer will be deallocated in order to prevent memory leaks. If counterP is non-null, the pointer will not be affected, and the user will be responsible for releasing any associated resources.
Parameter descriptions
pointer |
The T instance to be managed by this pointer. |
counterP |
An atomic counter representing the count on the referenced object. |
CountingPointer(const CountingPointer< T > & second);
Attaches to and increments the reference-count on second's body.
~CountingPointer(void);
Decrements the body's reference-count and deletes it if there are no other references.
PointerType get(void) const;
Retrieves the handle's pointer value without validating it. Throws no exceptions.
Note
The memory associated with this pointer may be deallocated if the counting pointer is instructed to release it. In general, using this method is not recommended.
Deprecated.
bool isValid(void) const;
Returns true if pointer is non-null, otherwise returns false. Throws no exceptions.
Deprecated. use conversion to unspecified bool type instead.
void swapWith(CountingPointer< T > & second);
Swap bodies (if any) with another handle. Throws no exceptions.
Deprecated.
void validate(void) const;
Checks if the pointer is null, and throws an rwsf::NullPointerException if it is.
Deprecated. Use operator*() instead.
ReferenceType operator *(void) const;
Verifies that the underlying is not null, dereferences, and returns the underlying body.
Exceptions
NullPointerException |
If the underlying pointer is null. |
CountingPointer< T > & operator=(const CountingPointer< T > & second);
Attaches to second's body, and increments it's reference-count. If there was already a body associated with this, it's count is decremented, and the body is deleted if there are no other references.
CountingPointer< T > & operator=(PointerType pointer);
Attaches to second's body, and increments it's reference-count. If there was already a body associated with this, it's count is decremented, and the body is deleted if there are no other references.
PointerType operator->(void) const;
Verifies that the underlying body is not null, and returns an instance of the underlying body.
Exceptions
NullPointerException |
If the underlying pointer is null. |
operator CountingPointer();
Conversion operator. Tries to return the contained pointer as a pointer of type O. If the conversion is not possible, returns an invalid CountingPointer.
The automatic dynamic_cast associated with this method may be removed in a future release, and should not be relied upon in user code. Users can define the macro RWSF_CORE_DISABLE_DEPRECATED before including this header file in order to check their code for conversions that should use an explicit cast.
Note
This method performs a dynamic_cast on O in order to convert it to a type T. This may be an expensive operation if a dynamic_cast is not needed for the conversion. It is recommended that users use the explicit cast methods associated with this class in order to convert between different CountingPointer types.
operator typename CountingPointer() const;
Returns an unspecified boolean type to allow for testing if the associated pointer is null. The boolean type is not specified in order to avoid unintended implicit conversions.
© Copyright Rogue Wave Software, Inc. All Rights Reserved. All Rights Reserved. Rogue Wave is a registered trademark of Rogue Wave Software, Inc. in the United States and other countries. HydraExpress is a trademark of Rogue Wave Software, Inc. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.