Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / intrusive / example / doc_list.cpp
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
26    MyClass(int i)
27       :  int_(i)
28    {}
29 };
30
31 //Define a list that will store MyClass using the public base hook
32 typedef list<MyClass>   BaseList;
33
34 //Define a list that will store MyClass using the public member hook
35 typedef list< MyClass
36             , member_hook< MyClass, list_member_hook<>, &MyClass::member_hook_>
37             > MemberList;
38
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()); it != itend; ++it)
52       baselist.push_front(*it);
53
54    //Now insert them in the same order as in vector in the member hook list
55    for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
56       memberlist.push_back(*it);
57
58    //Now test lists
59    {
60       BaseList::reverse_iterator rbit(baselist.rbegin());
61       MemberList::iterator mit(memberlist.begin());
62       VectIt  it(values.begin()), itend(values.end());
63
64       //Test the objects inserted in the base hook list
65       for(; it != itend; ++it, ++rbit)
66          if(&*rbit != &*it)   return 1;
67
68       //Test the objects inserted in the member hook list
69       for(it = values.begin(); it != itend; ++it, ++mit)
70          if(&*mit != &*it)    return 1;
71    }
72
73    return 0;
74 }
75 //]