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

RTI Routing Service Version 6.0.1 Copyright © Sun Nov 17 2019 Real-Time Innovations, Inc