~ubuntu-branches/ubuntu/breezy/ace/breezy

« back to all changes in this revision

Viewing changes to ace/Bound_Ptr.h

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad, Benjamin Montgomery, Adam Conrad
  • Date: 2005-09-18 22:51:38 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge) (0.1.2 woody)
  • Revision ID: james.westby@ubuntu.com-20050918225138-seav22q6fyylb536
Tags: 5.4.7-3ubuntu1
[ Benjamin Montgomery ]
* Added a patch for amd64 and powerpc that disables the compiler
  option -fvisibility-inlines-hidden

[ Adam Conrad ]
* Added DPATCH_OPTION_CPP=1 to debian/patches/00options to make
  Benjamin's above changes work correctly with dpatch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// -*- C++ -*-
2
 
 
3
 
//=============================================================================
4
 
/**
5
 
 *  @file    Bound_Ptr.h
6
 
 *
7
 
 *  Bound_Ptr.h,v 4.6 2003/07/19 19:04:10 dhinton Exp
8
 
 *
9
 
 *  @author Christopher Kohlhoff <chris@kohlhoff.com>
10
 
 */
11
 
//=============================================================================
12
 
 
13
 
#ifndef ACE_BOUND_PTR_H
14
 
#define ACE_BOUND_PTR_H
15
 
 
16
 
#include /**/ "ace/pre.h"
17
 
 
18
 
#include "ace/config-all.h"
19
 
 
20
 
#if !defined (ACE_LACKS_PRAGMA_ONCE)
21
 
# pragma once
22
 
#endif /* ACE_LACKS_PRAGMA_ONCE */
23
 
 
24
 
#include "ace/Auto_Ptr.h"
25
 
 
26
 
/**
27
 
 * @class ACE_Bound_Ptr_Counter
28
 
 *
29
 
 * @brief An ACE_Bound_Ptr_Counter<ACE_LOCK> object encapsulates an
30
 
 *        object reference count.
31
 
 *
32
 
 * Do not use this class directly, use ACE_Strong_Bound_Ptr or
33
 
 * ACE_Weak_Bound_Ptr instead.
34
 
 */
35
 
template <class ACE_LOCK>
36
 
class ACE_Bound_Ptr_Counter
37
 
{
38
 
public:
39
 
  /// Declare the dynamic allocation hooks.
40
 
  ACE_ALLOC_HOOK_DECLARE;
41
 
 
42
 
  ACE_Bound_Ptr_Counter (int init_obj_ref_count = 0);
43
 
  ~ACE_Bound_Ptr_Counter (void);
44
 
 
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);
48
 
 
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);
53
 
 
54
 
  /// Decreases both the object and counter reference counts and
55
 
  /// deletes whichever has no more references. Returns the new object
56
 
  /// reference count.
57
 
  static int detach_strong (ACE_Bound_Ptr_Counter<ACE_LOCK> *counter);
58
 
 
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);
62
 
 
63
 
  /// Increase the counter reference count and return argument.
64
 
  static void attach_weak (ACE_Bound_Ptr_Counter<ACE_LOCK> *counter);
65
 
 
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);
69
 
 
70
 
  /// Determine whether the object has been deleted.
71
 
  static int object_was_deleted (ACE_Bound_Ptr_Counter<ACE_LOCK> *counter);
72
 
 
73
 
private:
74
 
 
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);
78
 
 
79
 
private:
80
 
 
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.
84
 
  int obj_ref_count_;
85
 
 
86
 
  /// Reference count of this counter.
87
 
  int self_ref_count_;
88
 
 
89
 
  /// Mutex variable to synchronize access to the reference counts.
90
 
  ACE_LOCK lock_;
91
 
};
92
 
 
93
 
// Forward decl.
94
 
template <class X, class ACE_LOCK> class ACE_Weak_Bound_Ptr;
95
 
 
96
 
/**
97
 
 * @class ACE_Strong_Bound_Ptr
98
 
 *
99
 
 * @brief This class implements support for a reference counted
100
 
 *        pointer.
101
 
 *
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.
107
 
 */
