• 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_list_code
13 #include <boost/intrusive/list.hpp>
14 #include <vector>
15 
16 using namespace boost::intrusive;
17 
18 class MyClass : public list_base_hook<>   //This is a derivation hook
19 {
20    int int_;
21 
22    public:
23    //This is a member hook
24    list_member_hook<> member_hook_;
25 
MyClass(int i)26    MyClass(int i)
27       :  int_(i)
28    {}
29    //<-
get_int() const30    int get_int() const { return int_; }
31    //->
32 };
33 
34 //Define a list that will store MyClass using the public base hook
35 typedef list<MyClass>   BaseList;
36 
37 //Define a list that will store MyClass using the public member hook
38 typedef list< MyClass
39             , member_hook< MyClass, list_member_hook<>, &MyClass::member_hook_>
40             > MemberList;
41 
main()42 int main()
43 {
44    typedef std::vector<MyClass>::iterator VectIt;
45 
46    //Create several MyClass objects, each one with a different value
47    std::vector<MyClass> values;
48    for(int i = 0; i < 100; ++i)  values.push_back(MyClass(i));
49 
50    BaseList baselist;
51    MemberList memberlist;
52 
53    //Now insert them in the reverse order in the base hook list
54    for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
55       baselist.push_front(*it);
56 
57    //Now insert them in the same order as in vector in the member hook list
58    for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
59       memberlist.push_back(*it);
60 
61    //Now test lists
62    {
63       BaseList::reverse_iterator rbit(baselist.rbegin());
64       MemberList::iterator mit(memberlist.begin());
65       VectIt  it(values.begin()), itend(values.end());
66 
67       //Test the objects inserted in the base hook list
68       for(; it != itend; ++it, ++rbit)
69          if(&*rbit != &*it)   return 1;
70 
71       //Test the objects inserted in the member hook list
72       for(it = values.begin(); it != itend; ++it, ++mit)
73          if(&*mit != &*it)    return 1;
74    }
75 
76    return 0;
77 }
78 //]
79