5
#ifndef SQUID_CBC_POINTER_H
6
#define SQUID_CBC_POINTER_H
8
#include "TextException.h"
14
* Safely points to a cbdata-protected class (cbc), such as an AsyncJob.
15
* When a cbc we communicate with disappears without
16
* notice or a notice has not reached us yet, this class prevents
17
* dereferencing the pointer to the gone cbc object.
23
CbcPointer(); // a nil pointer
24
CbcPointer(Cbc *aCbc);
25
CbcPointer(const CbcPointer &p);
28
Cbc *raw() const; ///< a temporary raw Cbc pointer; may be invalid
29
Cbc *get() const; ///< a temporary valid raw Cbc pointer or NULL
30
Cbc &operator *() const; ///< a valid Cbc reference or exception
31
Cbc *operator ->() const; ///< a valid Cbc pointer or exception
33
// no bool operator because set() != valid()
34
bool set() const { return cbc != NULL; } ///< was set but may be invalid
35
Cbc *valid() const { return get(); } ///< was set and is valid
36
bool operator !() const { return !valid(); } ///< invalid or was not set
37
bool operator ==(const CbcPointer<Cbc> &o) const { return lock == o.lock; }
39
CbcPointer &operator =(const CbcPointer &p);
41
/// support converting a child cbc pointer into a parent cbc pointer
42
template <typename Other>
43
CbcPointer(const CbcPointer<Other> &o): cbc(o.raw()), lock(NULL) {
45
lock = cbdataReference(o->toCbdata());
48
/// support assigning a child cbc pointer to a parent cbc pointer
49
template <typename Other>
50
CbcPointer &operator =(const CbcPointer<Other> &o) {
51
if (this != &o) { // assignment to self
53
cbc = o.raw(); // so that set() is accurate
55
lock = cbdataReference(o->toCbdata());
60
void clear(); ///< make pointer not set; does not invalidate cbdata
62
std::ostream &print(std::ostream &os) const;
65
Cbc *cbc; // a possibly invalid pointer to a cbdata class
66
void *lock; // a valid pointer to cbc's cbdata or nil
71
std::ostream &operator <<(std::ostream &os, const CbcPointer<Cbc> &p)
79
CbcPointer<Cbc>::CbcPointer(): cbc(NULL), lock(NULL)
84
CbcPointer<Cbc>::CbcPointer(Cbc *aCbc): cbc(aCbc), lock(NULL)
87
lock = cbdataReference(cbc->toCbdata());
91
CbcPointer<Cbc>::CbcPointer(const CbcPointer &d): cbc(d.cbc), lock(NULL)
93
if (d.lock && cbdataReferenceValid(d.lock))
94
lock = cbdataReference(d.lock);
98
CbcPointer<Cbc>::~CbcPointer()
104
CbcPointer<Cbc> &CbcPointer<Cbc>::operator =(const CbcPointer &d)
106
if (this != &d) { // assignment to self
109
if (d.lock && cbdataReferenceValid(d.lock))
110
lock = cbdataReference(d.lock);
117
CbcPointer<Cbc>::clear()
119
cbdataReferenceDone(lock); // lock may be nil before and will be nil after
125
CbcPointer<Cbc>::raw() const
132
CbcPointer<Cbc>::get() const
134
return (lock && cbdataReferenceValid(lock)) ? cbc : NULL;
139
CbcPointer<Cbc>::operator *() const
148
CbcPointer<Cbc>::operator ->() const
156
std::ostream &CbcPointer<Cbc>::print(std::ostream &os) const
158
return os << cbc << '/' << lock;
162
#endif /* SQUID_CBC_POINTER_H */