pointer.hpp 5.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// Copyright 2013 Francisco Jerez
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

#ifndef CLOVER_UTIL_POINTER_HPP
#define CLOVER_UTIL_POINTER_HPP

#include <atomic>

namespace clover {
   ///
   /// Base class for objects that support reference counting.
   ///
   class ref_counter {
   public:
      ref_counter() : _ref_count(1) {}

      unsigned
      ref_count() {
         return _ref_count;
      }

      void
      retain() {
         _ref_count++;
      }

      bool
      release() {
         return (--_ref_count) == 0;
      }

   private:
      std::atomic<unsigned> _ref_count;
   };

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
   ///
   /// Simple reference to a clover::ref_counter object.  Unlike
   /// clover::intrusive_ptr and clover::intrusive_ref, it does nothing
   /// special when the reference count drops to zero.
   ///
   class ref_holder {
   public:
      ref_holder(ref_counter &o) : p(&o) {
         p->retain();
      }

      ref_holder(const ref_holder &ref) :
         ref_holder(*ref.p) {
      }

      ref_holder(ref_holder &&ref) :
         p(ref.p) {
         ref.p = NULL;
      }

      ~ref_holder() {
         if (p)
            p->release();
      }

      ref_holder &
      operator=(ref_holder ref) {
         std::swap(ref.p, p);
         return *this;
      }

      bool
      operator==(const ref_holder &ref) const {
         return p == ref.p;
      }

      bool
      operator!=(const ref_holder &ref) const {
         return p != ref.p;
      }

   private:
      ref_counter *p;
   };

100
101
102
103
104
   ///
   /// Intrusive smart pointer for objects that implement the
   /// clover::ref_counter interface.
   ///
   template<typename T>
105
   class intrusive_ptr {
106
   public:
107
108
109
      intrusive_ptr(T *q = NULL) : p(q) {
         if (p)
            p->retain();
110
111
      }

112
113
      intrusive_ptr(const intrusive_ptr &ptr) :
         intrusive_ptr(ptr.p) {
114
115
      }

116
117
118
      intrusive_ptr(intrusive_ptr &&ptr) :
         p(ptr.p) {
         ptr.p = NULL;
119
120
      }

121
      ~intrusive_ptr() {
122
123
124
125
         if (p && p->release())
            delete p;
      }

126
127
128
      intrusive_ptr &
      operator=(intrusive_ptr ptr) {
         std::swap(ptr.p, p);
129
130
131
         return *this;
      }

132
133
134
135
136
137
138
139
140
141
      bool
      operator==(const intrusive_ptr &ref) const {
         return p == ref.p;
      }

      bool
      operator!=(const intrusive_ptr &ref) const {
         return p != ref.p;
      }

142
143
144
145
146
147
148
149
150
151
      T &
      operator*() const {
         return *p;
      }

      T *
      operator->() const {
         return p;
      }

152
153
154
155
156
      T *
      operator()() const {
         return p;
      }

157
158
159
160
      explicit operator bool() const {
         return p;
      }

161
162
163
164
      explicit operator T *() const {
         return p;
      }

165
166
167
168
   private:
      T *p;
   };

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
   ///
   /// Intrusive smart reference for objects that implement the
   /// clover::ref_counter interface.
   ///
   template<typename T>
   class intrusive_ref {
   public:
      intrusive_ref(T &o) : p(&o) {
         p->retain();
      }

      intrusive_ref(const intrusive_ref &ref) :
         intrusive_ref(*ref.p) {
      }

      intrusive_ref(intrusive_ref &&ref) :
         p(ref.p) {
         ref.p = NULL;
      }

      ~intrusive_ref() {
         if (p && p->release())
            delete p;
      }

      intrusive_ref &
      operator=(intrusive_ref ref) {
         std::swap(ref.p, p);
         return *this;
      }

      bool
      operator==(const intrusive_ref &ref) const {
         return p == ref.p;
      }

      bool
      operator!=(const intrusive_ref &ref) const {
         return p != ref.p;
      }

      T &
      operator()() const {
         return *p;
      }

      operator T &() const {
         return *p;
      }

   private:
      T *p;
   };

   ///
224
225
   /// Initialize a clover::intrusive_ref from a newly created object
   /// using the specified constructor arguments.
226
   ///
227
228
229
230
231
   template<typename T, typename... As>
   intrusive_ref<T>
   create(As &&... as) {
      intrusive_ref<T> ref { *new T(std::forward<As>(as)...) };
      ref().release();
232
233
234
      return ref;
   }

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
   ///
   /// Class that implements the usual pointer interface but in fact
   /// contains the object it seems to be pointing to.
   ///
   template<typename T>
   class pseudo_ptr {
   public:
      pseudo_ptr(T x) : x(x) {
      }

      pseudo_ptr(const pseudo_ptr &p) : x(p.x) {
      }

      pseudo_ptr &
      operator=(const pseudo_ptr &p) {
         x = p.x;
         return *this;
      }

      T &
      operator*() {
         return x;
      }

      T *
      operator->() {
         return &x;
      }

      explicit operator bool() const {
         return true;
      }

   private:
      T x;
   };
}

#endif