RTI Routing Service  Version 6.0.0
 All Data Structures Files Functions Typedefs Enumerations Enumerator Groups Pages
Output.hpp
1 /*
2  * (c) Copyright, Real-Time Innovations, 2017.
3  * All rights reserved.
4  *
5  * No duplications, whole or partial, manual or electronic, may be made
6  * without express written permission. Any such copies, or
7  * revisions thereof, must display this notice unaltered.
8  * This code contains trade secrets of Real-Time Innovations, Inc.
9  */
10 
11 #ifndef RTI_ROUTING_PROCESSOR_OUTPUT_HPP_
12 #define RTI_ROUTING_PROCESSOR_OUTPUT_HPP_
13 
14 #include <dds/core/Value.hpp>
15 #include <dds/core/SafeEnumeration.hpp>
16 #include <rti/core/NativeValueType.hpp>
17 
18 #include "routingservice/routingservice_processor.h"
19 #include "routingservice/routingservice_adapter_new.h"
20 #include <rti/routing/adapter/StreamWriter.hpp>
21 
22 namespace rti { namespace routing { namespace processor {
23 
24 class Route;
25 
26 template <typename Data, typename Info> class TypedOutput;
27 
28 inline
29 rti::routing::StreamInfo stream_info_from_native_output(
30  RTI_RoutingServiceStreamWriterExt *native,
31  RTI_RoutingServiceRoute *native_route)
32 {
34  *RTI_RoutingServiceRoute_get_output_stream_info(native_route, native));
35 }
36 
37 inline
38 std::string name_from_native_output(
39  RTI_RoutingServiceStreamWriterExt *native,
40  RTI_RoutingServiceRoute *native_route)
41 {
42  return RTI_RoutingServiceRoute_get_output_name(
43  native_route,
44  native);
45 }
46 
47 template <typename Data, typename Info = dds::sub::SampleInfo>
48 class TypedOutput;
49 
65 public:
66 
67  /*i
68  */
69  Output(
70  RTI_RoutingServiceStreamWriterExt *native,
71  int32_t index,
72  RTI_RoutingServiceRoute *native_route,
73  RTI_RoutingServiceEnvironment *native_env)
74  : native_(native),
75  index_(index),
76  native_env_(native_env),
77  stream_info_(stream_info_from_native_output(native, native_route)),
78  name_(name_from_native_output(native, native_route))
79  {
80  }
81 
82  /*i
83  */
84  const rti::routing::StreamInfo& stream_info() const
85  {
86  return stream_info_;
87  }
88 
89  /*i
90  *
91  */
92  const std::string& name() const
93  {
94  return name_;
95  }
96 
97  /*i
98  *
99  */
100  int32_t index()
101  {
102  return index_;
103  }
104 
114  template <typename Data, typename Info>
116  {
117  return this;
118  }
119 
129  template <typename Data>
131  {
132  return this;
133  }
134 
135 
136 private:
137  /*i
138  */
139  rti::routing::adapter::StreamWriter& stream_writer()
140  {
141  return *this;
142  }
143 
144  int write(
145  const std::vector<SamplePtr>& sample_seq,
146  const std::vector<InfoPtr>& info_seq) RTI_FINAL
147  {
148  int count = native_->write(
149  native_->stream_writer_data,
150  &sample_seq[0],
151  &info_seq[0],
152  sample_seq.size(),
153  native_env_);
154  RTI_ROUTING_THROW_ON_ENV_ERROR(native_env_);
155  return count;
156  }
157 
158  virtual void update(const std::map<std::string, std::string>&) RTI_FINAL
159  {
160  throw dds::core::PreconditionNotMetError(
161  "update not applicable within a Processor notification context");
162  }
163 
164 
165  RTI_RoutingServiceStreamWriterExt* native()
166  {
167  return native_;
168  }
169 
170 private:
171  template <typename Data, typename Info> friend class TypedOutput;
172  friend class Route;
173  RTI_RoutingServiceStreamWriterExt *native_;
174  int32_t index_;
175  RTI_RoutingServiceEnvironment *native_env_;
176  rti::routing::StreamInfo stream_info_;
177  std::string name_;
178 };
179 
188 template <typename Data, typename Info>
190 public:
191  TypedOutput(Output *output) ;
192 
193  TypedOutput<Data, Info>* operator->();
194 
201  const rti::routing::StreamInfo& stream_info() const;
202 
208  const std::string& name() const;
209 
221  void write(const Data& sample, const Info& info);
222 
223  /*
224  * @brief Writes a sample given its data only.
225  *
226  * This operation will call rti::routing::adapter::StreamWriter::write
227  * on the underlying rti::routing::adapter::StreamWriter, providing a null
228  * value for the info sample. The underlying adapter will compute any
229  * associated metadata from the data sample.
230  */
231  void write(const Data& sample);
232 
251  Data create_data();
252 
253 private:
254  friend class rti::routing::processor::Route;
255 
256  Output *output_;
257 };
258 
259 template <typename Data, typename Info>
260 struct create_data_from_output {
261 
262  static Data get(TypedOutput<Data, Info>& )
263  {
264  return Data();
265  }
266 };
267 
268 
269 template <typename Info>
270 struct create_data_from_output<dds::core::xtypes::DynamicData, Info> {
271 
272  static dds::core::xtypes::DynamicData get(TypedOutput<dds::core::xtypes::DynamicData, Info>& output)
273  {
274  if (output->stream_info().type_info().type_representation_kind()
275  != TypeRepresentationKind::DYNAMIC_TYPE) {
276  throw dds::core::PreconditionNotMetError(
277  "inconsistent data representation kind");
278  }
279  dds::core::xtypes::DynamicType *type_code =
280  static_cast<dds::core::xtypes::DynamicType *> (
281  output->stream_info().type_info().type_representation());
282  return dds::core::xtypes::DynamicData(*type_code);
283  }
284 };
285 
286 
287 template <typename Data, typename Info>
288 TypedOutput<Data,Info>::TypedOutput(Output* output) : output_(output)
289 {
290 }
291 
292 template <typename Data, typename Info>
294 {
295  return output_->stream_info_;
296 }
297 
298 template <typename Data, typename Info>
299 const std::string& TypedOutput<Data, Info>::name() const
300 {
301  return output_->name_;
302 }
303 
304 template <typename Data, typename Info>
306 {
307  return this;
308 }
309 
310 template <typename Data, typename Info>
311 void TypedOutput<Data,Info>::write(const Data& sample, const Info& info)
312 {
313  const RTI_RoutingServiceSample out_samples[1] = {
314  const_cast<void *> (reinterpret_cast<const void *> (&sample))
315  };
316  const RTI_RoutingServiceSampleInfo out_infos[1] = {
317  const_cast<void *> (reinterpret_cast<const void *> (&info))
318  };
319  output_->native_->write(
320  output_->native_->stream_writer_data,
321  reinterpret_cast<const RTI_RoutingServiceSample *> (&out_samples),
322  reinterpret_cast<const RTI_RoutingServiceSample *> (&out_infos),
323  1,
324  output_->native_env_);
325  RTI_ROUTING_THROW_ON_ENV_ERROR(output_->native_env_);
326 }
327 
328 template <typename Data, typename Info>
329 void TypedOutput<Data,Info>::write(const Data& sample)
330 {
331  const RTI_RoutingServiceSample out_samples[1] = {
332  const_cast<void *> (reinterpret_cast<const void *> (&sample))
333  };
334  output_->native_->write(
335  output_->native_->stream_writer_data,
336  reinterpret_cast<const RTI_RoutingServiceSample *> (&out_samples),
337  NULL,
338  1,
339  output_->native_env_);
340  RTI_ROUTING_THROW_ON_ENV_ERROR(output_->native_env_);
341 }
342 
343 template <typename Data, typename Info>
345 {
346  return create_data_from_output<Data, Info>::get(*this);
347 }
348 
349 
358 
359 } } }
360 
361 
362 #endif // RTI_ROUTING_PROCESSOR_OUTPUT_HPP_

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