1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga  2008-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_any_hook
13 #include <vector>
14 #include <boost/intrusive/any_hook.hpp>
15 #include <boost/intrusive/slist.hpp>
16 #include <boost/intrusive/list.hpp>
17 
18 using namespace boost::intrusive;
19 
20 class MyClass : public any_base_hook<> //Base hook
21 {
22    int int_;
23 
24    public:
25    any_member_hook<> member_hook_;  //Member hook
26 
MyClass(int i=0)27    MyClass(int i = 0) : int_(i)
28    {}
29 };
30 
main()31 int main()
32 {
33    //Define a base hook option that converts any_base_hook to a slist hook
34    typedef any_to_slist_hook < base_hook< any_base_hook<> > >     BaseSlistOption;
35    typedef slist<MyClass, BaseSlistOption>                        BaseSList;
36 
37    //Define a member hook option that converts any_member_hook to a list hook
38    typedef any_to_list_hook< member_hook
39          < MyClass, any_member_hook<>, &MyClass::member_hook_> >  MemberListOption;
40    typedef list<MyClass, MemberListOption>                        MemberList;
41 
42    //Create several MyClass objects, each one with a different value
43    std::vector<MyClass> values;
44    for(int i = 0; i < 100; ++i){ values.push_back(MyClass(i)); }
45 
46    BaseSList base_slist;   MemberList member_list;
47 
48    //Now insert them in reverse order in the slist and in order in the list
49    for(std::vector<MyClass>::iterator it(values.begin()), itend(values.end()); it != itend; ++it)
50       base_slist.push_front(*it), member_list.push_back(*it);
51 
52    //Now test lists
53    BaseSList::iterator bit(base_slist.begin());
54    MemberList::reverse_iterator mrit(member_list.rbegin());
55    std::vector<MyClass>::reverse_iterator rit(values.rbegin()), ritend(values.rend());
56 
57    //Test the objects inserted in the base hook list
58    for(; rit != ritend; ++rit, ++bit, ++mrit)
59       if(&*bit != &*rit || &*mrit != &*rit)  return 1;
60    return 0;
61 }
62 //]
63