RTI Routing Service  Version 6.0.1
 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 #include <dds/pub/DataWriter.hpp>
18 #include <dds/core/xtypes/DynamicData.hpp>
19 
20 #include "routingservice/routingservice_processor.h"
21 #include "routingservice/routingservice_adapter_new.h"
22 #include <rti/routing/adapter/StreamWriter.hpp>
23 
24 namespace rti { namespace routing { namespace processor {
25 
26 class Route;
27 
28 template <typename Data, typename Info> class TypedOutput;
29 
30 inline
31 rti::routing::StreamInfo stream_info_from_native_output(
32  RTI_RoutingServiceStreamWriterExt *native,
33  RTI_RoutingServiceRoute *native_route)
34 {
36  *RTI_RoutingServiceRoute_get_output_stream_info(native_route, native));
37 }
38 
39 inline
40 std::string name_from_native_output(
41  RTI_RoutingServiceStreamWriterExt *native,
42  RTI_RoutingServiceRoute *native_route)
43 {
44  return RTI_RoutingServiceRoute_get_output_name(
45  native_route,
46  native);
47 }
48 
49 template <typename Data, typename Info = dds::sub::SampleInfo>
50 class TypedOutput;
51 
67 public:
68 
69  /*i
70  */
71  Output(
72  RTI_RoutingServiceStreamWriterExt *native,
73  int32_t index,
74  RTI_RoutingServiceRoute *native_route,
75  RTI_RoutingServiceEnvironment *native_env)
76  : native_(native),
77  index_(index),
78  native_route_(native_route),
79  native_env_(native_env),
80  stream_info_(stream_info_from_native_output(native, native_route)),
81  name_(name_from_native_output(native, native_route))
82  {
83  }
84 
85  /*i
86  */
87  const rti::routing::StreamInfo& stream_info() const
88  {
89  return stream_info_;
90  }
91 
92  /*i
93  *
94  */
95  const std::string& name() const
96  {
97  return name_;
98  }
99 
100  /*i
101  *
102  */
103  int32_t index()
104  {
105  return index_;
106  }
107 
117  template <typename Data, typename Info>
119  {
120  return this;
121  }
122 
132  template <typename Data>
134  {
135  return this;
136  }
137 
138 
139 private:
140  /*i
141  */
142  rti::routing::adapter::StreamWriter& stream_writer()
143  {
144  return *this;
145  }
146 
147  int write(
148  const std::vector<SamplePtr>& sample_seq,
149  const std::vector<InfoPtr>& info_seq) RTI_FINAL
150  {
151  int count = native_->write(
152  native_->stream_writer_data,
153  &sample_seq[0],
154  &info_seq[0],
155  sample_seq.size(),
156  native_env_);
157  RTI_ROUTING_THROW_ON_ENV_ERROR(native_env_);
158  return count;
159  }
160 
161  virtual void update(const std::map<std::string, std::string>&) RTI_FINAL
162  {
163  throw dds::core::PreconditionNotMetError(
164  "update not applicable within a Processor notification context");
165  }
166 
167 
168  RTI_RoutingServiceStreamWriterExt* native()
169  {
170  return native_;
171  }
172 
173 private:
174  template <typename Data, typename Info> friend class TypedOutput;
175  friend class Route;
176  RTI_RoutingServiceStreamWriterExt *native_;
177  int32_t index_;
178  RTI_RoutingServiceRoute *native_route_;
179  RTI_RoutingServiceEnvironment *native_env_;
180  rti::routing::StreamInfo stream_info_;
181  std::string name_;
182 };
183 
192 template <typename Data, typename Info>
194 public:
195  TypedOutput(Output *output) ;
196 
197  TypedOutput<Data, Info>* operator->();
198 
205  const rti::routing::StreamInfo& stream_info() const;
206 
212  const std::string& name() const;
213 
225  void write(const Data& sample, const Info& info);
226 
227  /*
228  * @brief Writes a sample given its data only.
229  *
230  * This operation will call rti::routing::adapter::StreamWriter::write
231  * on the underlying rti::routing::adapter::StreamWriter, providing a null
232  * value for the info sample. The underlying adapter will compute any
233  * associated metadata from the data sample.
234  */
235  void write(const Data& sample);
236 
255  Data create_data();
256 
273  dds::pub::DataWriter<Data> dds_data_writer()
274  {
275  DDS_DataWriter *native_writer = RTI_RoutingServiceRoute_get_dds_writer(
276  output_->native_route_,
277  output_->native_);
278  if (native_writer == NULL) {
279  throw dds::core::InvalidArgumentError(
280  "invalid argument: input does not hold a DDS StreamWriter");
281  }
282 
283  typedef dds::pub::DataWriter<Data> data_writer_type;
284  return rti::core::detail::create_from_native_entity<data_writer_type>(
285  native_writer);
286  }
287 
288 private:
289  friend class rti::routing::processor::Route;
290 
291  Output *output_;
292 };
293 
294 template <typename Data, typename Info>
295 struct create_data_from_output {
296 
297  static Data get(TypedOutput<Data, Info>& )
298  {
299  return Data();
300  }
301 };
302 
303 
304 template <typename Info>
305 struct create_data_from_output<dds::core::xtypes::DynamicData, Info> {
306 
307  static dds::core::xtypes::DynamicData get(TypedOutput<dds::core::xtypes::DynamicData, Info>& output)
308  {
309  if (output->stream_info().type_info().type_representation_kind()
310  != TypeRepresentationKind::DYNAMIC_TYPE) {
311  throw dds::core::PreconditionNotMetError(
312  "inconsistent data representation kind");
313  }
314  dds::core::xtypes::DynamicType *type_code =
315  static_cast<dds::core::xtypes::DynamicType *> (
316  output->stream_info().type_info().type_representation());
317  return dds::core::xtypes::DynamicData(*type_code);
318  }
319 };
320 
321 
322 template <typename Data, typename Info>
323 TypedOutput<Data,Info>::TypedOutput(Output* output) : output_(output)
324 {
325 }
326 
327 template <typename Data, typename Info>
329 {
330  return output_->stream_info_;
331 }
332 
333 template <typename Data, typename Info>
334 const std::string& TypedOutput<Data, Info>::name() const
335 {
336  return output_->name_;
337 }
338 
339 template <typename Data, typename Info>
341 {
342  return this;
343 }
344 
345 template <typename Data, typename Info>
346 void TypedOutput<Data,Info>::write(const Data& sample, const Info& info)
347 {
348  const RTI_RoutingServiceSample out_samples[1] = {
349  const_cast<void *> (reinterpret_cast<const void *> (&sample))
350  };
351  const RTI_RoutingServiceSampleInfo out_infos[1] = {
352  const_cast<void *> (reinterpret_cast<const void *> (&info))
353  };
354  output_->native_->write(
355  output_->native_->stream_writer_data,
356  reinterpret_cast<const RTI_RoutingServiceSample *> (&out_samples),
357  reinterpret_cast<const RTI_RoutingServiceSample *> (&out_infos),
358  1,
359  output_->native_env_);
360  RTI_ROUTING_THROW_ON_ENV_ERROR(output_->native_env_);
361 }
362 
363 template <typename Data, typename Info>
364 void TypedOutput<Data,Info>::write(const Data& sample)
365 {
366  const RTI_RoutingServiceSample out_samples[1] = {
367  const_cast<void *> (reinterpret_cast<const void *> (&sample))
368  };
369  output_->native_->write(
370  output_->native_->stream_writer_data,
371  reinterpret_cast<const RTI_RoutingServiceSample *> (&out_samples),
372  NULL,
373  1,
374  output_->native_env_);
375  RTI_ROUTING_THROW_ON_ENV_ERROR(output_->native_env_);
376 }
377 
378 template <typename Data, typename Info>
380 {
381  return create_data_from_output<Data, Info>::get(*this);
382 }
383 
384 
393 typedef dds::pub::DataWriter<dds::core::xtypes::DynamicData> DynamicDataWriter;
394 
395 } } }
396 
397 
398 #endif // RTI_ROUTING_PROCESSOR_OUTPUT_HPP_

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