CbcPointer.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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#ifndef SQUID_CBC_POINTER_H
10#define SQUID_CBC_POINTER_H
11
12#include "base/TextException.h"
13#include "cbdata.h"
14#include "debug/Stream.h"
15
24template<class Cbc>
26{
27public:
28 CbcPointer(); // a nil pointer
29 CbcPointer(Cbc *aCbc);
33
34 Cbc *raw() const;
35 Cbc *get() const;
36 Cbc &operator *() const;
37 Cbc *operator ->() const;
38
39 // no bool operator because set() != valid()
40 bool set() const { return cbc != nullptr; }
41 Cbc *valid() const { return get(); }
42 bool operator !() const { return !valid(); }
43 bool operator ==(const CbcPointer<Cbc> &o) const { return lock == o.lock; }
44
47
49 template <typename Other>
50 CbcPointer(const CbcPointer<Other> &o): cbc(o.raw()), lock(nullptr) {
51 if (o.valid())
52 lock = cbdataReference(o->toCbdata());
53 }
54
56 template <typename Other>
58 if (this != &o) { // assignment to self
59 clear();
60 cbc = o.raw(); // so that set() is accurate
61 if (o.valid())
62 lock = cbdataReference(o->toCbdata());
63 }
64 return *this;
65 }
66
67 void clear();
68
69 std::ostream &print(std::ostream &os) const;
70
71private:
72 Cbc *cbc; // a possibly invalid pointer to a cbdata class
73 void *lock; // a valid pointer to cbc's cbdata or nil
74};
75
76template <class Cbc>
77inline
78std::ostream &operator <<(std::ostream &os, const CbcPointer<Cbc> &p)
79{
80 return p.print(os);
81}
82
83// inlined methods
84
85template<class Cbc>
86CbcPointer<Cbc>::CbcPointer(): cbc(nullptr), lock(nullptr)
87{
88}
89
90template<class Cbc>
91CbcPointer<Cbc>::CbcPointer(Cbc *aCbc): cbc(aCbc), lock(nullptr)
92{
93 if (cbc)
94 lock = cbdataReference(cbc->toCbdata());
95}
96
97template<class Cbc>
98CbcPointer<Cbc>::CbcPointer(const CbcPointer &d): cbc(d.cbc), lock(nullptr)
99{
100 if (d.lock && cbdataReferenceValid(d.lock))
102}
103
104template<class Cbc>
105CbcPointer<Cbc>::CbcPointer(CbcPointer &&d): cbc(d.cbc), lock(d.lock)
106{
107 d.cbc = nullptr;
108 d.lock = nullptr;
109}
110
111template<class Cbc>
113{
114 clear();
115}
116
117template<class Cbc>
119{
120 if (this != &d) { // assignment to self
121 clear();
122 cbc = d.cbc;
123 if (d.lock && cbdataReferenceValid(d.lock))
124 lock = cbdataReference(d.lock);
125 }
126 return *this;
127}
128
129template<class Cbc>
131{
132 if (this != &d) { // assignment to self
133 clear();
134 cbc = d.cbc;
135 d.cbc = nullptr;
136 lock = d.lock;
137 d.lock = nullptr;
138 }
139 return *this;
140}
141
142template<class Cbc>
143void
145{
146#if USE_CBDATA_DEBUG
147 debugs(45, 3, "cbc=" << (void*)cbc << ", lock=" << (void*)lock);
148#endif
149 cbdataReferenceDone(lock); // lock may be nil before and will be nil after
150 cbc = nullptr;
151}
152
153template<class Cbc>
154Cbc *
156{
157 return cbc;
158}
159
160template<class Cbc>
161Cbc *
163{
164 return (lock && cbdataReferenceValid(lock)) ? cbc : nullptr;
165}
166
167template<class Cbc>
168Cbc &
170{
171 Cbc *c = get();
172 assert(c);
173 return *c;
174}
175
176template<class Cbc>
177Cbc *
179{
180 Cbc *c = get();
181 assert(c);
182 return c;
183}
184
185template <class Cbc>
186std::ostream &CbcPointer<Cbc>::print(std::ostream &os) const
187{
188 return os << cbc << '/' << lock;
189}
190
191#endif /* SQUID_CBC_POINTER_H */
192
std::ostream & operator<<(std::ostream &os, const CbcPointer< Cbc > &p)
Definition: CbcPointer.h:78
#define assert(EX)
Definition: assert.h:19
int cbdataReferenceValid(const void *p)
Definition: cbdata.cc:398
#define cbdataReferenceDone(var)
Definition: cbdata.h:350
#define cbdataReference(var)
Definition: cbdata.h:341
void * lock
Definition: CbcPointer.h:73
Cbc * valid() const
was set and is valid
Definition: CbcPointer.h:41
CbcPointer(const CbcPointer &p)
Definition: CbcPointer.h:98
void clear()
make pointer not set; does not invalidate cbdata
Definition: CbcPointer.h:144
bool operator!() const
invalid or was not set
Definition: CbcPointer.h:42
Cbc * get() const
a temporary valid raw Cbc pointer or NULL
Definition: CbcPointer.h:162
Cbc * cbc
Definition: CbcPointer.h:72
Cbc * operator->() const
a valid Cbc pointer or exception
Definition: CbcPointer.h:178
CbcPointer(CbcPointer &&)
Definition: CbcPointer.h:105
std::ostream & print(std::ostream &os) const
Definition: CbcPointer.h:186
CbcPointer & operator=(const CbcPointer &p)
Definition: CbcPointer.h:118
CbcPointer(Cbc *aCbc)
Definition: CbcPointer.h:91
Cbc & operator*() const
a valid Cbc reference or exception
Definition: CbcPointer.h:169
bool operator==(const CbcPointer< Cbc > &o) const
Definition: CbcPointer.h:43
bool set() const
was set but may be invalid
Definition: CbcPointer.h:40
Cbc * raw() const
a temporary raw Cbc pointer; may be invalid
Definition: CbcPointer.h:155
CbcPointer(const CbcPointer< Other > &o)
support converting a child cbc pointer into a parent cbc pointer
Definition: CbcPointer.h:50
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:196

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors