]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/base/CbcPointer.h
5 #ifndef SQUID_CBC_POINTER_H
6 #define SQUID_CBC_POINTER_H
8 #include "base/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
) {
52 cbc
= o
.raw(); // so that set() is accurate
54 lock
= cbdataReference(o
->toCbdata());
58 void clear(); ///< make pointer not set; does not invalidate cbdata
60 std::ostream
&print(std::ostream
&os
) const;
63 Cbc
*cbc
; // a possibly invalid pointer to a cbdata class
64 void *lock
; // a valid pointer to cbc's cbdata or nil
69 std::ostream
&operator <<(std::ostream
&os
, const CbcPointer
<Cbc
> &p
) {
76 CbcPointer
<Cbc
>::CbcPointer(): cbc(NULL
), lock(NULL
)
81 CbcPointer
<Cbc
>::CbcPointer(Cbc
*aCbc
): cbc(aCbc
), lock(NULL
)
84 lock
= cbdataReference(cbc
->toCbdata());
88 CbcPointer
<Cbc
>::CbcPointer(const CbcPointer
&d
): cbc(d
.cbc
), lock(NULL
)
90 if (d
.lock
&& cbdataReferenceValid(d
.lock
))
91 lock
= cbdataReference(d
.lock
);
95 CbcPointer
<Cbc
>::~CbcPointer()
101 CbcPointer
<Cbc
> &CbcPointer
<Cbc
>::operator =(const CbcPointer
&d
)
105 if (d
.lock
&& cbdataReferenceValid(d
.lock
))
106 lock
= cbdataReference(d
.lock
);
112 CbcPointer
<Cbc
>::clear()
114 cbdataReferenceDone(lock
); // lock may be nil before and will be nil after
120 CbcPointer
<Cbc
>::raw() const
127 CbcPointer
<Cbc
>::get() const
129 return (lock
&& cbdataReferenceValid(lock
)) ? cbc
: NULL
;
134 CbcPointer
<Cbc
>::operator *() const
143 CbcPointer
<Cbc
>::operator ->() const
151 std::ostream
&CbcPointer
<Cbc
>::print(std::ostream
&os
) const {
152 return os
<< cbc
<< '/' << lock
;
156 #endif /* SQUID_CBC_POINTER_H */