1 /*=============================================================================
2 Copyright (c) 2010.2017 Daniel James
4 Use, modification and distribution is subject to the Boost Software
5 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
9 #if !defined(BOOST_SPIRIT_QUICKBOOK_CLEANUP_HPP)
10 #define BOOST_SPIRIT_QUICKBOOK_CLEANUP_HPP
14 // This header defines a class which will store pointers and delete what
15 // they're pointing to on destruction. Add an object, and you can use
16 // pointers and references to it during the cleanup object's lifespan.
20 // struct wonder_struct {
21 // quickbook::cleanup cleanup;
25 // thing& t = w.cleanup.add(new thing());
27 // Can now use 't' until the wonder_struct is destroyed.
29 // Anything added to cleanup is destroyed in reverse order, so it
30 // should be okay for an object to depend on something that was previously
39 cleanup() : first_(0) {}
41 template <typename T> T& add(T*);
44 detail::cleanup_node* first_;
46 cleanup& operator=(cleanup const&);
47 cleanup(cleanup const&);
52 template <typename T> void delete_impl(void* ptr)
54 delete static_cast<T*>(ptr);
63 cleanup_node() : ptr_(0), del_(0), next_(0) {}
64 cleanup_node(void* ptr, void (*del)(void* x))
65 : ptr_(ptr), del_(del), next_(0)
73 void move_assign(cleanup_node& n)
82 cleanup_node(cleanup_node const&);
83 cleanup_node& operator=(cleanup_node const&);
87 template <typename T> T& cleanup::add(T* ptr)
89 detail::cleanup_node n(ptr, &detail::delete_impl<T>);
90 detail::cleanup_node* n2 = new detail::cleanup_node();
97 inline cleanup::~cleanup()
100 detail::cleanup_node* to_delete = first_;
101 first_ = first_->next_;