Intrusive_Auto_Ptr.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    Intrusive_Auto_Ptr.h
00006  *
00007  *  $Id: Intrusive_Auto_Ptr.h 81388 2008-04-23 14:02:05Z johnnyw $
00008  *
00009  *  @author Iliyan Jeliazkov <iliyan@ociweb.com>
00010  *
00011  *  @note Modeled on http://www.boost.org/boost/intrusive_ptr.hpp
00012  */
00013 //=============================================================================
00014 
00015 #ifndef ACE_INTRUSIVE_AUTO_PTR_H
00016 #define ACE_INTRUSIVE_AUTO_PTR_H
00017 
00018 #include /**/ "ace/pre.h"
00019 
00020 #include "ace/Auto_Ptr.h"
00021 #include "ace/Atomic_Op.h"
00022 
00023 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00024 # pragma once
00025 #endif /* ACE_LACKS_PRAGMA_ONCE */
00026 
00027 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00028 
00029 // Forward decl.
00030 template <class X> class ACE_Intrusive_Auto_Ptr;
00031 
00032 /**
00033  * @class ACE_Intrusive_Auto_Ptr
00034  *
00035  * @brief This class implements support for a reference counted
00036  * auto_ptr. It assumes reference counting abilities of the
00037  * parameterizing class.
00038  *
00039  * Assigning or copying instances of an ACE_Intrusive_Auto_Ptr will
00040  * automatically increment the reference count. When the last instance
00041  * that references a ACE_Intrusive_Auto_Ptr instance is destroyed or
00042  * overwritten, it will invoke delete on its underlying pointer.
00043  *
00044  * The ACE_Intrusive_Auto_Ptr works by maintaining a reference to a
00045  * separate representation object, ACE_Intrusive_Auto_Ptr_Rep. That
00046  * separate representation object contains the reference count and the
00047  * actual pointer value.
00048  */
00049 template <class X>
00050 class ACE_Intrusive_Auto_Ptr
00051 {
00052 protected:
00053 
00054     /// Used to define a proper boolean conversion for "if (sp) ..."
00055   static void unspecified_bool(ACE_Intrusive_Auto_Ptr<X>***){};
00056   typedef void (*unspecified_bool_type)(ACE_Intrusive_Auto_Ptr<X>***);
00057 
00058 public:
00059 
00060   /// Enables "if (sp) ..."
00061   operator unspecified_bool_type() const
00062     {
00063         return rep_ == 0 ? 0: unspecified_bool;
00064     }
00065 
00066 
00067   // = Initialization and termination methods.
00068 
00069   /// Constructor that initializes an ACE_Intrusive_Auto_Ptr to
00070   /// the specified pointer value.
00071   ACE_Intrusive_Auto_Ptr (X *p = 0, bool addref = true);
00072 
00073   /// Copy constructor binds the new ACE_Intrusive_Auto_Ptr to the
00074   /// representation object referenced by @a r.
00075   /// An ACE_Intrusive_Auto_Ptr_Rep is created if necessary.
00076   ACE_Intrusive_Auto_Ptr (const ACE_Intrusive_Auto_Ptr<X> &r);
00077 
00078   // Derived class copy ctor
00079   template<class U> ACE_Intrusive_Auto_Ptr(const ACE_Intrusive_Auto_Ptr<U> & rhs);
00080 
00081   /// Destructor. Releases the reference to the underlying representation.
00082   /// If the release of that reference causes its reference count to reach 0,
00083   /// the representation object will also be destroyed.
00084   virtual ~ACE_Intrusive_Auto_Ptr (void);
00085 
00086   /// Assignment operator that binds the current object and @a r to the same
00087   /// ACE_Intrusive_Auto_Ptr_Rep. An ACE_Intrusive_Auto_Ptr_Rep
00088   /// is created if necessary.
00089   void operator = (const ACE_Intrusive_Auto_Ptr<X> &r);
00090 
00091   /// Redirection operator
00092   X *operator-> (void) const;
00093 
00094   /// Accessor method.
00095   X &operator *() const;
00096 
00097   /// Releases the reference to the underlying representation object.
00098   /// @retval The pointer value prior to releasing it.
00099   X *release (void);
00100 
00101   /// Releases the current pointer value and then sets a new
00102   /// pointer value specified by @a p.
00103   void reset (X *p = 0);
00104 
00105   /// Get the pointer value.
00106   X *get (void) const;
00107 
00108    /// Get the reference count value.
00109   long count (void) const;
00110 
00111   /// Returns @c true if this object does not contain a valid pointer.
00112   //  bool null (void) const;
00113 
00114   /// Declare the dynamic allocation hooks.
00115   ACE_ALLOC_HOOK_DECLARE;
00116 
00117 protected:
00118 
00119   /// Protect operations on the ACE_Intrusive_Auto_Ptr.
00120   X *rep_;
00121 };
00122 
00123   /// Equality operator that returns @c true if both
00124   /// ACE_Intrusive_Auto_Ptr objects point to the same underlying
00125   /// representation. It does not compare the actual pointers.
00126   /**
00127    * @note It also returns @c true if both objects have just been
00128    *       instantiated and not used yet.
00129    */
00130 template<class T, class U>
00131 bool operator==(ACE_Intrusive_Auto_Ptr<T> const & a, ACE_Intrusive_Auto_Ptr<U> const & b);
00132 
00133 /// Inequality operator, which is the opposite of equality.
00134 template<class T, class U>
00135 bool operator!=(ACE_Intrusive_Auto_Ptr<T> const & a, ACE_Intrusive_Auto_Ptr<U> const & b);
00136 
00137 template<class T, class U>
00138 bool operator==(ACE_Intrusive_Auto_Ptr<T> const & a, U * b);
00139 
00140 template<class T, class U>
00141 bool operator!=(ACE_Intrusive_Auto_Ptr<T> & a, U * b);
00142 
00143 template<class T, class U>
00144 bool operator==(T * a, ACE_Intrusive_Auto_Ptr<U> const & b);
00145 
00146 template<class T, class U>
00147 bool operator!=(T * a, ACE_Intrusive_Auto_Ptr<U> const & b);
00148 
00149 ACE_END_VERSIONED_NAMESPACE_DECL
00150 
00151 #if defined (__ACE_INLINE__)
00152 #include "ace/Intrusive_Auto_Ptr.inl"
00153 #endif /* __ACE_INLINE __ */
00154 
00155 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
00156 #include "ace/Intrusive_Auto_Ptr.cpp"
00157 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
00158 
00159 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
00160 #pragma implementation ("Intrusive_Auto_Ptr.cpp")
00161 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
00162 
00163 #include /**/ "ace/post.h"
00164 
00165 #endif /* ACE_INTRUSIVE_AUTO_PTR_H */

Generated on Tue Feb 2 17:18:39 2010 for ACE by  doxygen 1.4.7