]> git.ipfire.org Git - thirdparty/squid.git/blob - src/base/RefCount.h
Fix printf format for size_t and mb_size_t
[thirdparty/squid.git] / src / base / RefCount.h
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section -- Refcount allocator */
10
11 #ifndef SQUID_REFCOUNT_H_
12 #define SQUID_REFCOUNT_H_
13
14 // reference counting requires the Lock API on base classes
15 #include "base/Lock.h"
16
17 #include <iostream>
18
19 /**
20 * Template for Reference Counting pointers.
21 *
22 * Objects of type 'C' must inherit from 'RefCountable' in base/Lock.h
23 * which provides the locking interface used by reference counting.
24 */
25 template <class C>
26 class RefCount
27 {
28
29 public:
30 RefCount () : p_ (NULL) {}
31
32 RefCount (C const *p) : p_(p) { reference (*this); }
33
34 ~RefCount() {
35 dereference();
36 }
37
38 RefCount (const RefCount &p) : p_(p.p_) {
39 reference (p);
40 }
41
42 #if __cplusplus >= 201103L
43 RefCount (RefCount &&p) : p_(std::move(p.p_)) {
44 p.p_=NULL;
45 }
46 #endif
47
48 RefCount& operator = (const RefCount& p) {
49 // DO NOT CHANGE THE ORDER HERE!!!
50 // This preserves semantics on self assignment
51 C const *newP_ = p.p_;
52 reference(p);
53 dereference(newP_);
54 return *this;
55 }
56
57 #if __cplusplus >= 201103L
58 RefCount& operator = (RefCount&& p) {
59 if (this != &p) {
60 dereference(p.p_);
61 p.p_ = NULL;
62 }
63 return *this;
64 }
65 #endif
66
67 bool operator !() const { return !p_; }
68
69 C * operator-> () const {return const_cast<C *>(p_); }
70
71 C & operator * () const {return *const_cast<C *>(p_); }
72
73 C const * getRaw() const {return p_; }
74
75 C * getRaw() {return const_cast<C *>(p_); }
76
77 bool operator == (const RefCount& p) const {
78 return p.p_ == p_;
79 }
80
81 bool operator != (const RefCount &p) const {
82 return p.p_ != p_;
83 }
84
85 private:
86 void dereference(C const *newP = NULL) {
87 /* Setting p_ first is important:
88 * we may be freed ourselves as a result of
89 * delete p_;
90 */
91 C const (*tempP_) (p_);
92 p_ = newP;
93
94 if (tempP_ && tempP_->unlock() == 0)
95 delete tempP_;
96 }
97
98 void reference (const RefCount& p) {
99 if (p.p_)
100 p.p_->lock();
101 }
102
103 C const *p_;
104
105 };
106
107 template <class C>
108 inline std::ostream &operator <<(std::ostream &os, const RefCount<C> &p)
109 {
110 if (p != NULL)
111 return os << p.getRaw() << '*' << p->LockCount();
112 else
113 return os << "NULL";
114 }
115
116 #endif /* SQUID_REFCOUNT_H_ */
117