• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga  2006-2013
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 //[doc_clone_from
13 #include <boost/intrusive/list.hpp>
14 #include <iostream>
15 #include <vector>
16 
17 using namespace boost::intrusive;
18 
19 //A class that can be inserted in an intrusive list
20 class my_class : public list_base_hook<>
21 {
22    public:
operator ==(const my_class & a,const my_class & b)23    friend bool operator==(const my_class &a, const my_class &b)
24    {  return a.int_ == b.int_;   }
25 
26    int int_;
27 
28    //...
29 };
30 
31 //Definition of the intrusive list
32 typedef list<my_class> my_class_list;
33 
34 //Cloner object function
35 struct new_cloner
36 {
operator ()new_cloner37    my_class *operator()(const my_class &clone_this)
38    {  return new my_class(clone_this);  }
39 };
40 
41 //The disposer object function
42 struct delete_disposer
43 {
operator ()delete_disposer44    void operator()(my_class *delete_this)
45    {  delete delete_this;  }
46 };
47 
main()48 int main()
49 {
50    const int MaxElem = 100;
51    std::vector<my_class> nodes(MaxElem);
52 
53    //Fill all the nodes and insert them in the list
54    my_class_list list;
55 
56    for(int i = 0; i < MaxElem; ++i) nodes[i].int_ = i;
57 
58    list.insert(list.end(), nodes.begin(), nodes.end());
59 
60    //Now clone "list" using "new" and "delete" object functions
61    my_class_list cloned_list;
62    cloned_list.clone_from(list, new_cloner(), delete_disposer());
63 
64    //Test that both are equal
65    if(cloned_list != list)
66       std::cout << "Both lists are different" << std::endl;
67    else
68       std::cout << "Both lists are equal" << std::endl;
69 
70    //Don't forget to free the memory from the second list
71    cloned_list.clear_and_dispose(delete_disposer());
72    return 0;
73 }
74 //]
75