1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2006-2014
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/link_mode.hpp>
13 #include <boost/intrusive/list.hpp>
14 #include <boost/intrusive/derivation_value_traits.hpp>
15 #include <vector>
16
17 struct simple_node
18 {
19 simple_node *prev_;
20 simple_node *next_;
21 };
22
23 //Define the node traits. A single node_traits will be enough.
24 struct simple_node_traits
25 {
26 typedef simple_node node;
27 typedef node * node_ptr;
28 typedef const node * const_node_ptr;
get_nextsimple_node_traits29 static node *get_next(const node *n) { return n->next_; }
set_nextsimple_node_traits30 static void set_next(node *n, node *next) { n->next_ = next; }
get_previoussimple_node_traits31 static node *get_previous(const node *n) { return n->prev_; }
set_previoussimple_node_traits32 static void set_previous(node *n, node *prev) { n->prev_ = prev; }
33 };
34
35 //[doc_derivation_value_traits_value_traits
36 class base_1{};
37 class base_2{};
38
39 struct value_1 : public base_1, public simple_node
40 {
41 int id_;
42 simple_node node_;
43 };
44
45 struct value_2 : public base_1, public base_2, public simple_node
46 {
47 simple_node node_;
48 float id_;
49 };
50
51 using namespace boost::intrusive;
52
53 //Now define the needed value traits using derivation_value_traits
54 typedef derivation_value_traits<value_1, simple_node_traits, normal_link> ValueTraits1;
55 typedef derivation_value_traits<value_2, simple_node_traits, normal_link> ValueTraits2;
56
57 //Now define two intrusive lists. Both lists will use the same algorithms:
58 // circular_list_algorithms<simple_node_traits>
59 typedef list <value_1, value_traits<ValueTraits1> > Value1List;
60 typedef list <value_2, value_traits<ValueTraits2> > Value2List;
61 //]
62
63 //[doc_derivation_value_traits_test
main()64 int main()
65 {
66 typedef std::vector<value_1> Vect1;
67 typedef std::vector<value_2> Vect2;
68
69 //Create values, with a different internal number
70 Vect1 values1;
71 Vect2 values2;
72 for(int i = 0; i < 100; ++i){
73 value_1 v1; v1.id_ = i; values1.push_back(v1);
74 value_2 v2; v2.id_ = (float)i; values2.push_back(v2);
75 }
76
77 //Create the lists with the objects
78 Value1List list1(values1.begin(), values1.end());
79 Value2List list2(values2.begin(), values2.end());
80
81 //Now test both lists
82 Value1List::const_iterator bit1(list1.begin()), bitend1(list1.end());
83 Value2List::const_iterator bit2(list2.begin()), bitend2(list2.end());
84
85 Vect1::const_iterator it1(values1.begin()), itend1(values1.end());
86 Vect2::const_iterator it2(values2.begin()), itend2(values2.end());
87
88 //Test the objects inserted in our lists
89 for(; it1 != itend1; ++it1, ++bit1, ++it2, ++bit2){
90 if(&*bit1 != &*it1 || &*bit2 != &*it2) return 1;
91 }
92 return 0;
93 }
94 //]
95