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

Refcounted_Auto_Ptr.h

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    Refcounted_Auto_Ptr.h
00006  *
00007  *  $Id: Refcounted_Auto_Ptr.h,v 1.1.1.2 2003/02/21 18:36:32 chad Exp $
00008  *
00009  *  @author John Tucker <JTucker@infoglide.com>
00010  */
00011 //=============================================================================
00012 
00013 #ifndef ACE_REFCOUNTED_AUTO_PTR_H
00014 #define ACE_REFCOUNTED_AUTO_PTR_H
00015 #include "ace/pre.h"
00016 
00017 #include "ace/OS.h"
00018 #include "ace/Auto_Ptr.h"
00019 
00020 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00021 # pragma once
00022 #endif /* ACE_LACKS_PRAGMA_ONCE */
00023 
00024 // Forward decl.
00025 template <class X, class ACE_LOCK> class ACE_Refcounted_Auto_Ptr_Rep;
00026 template <class X, class ACE_LOCK> class ACE_Refcounted_Auto_Ptr;
00027 
00028 /**
00029  * @class ACE_Refcounted_Auto_Ptr
00030  *
00031  * @brief This class implements support for a reference counted auto_ptr.
00032  * Assigning or copying instances of an ACE_Refcounted_Auto_Ptr
00033  * will automatically increment the reference count. When the last
00034  * instance that references a ACE_Refcounted_Auto_Ptr instance is
00035  * destroyed or overwritten, it will invoke delete on its underlying
00036  * pointer.
00037  */
00038 template <class X, class ACE_LOCK>
00039 class ACE_Refcounted_Auto_Ptr
00040 {
00041 public:
00042 
00043   // = Initialization and termination methods.
00044 
00045   /// Constructor that initializes an @c ACE_Refcounted_Auto_Ptr to
00046   /// point to the result immediately.
00047   ACE_Refcounted_Auto_Ptr (X *p = 0);
00048 
00049   /// Copy constructor binds the created object and @c r to the same
00050   /// @c ACE_Refcounted_Auto_Ptr_Rep. An @c ACE_Refcounted_Auto_Ptr_Rep
00051   /// is created if necessary.
00052   ACE_Refcounted_Auto_Ptr (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r);
00053 
00054   /// Destructor.
00055   virtual ~ACE_Refcounted_Auto_Ptr (void);
00056 
00057   /// Assignment operator that binds the current object and @c r to the same
00058   /// @c ACE_Refcounted_Auto_Ptr_Rep. An @c ACE_Refcounted_Auto_Ptr_Rep
00059   /// is created if necessary.
00060   void operator = (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r);
00061 
00062   /// Equality operator that returns 1 if both 
00063   /// ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> objects point to the same 
00064   /// ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> object.  Attention: It 
00065   /// also returns 1 if both objects have just been instantiated and 
00066   /// not used yet.
00067   int operator == (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r) const;
00068 
00069   /// Inequality operator, which is the opposite of equality.
00070   int operator != (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r) const;
00071 
00072   /// Redirection operator
00073   X *operator-> (void) const;
00074 
00075   // = Accessor methods.
00076 
00077   X &operator *() const;
00078 
00079   /// Sets the pointer value to 0 and returns its old value.
00080   X *release (void);
00081   
00082   /// Invokes delete on the previous pointer value and then sets the
00083   /// pointer value to the specified value.
00084   void reset (X *p = 0);
00085 
00086   /// Get the pointer value.
00087   X *get (void) const;
00088 
00089   /// Get the reference count value.
00090   int count (void) const;
00091 
00092   // = Utility method.
00093   
00094   /// Allows us to check for NULL on all ACE_Refcounted_Auto_Ptr objects.
00095   int null (void) const;
00096 
00097   /// Declare the dynamic allocation hooks.
00098   ACE_ALLOC_HOOK_DECLARE;
00099 
00100 protected:
00101   /// the ACE_Refcounted_Auto_Ptr_Rep
00102   typedef ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> AUTO_REFCOUNTED_PTR_REP;
00103 
00104   /// Protect operations on the <ACE_Refcounted_Auto_Ptr>.
00105   AUTO_REFCOUNTED_PTR_REP *rep_;
00106 };
00107 
00108 /**
00109  * @class ACE_Refcounted_Auto_Ptr_Rep
00110  *
00111  * @brief An ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> object
00112  * encapsulates a pointer to an object of type X.  It is pointed to by
00113  * ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> object[s] and only accessible
00114  * through them. 
00115  */
00116 template <class X, class ACE_LOCK>
00117 class ACE_Refcounted_Auto_Ptr_Rep
00118 {
00119 private:
00120   friend class ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>;
00121 
00122   /// Sets the pointer value to 0 and returns its old value.
00123   X *release (void);
00124 
00125   /// Invokes delete on the previous pointer value and then
00126   /// sets the pointer value to the specified value.
00127   void reset (X *p = 0);
00128 
00129   /// Get the pointer value.
00130   X *get (void) const;
00131 
00132   /// Get the reference count value.
00133   int count (void) const;
00134 
00135   /// Declare the dynamic allocation hooks.
00136   ACE_ALLOC_HOOK_DECLARE;
00137 
00138   // = Encapsulate reference count and object lifetime of instances.
00139   // These methods must go after the others to work around a bug with
00140   // Borland's C++ Builder...
00141 
00142   /// Allocate a new ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> instance,
00143   /// returning NULL if it cannot be created.
00144   static ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *internal_create (X *p);
00145 
00146   /// Create a ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> and initialize
00147   /// the reference count.
00148   static ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *create (X *p);
00149 
00150   /// Increase the reference count and return argument. Uses the
00151   /// attribute "ace_lock_" to synchronize reference count updating.
00152   ///
00153   /// Precondition (rep != 0).
00154   static ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *attach (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *&rep);
00155 
00156   /// Decreases the reference count and and deletes rep if there are no
00157   /// more references to rep.
00158   ///
00159   /// Precondition (rep != 0)
00160   static void detach (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *&rep);
00161 
00162   /// Decreases the rep's reference count and and deletes rep if there
00163   /// are no more references to rep. Then assigns new_rep to rep.
00164   ///
00165   /// Precondition (rep != 0 && new_rep != 0)
00166   static void assign (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *&rep,
00167                       ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *new_rep);
00168 
00169   /// Pointer to the result.
00170   ACE_Auto_Basic_Ptr<X> ptr_;
00171 
00172   /// Reference count.
00173   int ref_count_;
00174 
00175   // = Mutex variable to protect the <ptr_>.
00176 
00177   /// Synchronization variable for the MT_SAFE <ACE_Hash_Map_Manager_Ex>.
00178   ACE_LOCK lock_;
00179 
00180 private:
00181   /// Allows us to check for NULL on all ACE_Refcounted_Auto_Ptr objects.
00182   int null (void) const;
00183 
00184   // = Constructor and destructor private.
00185   ACE_Refcounted_Auto_Ptr_Rep (X *p = 0);
00186   ~ACE_Refcounted_Auto_Ptr_Rep (void);
00187 };
00188 
00189 #include "ace/Refcounted_Auto_Ptr.i"
00190 
00191 #include "ace/post.h"
00192 #endif /* ACE_REFCOUNTED_AUTO_PTR_H */
00193 

Generated on Mon Jun 16 11:21:02 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002