RefCount.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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#include <utility>
19
26template <class C>
28{
29
30public:
33 template<typename... Args>
34 inline static auto Make(Args&&... args) {
35 return RefCount<C>(new C(std::forward<Args>(args)...));
36 }
37 RefCount () : p_ (nullptr) {}
38
39 RefCount (C const *p) : p_(p) { reference (*this); }
40
43 }
44
45 RefCount (const RefCount &p) : p_(p.p_) {
46 reference (p);
47 }
48
49 RefCount (RefCount &&p) : p_(std::move(p.p_)) {
50 p.p_=nullptr;
51 }
52
54 template <class Other>
56 reference(*this);
57 }
58
60 // DO NOT CHANGE THE ORDER HERE!!!
61 // This preserves semantics on self assignment
62 C const *newP_ = p.p_;
63 reference(p);
64 dereference(newP_);
65 return *this;
66 }
67
69 if (this != &p) {
70 dereference(p.p_);
71 p.p_ = nullptr;
72 }
73 return *this;
74 }
75
76 RefCount &operator =(std::nullptr_t) { dereference(); return *this; }
77
78 explicit operator bool() const { return p_; }
79
80 bool operator !() const { return !p_; }
81
82 C * operator-> () const {return const_cast<C *>(p_); }
83
84 C & operator * () const {
85 assert(p_);
86 return *const_cast<C *>(p_);
87 }
88
89 C * getRaw() const { return const_cast<C *>(p_); }
90
91 bool operator == (const RefCount& p) const {
92 return p.p_ == p_;
93 }
94
95 bool operator != (const RefCount &p) const {
96 return p.p_ != p_;
97 }
98
99 template <class Other>
100 bool operator ==(const Other * const p) const
101 {
102 return p == p_;
103 }
104
105 template <class Other>
106 bool operator !=(const Other * const p) const
107 {
108 return p != p_;
109 }
110
111private:
112 void dereference(C const *newP = nullptr) {
113 /* Setting p_ first is important:
114 * we may be freed ourselves as a result of
115 * delete p_;
116 */
117 C const (*tempP_) (p_);
118 p_ = newP;
119
120 if (tempP_ && tempP_->unlock() == 0)
121 delete tempP_;
122 }
123
124 void reference (const RefCount& p) {
125 if (p.p_)
126 p.p_->lock();
127 }
128
129 C const *p_;
130
131};
132
133template <class C>
134inline std::ostream &operator <<(std::ostream &os, const RefCount<C> &p)
135{
136 if (p != nullptr)
137 return os << p.getRaw() << '*' << p->LockCount();
138 else
139 return os << "NULL";
140}
141
142#endif /* SQUID_REFCOUNT_H_ */
143
std::ostream & operator<<(std::ostream &os, const RefCount< C > &p)
Definition: RefCount.h:134
#define assert(EX)
Definition: assert.h:17
RefCount(C const *p)
Definition: RefCount.h:39
void reference(const RefCount &p)
Definition: RefCount.h:124
C * operator->() const
Definition: RefCount.h:82
RefCount(const RefCount &p)
Definition: RefCount.h:45
RefCount(RefCount &&p)
Definition: RefCount.h:49
bool operator!=(const RefCount &p) const
Definition: RefCount.h:95
C * getRaw() const
Definition: RefCount.h:89
RefCount & operator=(const RefCount &p)
Definition: RefCount.h:59
void dereference(C const *newP=nullptr)
Definition: RefCount.h:112
~RefCount()
Definition: RefCount.h:41
bool operator!() const
Definition: RefCount.h:80
static auto Make(Args &&... args)
Definition: RefCount.h:34
C const * p_
Definition: RefCount.h:129
bool operator==(const RefCount &p) const
Definition: RefCount.h:91
RefCount(const RefCount< Other > &p)
Base::Pointer = Derived::Pointer.
Definition: RefCount.h:55
C & operator*() const
Definition: RefCount.h:84
RefCount()
Definition: RefCount.h:37
static uint32 C
Definition: md4.c:43
pid_t Other()
Definition: Instance.cc:128
STL namespace.

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors