]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/base/TidyPointer.h
2 * Copyright (C) 1996-2015 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_BASE_TIDYPOINTER_H
10 #define SQUID_BASE_TIDYPOINTER_H
13 * A pointer that deletes the object it points to when the pointer's owner or
14 * context is gone. Similar to std::auto_ptr but without confusing assignment
15 * and with a customizable cleanup method. Prevents memory leaks in
16 * the presence of exceptions and processing short cuts.
18 template <typename T
, void (*DeAllocator
)(T
*t
)> class TidyPointer
22 typedef void DCB (T
*t
);
23 TidyPointer(T
*t
= NULL
)
26 bool operator !() const { return !raw
; }
27 /// Returns raw and possibly NULL pointer
28 T
*get() const { return raw
; }
29 /// Address of the raw pointer, for pointer-setting functions
30 T
**addr() { return &raw
; }
31 /// Reset raw pointer - delete last one and save new one.
37 /// Forget the raw pointer without freeing it. Become a nil pointer.
43 /// Deallocate raw pointer.
48 /// Forbidden copy constructor.
49 TidyPointer(TidyPointer
<T
, DeAllocator
> const &);
50 /// Forbidden assigment operator.
51 TidyPointer
<T
, DeAllocator
> & operator = (TidyPointer
<T
, DeAllocator
> const &);
52 /// Deallocate raw pointer. Become a nil pointer.
53 void deletePointer() {
59 T
*raw
; ///< pointer to T object or NULL
62 /// DeAllocator for pointers that need free(3) from the std C library
63 template<typename T
> void tidyFree(T
*p
)
68 #endif // SQUID_BASE_TIDYPOINTER_H