108
 
template <class X, class ACE_LOCK>
109
 
class ACE_Strong_Bound_Ptr
110
 
{
111
 
public:
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);
115
 
 
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);
119
 
 
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);
122
 
 
123
 
  /// Constructor binds <this> and <r> to the same object.
124
 
  ACE_Strong_Bound_Ptr (const ACE_Weak_Bound_Ptr<X, ACE_LOCK> &r);
125
 
 
126
 
  /// Destructor.
127
 
  ~ACE_Strong_Bound_Ptr (void);
128
 
 
129
 
  /// Assignment operator that binds <this> and <r> to the same object.
130
 
  void operator = (const ACE_Strong_Bound_Ptr<X, ACE_LOCK> &r);
131
 
 
132
 
  /// Assignment operator that binds <this> and <r> to the same object.
133
 
  void operator = (const ACE_Weak_Bound_Ptr<X, ACE_LOCK> &r);
134
 
 
135
 
  /// Equality operator that returns 1 if both ACE_Strong_Bound_Ptr
136
 
  /// instances point to the same underlying object.
137
 
  /**
138
 
   * @note It also returns 1 if both objects have just been
139
 
   *       instantiated and not used yet.
140
 
   */
141
 
  int operator == (const ACE_Strong_Bound_Ptr<X, ACE_LOCK> &r) const;
142
 
 
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.
145
 
  /**
146
 
   *
147
 
   * @note It also returns 1 if both objects have just been
148
 
   *       instantiated and not used yet.
149
 
   */
150
 
  int operator == (const ACE_Weak_Bound_Ptr<X, ACE_LOCK> &r) const;
151
 
 
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;
155
 
 
156
 
  /// Inequality operator, which is the opposite of equality.
157
 
  int operator != (const ACE_Strong_Bound_Ptr<X, ACE_LOCK> &r) const;
158
 
 
159
 
  /// Inequality operator, which is the opposite of equality.
160
 
  int operator != (const ACE_Weak_Bound_Ptr<X, ACE_LOCK> &r) const;
161
 
 
162
 
  /// Inequality operator, which is the opposite of equality.
163
 
  int operator != (X *p) const;
164
 
 
165
 
  /// Redirection operator
166
 
  X *operator-> (void) const;
167
 
 
168
 
  /// Dereference operator
169
 
  X &operator * (void) const;
170
 
 
171
 
  /// Get the pointer value.
172
 
  X *get (void) const;
173
 
 
174
 
  /// Resets the ACE_Strong_Bound_Ptr to refer to a different
175
 
  /// underlying object.
176
 
  void reset (X *p = 0);
177
 
 
178
 
  /// Resets the ACE_Strong_Bound_Ptr to refer to a different
179
 
  /// underlying object, ownership of which is stolen from the
180
 
  /// auto_ptr.
181
 
  void reset (auto_ptr<X> p);
182
 
 
183
 
  /// Allows us to check for NULL on all ACE_Strong_Bound_Ptr
184
 
  /// objects.
185
 
  int null (void) const;
186
 
 
187
 
  /// Declare the dynamic allocation hooks.
188
 
  ACE_ALLOC_HOOK_DECLARE;
189
 
 
190
 
private:
191
 
  friend class ACE_Weak_Bound_Ptr<X, ACE_LOCK>;
192
 
 
193
 
  /// The ACE_Bound_Ptr_Counter type.
194
 
  typedef ACE_Bound_Ptr_Counter<ACE_LOCK> COUNTER;
195
 
 
196
 
  /// The reference counter.
197
 
  COUNTER *counter_;
198
 
 
199
 
  /// The underlying object.
200
 
  X *ptr_;
201
 
};
202
 
 
203
 
/**
204
 
 * @class ACE_Weak_Bound_Ptr
205
 
 *
206
 
 * @brief This class implements support for a weak pointer that complements
207
 
 * ACE_Strong_Bound_Ptr.
208
 
 *
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.
218
 
 */
219
 
template <class X, class ACE_LOCK>
220
 
class ACE_Weak_Bound_Ptr
221
 
