dlr::common::ReferenceCount Class Reference

The ReferenceCount class provides a convenient way to track a shared resource so you know when to delete it. More...

#include <referenceCount.h>

List of all members.

Public Member Functions

 ReferenceCount (size_t count=1)
 The default constructor sets the reference count to 1, indicating a new and unshared condition.
 ReferenceCount (const ReferenceCount &other)
 This is the copy constructor.
 ~ReferenceCount ()
 Decrements the count and destroys the ReferenceCount instance.
ReferenceCountoperator++ ()
 The pre-increment operator increments the count by one.
ReferenceCount operator++ (int)
 Due to the semantics of the ReferenceCount class, the post-increment operator is identical to the the pre-increment operator.
ReferenceCountoperator-- ()
 The pre-decrement operator decrements the count by one.
ReferenceCount operator-- (int)
 Due to the semantics of the ReferenceCount class, the post-decrement operator is identical to the the pre-decrement operator.
ReferenceCountoperator+= (size_t offset)
 This operator has the same effect as incrementing *this offset times, but runs in O(1) time.
ReferenceCountoperator-= (size_t offset)
 This operator has the same effect as decrementing *this offset times, but runs in O(1) time.
ReferenceCountoperator= (const ReferenceCount &source)
 The assignment operator copies its argument.
size_t count () const
 This member function returns the current count.
bool isShared () const
 This member function returns true if more than one ReferenceCount object is sharing the same data.
void reset (size_t count=1)
 This member function decrements the count and releases the reference, then reinitializes with a fresh count.
bool shared () const
 This member function is identical to member function isShared().


Detailed Description

The ReferenceCount class provides a convenient way to track a shared resource so you know when to delete it.

One note regarding thread safety: if you use ReferenceCount as a member of a class which is shared between threads, and which manages its own mutex locking (such as dlrThread::Monitor) construction and destruction of the members of the containing class will most likely happen outside of the locked sections of code. This means that the ReferenceCount copy constructor and destructor will be called without protection, which is not thread-safe. To solve this, you can dynamically allocate the ReferenceCount instance using new, keep a pointer to the ReferenceCount as a member of the class, and explicitly manage copying, incrementing, and decrementing of the ReferenceCount inside the locked sections of code.

Definition at line 40 of file referenceCount.h.


Constructor & Destructor Documentation

dlr::common::ReferenceCount::ReferenceCount ( size_t  count = 1  )  [inline]

The default constructor sets the reference count to 1, indicating a new and unshared condition.

To indicate that there are existing references, pass a constructor argument greater than 1. Use an argument of zero to indicate that the shared data has not been initialized.

Parameters:
count This argument specifies to what value the count should be initialized. For most applications, this argument should be set to 1.

Definition at line 54 of file referenceCount.h.

References count().

dlr::common::ReferenceCount::ReferenceCount ( const ReferenceCount other  )  [inline]

This is the copy constructor.

After copying, both ReferenceCount instances share the same count, and the count is incremented by one.

Parameters:
other The ReferenceCount instance to be copied.

Definition at line 69 of file referenceCount.h.

dlr::common::ReferenceCount::~ReferenceCount (  )  [inline]

Decrements the count and destroys the ReferenceCount instance.

Definition at line 78 of file referenceCount.h.


Member Function Documentation

size_t dlr::common::ReferenceCount::count (  )  const [inline]

This member function returns the current count.

Returns:
The internal reference count.

Definition at line 186 of file referenceCount.h.

Referenced by ReferenceCount(), and reset().

bool dlr::common::ReferenceCount::isShared (  )  const [inline]

This member function returns true if more than one ReferenceCount object is sharing the same data.

That is, it returns true if the internal count is greater than 1.

Returns:
true if the internal count is greater than 1.

Definition at line 197 of file referenceCount.h.

Referenced by shared().

ReferenceCount dlr::common::ReferenceCount::operator++ ( int   )  [inline]

Due to the semantics of the ReferenceCount class, the post-increment operator is identical to the the pre-increment operator.

Returns:
A Reference to the incremented ReferenceCount instance.

Definition at line 101 of file referenceCount.h.

ReferenceCount& dlr::common::ReferenceCount::operator++ (  )  [inline]

The pre-increment operator increments the count by one.

Returns:
A Reference to the incremented ReferenceCount instance.

Definition at line 87 of file referenceCount.h.

ReferenceCount& dlr::common::ReferenceCount::operator+= ( size_t  offset  )  [inline]

This operator has the same effect as incrementing *this offset times, but runs in O(1) time.

Parameters:
offset This argument specifies how many times to increment
Returns:
A reference to *this.

Definition at line 137 of file referenceCount.h.

ReferenceCount dlr::common::ReferenceCount::operator-- ( int   )  [inline]

Due to the semantics of the ReferenceCount class, the post-decrement operator is identical to the the pre-decrement operator.

Returns:
A Reference to the decremented ReferenceCount instance.

Definition at line 126 of file referenceCount.h.

ReferenceCount& dlr::common::ReferenceCount::operator-- (  )  [inline]

The pre-decrement operator decrements the count by one.

Returns:
A Reference to the decremented ReferenceCount instance.

Definition at line 110 of file referenceCount.h.

ReferenceCount& dlr::common::ReferenceCount::operator-= ( size_t  offset  )  [inline]

This operator has the same effect as decrementing *this offset times, but runs in O(1) time.

Parameters:
offset This argument specifies how many times to decrement
Returns:
A reference to *this.

Definition at line 151 of file referenceCount.h.

ReferenceCount& dlr::common::ReferenceCount::operator= ( const ReferenceCount source  )  [inline]

The assignment operator copies its argument.

After copying, both ReferenceCount instances share the same count, and the count is incremented by one.

Parameters:
source The ReferenceCount instance to be copied.
Returns:
A reference to *this.

Definition at line 169 of file referenceCount.h.

References m_countPtr.

void dlr::common::ReferenceCount::reset ( size_t  count = 1  )  [inline]

This member function decrements the count and releases the reference, then reinitializes with a fresh count.

Use this if you want to release the shared resource and create a new one.

Parameters:
count This argument specifies to what value the count should be reinitialized. For most applications, this argument should be set to 1.

Definition at line 214 of file referenceCount.h.

References count().

bool dlr::common::ReferenceCount::shared (  )  const [inline]

This member function is identical to member function isShared().

It is included for backwards compatibility with previous versions of ReferenceCount.

Returns:
true if the internal count is greater than 1.

Definition at line 231 of file referenceCount.h.

References isShared().


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

Generated on Tue Nov 24 23:57:57 2009 for dlrCommon Utility Library by  doxygen 1.5.8