10 #ifndef HPP_SERVICE_DYNAMIC_DATA_CONVERTER_HPP_
11 #define HPP_SERVICE_DYNAMIC_DATA_CONVERTER_HPP_
16 #include <dds/core/macros.hpp>
18 #include "osapi/osapi_heap.h"
19 #include "routingservice/routingservice_log.h"
20 #include "routingservice/routingservice_common.h"
22 #include <dds/core/Value.hpp>
23 #include <dds/core/types.hpp>
24 #include <rtiboost/assert.hpp>
25 #include <dds/core/xtypes/DynamicType.hpp>
26 #include <dds/core/xtypes/DynamicData.hpp>
27 #include <rti/core/NativeValueType.hpp>
28 #include <rti/service/ServiceException.hpp>
29 #include <rti/service/DynamicBuffer.hpp>
31 namespace rti {
namespace service {
33 class DynamicDataConverter {
40 ROUTERDynamicDataConverter *converter,
41 DDS_DynamicData *native) :
42 converter_(converter),
49 ROUTERDynamicDataConverter_returnSample(converter_, native_);
52 dds::core::xtypes::DynamicData&
get()
54 return rti::core::native_conversions::cast_from_native<
55 dds::core::xtypes::DynamicData>(*native_);
59 ROUTERDynamicDataConverter *converter_;
60 DDS_DynamicData *native_;
66 ScopedBuffer() : converter_(NULL), native_(NULL), native_length_(0)
71 ROUTERDynamicDataConverter *converter,
73 uint32_t native_length) :
74 converter_(converter),
76 native_length_(native_length)
82 if (native_ != NULL) {
83 ROUTERDynamicDataConverter_returnBuffer(converter_, native_);
89 assign(converter_, NULL, 0);
93 ROUTERDynamicDataConverter *converter,
95 uint32_t native_length)
97 if (native_ != NULL) {
98 ROUTERDynamicDataConverter_returnBuffer(
100 const_cast<void *>(native_));
102 converter_ = converter;
104 native_length_ = native_length;
107 ScopedBuffer& operator =(ScopedBuffer& other)
109 assign(other.converter_, other.native_, other.native_length_);
118 template <
typename T>
120 return static_cast<const T *
>(native_);
125 return native_length_;
129 ROUTERDynamicDataConverter *converter_;
131 uint32_t native_length_;
138 DynamicDataConverter() : initialized_(false) {}
144 DynamicDataConverter(
145 const dds::core::xtypes::DynamicType& type_code,
146 const DynamicBufferProperty &property) :
149 set(type_code, property);
159 const dds::core::xtypes::DynamicType& type_code,
160 const DynamicBufferProperty &property)
163 if (!ROUTERDynamicDataConverter_initialize(
165 &(type_code.native()),
166 (RTI_UINT32)
property.min_size())) {
167 RTI_THROW_SERVICE_EXCEPTION(
168 &RTI_LOG_INIT_FAILURE_s,
175 ~DynamicDataConverter()
178 ROUTERDynamicDataConverter_finalize(&converter_);
183 const dds::core::xtypes::DynamicData& sample,
184 const DDS_DataRepresentationId_t target_representation)
186 DDS_DynamicData *native = ROUTERDynamicDataConverter_getSample(
188 const_cast<DDS_DynamicData *>(&sample.native()),
189 target_representation);
190 if (native == NULL) {
191 RTI_THROW_SERVICE_EXCEPTION(
192 &RTI_LOG_ANY_FAILURE_s,
193 "get native sample");
195 return ScopedSample(&converter_, native);
199 const dds::core::xtypes::DynamicData& sample,
200 const DDS_DataRepresentationId_t target_representation)
202 DDS_UnsignedLong native_length = 0;
203 void *native = ROUTERDynamicDataConverter_getBuffer(
206 const_cast<DDS_DynamicData *>(&sample.native()),
207 target_representation);
208 if (native == NULL) {
209 RTI_THROW_SERVICE_EXCEPTION(
210 &RTI_LOG_ANY_FAILURE_s,
211 "get native buffer");
213 return ScopedBuffer(&converter_, native, (uint32_t) native_length);
217 ScopedBuffer& scoped_buffer,
218 const dds::core::xtypes::DynamicData& sample,
219 const DDS_DataRepresentationId_t target_representation)
221 DDS_UnsignedLong native_length = 0;
228 scoped_buffer.clear();
229 void *native = ROUTERDynamicDataConverter_getBuffer(
232 const_cast<DDS_DynamicData *>(&sample.native()),
233 target_representation);
234 if (native == NULL) {
235 RTI_THROW_SERVICE_EXCEPTION(
236 &RTI_LOG_ANY_FAILURE_s,
237 "get native buffer");
239 scoped_buffer.assign(&converter_, native, (uint32_t) native_length);
243 ROUTERDynamicDataConverter converter_;