3
//=============================================================================
7
* Bound_Ptr.h,v 4.6 2003/07/19 19:04:10 dhinton Exp
9
* @author Christopher Kohlhoff <chris@kohlhoff.com>
11
//=============================================================================
13
#ifndef ACE_BOUND_PTR_H
14
#define ACE_BOUND_PTR_H
16
#include /**/ "ace/pre.h"
18
#include "ace/config-all.h"
20
#if !defined (ACE_LACKS_PRAGMA_ONCE)
22
#endif /* ACE_LACKS_PRAGMA_ONCE */
24
#include "ace/Auto_Ptr.h"
27
* @class ACE_Bound_Ptr_Counter
29
* @brief An ACE_Bound_Ptr_Counter<ACE_LOCK> object encapsulates an
30
* object reference count.
32
* Do not use this class directly, use ACE_Strong_Bound_Ptr or
33
* ACE_Weak_Bound_Ptr instead.
35
template <class ACE_LOCK>
36
class ACE_Bound_Ptr_Counter
39
/// Declare the dynamic allocation hooks.
40
ACE_ALLOC_HOOK_DECLARE;
42
ACE_Bound_Ptr_Counter (int init_obj_ref_count = 0);
43
~ACE_Bound_Ptr_Counter (void);
45
/// Create a ACE_Bound_Ptr_Counter<ACE_LOCK> and initialize the
46
/// reference count to indicate ownership by a strong pointer.
47
static ACE_Bound_Ptr_Counter<ACE_LOCK> *create_strong (void);
49
/// Increase both the object and counter reference counts and return
50
/// the new object reference count. A return value of -1 indicates
51
/// that the object has already been destroyed.
52
static int attach_strong (ACE_Bound_Ptr_Counter<ACE_LOCK> *counter);
54
/// Decreases both the object and counter reference counts and
55
/// deletes whichever has no more references. Returns the new object
57
static int detach_strong (ACE_Bound_Ptr_Counter<ACE_LOCK> *counter);
59
/// Create a ACE_Bound_Ptr_Counter<ACE_LOCK> and initialize the
60
/// reference count to indicate no ownership.
61
static ACE_Bound_Ptr_Counter<ACE_LOCK> *create_weak (void);
63
/// Increase the counter reference count and return argument.
64
static void attach_weak (ACE_Bound_Ptr_Counter<ACE_LOCK> *counter);
66
/// Decreases the counter reference count and deletes the counter if
67
/// it has no more references.
68
static void detach_weak (ACE_Bound_Ptr_Counter<ACE_LOCK> *counter);
70
/// Determine whether the object has been deleted.
71
static int object_was_deleted (ACE_Bound_Ptr_Counter<ACE_LOCK> *counter);
75
/// Allocate a new ACE_Bound_Ptr_Counter<ACE_LOCK> instance,
76
/// returning NULL if it cannot be created.
77
static ACE_Bound_Ptr_Counter<ACE_LOCK> *internal_create (int init_obj_ref_count);
81
/// Reference count of underlying object. Is set to -1 once the
82
/// object has been destroyed to indicate to all weak pointers that
83
/// it is no longer valid.
86
/// Reference count of this counter.
89
/// Mutex variable to synchronize access to the reference counts.
94
template <class X, class ACE_LOCK> class ACE_Weak_Bound_Ptr;
97
* @class ACE_Strong_Bound_Ptr
99
* @brief This class implements support for a reference counted
102
* Assigning or copying instances of an ACE_Strong_Bound_Ptr will
103
* automatically increment the reference count of the underlying object.
104
* When the last instance of an ACE_Strong_Bound_Ptr that references a
105
* particular object is destroyed or overwritten, it will invoke delete
106
* on its underlying pointer.
108
template <class X, class ACE_LOCK>
109
class ACE_Strong_Bound_Ptr
112
/// Constructor that initializes an ACE_Strong_Bound_Ptr to point to the
113
/// object \<p\> immediately.
114
ACE_EXPLICIT ACE_Strong_Bound_Ptr (X *p = 0);
116
/// Constructor that initializes an ACE_Strong_Bound_Ptr by stealing
117
/// ownership of an object from an auto_ptr.
118
ACE_EXPLICIT ACE_Strong_Bound_Ptr (auto_ptr<X> p);
120
/// Copy constructor binds <this> and <r> to the same object.
121
ACE_Strong_Bound_Ptr (const ACE_Strong_Bound_Ptr<X, ACE_LOCK> &r);
123
/// Constructor binds <this> and <r> to the same object.
124
ACE_Strong_Bound_Ptr (const ACE_Weak_Bound_Ptr<X, ACE_LOCK> &r);
127
~ACE_Strong_Bound_Ptr (void);
129
/// Assignment operator that binds <this> and <r> to the same object.
130
void operator = (const ACE_Strong_Bound_Ptr<X, ACE_LOCK> &r);
132
/// Assignment operator that binds <this> and <r> to the same object.
133
void operator = (const ACE_Weak_Bound_Ptr<X, ACE_LOCK> &r);
135
/// Equality operator that returns 1 if both ACE_Strong_Bound_Ptr
136
/// instances point to the same underlying object.
138
* @note It also returns 1 if both objects have just been
139
* instantiated and not used yet.
141
int operator == (const ACE_Strong_Bound_Ptr<X, ACE_LOCK> &r) const;
143
/// Equality operator that returns 1 if the ACE_Strong_Bound_Ptr and
144
/// ACE_Weak_Bound_Ptr objects point to the same underlying object.
147
* @note It also returns 1 if both objects have just been
148
* instantiated and not used yet.
150
int operator == (const ACE_Weak_Bound_Ptr<X, ACE_LOCK> &r) const;
152
/// Equality operator that returns 1 if the ACE_Strong_Bound_Ptr and
153
/// the raw pointer point to the same underlying object.
154
int operator == (X *p) const;
156
/// Inequality operator, which is the opposite of equality.
157
int operator != (const ACE_Strong_Bound_Ptr<X, ACE_LOCK> &r) const;
159
/// Inequality operator, which is the opposite of equality.
160
int operator != (const ACE_Weak_Bound_Ptr<X, ACE_LOCK> &r) const;
162
/// Inequality operator, which is the opposite of equality.
163
int operator != (X *p) const;
165
/// Redirection operator
166
X *operator-> (void) const;
168
/// Dereference operator
169
X &operator * (void) const;
171
/// Get the pointer value.
174
/// Resets the ACE_Strong_Bound_Ptr to refer to a different
175
/// underlying object.
176
void reset (X *p = 0);
178
/// Resets the ACE_Strong_Bound_Ptr to refer to a different
179
/// underlying object, ownership of which is stolen from the
181
void reset (auto_ptr<X> p);
183
/// Allows us to check for NULL on all ACE_Strong_Bound_Ptr
185
int null (void) const;
187
/// Declare the dynamic allocation hooks.
188
ACE_ALLOC_HOOK_DECLARE;
191
friend class ACE_Weak_Bound_Ptr<X, ACE_LOCK>;
193
/// The ACE_Bound_Ptr_Counter type.
194
typedef ACE_Bound_Ptr_Counter<ACE_LOCK> COUNTER;
196
/// The reference counter.
199
/// The underlying object.
204
* @class ACE_Weak_Bound_Ptr
206
* @brief This class implements support for a weak pointer that complements
207
* ACE_Strong_Bound_Ptr.
209
* Unlike ACE_Strong_Bound_Ptr, assigning or copying instances of an
210
* ACE_Weak_Bound_Ptr will not automatically increment the reference
211
* count of the underlying object. What ACE_Weak_Bound_Ptr does is
212
* preserve the knowledge that the object is in fact reference
213
* counted, and thus provides an alternative to raw pointers where
214
* non-ownership associations must be maintained. When the last
215
* instance of an ACE_Strong_Bound_Ptr that references a particular
216
* object is destroyed or overwritten, the corresponding
217
* ACE_Weak_Bound_Ptr instances are set to NULL.
219
template <class X, class ACE_LOCK>
220
class ACE_Weak_Bound_Ptr
223
/// Constructor that initializes an ACE_Weak_Bound_Ptr to point to
224
/// the object \<p\> immediately.
225
ACE_EXPLICIT ACE_Weak_Bound_Ptr (X *p = 0);
227
/// Copy constructor binds <this> and <r> to the same object.
228
ACE_Weak_Bound_Ptr (const ACE_Weak_Bound_Ptr<X, ACE_LOCK> &r);
230
/// Constructor binds <this> and <r> to the same object.
231
ACE_Weak_Bound_Ptr (const ACE_Strong_Bound_Ptr<X, ACE_LOCK> &r);
234
~ACE_Weak_Bound_Ptr (void);
236
/// Assignment operator that binds <this> and <r> to the same object.
237
void operator = (const ACE_Weak_Bound_Ptr<X, ACE_LOCK> &r);
239
/// Assignment operator that binds <this> and <r> to the same object.
240
void operator = (const ACE_Strong_Bound_Ptr<X, ACE_LOCK> &r);
242
/// Equality operator that returns 1 if both ACE_Weak_Bound_Ptr
243
/// objects point to the same underlying object.
245
* @note It also returns 1 if both objects have just been
246
* instantiated and not used yet.
248
int operator == (const ACE_Weak_Bound_Ptr<X, ACE_LOCK> &r) const;
250
/// Equality operator that returns 1 if the ACE_Weak_Bound_Ptr and
251
/// ACE_Strong_Bound_Ptr objects point to the same underlying
254
* @note It also returns 1 if both objects have just been
255
* instantiated and not used yet.
257
int operator == (const ACE_Strong_Bound_Ptr<X, ACE_LOCK> &r) const;
259
/// Equality operator that returns 1 if the ACE_Weak_Bound_Ptr and
260
/// the raw pointer point to the same underlying object.
261
int operator == (X *p) const;
263
/// Inequality operator, which is the opposite of equality.
264
int operator != (const ACE_Weak_Bound_Ptr<X, ACE_LOCK> &r) const;
266
/// Inequality operator, which is the opposite of equality.
267
int operator != (const ACE_Strong_Bound_Ptr<X, ACE_LOCK> &r) const;
269
/// Inequality operator, which is the opposite of equality.
270
int operator != (X *p) const;
272
/// Redirection operator.
274
* It returns a temporary strong pointer and makes use of the
275
* chaining properties of operator-> to ensure that the underlying
276
* object does not disappear while you are using it. If you are
277
* certain of the lifetimes of the object, and do not want to incur
278
* the locking overhead, then use the unsafe_get method instead.
280
ACE_Strong_Bound_Ptr<X, ACE_LOCK> operator-> (void) const;
282
/// Obtain a strong pointer corresponding to this weak pointer. This
283
/// function is useful to create a temporary strong pointer for
284
/// conversion to a reference.
285
ACE_Strong_Bound_Ptr<X, ACE_LOCK> strong (void) const;
287
/// Get the pointer value. Warning: this does not affect the
288
/// reference count of the underlying object, so it may disappear on
289
/// you while you are using it if you are not careful.
290
X *unsafe_get (void) const;
292
/// Resets the ACE_Weak_Bound_Ptr to refer to a different underlying
294
void reset (X *p = 0);
296
/// Increment the reference count on the underlying object.
298
* Returns the new reference count on the object. This function may
299
* be used to integrate the bound pointers into an external
300
* reference counting mechanism such as those used by COM or CORBA
305
/// Decrement the reference count on the underlying object, which is deleted
306
/// if the count has reached zero.
308
* Returns the new reference count on the object. This function may
309
* be used to integrate the bound pointers into an external
310
* reference counting mechanism such as those used by COM or CORBA
313
int remove_ref (void);
315
/// Allows us to check for NULL on all ACE_Weak_Bound_Ptr objects.
316
int null (void) const;
318
/// Declare the dynamic allocation hooks.
319
ACE_ALLOC_HOOK_DECLARE;
322
friend class ACE_Strong_Bound_Ptr<X, ACE_LOCK>;
324
/// The ACE_Bound_Ptr_Counter type.
325
typedef ACE_Bound_Ptr_Counter<ACE_LOCK> COUNTER;
327
/// The reference counter.
330
/// The underlying object.
334
#include "ace/Bound_Ptr.i"
336
#include /**/ "ace/post.h"
338
#endif /* ACE_BOUND_PTR_H */