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 };
31 
32 //Define an slist that will store MyClass using the public base hook
33 typedef slist<MyClass> BaseList;
34 
35 //Define an slist that will store MyClass using the public member hook
36 typedef member_hook<MyClass, slist_member_hook<>, &MyClass::member_hook_> MemberOption;
37 typedef slist<MyClass, MemberOption> MemberList;
38 
main()39 int main()
40 {
41    typedef std::vector<MyClass>::iterator VectIt;
42    typedef std::vector<MyClass>::reverse_iterator VectRit;
43 
44    //Create several MyClass objects, each one with a different value
45    std::vector<MyClass> values;
46    for(int i = 0; i < 100; ++i)  values.push_back(MyClass(i));
47 
48    BaseList baselist;
49    MemberList memberlist;
50 
51    //Now insert them in the reverse order in the base hook list
52    for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
53       baselist.push_front(*it);
54 
55    //Now insert them in the same order as in vector in the member hook list
56    for(BaseList::iterator it(baselist.begin()), itend(baselist.end())
57       ; it != itend; ++it){
58       memberlist.push_front(*it);
59    }
60 
61    //Now test lists
62    {
63       BaseList::iterator bit(baselist.begin());
64       MemberList::iterator mit(memberlist.begin());
65       VectRit rit(values.rbegin()), ritend(values.rend());
66       VectIt  it(values.begin()), itend(values.end());
67 
68       //Test the objects inserted in the base hook list
69       for(; rit != ritend; ++rit, ++bit)
70          if(&*bit != &*rit)   return 1;
71 
72       //Test the objects inserted in the member hook list
73       for(; it != itend; ++it, ++mit)
74          if(&*mit != &*it)    return 1;
75    }
76 
77    return 0;
78 }
79 //]
80