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
33 namespace detail { struct cleanup_node; }
36 cleanup() : first_(0) {}
38 template <typename T> T& add(T*);
41 detail::cleanup_node* first_;
43 cleanup& operator=(cleanup const&);
44 cleanup(cleanup const&);
50 void delete_impl(void* ptr) {
51 delete static_cast<T*>(ptr);
60 cleanup_node() : ptr_(0), del_(0), next_(0) {}
61 cleanup_node(void* ptr, void (*del)(void* x))
62 : ptr_(ptr), del_(del), next_(0) {}
67 void move_assign(cleanup_node& n) {
74 cleanup_node(cleanup_node const&);
75 cleanup_node& operator=(cleanup_node const&);
80 T& cleanup::add(T* ptr)
82 detail::cleanup_node n(ptr, &detail::delete_impl<T>);
83 detail::cleanup_node* n2 = new detail::cleanup_node();
90 inline cleanup::~cleanup() {
92 detail::cleanup_node* to_delete = first_;
93 first_ = first_->next_;