RTI Routing Service  Version 6.0.0
 All Data Structures Files Functions Typedefs Enumerations Enumerator Groups Pages
LoanedSample.hpp
1 /* $Id$
2 
3 (c) Copyright, Real-Time Innovations, 2015-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_SAMPLE_HPP_
15 #define RTI_ROUTING_PROCESSOR_LOANED_SAMPLE_HPP_
16 
17 #include <exception>
18 
19 namespace rti { namespace routing { namespace processor {
20 
27 template <typename T, typename U>
28 class LoanedSample
29 {
30 public:
34  typedef T DataType;
38  typedef U InfoType;
39 
40  LoanedSample()
41  : _data_ptr(NULL),
42  _info_ptr(NULL)
43  {}
44 
45  LoanedSample(const DataType * the_data, const InfoType * the_info)
46  : _data_ptr(the_data),
47  _info_ptr(the_info)
48  {}
49 
50 
51  LoanedSample(const DataType & the_data, const InfoType & the_info)
52  : _data_ptr(&the_data),
53  _info_ptr(&the_info)
54  {}
55 
56  LoanedSample(const void * the_data, const void * the_info)
57  : _data_ptr(static_cast<const T*>(the_data)),
58  _info_ptr(static_cast<const U*>(the_info))
59  {}
60 
61 
62  const DataType & data() const
63  {
64  if (!info().valid()) {
65  throw dds::core::PreconditionNotMetError("Invalid data");
66  }
67 
68  return *_data_ptr;
69  }
70 
74  const InfoType & info() const {
75  return *_info_ptr;
76  }
77 
78 
79  operator const DataType & () const
80  {
81  return data();
82  }
83 
84  LoanedSample<DataType, InfoType> * operator ->() {
85  return this;
86  }
87 
88  const LoanedSample<DataType, InfoType> * operator ->() const {
89  return this;
90  }
91 
95  bool operator==(const LoanedSample& other) const
96  {
97  if (info() != other.info()) {
98  return false;
99  }
100 
101  if (info().valid()) {
102  if (data() != other.data()) {
103  return false;
104  }
105  }
106 
107  return true;
108  }
109 
110  bool operator!=(const LoanedSample& other) const
111  {
112  return !(*this == other);
113  }
114 
115  void swap(LoanedSample & other) throw()
116  {
117  using std::swap;
118 
119  swap(_data_ptr, other._data_ptr);
120  swap(_info_ptr, other._info_ptr);
121  }
122 
123 private:
124  const DataType * _data_ptr;
125  const InfoType * _info_ptr;
126 };
127 
128 } } }
129 
130 #endif // RTI_ROUTING_PROCESSOR_LOANED_SAMPLE_HPP_

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