{
222
 
public:
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);
226
 
 
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);
229
 
 
230
 
  /// Constructor binds <this> and <r> to the same object.
231
 
  ACE_Weak_Bound_Ptr (const ACE_Strong_Bound_Ptr<X, ACE_LOCK> &r);
232
 
 
233
 
  /// Destructor.
234
 
  ~ACE_Weak_Bound_Ptr (void);
235
 
 
236
 
  /// Assignment operator that binds <this> and <r> to the same object.
237
 
  void operator = (const ACE_Weak_Bound_Ptr<X, ACE_LOCK> &r);
238
 
 
239
 
  /// Assignment operator that binds <this> and <r> to the same object.
240
 
  void operator = (const ACE_Strong_Bound_Ptr<X, ACE_LOCK> &r);
241
 
 
242
 
  /// Equality operator that returns 1 if both ACE_Weak_Bound_Ptr
243
 
  /// objects point to the same underlying object.
244
 
  /**
245
 
   * @note It also returns 1 if both objects have just been
246
 
   *       instantiated and not used yet.
247
 
   */
248
 
  int operator == (const ACE_Weak_Bound_Ptr<X, ACE_LOCK> &r) const;
249
 
 
250
 
  /// Equality operator that returns 1 if the ACE_Weak_Bound_Ptr and
251
 
  /// ACE_Strong_Bound_Ptr objects point to the same underlying
252
 
  /// object.
253
 
  /**
254
 
   * @note It also returns 1 if both objects have just been
255
 
   *       instantiated and not used yet.
256
 
   */
257
 
  int operator == (const ACE_Strong_Bound_Ptr<X, ACE_LOCK> &r) const;
258
 
 
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;
262
 
 
263
 
  /// Inequality operator, which is the opposite of equality.
264
 
  int operator != (const ACE_Weak_Bound_Ptr<X, ACE_LOCK> &r) const;
265
 
 
266
 
  /// Inequality operator, which is the opposite of equality.
267
 
  int operator != (const ACE_Strong_Bound_Ptr<X, ACE_LOCK> &r) const;
268
 
 
269
 
  /// Inequality operator, which is the opposite of equality.
270
 
  int operator != (X *p) const;
271
 
 
272
 
  /// Redirection operator.
273
 
  /**
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.
279
 
   */
280
 
  ACE_Strong_Bound_Ptr<X, ACE_LOCK> operator-> (void) const;
281
 
 
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;
286
 
 
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;
291
 
 
292
 
  /// Resets the ACE_Weak_Bound_Ptr to refer to a different underlying
293
 
  /// object.
294
 
  void reset (X *p = 0);
295
 
 
296
 
  /// Increment the reference count on the underlying object.
297
 
  /**
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
301
 
   * servants.
302
 
   */
303
 
  int add_ref (void);
304
 
 
305
 
  /// Decrement the reference count on the underlying object, which is deleted
306
 
  /// if the count has reached zero.
307
 
  /**
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
311
 
   * servants.
312
 
   */
313
 
  int remove_ref (void);
314
 
 
315
 
  /// Allows us to check for NULL on all ACE_Weak_Bound_Ptr objects.
316
 
  int null (void) const;
317
 
 
318
 
  /// Declare the dynamic allocation hooks.
319
 
  ACE_ALLOC_HOOK_DECLARE;
320
 
 
321
 
private:
322
 
  friend class ACE_Strong_Bound_Ptr<X, ACE_LOCK>;
323
 
 
324
 
  /// The ACE_Bound_Ptr_Counter type.
325
 
  typedef ACE_Bound_Ptr_Counter<ACE_LOCK> COUNTER;
326
 
 
327
 
  /// The reference counter.
328
 
  COUNTER *counter_;
329
 
 
330
 
  /// The underlying object.
331
 
  X *ptr_;
332
 
};
333
 
 
334
 
#include "ace/Bound_Ptr.i"
335
 
 
336
 
#include /**/ "ace/post.h"
337
 
 
338
 
#endif /* ACE_BOUND_PTR_H */