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 /////////////////////////////////////////////////////////////////////////////
12 //[doc_value_traits_code_legacy
13 #include <boost/intrusive/link_mode.hpp>
14 #include <boost/intrusive/list.hpp>
15 #include <boost/intrusive/slist.hpp>
17 #include <boost/intrusive/trivial_value_traits.hpp>
21 //This node is the legacy type we can't modify and we want to insert in
22 //intrusive list and slist containers using only two pointers, since
23 //we know the object will never be at the same time in both lists.
32 //[doc_value_traits_value_traits
33 //Define our own NodeTraits that will configure singly and doubly linked
34 //list algorithms. Note that this node traits is compatible with
35 //circular_slist_algorithms and circular_list_algorithms.
37 namespace bi
= boost::intrusive
;
39 struct legacy_node_traits
41 typedef legacy_value node
;
42 typedef legacy_value
* node_ptr
;
43 typedef const legacy_value
* const_node_ptr
;
45 static node
*get_next(const node
*n
) { return n
->next_
; }
46 static void set_next(node
*n
, node
*next
) { n
->next_
= next
; }
47 static node
*get_previous(const node
*n
) { return n
->prev_
; }
48 static void set_previous(node
*n
, node
*prev
) { n
->prev_
= prev
; }
51 //This ValueTraits will configure list and slist. In this case,
52 //legacy_node_traits::node is the same as the
53 //legacy_value_traits::value_type so to_node_ptr/to_value_ptr
54 //functions are trivial.
55 struct legacy_value_traits
57 typedef legacy_node_traits node_traits
;
58 typedef node_traits::node_ptr node_ptr
;
59 typedef node_traits::const_node_ptr const_node_ptr
;
60 typedef legacy_value value_type
;
61 typedef legacy_value
* pointer
;
62 typedef const legacy_value
* const_pointer
;
63 static const bi::link_mode_type link_mode
= bi::normal_link
;
64 static node_ptr
to_node_ptr (value_type
&value
) { return node_ptr(&value
); }
65 static const_node_ptr
to_node_ptr (const value_type
&value
) { return const_node_ptr(&value
); }
66 static pointer
to_value_ptr(node_ptr n
) { return pointer(n
); }
67 static const_pointer
to_value_ptr(const_node_ptr n
) { return const_pointer(n
); }
72 //[doc_value_traits_trivial
74 typedef bi::trivial_value_traits
<legacy_node_traits
, bi::normal_link
> trivial_legacy_value_traits
;
78 //[doc_value_traits_test
79 //Now define an intrusive list and slist that will store legacy_value objects
80 typedef bi::value_traits
<legacy_value_traits
> ValueTraitsOption
;
81 typedef bi::value_traits
<trivial_legacy_value_traits
> TrivialValueTraitsOption
;
83 typedef bi::list
<legacy_value
, ValueTraitsOption
> LegacyAbiList
;
84 typedef bi::slist
<legacy_value
, ValueTraitsOption
> LegacyAbiSlist
;
85 typedef bi::list
<legacy_value
, TrivialValueTraitsOption
> TrivialLegacyAbiList
;
86 typedef bi::slist
<legacy_value
, TrivialValueTraitsOption
> TrivialLegacyAbiSlist
;
91 typedef std::vector
<legacy_value
> Vect
;
93 //Create legacy_value objects, with a different internal number
95 for(int i
= 0; i
< 100; ++i
){
96 legacy_value value
; value
.id_
= i
; legacy_vector
.push_back(value
);
99 //Create the list with the objects
100 List
mylist(legacy_vector
.begin(), legacy_vector
.end());
102 //Now test both lists
103 typename
List::const_iterator
bit(mylist
.begin()), bitend(mylist
.end());
104 typename
Vect::const_iterator
it(legacy_vector
.begin()), itend(legacy_vector
.end());
106 //Test the objects inserted in our list
107 for(; it
!= itend
; ++it
, ++bit
)
108 if(&*bit
!= &*it
) return false;
114 return test_list
<LegacyAbiList
>() && test_list
<LegacyAbiSlist
>() &&
115 test_list
<TrivialLegacyAbiList
>() && test_list
<TrivialLegacyAbiSlist
>()