]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/base/CbcPointer.h
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_CBC_POINTER_H
10 #define SQUID_CBC_POINTER_H
12 #include "base/TextException.h"
19 * Safely points to a cbdata-protected class (cbc), such as an AsyncJob.
20 * When a cbc we communicate with disappears without
21 * notice or a notice has not reached us yet, this class prevents
22 * dereferencing the pointer to the gone cbc object.
28 CbcPointer(); // a nil pointer
29 CbcPointer(Cbc
*aCbc
);
30 CbcPointer(const CbcPointer
&p
);
33 Cbc
*raw() const; ///< a temporary raw Cbc pointer; may be invalid
34 Cbc
*get() const; ///< a temporary valid raw Cbc pointer or NULL
35 Cbc
&operator *() const; ///< a valid Cbc reference or exception
36 Cbc
*operator ->() const; ///< a valid Cbc pointer or exception
38 // no bool operator because set() != valid()
39 bool set() const { return cbc
!= NULL
; } ///< was set but may be invalid
40 Cbc
*valid() const { return get(); } ///< was set and is valid
41 bool operator !() const { return !valid(); } ///< invalid or was not set
42 bool operator ==(const CbcPointer
<Cbc
> &o
) const { return lock
== o
.lock
; }
44 CbcPointer
&operator =(const CbcPointer
&p
);
46 /// support converting a child cbc pointer into a parent cbc pointer
47 template <typename Other
>
48 CbcPointer(const CbcPointer
<Other
> &o
): cbc(o
.raw()), lock(NULL
) {
50 lock
= cbdataReference(o
->toCbdata());
53 /// support assigning a child cbc pointer to a parent cbc pointer
54 template <typename Other
>
55 CbcPointer
&operator =(const CbcPointer
<Other
> &o
) {
56 if (this != &o
) { // assignment to self
58 cbc
= o
.raw(); // so that set() is accurate
60 lock
= cbdataReference(o
->toCbdata());
65 void clear(); ///< make pointer not set; does not invalidate cbdata
67 std::ostream
&print(std::ostream
&os
) const;
70 Cbc
*cbc
; // a possibly invalid pointer to a cbdata class
71 void *lock
; // a valid pointer to cbc's cbdata or nil
76 std::ostream
&operator <<(std::ostream
&os
, const CbcPointer
<Cbc
> &p
)
84 CbcPointer
<Cbc
>::CbcPointer(): cbc(NULL
), lock(NULL
)
89 CbcPointer
<Cbc
>::CbcPointer(Cbc
*aCbc
): cbc(aCbc
), lock(NULL
)
92 lock
= cbdataReference(cbc
->toCbdata());
96 CbcPointer
<Cbc
>::CbcPointer(const CbcPointer
&d
): cbc(d
.cbc
), lock(NULL
)
98 if (d
.lock
&& cbdataReferenceValid(d
.lock
))
99 lock
= cbdataReference(d
.lock
);
103 CbcPointer
<Cbc
>::~CbcPointer()
109 CbcPointer
<Cbc
> &CbcPointer
<Cbc
>::operator =(const CbcPointer
&d
)
111 if (this != &d
) { // assignment to self
114 if (d
.lock
&& cbdataReferenceValid(d
.lock
))
115 lock
= cbdataReference(d
.lock
);
122 CbcPointer
<Cbc
>::clear()
125 debugs(45, 3, "cbc=" << (void*)cbc
<< ", lock=" << (void*)lock
);
127 cbdataReferenceDone(lock
); // lock may be nil before and will be nil after
133 CbcPointer
<Cbc
>::raw() const
140 CbcPointer
<Cbc
>::get() const
142 return (lock
&& cbdataReferenceValid(lock
)) ? cbc
: NULL
;
147 CbcPointer
<Cbc
>::operator *() const
156 CbcPointer
<Cbc
>::operator ->() const
164 std::ostream
&CbcPointer
<Cbc
>::print(std::ostream
&os
) const
166 return os
<< cbc
<< '/' << lock
;
169 #endif /* SQUID_CBC_POINTER_H */