1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2007-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 #include <boost/intrusive/list.hpp>
13 #include <boost/intrusive/slist.hpp>
14 #include <boost/intrusive/set.hpp>
15 #include <boost/intrusive/unordered_set.hpp>
16 #include <vector>
17
18 using namespace boost::intrusive;
19
20 struct VirtualBase
21 {
~VirtualBaseVirtualBase22 virtual ~VirtualBase(){}
23 };
24
25 struct VirtualBase2
26 {
~VirtualBase2VirtualBase227 virtual ~VirtualBase2(){}
28 };
29
30 struct VirtualBase3
31 {
32 };
33
34 class NonVirtualBase
35 : public virtual VirtualBase
36 , public virtual VirtualBase2
37 {
38 protected:
NonVirtualBase()39 NonVirtualBase()
40 : dummy()
41 {}
42
43 //<-
get_dummy() const44 const int *get_dummy() const { return dummy; }
45 //->
46
47 private:
48 int dummy[10];
49 };
50
51 class MyClass
52 : public NonVirtualBase
53 , public virtual VirtualBase3
54 {
55 int int_;
56
57 public:
58 list_member_hook<> list_hook_;
MyClass(int i=0)59 MyClass(int i = 0)
60 : int_(i)
61 {}
62 //<-
get_int() const63 int get_int() const { return int_; }
64 //->
65 };
66
67 //Define a list that will store MyClass using the public base hook
68 typedef member_hook< MyClass, list_member_hook<>, &MyClass::list_hook_ > MemberHook;
69 typedef list<MyClass, MemberHook> List;
70
main()71 int main()
72 {
73 #ifndef _MSC_VER
74 typedef std::vector<MyClass>::iterator VectIt;
75 typedef std::vector<MyClass>::reverse_iterator VectRit;
76
77 //Create several MyClass objects, each one with a different value
78 std::vector<MyClass> values;
79 for(int i = 0; i < 100; ++i) values.push_back(MyClass(i));
80
81 List my_list;
82
83 //Now insert them in the reverse order
84 //in the base hook intrusive list
85 for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
86 my_list.push_front(*it);
87
88 //Now test lists
89 {
90 List::const_iterator list_it(my_list.cbegin());
91 VectRit vect_it(values.rbegin()), vect_itend(values.rend());
92
93 //Test the objects inserted in the base hook list
94 for(; vect_it != vect_itend; ++vect_it, ++list_it)
95 if(&*list_it != &*vect_it)
96 return 1;
97 }
98 #endif
99 return 0;
100 }
101