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    //<-
get_int() const30    int get_int() const { return int_; }
31    //->
32 };
33 
main()34 int main()
35 {
36    //Define a base hook option that converts any_base_hook to a slist hook
37    typedef any_to_slist_hook < base_hook< any_base_hook<> > >     BaseSlistOption;
38    typedef slist<MyClass, BaseSlistOption>                        BaseSList;
39 
40    //Define a member hook option that converts any_member_hook to a list hook
41    typedef any_to_list_hook< member_hook
42          < MyClass, any_member_hook<>, &MyClass::member_hook_> >  MemberListOption;
43    typedef list<MyClass, MemberListOption>                        MemberList;
44 
45    //Create several MyClass objects, each one with a different value
46    std::vector<MyClass> values;
47    for(int i = 0; i < 100; ++i){ values.push_back(MyClass(i)); }
48 
49    BaseSList base_slist;   MemberList member_list;
50 
51    //Now insert them in reverse order in the slist and in order in the list
52    for(std::vector<MyClass>::iterator it(values.begin()), itend(values.end()); it != itend; ++it)
53       base_slist.push_front(*it), member_list.push_back(*it);
54 
55    //Now test lists
56    BaseSList::iterator bit(base_slist.begin());
57    MemberList::reverse_iterator mrit(member_list.rbegin());
58    std::vector<MyClass>::reverse_iterator rit(values.rbegin()), ritend(values.rend());
59 
60    //Test the objects inserted in the base hook list
61    for(; rit != ritend; ++rit, ++bit, ++mrit)
62       if(&*bit != &*rit || &*mrit != &*rit)  return 1;
63    return 0;
64 }
65 //]
66