From 558982c5ade8ae35156d8e6d05e117d49bfa6d45 Mon Sep 17 00:00:00 2001 From: Tim Moore Date: Sun, 29 Jun 2008 23:04:31 +0200 Subject: Add auto_free_ref to auto_free stuff; bug 6694 Also add auto_free.h to EXTRA_DIST. --- auto_free.h | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'auto_free.h') diff --git a/auto_free.h b/auto_free.h index b13e7371..58290821 100644 --- a/auto_free.h +++ b/auto_free.h @@ -27,14 +27,32 @@ public: } private: // No copying allowed. - auto_free(const auto_free& af) + auto_free(const auto_free& af); + // No assignment either + auto_free& operator=(const auto_free& rhs); + void* _ptr; +}; + +// Use this to free a pointer whose value may change after the initial +// allocation e.g., be realloced. +template +class auto_free_ref +{ +public: + typedef T pointer_type; + auto_free_ref(pointer_type& ptr) : _ptr(ptr) { } - // No assignment either - auto_free& operator=(const auto_free& rhs) + ~auto_free_ref() { - return *this; + if (_ptr) + std::free(_ptr); } - void* _ptr; +private: + // No copying allowed. + auto_free_ref(const auto_free_ref& af); + // No assignment either + auto_free_ref& operator=(const auto_free_ref& rhs); + pointer_type& _ptr; }; #endif -- cgit