Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

ACE_Bound_Ptr_Counter Class Template Reference

An ACE_Bound_Ptr_Counter<ACE_LOCK> object encapsulates an object reference count. More...

#include <Bound_Ptr.h>

Collaboration diagram for ACE_Bound_Ptr_Counter:

Collaboration graph
[legend]
List of all members.

Public Methods

 ACE_Bound_Ptr_Counter (int init_obj_ref_count=0)
 ~ACE_Bound_Ptr_Counter (void)

Static Public Methods

ACE_Bound_Ptr_Counter< ACE_LOCK > * create_strong (void)
 Create a ACE_Bound_Ptr_Counter<ACE_LOCK> and initialize the reference count to indicate ownership by a strong pointer. More...

int attach_strong (ACE_Bound_Ptr_Counter< ACE_LOCK > *counter)
 Increase both the object and counter reference counts and return the new object reference count. A return value of -1 indicates that the object has already been destroyed. More...

int detach_strong (ACE_Bound_Ptr_Counter< ACE_LOCK > *counter)
 Decreases both the object and counter reference counts and deletes whichever has no more references. Returns the new object reference count. More...

ACE_Bound_Ptr_Counter< ACE_LOCK > * create_weak (void)
 Create a ACE_Bound_Ptr_Counter<ACE_LOCK> and initialize the reference count to indicate no ownership. More...

void attach_weak (ACE_Bound_Ptr_Counter< ACE_LOCK > *counter)
 Increase the counter reference count and return argument. More...

void detach_weak (ACE_Bound_Ptr_Counter< ACE_LOCK > *counter)
 Decreases the counter reference count and deletes the counter if it has no more references. More...

int object_was_deleted (ACE_Bound_Ptr_Counter< ACE_LOCK > *counter)
 Determine whether the object has been deleted. More...


Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks. More...


Static Private Methods

ACE_Bound_Ptr_Counter< ACE_LOCK > * internal_create (int init_obj_ref_count)
 Allocate a new ACE_Bound_Ptr_Counter<ACE_LOCK> instance, returning NULL if it cannot be created. More...


Private Attributes

int obj_ref_count_
 Reference count of underlying object. Is set to -1 once the object has been destroyed to indicate to all weak pointers that it is no longer valid. More...

int self_ref_count_
 Reference count of this counter. More...

ACE_LOCK lock_
 Mutex variable to synchronize access to the reference counts. More...


Detailed Description

template<class ACE_LOCK>
class ACE_Bound_Ptr_Counter< ACE_LOCK >

An ACE_Bound_Ptr_Counter<ACE_LOCK> object encapsulates an object reference count.

Do not use this class directly, use ACE_Strong_Bound_Ptr or ACE_Weak_Bound_Ptr instead.

Definition at line 36 of file Bound_Ptr.h.


Constructor & Destructor Documentation

template<class ACE_LOCK>
ACE_Bound_Ptr_Counter< ACE_LOCK >::ACE_Bound_Ptr_Counter int    init_obj_ref_count = 0 [inline]
 

Definition at line 130 of file Bound_Ptr.i.

00131   : obj_ref_count_ (init_obj_ref_count),
00132     self_ref_count_ (1)
00133 {
00134 }

template<class ACE_LOCK>
ACE_Bound_Ptr_Counter< ACE_LOCK >::~ACE_Bound_Ptr_Counter void    [inline]
 

Definition at line 137 of file Bound_Ptr.i.

00138 {
00139 }


Member Function Documentation

template<class ACE_LOCK>
int ACE_Bound_Ptr_Counter< ACE_LOCK >::attach_strong ACE_Bound_Ptr_Counter< ACE_LOCK > *    counter [inline, static]
 

Increase both the object and counter reference counts and return the new object reference count. A return value of -1 indicates that the object has already been destroyed.

Definition at line 35 of file Bound_Ptr.i.

References ACE_GUARD_RETURN, lock_, obj_ref_count_, and self_ref_count_.

