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