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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors