]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/intrusive/example/doc_any_hook.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2008-2013
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)
9 // See http://www.boost.org/libs/intrusive for documentation.
11 /////////////////////////////////////////////////////////////////////////////
14 #include <boost/intrusive/any_hook.hpp>
15 #include <boost/intrusive/slist.hpp>
16 #include <boost/intrusive/list.hpp>
18 using namespace boost::intrusive
;
20 class MyClass
: public any_base_hook
<> //Base hook
25 any_member_hook
<> member_hook_
; //Member hook
27 MyClass(int i
= 0) : int_(i
)
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
;
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
;
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
)); }
46 BaseSList base_slist
; MemberList member_list
;
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
);
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());
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;