RTI Routing Service  Version 6.0.0
 All Data Structures Files Functions Typedefs Enumerations Enumerator Groups Pages
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 <rti/routing/processor/LoanedSample.hpp>
25 #include <rti/routing/adapter/StreamReader.hpp>
26 #include <rti/routing/processor/SampleIterator.hpp>
27 
28 namespace rti { namespace routing { namespace processor {
29 
30 template <typename T, typename U> class TypedInput;
31 
32 namespace detail {
33 
34 struct NativeSamples {
35 
36 public:
37 
38  NativeSamples() : sample_array_(NULL), info_array_(NULL), length_(0)
39  {
40  }
41 
42  RTI_RoutingServiceSample *sample_array_;
43  RTI_RoutingServiceSampleInfo *info_array_;
44  int length_;
45 };
46 
47 }
48 
95 template <typename T, typename U = dds::sub::SampleInfo>
97 public:
98 
100  typedef typename std::vector<StreamReader::SamplePtr> SampleSeqType;
101  typedef typename std::vector<StreamReader::InfoPtr> InfoSeqType;
102 
108  typedef typename SampleIterator<T, U>::value_type value_type;
109  typedef std::ptrdiff_t difference_type;
110 
114  LoanedSamples() : reader_(NULL), native_env_(NULL)
115  {
116  }
117 
118 public:
119  // Private c-tor only to be used by static method create_from_loans()
121  RTI_RoutingServiceStreamReaderExt *native_reader,
122  detail::NativeSamples& native_samples,
123  RTI_RoutingServiceEnvironment *native_env)
124  : reader_(native_reader),
125  native_samples_(native_samples),
126  native_env_(native_env)
127  {
128  }
129 
130 public:
131 
138  ~LoanedSamples() throw()
139  {
140  try {
141  return_loan();
142  } catch (const std::exception& ex) { // Do not throw in destructor
143 
144  }
145  }
146 
147 
158  LoanedSample<T,U> operator [] (size_t index)
159  {
160  return LoanedSample<T,U>(
161  native_samples_.sample_array_[index],
162  native_samples_.info_array_[index]);
163  }
164 
168  int32_t length() const
169  {
170  return native_samples_.length_;
171  }
172 
195  void return_loan()
196  {
197  if (reader_) {
198  reader_->return_loan(
199  reader_->stream_reader_data,
200  native_samples_.sample_array_,
201  native_samples_.info_array_,
202  native_samples_.length_,
203  native_env_);
204  RTI_ROUTING_THROW_ON_ENV_ERROR(native_env_);
205  reader_ = NULL; // Indicate that this object doesn't hold a loan anymore
206  }
207  }
208 
209 
214  {
215  return iterator(
216  native_samples_.sample_array_,
217  native_samples_.info_array_,
218  native_samples_.length_,
219  0);
220  }
221 
226  {
227  return iterator(
228  native_samples_.sample_array_,
229  native_samples_.info_array_,
230  native_samples_.length_,
231  native_samples_.length_);
232  }
233 
238  {
239  return const_iterator(
240  native_samples_.sample_array_,
241  native_samples_.info_array_,
242  0);
243  }
244 
249  {
250  return const_iterator(
251  native_samples_.sample_array_,
252  native_samples_.info_array_,
253  native_samples_.length_);
254  }
255 
259  void swap(LoanedSamples& other) throw()
260  {
261  std::swap(reader_, other.reader_);
262  std::swap(native_samples_, other.native_samples_);
263  std::swap(native_env_, other.native_env_);
264  }
265 
266 #if !defined(RTI_CXX11_RVALUE_REFERENCES)
267  // Enables the safe-move-constructor idiom without C++11 move constructors
268  struct MoveProxy {
269  MoveProxy() : reader_(NULL)
270  {
271  }
272 
273  RTI_RoutingServiceStreamReaderExt *reader_;
274  detail::NativeSamples native_samples_;
275  RTI_RoutingServiceEnvironment *native_env_;
276  };
277 
278  LoanedSamples(MoveProxy proxy) throw() // move constructor idiom
279  : reader_(proxy.reader_),
280  native_samples_(proxy.native_samples_),
281  native_env_(proxy.native_env_)
282  {
283  }
284 
285  LoanedSamples& operator= (MoveProxy proxy) throw ()
286  {
287  // copy-and-swap idiom: copy new value, use temp's destructor to
288  // clean up existing values
289  LoanedSamples temp(proxy);
290  temp.swap(*this);
291  return *this;
292  }
293 
294  operator MoveProxy () throw() // move-constructor idiom
295  {
296  MoveProxy proxy;
297 
298  // move data to the proxy and return *this to an 'emtpy' state
299  std::swap(reader_, proxy.reader_);
300  std::swap(native_samples_, proxy.native_samples_);
301  std::swap(native_env_, proxy.native_env_);
302 
303  return proxy;
304  }
305 
306 private:
307  // Direct assignment from one LoanedSamples to another is disabled.
308  // Use the move function to assign one LoanedSamples to another.
310  LoanedSamples & operator = (LoanedSamples &);
311 
312 #else
313 
314 
316  :reader_(NULL), native_env_(NULL)
317  {
318  other.swap(*this);
319  }
320 
321 
322  LoanedSamples& operator= (LoanedSamples&& other) throw ()
323  {
324  // clean up existing values
325  LoanedSamples temp(std::move(other));
326  temp.swap(*this);
327  return *this;
328  }
329 
330 #endif // !defined(RTI_CXX11_RVALUE_REFERENCES)
331 
332 private:
333  friend class TypedInput<T, U>;
334 
335  void release()
336  {
337  reader_ = NULL;
338  }
339 
340  detail::NativeSamples& native_samples()
341  {
342  return native_samples_;
343  }
344 
345  bool has_infos()
346  {
347  return native_samples_.info_array_ != NULL;
348  }
349 
350 private:
351  // reference to the reader that created this LoanedSamples object
352  RTI_RoutingServiceStreamReaderExt *reader_;
353  detail::NativeSamples native_samples_;
354  RTI_RoutingServiceEnvironment *native_env_;
355 
356 };
357 
358 template <typename T, typename U>
359 LoanedSamples<T, U> move(LoanedSamples<T,U> & ls) OMG_NOEXCEPT
360 {
361 #if defined(RTI_CXX11_RVALUE_REFERENCES)
362  return std::move(ls);
363 #else
364  return LoanedSamples<T,U>(typename LoanedSamples<T,U>::MoveProxy(ls));
365 #endif
366 }
367 
368 
369 } } }
370 
371 #endif // RTI_ROUTING_PROCESSOR_LOANED_SAMPLES_HPP_

RTI Routing Service Version 6.0.0 Copyright © Sun Mar 3 2019 Real-Time Innovations, Inc