Also use Objects as part of an operation but as a result don't generate Any operation...
[ACE_TAO.git] / ACE / ace / Refcounted_Auto_Ptr.h
blob670243dc1b577da8d385d1d90650e03bb67b4c55
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file Refcounted_Auto_Ptr.h
7 * @author John Tucker <JTucker@infoglide.com>
8 */
9 //=============================================================================
11 #ifndef ACE_REFCOUNTED_AUTO_PTR_H
12 #define ACE_REFCOUNTED_AUTO_PTR_H
14 #include /**/ "ace/pre.h"
16 #include "ace/Auto_Ptr.h"
17 #include "ace/Atomic_Op.h"
19 #if !defined (ACE_LACKS_PRAGMA_ONCE)
20 # pragma once
21 #endif /* ACE_LACKS_PRAGMA_ONCE */
23 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
25 // Forward decl.
26 template <class X, class ACE_LOCK> class ACE_Refcounted_Auto_Ptr_Rep;
27 template <class X, class ACE_LOCK> class ACE_Refcounted_Auto_Ptr;
29 /**
30 * @class ACE_Refcounted_Auto_Ptr
32 * @brief This class implements support for a reference counted auto_ptr.
33 * Assigning or copying instances of an ACE_Refcounted_Auto_Ptr
34 * will automatically increment the reference count. When the last
35 * instance that references a ACE_Refcounted_Auto_Ptr instance is
36 * destroyed or overwritten, it will invoke delete on its underlying
37 * pointer.
39 * The ACE_Refcounted_Auto_Ptr works by maintaining a reference to a
40 * separate representation object, ACE_Refcounted_Auto_Ptr_Rep. That
41 * separate representation object contains the reference count and
42 * the actual pointer value.
44 template <class X, class ACE_LOCK>
45 class ACE_Refcounted_Auto_Ptr
47 public:
48 /// Constructor that initializes an ACE_Refcounted_Auto_Ptr to
49 /// the specified pointer value.
50 explicit ACE_Refcounted_Auto_Ptr (X *p = 0);
52 /// Copy constructor binds the new ACE_Refcounted_Auto_Ptr to the
53 /// representation object referenced by @a r.
54 /// An ACE_Refcounted_Auto_Ptr_Rep is created if necessary.
55 ACE_Refcounted_Auto_Ptr (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r);
57 /// Destructor. Releases the reference to the underlying representation.
58 /// If the release of that reference causes its reference count to reach 0,
59 /// the representation object will also be destroyed.
60 virtual ~ACE_Refcounted_Auto_Ptr (void);
62 /// Assignment operator that binds the current object and @a r to the same
63 /// ACE_Refcounted_Auto_Ptr_Rep. An ACE_Refcounted_Auto_Ptr_Rep
64 /// is created if necessary.
65 void operator = (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r);
67 /// Equality operator that returns @c true if both
68 /// ACE_Refcounted_Auto_Ptr objects point to the same underlying
69 /// representation. It does not compare the actual pointers.
70 /**
71 * @note It also returns @c true if both objects have just been
72 * instantiated and not used yet.
74 bool operator == (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r) const;
76 /// Inequality operator, which is the opposite of equality.
77 bool operator != (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r) const;
79 /// Redirection operator
80 X *operator-> (void) const;
82 /// Accessor method.
83 X &operator *() const;
85 /// Check rep easily.
86 bool operator !() const;
88 /// Check rep easily.
89 operator bool () const;
91 /// Releases the reference to the underlying representation object.
92 /// @retval The pointer value prior to releasing it.
93 X *release (void);
95 /// Releases the current pointer value and then sets a new
96 /// pointer value specified by @a p.
97 void reset (X *p = 0);
99 /// Get the pointer value.
100 X *get (void) const;
102 /// Get the reference count value.
103 long count (void) const;
105 /// Returns @c true if this object does not contain a valid pointer.
106 bool null (void) const;
108 /// Declare the dynamic allocation hooks.
109 ACE_ALLOC_HOOK_DECLARE;
111 protected:
112 /// the ACE_Refcounted_Auto_Ptr_Rep
113 typedef ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> AUTO_REFCOUNTED_PTR_REP;
115 /// Protect operations on the ACE_Refcounted_Auto_Ptr.
116 AUTO_REFCOUNTED_PTR_REP *rep_;
120 * @class ACE_Refcounted_Auto_Ptr_Rep
122 * @brief An ACE_Refcounted_Auto_Ptr_Rep object encapsulates a pointer
123 * to an object of type X. It uses a lock object of type ACE_LOCK to protect
124 * access to the reference count.
126 * @internal ACE_Refcounted_Auto_Ptr_Rep is used internally by the
127 * ACE_Refcounted_Auto_Ptr class and is only accessible through it.
129 template <class X, class ACE_LOCK>
130 class ACE_Refcounted_Auto_Ptr_Rep
132 private:
133 friend class ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>;
135 /// Get the pointer value.
136 X *get (void) const;
138 /// Get the reference count value.
139 long count (void) const;
141 /// Declare the dynamic allocation hooks.
142 ACE_ALLOC_HOOK_DECLARE;
144 // = Encapsulate reference count and object lifetime of instances.
145 // These methods must go after the others to work around a bug with
146 // Borland's C++ Builder...
148 /// Allocate a new ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> instance,
149 /// returning NULL if it cannot be created.
150 static ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *internal_create (X *p);
152 /// Create a ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> and initialize
153 /// the reference count.
154 static ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *create (X *p);
156 /// Increase the reference count on @a rep.
158 /// @retval @a rep if success, 0 if there's an error obtaining the lock
159 /// on @a rep.
160 static ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *attach (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *&rep);
162 /// Decreases the reference count and and deletes rep if there are no
163 /// more references to rep.
165 /// Precondition (rep != 0)
166 static void detach (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *&rep);
168 /// Pointer to the result.
169 ACE_Auto_Basic_Ptr<X> ptr_;
171 /// Reference count.
172 mutable ACE_Atomic_Op<ACE_LOCK, long> ref_count_;
174 private:
175 // = Constructor and destructor private.
176 ACE_Refcounted_Auto_Ptr_Rep (X *p = 0);
177 ~ACE_Refcounted_Auto_Ptr_Rep (void);
180 ACE_END_VERSIONED_NAMESPACE_DECL
182 #include "ace/Refcounted_Auto_Ptr.inl"
184 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
185 #include "ace/Refcounted_Auto_Ptr.cpp"
186 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
188 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
189 #pragma implementation ("Refcounted_Auto_Ptr.cpp")
190 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
192 #include /**/ "ace/post.h"
194 #endif /* ACE_REFCOUNTED_AUTO_PTR_H */