Imported Upstream version 1.51.0
[platform/upstream/boost.git] / libs / intrusive / example / doc_advanced_value_traits2.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga  2006-2012
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/member_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;
29    static node *get_next(const node *n)            {  return n->next_;  }
30    static void set_next(node *n, node *next)       {  n->next_ = next;  }
31    static node *get_previous(const node *n)        {  return n->prev_;  }
32    static void set_previous(node *n, node *prev)   {  n->prev_ = prev;  }
33 };
34
35 //[doc_advanced_value_traits2_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 typedef member_value_traits
54    <value_1, simple_node_traits, &value_1::node_, normal_link> ValueTraits1;
55 typedef member_value_traits
56 <value_2, simple_node_traits, &value_2::node_, normal_link> ValueTraits2;
57
58 //Now define two intrusive lists. Both lists will use the same algorithms:
59 // circular_list_algorithms<simple_node_traits>
60 typedef list <value_1, value_traits<ValueTraits1> > Value1List;
61 typedef list <value_2, value_traits<ValueTraits2> > Value2List;
62 //]
63
64 //[doc_advanced_value_traits2_test
65 int main()
66 {
67    typedef std::vector<value_1> Vect1;
68    typedef std::vector<value_2> Vect2;
69
70    //Create values, with a different internal number
71    Vect1 values1;
72    Vect2 values2;
73    for(int i = 0; i < 100; ++i){
74       value_1 v1;    v1.id_ = i;          values1.push_back(v1);
75       value_2 v2;    v2.id_ = (float)i;   values2.push_back(v2);
76    }
77
78    //Create the lists with the objects
79    Value1List list1(values1.begin(), values1.end());
80    Value2List list2(values2.begin(), values2.end());
81
82    //Now test both lists
83    Value1List::const_iterator bit1(list1.begin()), bitend1(list1.end());
84    Value2List::const_iterator bit2(list2.begin()), bitend2(list2.end());
85
86    Vect1::const_iterator it1(values1.begin()), itend1(values1.end());
87    Vect2::const_iterator it2(values2.begin()), itend2(values2.end());
88
89    //Test the objects inserted in our lists
90    for(; it1 != itend1; ++it1, ++bit1,  ++it2, ++bit2){
91       if(&*bit1 != &*it1 || &*bit2 != &*it2) return false;
92    }
93    return 0;
94 }
95 //]