00036 {
00037   ACE_GUARD_RETURN (ACE_LOCK, guard, counter->lock_, -1);
00038 
00039   // Can't attach a strong pointer to an object that has already been deleted.
00040   if (counter->obj_ref_count_ == -1)
00041     return -1;
00042 
00043   int new_obj_ref_count = ++counter->obj_ref_count_;
00044   ++counter->self_ref_count_;
00045 
00046   return new_obj_ref_count;
00047 }

template<class ACE_LOCK>
void ACE_Bound_Ptr_Counter< ACE_LOCK >::attach_weak ACE_Bound_Ptr_Counter< ACE_LOCK > *    counter [inline, static]
 

Increase the counter reference count and return argument.

Definition at line 94 of file Bound_Ptr.i.

References ACE_GUARD, lock_, and self_ref_count_.

00095 {
00096   ACE_GUARD (ACE_LOCK, guard, counter->lock_);
00097 
00098   ++counter->self_ref_count_;
00099 }

template<class ACE_LOCK>
ACE_Bound_Ptr_Counter< ACE_LOCK > * ACE_Bound_Ptr_Counter< ACE_LOCK >::create_strong void    [inline, static]
 

Create a ACE_Bound_Ptr_Counter<ACE_LOCK> and initialize the reference count to indicate ownership by a strong pointer.

Definition at line 19 of file Bound_Ptr.i.

References ACE_ASSERT, ACE_throw_bad_alloc, and internal_create.

00020 {
00021   // Set initial object reference count to 1.
00022   ACE_Bound_Ptr_Counter<ACE_LOCK> *temp = internal_create (1);
00023 #if defined (ACE_NEW_THROWS_EXCEPTIONS)
00024   if (temp == 0)
00025     ACE_throw_bad_alloc;
00026 #else
00027   ACE_ASSERT (temp != 0);
00028 #endif /* ACE_NEW_THROWS_EXCEPTIONS */
00029   return temp;
00030 }

template<class ACE_LOCK>
ACE_Bound_Ptr_Counter< ACE_LOCK > * ACE_Bound_Ptr_Counter< ACE_LOCK >::create_weak void    [inline, static]
 

Create a ACE_Bound_Ptr_Counter<ACE_LOCK> and initialize the reference count to indicate no ownership.

Definition at line 79 of file Bound_Ptr.i.

References ACE_ASSERT, ACE_throw_bad_alloc, and internal_create.

00080 {
00081   // Set initial object reference count to 0.
00082 
00083   ACE_Bound_Ptr_Counter<ACE_LOCK> *temp = internal_create (0);
00084 #if defined (ACE_NEW_THROWS_EXCEPTIONS)
00085   if (temp == 0)
00086     ACE_throw_bad_alloc;
00087 #else
00088   ACE_ASSERT (temp != 0);
00089 #endif /* ACE_NEW_THROWS_EXCEPTIONS */
00090   return temp;
00091 }

template<class ACE_LOCK>
int ACE_Bound_Ptr_Counter< ACE_LOCK >::detach_strong ACE_Bound_Ptr_Counter< ACE_LOCK > *    counter [inline, static]
 

Decreases both the object and counter reference counts and deletes whichever has no more references. Returns the new object reference count.

Definition at line 50 of file Bound_Ptr.i.

References ACE_GUARD_RETURN, lock_, obj_ref_count_, and self_ref_count_.

00051 {
00052   ACE_Bound_Ptr_Counter<ACE_LOCK> *counter_del = 0;
00053   int new_obj_ref_count;
00054 
00055   {
00056     ACE_GUARD_RETURN (ACE_LOCK, guard, counter->lock_, -1);
00057 
00058     if ((new_obj_ref_count = --counter->obj_ref_count_) == 0)
00059       // Change the object reference count to -1 to indicate that the
00060       // object has been deleted, as opposed to a weak pointer that
00061       // simply hasn't had any strong pointers created from it yet.
00062       counter->obj_ref_count_ = -1;
00063 
00064     if (--counter->self_ref_count_ == 0)
00065       // Since counter contains the lock held by the ACE_Guard, the
00066       // guard needs to be released before freeing the memory holding
00067       // the lock. So save the pointer to free, then release, then
00068       // free.
00069       counter_del = counter;
00070 
00071   }  // Release the lock
00072 
00073   delete counter_del;
00074 
00075   return new_obj_ref_count;
00076 }

