]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/intrusive/example/doc_derivation_value_traits.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-2014
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 /////////////////////////////////////////////////////////////////////////////
12 #include <boost/intrusive/link_mode.hpp>
13 #include <boost/intrusive/list.hpp>
14 #include <boost/intrusive/derivation_value_traits.hpp>
23 //Define the node traits. A single node_traits will be enough.
24 struct simple_node_traits
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
; }
35 //[doc_derivation_value_traits_value_traits
39 struct value_1
: public base_1
, public simple_node
45 struct value_2
: public base_1
, public base_2
, public simple_node
51 using namespace boost::intrusive
;
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
;
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
;
63 //[doc_derivation_value_traits_test
66 typedef std::vector
<value_1
> Vect1
;
67 typedef std::vector
<value_2
> Vect2
;
69 //Create values, with a different internal number
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
);
77 //Create the lists with the objects
78 Value1List
list1(values1
.begin(), values1
.end());
79 Value2List
list2(values2
.begin(), values2
.end());
82 Value1List::const_iterator
bit1(list1
.begin()), bitend1(list1
.end());
83 Value2List::const_iterator
bit2(list2
.begin()), bitend2(list2
.end());
85 Vect1::const_iterator
it1(values1
.begin()), itend1(values1
.end());
86 Vect2::const_iterator
it2(values2
.begin()), itend2(values2
.end());
88 //Test the objects inserted in our lists
89 for(; it1
!= itend1
; ++it1
, ++bit1
, ++it2
, ++bit2
){
90 if(&*bit1
!= &*it1
|| &*bit2
!= &*it2
) return false;