Fast DDS  Version 3.6.1.0
Fast DDS
TopicDataType.hpp
1 // Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
19 #ifndef FASTDDS_DDS_TOPIC__TOPICDATATYPE_HPP
20 #define FASTDDS_DDS_TOPIC__TOPICDATATYPE_HPP
21 
22 #include <functional>
23 #include <memory>
24 #include <string>
25 
26 #include <fastdds/dds/core/policy/QosPolicies.hpp>
27 #include <fastdds/dds/xtypes/type_representation/detail/dds_xtypes_typeobject.hpp>
28 #include <fastdds/fastdds_dll.hpp>
29 #include <fastdds/rtps/common/CdrSerialization.hpp>
30 #include <fastdds/rtps/common/InstanceHandle.hpp>
31 #include <fastdds/rtps/common/SerializedPayload.hpp>
32 #include <fastdds/utils/md5.hpp>
33 
34 // This version of TypeSupport has `is_bounded()`
35 #define TOPIC_DATA_TYPE_API_HAS_IS_BOUNDED
36 
37 // This version of TypeSupport has `is_plain()`
38 #define TOPIC_DATA_TYPE_API_HAS_IS_PLAIN
39 
40 // This version of TypeSupport has `construct_sample()`
41 #define TOPIC_DATA_TYPE_API_HAS_CONSTRUCT_SAMPLE
42 
43 namespace eprosima {
44 namespace fastdds {
45 
46 namespace rtps {
47 struct SerializedPayload_t;
48 struct InstanceHandle_t;
49 } // namespace rtps
50 
51 namespace dds {
52 
53 class TypeSupport;
54 
61 {
62 public:
63 
70  struct FASTDDS_EXPORTED_API Context
71  {
72  virtual ~Context() = default;
73  };
74 
78  FASTDDS_EXPORTED_API TopicDataType() = default;
79 
83  FASTDDS_EXPORTED_API virtual ~TopicDataType() = default;
84 
95  FASTDDS_EXPORTED_API virtual bool serialize(
96  const void* const data,
98  eprosima::fastdds::dds::DataRepresentationId_t data_representation) = 0;
99 
112  FASTDDS_EXPORTED_API virtual bool serialize_ctx(
113  const std::shared_ptr<Context>& context,
114  const void* const data,
115  rtps::SerializedPayload_t& payload,
117  {
118  static_cast<void>(context);
119  return serialize(data, payload, data_representation);
120  }
121 
129  FASTDDS_EXPORTED_API virtual bool deserialize(
130  rtps::SerializedPayload_t& payload,
131  void* data) = 0;
132 
143  FASTDDS_EXPORTED_API virtual bool deserialize_ctx(
144  const std::shared_ptr<Context>& context,
145  rtps::SerializedPayload_t& payload,
146  void* data)
147  {
148  static_cast<void>(context);
149  return deserialize(payload, data);
150  }
151 
159  FASTDDS_EXPORTED_API virtual uint32_t calculate_serialized_size(
160  const void* const data,
161  eprosima::fastdds::dds::DataRepresentationId_t data_representation) = 0;
162 
173  FASTDDS_EXPORTED_API virtual uint32_t calculate_serialized_size_ctx(
174  const std::shared_ptr<Context>& context,
175  const void* const data,
177  {
178  static_cast<void>(context);
179  return calculate_serialized_size(data, data_representation);
180  }
181 
187  FASTDDS_EXPORTED_API virtual void* create_data() = 0;
188 
197  FASTDDS_EXPORTED_API virtual void* create_data_ctx(
198  const std::shared_ptr<Context>& context)
199  {
200  static_cast<void>(context);
201  return create_data();
202  }
203 
209  FASTDDS_EXPORTED_API virtual void delete_data(
210  void* data) = 0;
211 
220  FASTDDS_EXPORTED_API virtual void delete_data_ctx(
221  const std::shared_ptr<Context>& context,
222  void* data)
223  {
224  static_cast<void>(context);
225  delete_data(data);
226  }
227 
236  FASTDDS_EXPORTED_API virtual bool compute_key(
237  rtps::SerializedPayload_t& payload,
238  rtps::InstanceHandle_t& ihandle,
239  bool force_md5 = false) = 0;
240 
252  FASTDDS_EXPORTED_API virtual bool compute_key_ctx(
253  const std::shared_ptr<Context>& context,
254  rtps::SerializedPayload_t& payload,
255  rtps::InstanceHandle_t& ihandle,
256  bool force_md5 = false)
257  {
258  static_cast<void>(context);
259  return compute_key(payload, ihandle, force_md5);
260  }
261 
270  FASTDDS_EXPORTED_API virtual bool compute_key(
271  const void* const data,
272  rtps::InstanceHandle_t& ihandle,
273  bool force_md5 = false) = 0;
274 
286  FASTDDS_EXPORTED_API virtual bool compute_key_ctx(
287  const std::shared_ptr<Context>& context,
288  const void* const data,
289  rtps::InstanceHandle_t& ihandle,
290  bool force_md5 = false)
291  {
292  static_cast<void>(context);
293  return compute_key(data, ihandle, force_md5);
294  }
295 
301  FASTDDS_EXPORTED_API inline void set_name(
302  const std::string& nam)
303  {
304  topic_data_typename_ = nam;
305  }
306 
312  FASTDDS_EXPORTED_API inline void set_name(
313  std::string&& nam)
314  {
315  topic_data_typename_ = std::move(nam);
316  }
317 
323  FASTDDS_EXPORTED_API inline const std::string& get_name() const
324  {
325  return topic_data_typename_;
326  }
327 
333  FASTDDS_EXPORTED_API inline const xtypes::TypeIdentifierPair& type_identifiers() const
334  {
335  return type_identifiers_;
336  }
337 
341  FASTDDS_EXPORTED_API virtual inline bool is_bounded() const
342  {
343  return false;
344  }
345 
353  FASTDDS_EXPORTED_API virtual inline bool is_bounded_ctx(
354  const std::shared_ptr<Context>& context) const
355  {
356  static_cast<void>(context);
357  return is_bounded();
358  }
359 
363  FASTDDS_EXPORTED_API virtual inline bool is_plain(
365  {
366  return false;
367  }
368 
377  FASTDDS_EXPORTED_API virtual inline bool is_plain_ctx(
378  const std::shared_ptr<Context>& context,
379  DataRepresentationId_t representation) const
380  {
381  static_cast<void>(context);
382  return is_plain(representation);
383  }
384 
392  FASTDDS_EXPORTED_API virtual inline bool construct_sample(
393  void* memory) const
394  {
395  static_cast<void>(memory);
396  return false;
397  }
398 
409  FASTDDS_EXPORTED_API virtual inline bool construct_sample_ctx(
410  const std::shared_ptr<Context>& context,
411  void* memory) const
412  {
413  static_cast<void>(context);
414  return construct_sample(memory);
415  }
416 
420  FASTDDS_EXPORTED_API virtual inline void register_type_object_representation()
421  {
422  }
423 
431  FASTDDS_EXPORTED_API virtual inline void register_type_object_representation_ctx(
432  const std::shared_ptr<Context>& context)
433  {
434  static_cast<void>(context);
436  }
437 
446  FASTDDS_EXPORTED_API virtual inline uint32_t get_max_serialized_size_ctx(
447  const std::shared_ptr<Context>& context)
448  {
449  static_cast<void>(context);
451  }
452 
456 
459 
460 protected:
461 
463 
464 private:
465 
467  std::string topic_data_typename_;
468 
469 };
470 
471 } // namespace dds
472 } // namespace fastdds
473 } // namespace eprosima
474 
475 #endif // FASTDDS_DDS_TOPIC__TOPICDATATYPE_HPP
Class TopicDataType used to provide the DomainRTPSParticipant with the methods to serialize,...
Definition: TopicDataType.hpp:61
virtual FASTDDS_EXPORTED_API bool serialize(const void *const data, rtps::SerializedPayload_t &payload, eprosima::fastdds::dds::DataRepresentationId_t data_representation)=0
Serialize method, it should be implemented by the user, since it is abstract.
virtual FASTDDS_EXPORTED_API bool is_plain_ctx(const std::shared_ptr< Context > &context, DataRepresentationId_t representation) const
Checks if the type is plain when using a specific encoding and a context It can be reimplemented by t...
Definition: TopicDataType.hpp:377
virtual FASTDDS_EXPORTED_API void * create_data()=0
Create a Data Type.
virtual FASTDDS_EXPORTED_API bool construct_sample(void *memory) const
Construct a sample on a memory location.
Definition: TopicDataType.hpp:392
FASTDDS_EXPORTED_API const xtypes::TypeIdentifierPair & type_identifiers() const
Get the type identifiers.
Definition: TopicDataType.hpp:333
FASTDDS_EXPORTED_API const std::string & get_name() const
Get topic data type name.
Definition: TopicDataType.hpp:323
FASTDDS_EXPORTED_API void set_name(std::string &&nam)
Set topic data type name.
Definition: TopicDataType.hpp:312
virtual FASTDDS_EXPORTED_API bool deserialize_ctx(const std::shared_ptr< Context > &context, rtps::SerializedPayload_t &payload, void *data)
Deserialize method with context, it can be reimplemented by the user to perform custom deserializatio...
Definition: TopicDataType.hpp:143
virtual FASTDDS_EXPORTED_API void register_type_object_representation()
Register TypeObject type representation.
Definition: TopicDataType.hpp:420
virtual FASTDDS_EXPORTED_API bool compute_key_ctx(const std::shared_ptr< Context > &context, rtps::SerializedPayload_t &payload, rtps::InstanceHandle_t &ihandle, bool force_md5=false)
Get the key associated with the data with context.
Definition: TopicDataType.hpp:252
virtual FASTDDS_EXPORTED_API bool is_bounded_ctx(const std::shared_ptr< Context > &context) const
Checks if the type is bounded with context.
Definition: TopicDataType.hpp:353
virtual FASTDDS_EXPORTED_API void delete_data(void *data)=0
Remove a previously created object.
virtual FASTDDS_EXPORTED_API ~TopicDataType()=default
Destructor.
virtual FASTDDS_EXPORTED_API bool construct_sample_ctx(const std::shared_ptr< Context > &context, void *memory) const
Construct a sample on a memory location using a context.
Definition: TopicDataType.hpp:409
virtual FASTDDS_EXPORTED_API void delete_data_ctx(const std::shared_ptr< Context > &context, void *data)
Remove a previously created object with context.
Definition: TopicDataType.hpp:220
virtual FASTDDS_EXPORTED_API void * create_data_ctx(const std::shared_ptr< Context > &context)
Create a Data Type with context.
Definition: TopicDataType.hpp:197
bool is_compute_key_provided
Indicates whether the method to obtain the key has been implemented.
Definition: TopicDataType.hpp:458
uint32_t max_serialized_type_size
Maximum serialized size of the type in bytes.
Definition: TopicDataType.hpp:455
FASTDDS_EXPORTED_API TopicDataType()=default
Constructor.
virtual FASTDDS_EXPORTED_API void register_type_object_representation_ctx(const std::shared_ptr< Context > &context)
Register TypeObject type representation It can be reimplemented by the user to perform custom registr...
Definition: TopicDataType.hpp:431
virtual FASTDDS_EXPORTED_API uint32_t calculate_serialized_size_ctx(const std::shared_ptr< Context > &context, const void *const data, eprosima::fastdds::dds::DataRepresentationId_t data_representation)
Calculates the serialized size of the provided data with context.
Definition: TopicDataType.hpp:173
virtual FASTDDS_EXPORTED_API uint32_t get_max_serialized_size_ctx(const std::shared_ptr< Context > &context)
Get the maximum serialized size of the type using a context It can be reimplemented by the user to pe...
Definition: TopicDataType.hpp:446
virtual FASTDDS_EXPORTED_API uint32_t calculate_serialized_size(const void *const data, eprosima::fastdds::dds::DataRepresentationId_t data_representation)=0
Calculates the serialized size of the provided data.
virtual FASTDDS_EXPORTED_API bool compute_key_ctx(const std::shared_ptr< Context > &context, const void *const data, rtps::InstanceHandle_t &ihandle, bool force_md5=false)
Get the key associated with the data with context.
Definition: TopicDataType.hpp:286
virtual FASTDDS_EXPORTED_API bool serialize_ctx(const std::shared_ptr< Context > &context, const void *const data, rtps::SerializedPayload_t &payload, eprosima::fastdds::dds::DataRepresentationId_t data_representation)
Serialize method with context, it can be reimplemented by the user to perform custom serialization de...
Definition: TopicDataType.hpp:112
xtypes::TypeIdentifierPair type_identifiers_
Definition: TopicDataType.hpp:462
virtual FASTDDS_EXPORTED_API bool is_plain(DataRepresentationId_t) const
Checks if the type is plain when using a specific encoding.
Definition: TopicDataType.hpp:363
virtual FASTDDS_EXPORTED_API bool compute_key(rtps::SerializedPayload_t &payload, rtps::InstanceHandle_t &ihandle, bool force_md5=false)=0
Get the key associated with the data.
virtual FASTDDS_EXPORTED_API bool is_bounded() const
Checks if the type is bounded.
Definition: TopicDataType.hpp:341
virtual FASTDDS_EXPORTED_API bool deserialize(rtps::SerializedPayload_t &payload, void *data)=0
Deserialize method, it should be implemented by the user, since it is abstract.
virtual FASTDDS_EXPORTED_API bool compute_key(const void *const data, rtps::InstanceHandle_t &ihandle, bool force_md5=false)=0
Get the key associated with the data.
FASTDDS_EXPORTED_API void set_name(const std::string &nam)
Set topic data type name.
Definition: TopicDataType.hpp:301
This class represents the structure TypeIdentifierPair defined by the user in the IDL file.
Definition: dds_xtypes_typeobject.hpp:24033
Definition: DomainParticipant.hpp:46
eprosima::fastdds::rtps::InstanceHandle_t InstanceHandle_t
Definition: InstanceHandle.hpp:31
enum eprosima::fastdds::dds::DataRepresentationId DataRepresentationId_t
Enum DataRepresentationId, different kinds of topic data representation.
An interface to provide the user with a context when serializing and deserializing data.
Definition: TopicDataType.hpp:71
Struct InstanceHandle_t, used to contain the key for WITH_KEY topics.
Definition: InstanceHandle.hpp:154
Structure SerializedPayload_t.
Definition: SerializedPayload.hpp:59