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