1 /* Copyright 2003-2018 Joaquin M Lopez Munoz.
2  * Distributed under the Boost Software License, Version 1.0.
3  * (See accompanying file LICENSE_1_0.txt or copy at
4  * http://www.boost.org/LICENSE_1_0.txt)
5  *
6  * See http://www.boost.org/libs/multi_index for library home page.
7  */
8 
9 #ifndef BOOST_MULTI_INDEX_DETAIL_RND_NODE_ITERATOR_HPP
10 #define BOOST_MULTI_INDEX_DETAIL_RND_NODE_ITERATOR_HPP
11 
12 #if defined(_MSC_VER)
13 #pragma once
14 #endif
15 
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
17 #include <boost/operators.hpp>
18 
19 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
20 #include <boost/serialization/nvp.hpp>
21 #include <boost/serialization/split_member.hpp>
22 #endif
23 
24 namespace boost{
25 
26 namespace multi_index{
27 
28 namespace detail{
29 
30 /* Iterator class for node-based indices with random access iterators. */
31 
32 template<typename Node>
33 class rnd_node_iterator:
34   public random_access_iterator_helper<
35     rnd_node_iterator<Node>,
36     typename Node::value_type,
37     typename Node::difference_type,
38     const typename Node::value_type*,
39     const typename Node::value_type&>
40 {
41 public:
42   /* coverity[uninit_ctor]: suppress warning */
rnd_node_iterator()43   rnd_node_iterator(){}
rnd_node_iterator(Node * node_)44   explicit rnd_node_iterator(Node* node_):node(node_){}
45 
operator *() const46   const typename Node::value_type& operator*()const
47   {
48     return node->value();
49   }
50 
operator ++()51   rnd_node_iterator& operator++()
52   {
53     Node::increment(node);
54     return *this;
55   }
56 
operator --()57   rnd_node_iterator& operator--()
58   {
59     Node::decrement(node);
60     return *this;
61   }
62 
operator +=(typename Node::difference_type n)63   rnd_node_iterator& operator+=(typename Node::difference_type n)
64   {
65     Node::advance(node,n);
66     return *this;
67   }
68 
operator -=(typename Node::difference_type n)69   rnd_node_iterator& operator-=(typename Node::difference_type n)
70   {
71     Node::advance(node,-n);
72     return *this;
73   }
74 
75 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
76   /* Serialization. As for why the following is public,
77    * see explanation in safe_mode_iterator notes in safe_mode.hpp.
78    */
79 
80   BOOST_SERIALIZATION_SPLIT_MEMBER()
81 
82   typedef typename Node::base_type node_base_type;
83 
84   template<class Archive>
save(Archive & ar,const unsigned int) const85   void save(Archive& ar,const unsigned int)const
86   {
87     node_base_type* bnode=node;
88     ar<<serialization::make_nvp("pointer",bnode);
89   }
90 
91   template<class Archive>
load(Archive & ar,const unsigned int)92   void load(Archive& ar,const unsigned int)
93   {
94     node_base_type* bnode;
95     ar>>serialization::make_nvp("pointer",bnode);
96     node=static_cast<Node*>(bnode);
97   }
98 #endif
99 
100   /* get_node is not to be used by the user */
101 
102   typedef Node node_type;
103 
get_node() const104   Node* get_node()const{return node;}
105 
106 private:
107   Node* node;
108 };
109 
110 template<typename Node>
operator ==(const rnd_node_iterator<Node> & x,const rnd_node_iterator<Node> & y)111 bool operator==(
112   const rnd_node_iterator<Node>& x,
113   const rnd_node_iterator<Node>& y)
114 {
115   return x.get_node()==y.get_node();
116 }
117 
118 template<typename Node>
operator <(const rnd_node_iterator<Node> & x,const rnd_node_iterator<Node> & y)119 bool operator<(
120   const rnd_node_iterator<Node>& x,
121   const rnd_node_iterator<Node>& y)
122 {
123   return Node::distance(x.get_node(),y.get_node())>0;
124 }
125 
126 template<typename Node>
operator -(const rnd_node_iterator<Node> & x,const rnd_node_iterator<Node> & y)127 typename Node::difference_type operator-(
128   const rnd_node_iterator<Node>& x,
129   const rnd_node_iterator<Node>& y)
130 {
131   return Node::distance(y.get_node(),x.get_node());
132 }
133 
134 } /* namespace multi_index::detail */
135 
136 } /* namespace multi_index */
137 
138 } /* namespace boost */
139 
140 #endif
141