diff options
Diffstat (limited to 'auto_free.h')
-rw-r--r-- | auto_free.h | 28 |
1 files changed, 23 insertions, 5 deletions
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 <typename T> +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 |