3 //=============================================================================
5 * @file Intrusive_Auto_Ptr.h
7 * @author Iliyan Jeliazkov <iliyan@ociweb.com>
9 * @note Modeled on http://www.boost.org/boost/intrusive_ptr.hpp
11 //=============================================================================
13 #ifndef ACE_INTRUSIVE_AUTO_PTR_H
14 #define ACE_INTRUSIVE_AUTO_PTR_H
16 #include /**/ "ace/pre.h"
18 #include "ace/Atomic_Op.h"
20 #if !defined (ACE_LACKS_PRAGMA_ONCE)
22 #endif /* ACE_LACKS_PRAGMA_ONCE */
24 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
27 template <class X
> class ACE_Intrusive_Auto_Ptr
;
30 * @class ACE_Intrusive_Auto_Ptr
32 * @brief This class implements support for a reference counted
33 * auto_ptr. It assumes reference counting abilities of the
34 * parameterizing class.
36 * Assigning or copying instances of an ACE_Intrusive_Auto_Ptr will
37 * automatically increment the reference count. When the last instance
38 * that references a ACE_Intrusive_Auto_Ptr instance is destroyed or
39 * overwritten, it will invoke delete on its underlying pointer.
41 * The ACE_Intrusive_Auto_Ptr works by maintaining a reference to a
42 * separate representation object, ACE_Intrusive_Auto_Ptr_Rep. That
43 * separate representation object contains the reference count and the
44 * actual pointer value.
47 class ACE_Intrusive_Auto_Ptr
50 /// Used to define a proper boolean conversion for "if (sp) ..."
51 static void unspecified_bool(ACE_Intrusive_Auto_Ptr
<X
>***){};
52 typedef void (*unspecified_bool_type
)(ACE_Intrusive_Auto_Ptr
<X
>***);
55 /// Enables "if (sp) ..."
56 operator unspecified_bool_type() const
58 return rep_
== 0 ? 0: unspecified_bool
;
61 /// Constructor that initializes an ACE_Intrusive_Auto_Ptr to
62 /// the specified pointer value.
63 ACE_Intrusive_Auto_Ptr (X
*p
= 0, bool addref
= true);
65 /// Copy constructor binds the new ACE_Intrusive_Auto_Ptr to the
66 /// representation object referenced by @a r.
67 /// An ACE_Intrusive_Auto_Ptr_Rep is created if necessary.
68 ACE_Intrusive_Auto_Ptr (const ACE_Intrusive_Auto_Ptr
<X
> &r
);
70 // Derived class copy ctor
71 template<class U
> ACE_Intrusive_Auto_Ptr(const ACE_Intrusive_Auto_Ptr
<U
> & rhs
);
73 /// Destructor. Releases the reference to the underlying representation.
74 /// If the release of that reference causes its reference count to reach 0,
75 /// the representation object will also be destroyed.
76 virtual ~ACE_Intrusive_Auto_Ptr ();
78 /// Assignment operator that binds the current object and @a r to the same
79 /// ACE_Intrusive_Auto_Ptr_Rep. An ACE_Intrusive_Auto_Ptr_Rep
80 /// is created if necessary.
81 void operator = (const ACE_Intrusive_Auto_Ptr
<X
> &r
);
83 /// Redirection operator
84 X
*operator-> () const;
87 X
&operator *() const;
89 /// Releases the reference to the underlying representation object.
90 /// @retval The pointer value prior to releasing it.
93 /// Releases the current pointer value and then sets a new
94 /// pointer value specified by @a p.
95 void reset (X
*p
= 0);
97 /// Get the pointer value.
100 /// Get the reference count value.
103 /// Declare the dynamic allocation hooks.
104 ACE_ALLOC_HOOK_DECLARE
;
107 /// Protect operations on the ACE_Intrusive_Auto_Ptr.
111 /// Equality operator that returns @c true if both
112 /// ACE_Intrusive_Auto_Ptr objects point to the same underlying
113 /// representation. It does not compare the actual pointers.
115 * @note It also returns @c true if both objects have just been
116 * instantiated and not used yet.
118 template<class T
, class U
>
119 bool operator==(ACE_Intrusive_Auto_Ptr
<T
> const & a
, ACE_Intrusive_Auto_Ptr
<U
> const & b
);
121 /// Inequality operator, which is the opposite of equality.
122 template<class T
, class U
>
123 bool operator!=(ACE_Intrusive_Auto_Ptr
<T
> const & a
, ACE_Intrusive_Auto_Ptr
<U
> const & b
);
125 template<class T
, class U
>
126 bool operator==(ACE_Intrusive_Auto_Ptr
<T
> const & a
, U
* b
);
128 template<class T
, class U
>
129 bool operator!=(ACE_Intrusive_Auto_Ptr
<T
> & a
, U
* b
);
131 template<class T
, class U
>
132 bool operator==(T
* a
, ACE_Intrusive_Auto_Ptr
<U
> const & b
);
134 template<class T
, class U
>
135 bool operator!=(T
* a
, ACE_Intrusive_Auto_Ptr
<U
> const & b
);
137 ACE_END_VERSIONED_NAMESPACE_DECL
139 #if defined (__ACE_INLINE__)
140 #include "ace/Intrusive_Auto_Ptr.inl"
141 #endif /* __ACE_INLINE __ */
143 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
144 #include "ace/Intrusive_Auto_Ptr.cpp"
145 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
147 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
148 #pragma implementation ("Intrusive_Auto_Ptr.cpp")
149 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
151 #include /**/ "ace/post.h"
153 #endif /* ACE_INTRUSIVE_AUTO_PTR_H */