RTI Routing Service Version 7.3.0
SampleIterator.hpp
1/* $Id$
2
3(c) Copyright, Real-Time Innovations, 2014-2016.
4All rights reserved.
5
6No duplications, whole or partial, manual or electronic, may be made
7without express written permission. Any such copies, or
8revisions thereof, must display this notice unaltered.
9This code contains trade secrets of Real-Time Innovations, Inc.
10
11
12modification history
13--------------------
141.0a,05may14,acr Added ValidSampleIterator and valid_samples helper
151.0a,20mar13,acr Created
16============================================================================= */
17
18#ifndef RTI_ROUTING_PROCESSOR_SAMPLE_ITERATOR_HPP_
19 #define RTI_ROUTING_PROCESSOR_SAMPLE_ITERATOR_HPP_
20
21
22 #include <rti/routing/processor/LoanedSample.hpp>
23
24
25namespace rti {
26namespace routing {
27namespace processor {
28
29/*i
30 * @brief Tag type to emulate/indicate that sample infos are not available
31 */
32struct no_sample_info_t {
33};
34
43template <typename T, typename U>
45public:
46 // iterator traits
47 typedef std::random_access_iterator_tag iterator_category;
48 typedef const LoanedSample<T, U> value_type;
49 typedef value_type reference;
50 typedef value_type pointer;
51 typedef std::ptrdiff_t difference_type;
52 typedef RTI_RoutingServiceSample* SampleSeqType;
53 typedef RTI_RoutingServiceSampleInfo* InfoSeqType;
54
55private:
56 static no_sample_info_t *_null_info_scrachtpad[1];
57 static InfoSeqType _null_info_seq;
58 static difference_type _null_info_pos;
59
60public:
62 : _sample_seq(0),
63 _info_seq(0),
64 _length(0),
65 _pos(0),
66 _info_pos(_pos)
67 {
68 }
69
71 const SampleSeqType sample_seq,
72 const InfoSeqType info_seq,
73 int length,
74 int position = 0)
75 : _sample_seq(sample_seq),
76 _info_seq(info_seq == NULL ? _null_info_seq : info_seq),
77 _length(length),
78 _pos(position),
79 _info_pos(info_seq == NULL ? _null_info_pos : _pos)
80 {
81 }
82
84 : _sample_seq(other._sample_seq),
85 _info_seq(other._info_seq),
86 _length(other._length),
87 _pos(other._pos),
88 _info_pos(other._info_pos)
89 {
90 }
91
92 SampleIterator & operator=(const SampleIterator<T, U> & other)
93 {
94 _sample_seq = other._sample_seq;
95 _info_seq = other._info_seq;
96 _length = other._length;
97 _pos = other._pos;
98 _info_pos = other._info_pos;
99
100 return *this;
101 }
102
103 value_type operator*() const
104 {
105 return value_type(_sample_seq[_pos], _info_seq[_info_pos]);
106 }
107
108 value_type operator->() const
109 {
110 return value_type(_sample_seq[_pos], _info_seq[_info_pos]);
111 }
112
113 value_type operator[](difference_type offset) const
114 {
115 return (_info_seq == _null_info_seq)
116 ? value_type(_sample_seq[_pos], NULL)
117 : value_type(_sample_seq[_pos + offset], _info_seq[_info_pos + offset]);
118 }
119
120 SampleIterator & operator++()
121 {
122 ++_pos;
123 return *this;
124 }
125
126 SampleIterator operator++(int)
127 {
128 SampleIterator temp(*this);
129 ++(*this);
130 return temp;
131 }
132
133 SampleIterator & operator--()
134 {
135 --_pos;
136 return *this;
137 }
138
139 SampleIterator operator--(int)
140 {
141 SampleIterator temp(*this);
142 --(*this);
143 return temp;
144 }
145
146 SampleIterator & operator-=(difference_type i)
147 {
148 _pos -= i;
149 return *this;
150 }
151
152 SampleIterator & operator+=(difference_type i)
153 {
154 _pos += i;
155 return *this;
156 }
157
158 friend SampleIterator operator-(
159 const SampleIterator& si,
160 difference_type i)
161 {
162 SampleIterator temp(si);
163 temp -= i;
164 return temp;
165 }
166
167 friend SampleIterator operator+(
168 const SampleIterator& si,
169 difference_type i)
170 {
171 SampleIterator temp(si);
172 temp += i;
173 return temp;
174 }
175
176 friend difference_type operator-(
177 const SampleIterator& s1,
178 const SampleIterator& s2)
179 {
180 return s1._pos - s2._pos;
181 }
182
183 friend bool operator<(const SampleIterator & s1,
184 const SampleIterator & s2)
185 {
186 return s1._pos < s2._pos;
187 }
188
189 friend bool operator>(const SampleIterator & s1,
190 const SampleIterator & s2)
191 {
192 return s1._pos > s2._pos;
193 }
194
195 friend bool operator<=(const SampleIterator & s1,
196 const SampleIterator & s2)
197 {
198 return s1._pos <= s2._pos;
199 }
200
201 friend bool operator>=(const SampleIterator & s1,
202 const SampleIterator & s2)
203 {
204 return s1._pos >= s2._pos;
205 }
206
207 friend bool operator==(const SampleIterator & s1,
208 const SampleIterator & s2)
209 {
210 return (s1._sample_seq == s2._sample_seq) && (s1._pos == s2._pos);
211 }
212
213 friend bool operator!=(const SampleIterator & s1,
214 const SampleIterator & s2)
215 {
216 return !(s1 == s2);
217 }
218
219 bool is_end() const
220 {
221 return (_length == _pos);
222 }
223
224 // Keep these members public otherwise some of the
225 // converting constructors will not compile.
226public:
227 SampleSeqType _sample_seq;
228 InfoSeqType _info_seq;
229 int _length;
230 difference_type _pos;
231 difference_type& _info_pos;
232};
233
234
235template <typename T, typename U>
236std::ptrdiff_t SampleIterator<T, U>::_null_info_pos = 0;
237
238template <typename T, typename U>
239no_sample_info_t * SampleIterator<T, U>::_null_info_scrachtpad[1] = {NULL};
240
241template <typename T, typename U>
242typename SampleIterator<T, U>::InfoSeqType SampleIterator<T, U>::_null_info_seq =
243reinterpret_cast<InfoSeqType> (SampleIterator<T, U>::_null_info_scrachtpad);
244
245} } }
246
247#endif // RTI_ROUTING_PROCESSOR_SAMPLE_ITERATOR_HPP_
A random-access iterator of LoanedSample.
Definition: SampleIterator.hpp:44