]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/intrusive/example/doc_value_traits.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / intrusive / example / doc_value_traits.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2006-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 //[doc_value_traits_code_legacy
13 #include <boost/intrusive/link_mode.hpp>
14 #include <boost/intrusive/list.hpp>
15 #include <boost/intrusive/slist.hpp>
16 //<-
17 #include <boost/intrusive/trivial_value_traits.hpp>
18 //->
19 #include <vector>
20
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.
24 struct legacy_value
25 {
26 legacy_value *prev_;
27 legacy_value *next_;
28 int id_;
29 };
30 //]
31
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.
36
37 namespace bi = boost::intrusive;
38
39 struct legacy_node_traits
40 {
41 typedef legacy_value node;
42 typedef legacy_value * node_ptr;
43 typedef const legacy_value * const_node_ptr;
44
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; }
49 };
50
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
56 {
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); }
68 };
69
70 //]
71
72 //[doc_value_traits_trivial
73
74 typedef bi::trivial_value_traits<legacy_node_traits, bi::normal_link> trivial_legacy_value_traits;
75
76 //]
77
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;
82
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;
87
88 template<class List>
89 bool test_list()
90 {
91 typedef std::vector<legacy_value> Vect;
92
93 //Create legacy_value objects, with a different internal number
94 Vect legacy_vector;
95 for(int i = 0; i < 100; ++i){
96 legacy_value value; value.id_ = i; legacy_vector.push_back(value);
97 }
98
99 //Create the list with the objects
100 List mylist(legacy_vector.begin(), legacy_vector.end());
101
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());
105
106 //Test the objects inserted in our list
107 for(; it != itend; ++it, ++bit)
108 if(&*bit != &*it) return false;
109 return true;
110 }
111
112 int main()
113 {
114 return test_list<LegacyAbiList>() && test_list<LegacyAbiSlist>() &&
115 test_list<TrivialLegacyAbiList>() && test_list<TrivialLegacyAbiSlist>()
116 ? 0 : 1;
117 }
118 //]