]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/intrusive/example/doc_slist.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-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 /////////////////////////////////////////////////////////////////////////////
13 #include <boost/intrusive/slist.hpp>
16 using namespace boost::intrusive
;
19 class MyClass
: public slist_base_hook
<>
24 //This is a member hook
25 slist_member_hook
<> member_hook_
;
31 int get_int() const { return int_
; }
35 //Define an slist that will store MyClass using the public base hook
36 typedef slist
<MyClass
> BaseList
;
38 //Define an slist that will store MyClass using the public member hook
39 typedef member_hook
<MyClass
, slist_member_hook
<>, &MyClass::member_hook_
> MemberOption
;
40 typedef slist
<MyClass
, MemberOption
> MemberList
;
44 typedef std::vector
<MyClass
>::iterator VectIt
;
45 typedef std::vector
<MyClass
>::reverse_iterator VectRit
;
47 //Create several MyClass objects, each one with a different value
48 std::vector
<MyClass
> values
;
49 for(int i
= 0; i
< 100; ++i
) values
.push_back(MyClass(i
));
52 MemberList memberlist
;
54 //Now insert them in the reverse order in the base hook list
55 for(VectIt
it(values
.begin()), itend(values
.end()); it
!= itend
; ++it
)
56 baselist
.push_front(*it
);
58 //Now insert them in the same order as in vector in the member hook list
59 for(BaseList::iterator
it(baselist
.begin()), itend(baselist
.end())
61 memberlist
.push_front(*it
);
66 BaseList::iterator
bit(baselist
.begin());
67 MemberList::iterator
mit(memberlist
.begin());
68 VectRit
rit(values
.rbegin()), ritend(values
.rend());
69 VectIt
it(values
.begin()), itend(values
.end());
71 //Test the objects inserted in the base hook list
72 for(; rit
!= ritend
; ++rit
, ++bit
)
73 if(&*bit
!= &*rit
) return 1;
75 //Test the objects inserted in the member hook list
76 for(; it
!= itend
; ++it
, ++mit
)
77 if(&*mit
!= &*it
) return 1;