RTI Routing Service Version 7.3.0
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
24namespace rti { namespace routing { namespace processor {
25
26class Route;
27
28template <typename Data, typename Info> class TypedOutput;
29
30template <typename Data, typename Info = dds::sub::SampleInfo>
31class TypedOutput;
32
44class Output {
45public:
46
47 /*i
48 */
49 Output(
50 RTI_RoutingServiceOutput *native,
51 int32_t index,
52 RTI_RoutingServiceRoute *native_route) :
53 native_(native),
54 index_(index),
55 native_route_(native_route),
56 stream_info_(*RTI_RoutingServiceOutput_get_stream_info(native)),
57 name_(RTI_RoutingServiceOutput_get_name(native))
58 {
59 }
60
61 /*i
62 */
63 const rti::routing::StreamInfo& stream_info() const
64 {
65 return stream_info_;
66 }
67
68 /*i
69 *
70 */
71 const std::string& name() const
72 {
73 return name_;
74 }
75
76 /*i
77 *
78 */
79 int32_t index()
80 {
81 return index_;
82 }
83
93 template <typename Data, typename Info>
95 {
96 return this;
97 }
98
108 template <typename Data>
110 {
111 return this;
112 }
113
114private:
115 template <typename Data, typename Info> friend class TypedOutput;
116 friend class Route;
117 RTI_RoutingServiceOutput *native_;
118 int32_t index_;
119 RTI_RoutingServiceRoute *native_route_;
120 rti::routing::StreamInfo stream_info_;
121 std::string name_;
122};
123
132template <typename Data, typename Info>
134public:
135 TypedOutput(Output *output) ;
136
137 TypedOutput<Data, Info>* operator->();
138
146
152 const std::string& name() const;
153
165 void write(const Data& sample, const Info& info);
166
167 /*
168 * @brief Writes a sample given its data only.
169 *
170 * This operation will call rti::routing::adapter::StreamWriter::write
171 * on the underlying rti::routing::adapter::StreamWriter, providing a null
172 * value for the info sample. The underlying adapter will compute any
173 * associated metadata from the data sample.
174 */
175 void write(const Data& sample);
176
195 Data create_data();
196
213 dds::pub::DataWriter<Data> dds_data_writer()
214 {
215 DDS_DataWriter *native_writer =
216 RTI_RoutingServiceOutput_get_dds_writer(output_->native_);
217 if (native_writer == NULL) {
218 throw dds::core::InvalidArgumentError(
219 "invalid argument: input does not hold a DDS StreamWriter");
220 }
221
222 typedef dds::pub::DataWriter<Data> data_writer_type;
223 return rti::core::detail::create_from_native_entity<data_writer_type>(
224 native_writer);
225 }
226
227private:
229
230 Output *output_;
231};
232
233template <typename Data, typename Info>
234struct create_data_from_output {
235
236 static Data get(TypedOutput<Data, Info>& )
237 {
238 return Data();
239 }
240};
241
242
243template <typename Info>
244struct create_data_from_output<dds::core::xtypes::DynamicData, Info> {
245
246 static dds::core::xtypes::DynamicData get(TypedOutput<dds::core::xtypes::DynamicData, Info>& output)
247 {
248 if (output->stream_info().type_info().type_representation_kind()
249 != TypeRepresentationKind::DYNAMIC_TYPE) {
250 throw dds::core::PreconditionNotMetError(
251 "inconsistent data representation kind");
252 }
253 dds::core::xtypes::DynamicType *type_code =
254 static_cast<dds::core::xtypes::DynamicType *> (
255 output->stream_info().type_info().type_representation());
256 return dds::core::xtypes::DynamicData(*type_code);
257 }
258};
259
260
261template <typename Data, typename Info>
262TypedOutput<Data,Info>::TypedOutput(Output* output) : output_(output)
263{
264}
265
266template <typename Data, typename Info>
268{
269 return output_->stream_info_;
270}
271
272template <typename Data, typename Info>
273const std::string& TypedOutput<Data, Info>::name() const
274{
275 return output_->name_;
276}
277
278template <typename Data, typename Info>
280{
281 return this;
282}
283
284template <typename Data, typename Info>
285void TypedOutput<Data, Info>::write(const Data& sample, const Info& info)
286{
287 if (!RTI_RoutingServiceOutput_write_sample(
288 output_->native_,
289 const_cast<void *> (reinterpret_cast<const void*> (&sample)),
290 const_cast<void *> (reinterpret_cast<const void*> (&info)))) {
291 throw dds::core::Error("error writing sample to native output");
292 }
293}
294
295template <typename Data, typename Info>
296void TypedOutput<Data,Info>::write(const Data& sample)
297{
298 if (!RTI_RoutingServiceOutput_write_sample(
299 output_->native_,
300 const_cast<void *> (reinterpret_cast<const void*> (&sample)),
301 NULL)) {
302 throw dds::core::Error("error writing sample to native output");
303 }
304}
305
306template <typename Data, typename Info>
308{
309 return create_data_from_output<Data, Info>::get(*this);
310}
311
312
321typedef dds::pub::DataWriter<dds::core::xtypes::DynamicData> DynamicDataWriter;
322
323} } }
324
325
326#endif // RTI_ROUTING_PROCESSOR_OUTPUT_HPP_
Definition of the stream information that RTI Routing Service needs to manage user data streams.
Definition: StreamInfo.hpp:106
Generic Representation of a Route's output.
Definition: Output.hpp:44
TypedOutput< Data, Info > get()
Returns a typed version of this Output.
Definition: Output.hpp:94
TypedOutput< Data, dds::sub::SampleInfo > get()
Returns a typed version of this Output.
Definition: Output.hpp:109
Representation of the Route object that owns a Processor.
Definition: Route.hpp:84
Representation of an Output whose data representation is DataRep, whose info representation is InfoRe...
Definition: Output.hpp:133
const std::string & name() const
Returns the name of this output.
Definition: Output.hpp:273
const rti::routing::StreamInfo & stream_info() const
Returns the StreamInfo associated with this object.
Definition: Output.hpp:267
Data create_data()
Creates a new data sample that can be used to write in this output.
Definition: Output.hpp:307
dds::pub::DataWriter< Data > dds_data_writer()
Returns the underlying DDS DataWriter that is part of this StreamWriter implementation,...
Definition: Output.hpp:213
void write(const Data &sample, const Info &info)
Writes the specified data and info sample in this output.
Definition: Output.hpp:285