template<class ACE_LOCK>
void ACE_Bound_Ptr_Counter< ACE_LOCK >::detach_weak ACE_Bound_Ptr_Counter< ACE_LOCK > *    counter [inline, static]
 

Decreases the counter reference count and deletes the counter if it has no more references.

Definition at line 102 of file Bound_Ptr.i.

References ACE_GUARD, lock_, and self_ref_count_.

00103 {
00104   ACE_Bound_Ptr_Counter<ACE_LOCK> *counter_del = 0;
00105 
00106   {
00107     ACE_GUARD (ACE_LOCK, guard, counter->lock_);
00108 
00109     if (--counter->self_ref_count_ == 0)
00110       // Since counter contains the lock held by the ACE_Guard, the
00111       // guard needs to be released before freeing the memory holding
00112       // the lock. So save the pointer to free, then release, then
00113       // free.
00114       counter_del = counter;
00115 
00116   }  // Release the lock
00117 
00118   delete counter_del;
00119 }

template<class ACE_LOCK>
ACE_Bound_Ptr_Counter< ACE_LOCK > * ACE_Bound_Ptr_Counter< ACE_LOCK >::internal_create int    init_obj_ref_count [inline, static, private]
 

Allocate a new ACE_Bound_Ptr_Counter<ACE_LOCK> instance, returning NULL if it cannot be created.

Definition at line 9 of file Bound_Ptr.i.

References ACE_NEW_RETURN.

Referenced by create_strong, and create_weak.

00010 {
00011   ACE_Bound_Ptr_Counter<ACE_LOCK> *temp = 0;
00012   ACE_NEW_RETURN (temp,
00013                   ACE_Bound_Ptr_Counter<ACE_LOCK> (init_obj_ref_count),
00014                   0);
00015   return temp;
00016 }

template<class ACE_LOCK>
int ACE_Bound_Ptr_Counter< ACE_LOCK >::object_was_deleted ACE_Bound_Ptr_Counter< ACE_LOCK > *    counter [inline, static]
 

Determine whether the object has been deleted.

Definition at line 122 of file Bound_Ptr.i.

References ACE_GUARD_RETURN, lock_, and obj_ref_count_.

00123 {
00124   ACE_GUARD_RETURN (ACE_LOCK, guard, counter->lock_, 0);
00125 
00126   return counter->obj_ref_count_ == -1;
00127 }


Member Data Documentation

template<class ACE_LOCK>
ACE_Bound_Ptr_Counter::ACE_ALLOC_HOOK_DECLARE
 

Declare the dynamic allocation hooks.

Definition at line 40 of file Bound_Ptr.h.

template<class ACE_LOCK>
ACE_LOCK ACE_Bound_Ptr_Counter::lock_ [private]
 

Mutex variable to synchronize access to the reference counts.

Definition at line 90 of file Bound_Ptr.h.

Referenced by attach_strong, attach_weak, detach_strong, detach_weak, and object_was_deleted.

template<class ACE_LOCK>
int ACE_Bound_Ptr_Counter::obj_ref_count_ [private]
 

Reference count of underlying object. Is set to -1 once the object has been destroyed to indicate to all weak pointers that it is no longer valid.

Definition at line 84 of file Bound_Ptr.h.

Referenced by attach_strong, detach_strong, and object_was_deleted.

template<class ACE_LOCK>
int ACE_Bound_Ptr_Counter::self_ref_count_ [private]
 

Reference count of this counter.

Definition at line 87 of file Bound_Ptr.h.

Referenced by attach_strong, attach_weak, detach_strong, and detach_weak.


The documentation for this class was generated from the following files:
Generated on Mon Jun 16 12:46:10 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002