• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /////////////////////////////////////////////////////////////////////////////
2  //
3  // (C) Copyright Ion Gaztanaga  2006-2014
4  //
5  // Distributed under the Boost Software License, Version 1.0.
6  //    (See accompanying file LICENSE_1_0.txt or copy at
7  //          http://www.boost.org/LICENSE_1_0.txt)
8  //
9  // See http://www.boost.org/libs/intrusive for documentation.
10  //
11  /////////////////////////////////////////////////////////////////////////////
12  #include <boost/core/no_exceptions_support.hpp>
13  //[doc_erasing_and_disposing
14  #include <boost/intrusive/list.hpp>
15  
16  using namespace boost::intrusive;
17  
18  //A class that can be inserted in an intrusive list
19  class my_class : public list_base_hook<>
20  {
21     public:
my_class(int i)22     my_class(int i)
23        :  int_(i)
24     {}
25  
26     int int_;
27     //...
28  };
29  
30  //Definition of the intrusive list
31  typedef list<my_class> my_class_list;
32  
33  //The predicate function
34  struct is_even
35  {
operator ()is_even36     bool operator()(const my_class &c) const
37     {  return 0 == (c.int_ % 2);  }
38  };
39  
40  //The disposer object function
41  struct delete_disposer
42  {
operator ()delete_disposer43     void operator()(my_class *delete_this)
44     {  delete delete_this;  }
45  };
46  
main()47  int main()
48  {
49     const int MaxElem = 100;
50  
51     //Fill all the nodes and insert them in the list
52     my_class_list list;
53  
54     //<-
55     #if 1
56     BOOST_TRY{
57     #else
58     //->
59     try{
60     //<-
61     #endif
62     //->
63        //Insert new objects in the container
64        for(int i = 0; i < MaxElem; ++i) list.push_back(*new my_class(i));
65  
66        //Now use remove_and_dispose_if to erase and delete the objects
67        list.remove_and_dispose_if(is_even(), delete_disposer());
68     }
69     //<-
70     #if 1
71     BOOST_CATCH(...){
72     #else
73     //->
74     catch(...){
75     //<-
76     #endif
77     //->
78        //If something throws, make sure that all the memory is freed
79        list.clear_and_dispose(delete_disposer());
80        //<-
81        #if 1
82        BOOST_RETHROW
83        #else
84        //->
85        throw;
86        //<-
87        #endif
88        //->
89     }
90     //<-
91     BOOST_CATCH_END
92     //->
93  
94     //Dispose remaining elements
95     list.erase_and_dispose(list.begin(), list.end(), delete_disposer());
96     return 0;
97  }
98  //]
99