RTI Routing Service  Version 7.0.0
LoanedSamples.hpp
1 /* $Id$
2 
3 (c) Copyright, Real-Time Innovations, 2013-2016.
4 All rights reserved.
5 
6 No duplications, whole or partial, manual or electronic, may be made
7 without express written permission. Any such copies, or
8 revisions thereof, must display this notice unaltered.
9 This 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 
29 namespace rti { namespace routing { namespace processor {
30 
31 template <typename T, typename U> class TypedInput;
32 
33 
80 template <typename T, typename U = dds::sub::SampleInfo>
82 public:
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 
107 public:
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 
117 public:
118 
125  ~LoanedSamples() throw()
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 
184  void return_loan()
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 
201  bool has_infos()
202  {
203  return (native_samples_.info_array != NULL);
204  }
205 
206 
210  iterator begin()
211  {
212  return iterator(
213  native_samples_.data_array,
214  native_samples_.info_array,
215  native_samples_.length);
216  }
217 
221  iterator end()
222  {
223  return iterator(
224  native_samples_.data_array,
225  native_samples_.info_array,
226  native_samples_.length,
227  native_samples_.length);
228  }
229 
233  const_iterator begin() const
234  {
235  return const_iterator(
236  native_samples_.data_array,
237  native_samples_.info_array,
238  native_samples_.length);
239  }
240 
244  const_iterator end() const
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 
302 private:
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 
331 private:
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 
344 private:
345  // reference to the reader that created this LoanedSamples object
346  RTI_RoutingServiceInput *input_;
347  RTI_RoutingServiceLoanedSamples native_samples_;
348 
349 };
350 
351 template <typename T, typename U>
352 LoanedSamples<T, U> move(LoanedSamples<T,U> & ls) OMG_NOEXCEPT
353 {
354 #if defined(RTI_CXX11_RVALUE_REFERENCES)
355  return std::move(ls);
356 #else
358 #endif
359 }
360 
361 
362 } } }
363 
364 #endif // RTI_ROUTING_PROCESSOR_LOANED_SAMPLES_HPP_
LoanedSamples()
Creates an empty LoanedSamples object.
Definition: LoanedSamples.hpp:99
Provides temporary access to a collection of samples (data and info) from a TypedInput.
Definition: LoanedSamples.hpp:81
iterator begin()
Gets an iterator to the first sample.
Definition: LoanedSamples.hpp:210
void swap(LoanedSamples &other)
Swaps two LoanedSamples containers.
Definition: LoanedSamples.hpp:256
A random-access iterator of LoanedSample.
Definition: SampleIterator.hpp:44
iterator end()
Gets an iterator to one past the last sample.
Definition: LoanedSamples.hpp:221
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 end() const
Gets an iterator to one past the last sample.
Definition: LoanedSamples.hpp:244
Provides a way to read samples of a specific type from a data domain. In the XML configuration file...
Definition: StreamReader.hpp:45
void return_loan()
Returns the samples to the TypedInput used to get these samples.
Definition: LoanedSamples.hpp:184
Definition: AdapterPlugin.hpp:25
LoanedSample< T, U > operator[](size_t index)
Provides access to the underlying LoanedSample object in array-like syntax.
Definition: LoanedSamples.hpp:145
int32_t length() const
Gets the number of samples in this collection.
Definition: LoanedSamples.hpp:157
Representation of an Input whose data representation is DataRep, whose info representation is InfoRep...
Definition: Input.hpp:32
~LoanedSamples()
Automatically returns the loan to the TypedInput used to obtained these samples.
Definition: LoanedSamples.hpp:125
const_iterator begin() const
Gets an iterator to the first sample.
Definition: LoanedSamples.hpp:233
SampleIterator< T, U > iterator
The iterator type.
Definition: LoanedSamples.hpp:91