Refcounted_Auto_Ptr.h

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

Generated on Thu Nov 9 09:42:01 2006 for ACE by doxygen 1.3.6