-
Notifications
You must be signed in to change notification settings - Fork 15
/
smart_ptr.h
544 lines (457 loc) · 14.2 KB
/
smart_ptr.h
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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
224
225
226
227
228
229
230
231
232
233
234
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
* strong_ptr - simple reference counted pointer.
*
* Copyright (c) 2013, oddman
*
* The is a non-intrusive implementation that allocates an additional
* int and pointer for every counted object.
*
* Permission to use, copy, modify, and/or distribute this software for
* any purpose with or without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef __SMART_PTR_H__
#define __SMART_PTR_H__
namespace smart_ptr {
class ref_count
{
public:
ref_count() : m_strong_ref_count(1), m_weak_ref_count(0)
{
}
~ref_count()
{
}
// increment use count
int inc_ref()
{
return ++m_strong_ref_count;
}
// increment weak reference count
int inc_weak_ref()
{
return ++m_weak_ref_count;
}
// decrement use count
int dec_ref()
{
int nRs = 0;
if (m_strong_ref_count > 0) {
nRs = --m_strong_ref_count;
}
return nRs;
}
// decrement weak reference count
int dec_weak_ref()
{
int nRs = 0;
if (m_weak_ref_count > 0) {
nRs = --m_weak_ref_count;
}
return nRs;
}
// return use count
int get_ref_count() const
{
return m_strong_ref_count;
}
// return true if _Uses == 0
bool expired() const
{
return (get_ref_count() == 0);
}
int get_weak_ref_count() const
{
return m_weak_ref_count;
}
private:
int m_strong_ref_count;
int m_weak_ref_count;
};
#if defined(WIN32) || defined(_WIN32)
template <class T> class _NoAddRefReleaseOnComPtr : public T {
private:
virtual unsigned long __stdcall AddRef(void) = 0;
virtual unsigned long __stdcall Release(void) = 0;
};
#endif // defined(WIN32) || defined(_WIN32)
// base class for strong_ptr and weak_ptr
template<class T, bool is_strong, typename mem_mgr>
class base_ptr
{
public:
explicit base_ptr(T *p=0) : m_counter(0), m_ptr(p)
{
if (m_ptr) {
if (is_strong) {
// allocate a new ref_count
m_counter = new ref_count;
}
}
}
base_ptr(const base_ptr& rhs) : m_counter(0), m_ptr(0)
{
acquire(rhs);
}
template<class Q, bool b, typename mem_mgr2>
base_ptr(const base_ptr<Q, b, mem_mgr2> &rhs) : m_counter(0), m_ptr(0)
{
acquire(rhs);
}
virtual ~base_ptr()
{
release();
}
operator T*() const throw() { return m_ptr; }
T& operator*() const throw() { return *m_ptr; }
#if defined(WIN32) || defined(_WIN32)
_NoAddRefReleaseOnComPtr<T>* operator->() const throw() { return (_NoAddRefReleaseOnComPtr<T>*)m_ptr; }
#else
T* operator->() const throw() { return m_ptr; }
#endif // defined(WIN32) || defined(_WIN32)
T* get() const throw() { return m_ptr; }
bool unique() const throw()
{ return (m_counter ? (1 == m_counter->get_ref_count()) : true); }
void reset(T *p=0)
{
base_ptr<T, is_strong, mem_mgr> ptr(p);
reset(ptr);
}
template <class Q, bool b, typename mem_mgr2>
void reset(const base_ptr<Q, b, mem_mgr2> &rhs)
{
if ((void *)this != (void *)&rhs) {
release();
acquire(rhs);
}
}
int use_count(void) const
{
int nRs = 0;
if (m_counter) {
nRs = m_counter->get_ref_count();
}
return nRs;
}
// swap pointers
template <class Q, bool b, typename mem_mgr2>
void swap(base_ptr<Q, b, mem_mgr2> & rhs)
{
private_swap(m_counter, rhs.m_counter);
private_swap(m_ptr, rhs.m_ptr);
}
base_ptr& operator=(const base_ptr &rhs)
{
reset(rhs);
return *this;
}
template <class Q, bool b, typename mem_mgr2>
base_ptr& operator=(const base_ptr<Q, b, mem_mgr2> &rhs)
{
reset(rhs);
return *this;
}
protected:
ref_count *m_counter;
T * m_ptr;
template <typename TP1, typename TP2>
static void private_swap(TP1 &obj1, TP2 &obj2)
{
TP1 tmp = obj1;
obj1 = static_cast<TP1>(obj2);
obj2 = static_cast<TP2>(tmp);
}
template <class Q, bool b, typename mem_mgr2>
void acquire(const base_ptr<Q, b, mem_mgr2> & rhs) throw()
{
if (rhs.m_counter && rhs.m_counter->get_ref_count()) {
m_counter = rhs.m_counter;
if (is_strong) {
m_counter->inc_ref();
} else {
m_counter->inc_weak_ref();
}
m_ptr = static_cast<T*>(rhs.m_ptr);
}
}
// decrement the count, delete if it is 0
void release(void)
{
if (m_counter) {
if (is_strong) {
if (0 == m_counter->dec_ref()) {
mem_mgr::deallocate(m_ptr);
m_ptr = 0;
}
} else {
m_counter->dec_weak_ref();
}
if (0 == m_counter->get_ref_count() && 0==m_counter->get_weak_ref_count()) {
delete m_counter;
}
m_counter = 0;
}
if (m_ptr) {
m_ptr = 0;
}
}
template<class Q, bool b, typename mem_mgr2> friend class base_ptr;
};
template<class T, bool bx, class Q, bool by, typename mem_mgr1, typename mem_mgr2>
bool operator<(const base_ptr<T, bx, mem_mgr1> &lhs, const base_ptr<Q, by, mem_mgr2> &rhs)
{
// test if left pointer < right pointer
return lhs.get() < rhs.get();
}
template <class T, typename mem_mgr> class weak_ptr;
template<typename T>
class std_mem_mgr {
public:
static void deallocate(T *p) { delete p; }
static T * allocate(void) { return new T(); }
template<typename A1> static T * allocate(A1 const &a1) { return new T(a1); }
template<typename A1, typename A2> static T * allocate(A1 const &a1, A2 const &a2) { return new T(a1, a2); }
template<typename A1, typename A2, typename A3> static T * allocate(A1 const &a1, A2 const &a2, A3 const &a3) { return new T(a1, a2, a3); }
template<typename A1, typename A2, typename A3, typename A4> static T * allocate(A1 const &a1, A2 const &a2, A3 const &a3, A4 const &a4) { return new T(a1, a2, a3, a4); }
template<typename A1, typename A2, typename A3, typename A4, typename A5> static T * allocate(A1 const &a1, A2 const &a2, A3 const &a3, A4 const &a4, A5 const &a5) { return new T(a1, a2, a3, a4, a5); }
template<typename A1, typename A2, typename A3, typename A4, typename A5, typename A6> static T * allocate(A1 const &a1, A2 const &a2, A3 const &a3, A4 const &a4, A5 const &a5, A6 const &a6) { return new T(a1, a2, a3, a4, a5, a6); }
};
template <class T, typename mem_mgr=std_mem_mgr<T> >
class strong_ptr : public base_ptr<T, true, mem_mgr>
{
typedef base_ptr<T, true, mem_mgr> baseClass;
public:
explicit strong_ptr(T* p = 0) : baseClass(p)
{
}
strong_ptr(const strong_ptr& rhs) : baseClass(rhs)
{
}
template<class Q, typename mem_mgr2>
strong_ptr(const strong_ptr<Q, mem_mgr2> &rhs) : baseClass(rhs)
{
}
// construct strong_ptr object that owns resource *rhs
template<class Q, typename mem_mgr2>
explicit strong_ptr(const weak_ptr<Q, mem_mgr2> &rhs) : baseClass(rhs)
{
}
~strong_ptr()
{
}
strong_ptr& operator=(const strong_ptr &rhs)
{
baseClass::operator = (rhs);
return *this;
}
template <class Q, typename mem_mgr2>
strong_ptr& operator=(const strong_ptr<Q, mem_mgr2> &rhs)
{
baseClass::operator = (rhs);
return *this;
}
template <class Q, typename mem_mgr2>
strong_ptr& operator=(const weak_ptr<Q, mem_mgr2> &rhs)
{
baseClass::operator = (rhs);
return *this;
}
};
template <class T, typename mem_mgr=std_mem_mgr<T> >
class weak_ptr : public base_ptr<T, false, mem_mgr>
{
typedef base_ptr<T, false, mem_mgr> baseClass;
public:
// construct empty weak_ptr object
weak_ptr()
{
}
// construct weak_ptr object for resource owned by rhs
template<class Q, typename mem_mgr2>
weak_ptr(const strong_ptr<Q, mem_mgr2> &rhs) : baseClass(rhs)
{
}
// construct weak_ptr object for resource pointed to by rhs
weak_ptr(const weak_ptr &rhs) : baseClass(rhs)
{
}
// construct weak_ptr object for resource pointed to by rhs
template<class Q, typename mem_mgr2>
weak_ptr(const weak_ptr<Q, mem_mgr2> &rhs) : baseClass(rhs)
{
}
~weak_ptr()
{
}
weak_ptr& operator=(const weak_ptr &rhs)
{
baseClass::operator =(rhs);
return *this;
}
template <class Q, typename mem_mgr2>
weak_ptr& operator=(const weak_ptr<Q, mem_mgr2> &rhs)
{
baseClass::operator = (rhs);
return *this;
}
template <class Q, typename mem_mgr2>
weak_ptr& operator=(const strong_ptr<Q, mem_mgr2> &rhs)
{
baseClass::operator = (rhs);
return *this;
}
// return true if resource no longer exists
bool expired() const
{
return baseClass::m_counter ? baseClass::m_counter->expired() : true;
}
// convert to strong_ptr
strong_ptr<T, mem_mgr> lock() const
{
return strong_ptr<T, mem_mgr>(*this);
}
private:
operator T*() const throw();
T& operator*() const throw();
T* operator->() const throw();
T* get() const throw();
};
//////////////////////////////////////////////////////////////////////////
//
// function make_strong_ptr group
//
template <typename T, typename mem_mgr=std_mem_mgr<T> >
class make_strong_ptr
{
public:
typedef strong_ptr<T, mem_mgr> pointer_type;
static pointer_type generate(void)
{
return pointer_type ( mem_mgr::allocate() );
}
template <typename A1>
static pointer_type generate(A1 const &a1)
{
return pointer_type ( mem_mgr::allocate(a1) );
}
template <typename A1, typename A2>
static pointer_type generate(A1 const &a1, A2 const &a2)
{
return pointer_type ( mem_mgr::allocate(a1, a2) );
}
template <typename A1, typename A2, typename A3>
static pointer_type generate(A1 const &a1, A2 const &a2, A3 const &a3)
{
return pointer_type ( mem_mgr::allocate(a1, a2, a3) );
}
template <typename A1, typename A2, typename A3, typename A4>
static pointer_type generate(A1 const &a1, A2 const &a2, A3 const &a3, A4 const &a4)
{
return pointer_type ( mem_mgr::allocate(a1, a2, a3, a4) );
}
template <typename A1, typename A2, typename A3, typename A4, typename A5>
static pointer_type generate(A1 const &a1, A2 const &a2, A3 const &a3, A4 const &a4, A5 const &a5)
{
return pointer_type ( mem_mgr::allocate(a1, a2, a3, a4, a5) );
}
template <typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
static pointer_type generate(A1 const &a1, A2 const &a2, A3 const &a3, A4 const &a4, A5 const &a5, A6 const &a6)
{
return pointer_type ( mem_mgr::allocate(a1, a2, a3, a4, a5, a6) );
}
};
//////////////////////////////////////////////////////////////////////////
// COM pointer support
//
template<typename T>
class com_mem_mgr {
public:
static void deallocate(T *p) { p->Release(); }
static T * allocate(T *p) { p->AddRef(); return p; } // we must hold the AddRef-ed pointer
};
template <typename T>
strong_ptr<T, com_mem_mgr<T> > make_com_strong_ptr(const T *rawPtr) {
return make_strong_ptr<T, com_mem_mgr<T> >::template generate<T*>(const_cast<T * &>(rawPtr));
}
//////////////////////////////////////////////////////////////////////////
// auto-released array support
//
template<typename T>
class array_mem_mgr {
public:
static void deallocate(T *p) { delete []p; }
static T * allocate(int n) { return new T[n]; }
};
template <class T, typename mem_mgr=array_mem_mgr<T> >
class strong_array : public base_ptr<T, true, mem_mgr>
{
typedef base_ptr<T, true, mem_mgr> baseClass;
public:
explicit strong_array(T* p = 0) : baseClass(p)
{
}
strong_array(const strong_array& rhs) : baseClass(rhs)
{
}
template<class Q>
strong_array(const strong_array<Q, mem_mgr> &rhs) : baseClass(rhs)
{
}
~strong_array()
{
}
const T & operator[](int i) const
{
return baseClass::get()[i];
}
T & operator[](int i)
{
return baseClass::get()[i];
}
strong_array& operator=(const strong_array &rhs)
{
baseClass::operator = (rhs);
return *this;
}
template <class Q>
strong_array& operator=(const strong_array<Q, mem_mgr> &rhs)
{
baseClass::operator = (rhs);
return *this;
}
private:
T& operator*() const throw();
T* operator->() const throw();
};
//////////////////////////////////////////////////////////////////////////
// define macros
#ifndef EMPTY_NAME_SPACE
#define EMPTY_NAME_SPACE
#endif // EMPTY_NAME_SPACE
// defining COM smart pointer type
#ifndef DEFINE_COM_STRONG_PTR
#define DEFINE_COM_STRONG_PTR(NAME_SPACE_T, TYPE) \
typedef smart_ptr::strong_ptr<NAME_SPACE_T::TYPE, smart_ptr::com_mem_mgr<NAME_SPACE_T::TYPE> > TYPE##ComPtr;
#endif // DEFINE_COM_STRONG_PTR
// defining standard smart pointer type
#ifndef DEFINE_STD_STRONG_PTR
#define DEFINE_STD_STRONG_PTR(NAME_SPACE_T, TYPE) \
typedef smart_ptr::strong_ptr<NAME_SPACE_T::TYPE, smart_ptr::std_mem_mgr<NAME_SPACE_T::TYPE> > TYPE##StdPtr;
#endif // DEFINE_STD_STRONG_PTR
// defining array style smart pointer type
#ifndef DEFINE_ARR_STRONG_PTR
#define DEFINE_ARR_STRONG_PTR(NAME_SPACE_T, TYPE) \
typedef smart_ptr::strong_ptr<NAME_SPACE_T::TYPE, smart_ptr::array_mem_mgr<NAME_SPACE_T::TYPE> > TYPE##ArrPtr;
#endif // DEFINE_ARR_STRONG_PTR
}; // namespace smart_ptr
#endif // __SMART_PTR_H__