RTI Routing Service Version 7.2.0
LoanedSamples.hpp
1/* $Id$
2
3(c) Copyright, Real-Time Innovations, 2013-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
12============================================================================= */
13
14#ifndef RTI_ROUTING_PROCESSOR_LOANED_SAMPLES_HPP_
15#define RTI_ROUTING_PROCESSOR_LOANED_SAMPLES_HPP_
16
17#ifdef RTI_CXX11_RVALUE_REFERENCES
18#include <utility> // std::move
19#endif
20
21#include <iterator>
22
23#include "routingservice/routingservice_adapter_new.h"
24#include <dds/core/types.hpp>
25#include <rti/routing/processor/LoanedSample.hpp>
26#include <rti/routing/adapter/StreamReader.hpp>
27#include <rti/routing/processor/SampleIterator.hpp>
28
29namespace rti { namespace routing { namespace processor {
30
31template <typename T, typename U> class TypedInput;
32
33
80template <typename T, typename U = dds::sub::SampleInfo>
82public:
83
85 typedef typename std::vector<StreamReader::SamplePtr> SampleSeqType;
86 typedef typename std::vector<StreamReader::InfoPtr> InfoSeqType;
87
93 typedef typename SampleIterator<T, U>::value_type value_type;
94 typedef std::ptrdiff_t difference_type;
95
99 LoanedSamples() : input_(NULL)
100 {
101 native_samples_.data_array = NULL;
102 native_samples_.info_array = NULL;
103 native_samples_.length = 0;
104
105 }
106
107public:
108 // Private c-tor only to be used by static method create_from_loans()
110 RTI_RoutingServiceInput *native_input,
111 RTI_RoutingServiceLoanedSamples& native_samples)
112 : input_(native_input),
113 native_samples_(native_samples)
114 {
115 }
116
117public:
118
126 {
127 try {
128 return_loan();
129 } catch (const std::exception&) { // Do not throw in destructor
130
131 }
132 }
133
134
145 LoanedSample<T,U> operator [] (size_t index)
146 {
147 return native_samples_.info_array == NULL
148 ? LoanedSample<T, U>(native_samples_.data_array[index], NULL)
149 : LoanedSample<T, U>(
150 native_samples_.data_array[index],
151 native_samples_.info_array[index]);
152 }
153
157 int32_t length() const
158 {
159 return native_samples_.length;
160 }
161
185 {
186 if (input_) {
187 if (!RTI_RoutingServiceInput_return_loan(
188 input_,
189 &native_samples_)) {
190 throw dds::core::Error("error returning loaned samples to native input");
191 }
192 input_ = NULL; // Indicate that this object doesn't hold a loan anymore
193 }
194 }
195
196
202 {
203 return (native_samples_.info_array != NULL);
204 }
205
206
211 {
212 return iterator(
213 native_samples_.data_array,
214 native_samples_.info_array,
215 native_samples_.length);
216 }
217
222 {
223 return iterator(
224 native_samples_.data_array,
225 native_samples_.info_array,
226 native_samples_.length,
227 native_samples_.length);
228 }
229
234 {
235 return const_iterator(
236 native_samples_.data_array,
237 native_samples_.info_array,
238 native_samples_.length);
239 }
240
245 {
246 return const_iterator(
247 native_samples_.data_array,
248 native_samples_.info_array,
249 native_samples_.length,
250 native_samples_.length);
251 }
252
256 void swap(LoanedSamples& other) throw()
257 {
258 std::swap(input_, other.input_);
259 std::swap(native_samples_, other.native_samples_);
260 }
261
262#if !defined(RTI_CXX11_RVALUE_REFERENCES)
263 // Enables the safe-move-constructor idiom without C++11 move constructors
264 struct MoveProxy {
265 MoveProxy() : input_(NULL)
266 {
267 native_samples_.data_array = NULL;
268 native_samples_.info_array = NULL;
269 native_samples_.length = 0;
270 }
271
272 RTI_RoutingServiceInput *input_;
273 RTI_RoutingServiceLoanedSamples native_samples_;
274 };
275
276 LoanedSamples(MoveProxy proxy) throw() // move constructor idiom
277 : input_(proxy.input_),
278 native_samples_(proxy.native_samples_)
279 {
280 }
281
282 LoanedSamples& operator= (MoveProxy proxy) throw ()
283 {
284 // copy-and-swap idiom: copy new value, use temp's destructor to
285 // clean up existing values
286 LoanedSamples temp(proxy);
287 temp.swap(*this);
288 return *this;
289 }
290
291 operator MoveProxy () throw() // move-constructor idiom
292 {
293 MoveProxy proxy;
294
295 // move data to the proxy and return *this to an 'emtpy' state
296 std::swap(input_, proxy.input_);
297 std::swap(native_samples_, proxy.native_samples_);
298
299 return proxy;
300 }
301
302private:
303 // Direct assignment from one LoanedSamples to another is disabled.
304 // Use the move function to assign one LoanedSamples to another.
306 LoanedSamples & operator = (LoanedSamples &);
307
308#else
309
310
312 :input_(NULL)
313 {
314 native_samples_.data_array = NULL;
315 native_samples_.info_array = NULL;
316 native_samples_.length = 0;
317 other.swap(*this);
318 }
319
320
321 LoanedSamples& operator= (LoanedSamples&& other) throw ()
322 {
323 // clean up existing values
324 LoanedSamples temp(std::move(other));
325 temp.swap(*this);
326 return *this;
327 }
328
329#endif // !defined(RTI_CXX11_RVALUE_REFERENCES)
330
331private:
332 friend class TypedInput<T, U>;
333
334 void release()
335 {
336 input_ = NULL;
337 }
338
339 RTI_RoutingServiceLoanedSamples& native_samples()
340 {
341 return native_samples_;
342 }
343
344private:
345 // reference to the reader that created this LoanedSamples object
346 RTI_RoutingServiceInput *input_;
347 RTI_RoutingServiceLoanedSamples native_samples_;
348
349};
350
351template <typename T, typename U>
352LoanedSamples<T, U> move(LoanedSamples<T,U> & ls) OMG_NOEXCEPT
353{
354#if defined(RTI_CXX11_RVALUE_REFERENCES)
355 return std::move(ls);
356#else
357 return LoanedSamples<T,U>(typename LoanedSamples<T,U>::MoveProxy(ls));
358#endif
359}
360
361
362} } }
363
364#endif // RTI_ROUTING_PROCESSOR_LOANED_SAMPLES_HPP_
Provides a way to read samples of a specific type from a data domain. In the XML configuration file,...
Definition: StreamReader.hpp:45
Provides temporary access to a collection of samples (data and info) from a TypedInput.
Definition: LoanedSamples.hpp:81
SampleIterator< T, U > iterator
The iterator type.
Definition: LoanedSamples.hpp:91
int32_t length() const
Gets the number of samples in this collection.
Definition: LoanedSamples.hpp:157
const_iterator end() const
Gets an iterator to one past the last sample.
Definition: LoanedSamples.hpp:244
iterator end()
Gets an iterator to one past the last sample.
Definition: LoanedSamples.hpp:221
LoanedSample< T, U > operator[](size_t index)
Provides access to the underlying LoanedSample object in array-like syntax.
Definition: LoanedSamples.hpp:145
bool has_infos()
Returns whether the Info part is available for each Data item of this set of loaned samples.
Definition: LoanedSamples.hpp:201
const_iterator begin() const
Gets an iterator to the first sample.
Definition: LoanedSamples.hpp:233
void swap(LoanedSamples &other)
Swaps two LoanedSamples containers.
Definition: LoanedSamples.hpp:256
iterator begin()
Gets an iterator to the first sample.
Definition: LoanedSamples.hpp:210
LoanedSamples()
Creates an empty LoanedSamples object.
Definition: LoanedSamples.hpp:99
void return_loan()
Returns the samples to the TypedInput used to get these samples.
Definition: LoanedSamples.hpp:184
~LoanedSamples()
Automatically returns the loan to the TypedInput used to obtained these samples.
Definition: LoanedSamples.hpp:125
A random-access iterator of LoanedSample.
Definition: SampleIterator.hpp:44