/*++ Copyright (c) 2011 Microsoft Corporation Module Name: ptr_scoped_buffer.h Abstract: Author: Leonardo de Moura (leonardo) 2011-03-03. Revision History: --*/ #ifndef _PTR_SCOPED_BUFFER_H_ #define _PTR_SCOPED_BUFFER_H_ #include"util.h" #include"debug.h" #include"buffer.h" template > class ptr_scoped_buffer : private ptr_buffer { D m_deallocator; void deallocate_all() { typename ptr_buffer::iterator it = ptr_buffer::begin(); typename ptr_buffer::iterator end = ptr_buffer::end(); for (; it != end; ++it) m_deallocator(*it); } public: typedef typename ptr_buffer::const_iterator const_iterator; ptr_scoped_buffer(D const & m = D()):ptr_buffer(), m_deallocator(m) {} ~ptr_scoped_buffer() { deallocate_all(); } void reset() { deallocate_all(); ptr_buffer::reset(); } void finalize() { deallocate_all(); ptr_buffer::finalize(); } /** \brief Release ownership of the pointers stored in the buffer */ void release() { ptr_buffer::reset(); } unsigned size() const { return ptr_buffer::size(); } bool empty() const { return ptr_buffer::empty(); } const_iterator begin() const { return ptr_buffer::begin(); } const_iterator end() const { return ptr_buffer::end(); } void push_back(T * elem) { return ptr_buffer::push_back(elem); } T * back() const { return ptr_buffer::back(); } void pop_back() { m_deallocator(back()); ptr_buffer::pop_back(); } T * get(unsigned idx) const { return ptr_buffer::get(idx); } void set(unsigned idx, T * e) { T * old_e = get(idx); if (e != old_e) m_deallocator(old_e); ptr_buffer::set(idx, e); } void append(unsigned n, T * const * elems) { ptr_buffer::append(n, elems); } }; #endif