37#ifndef VLINK_FASTDDS_IDL_PREFIX
38#define VLINK_FASTDDS_IDL_PREFIX "dds::"
44#if __has_include(<fastcdr/Cdr.h>)
45[[maybe_unused]]
static constexpr bool kRtHasFastcdr =
true;
47#include <fastcdr/Cdr.h>
49#define FASTCDR_VERSION_MAJOR 1
50[[maybe_unused]]
static constexpr bool kRtHasFastcdr =
false;
79#if __has_include(<google/protobuf/message_lite.h>)
80[[maybe_unused]]
static constexpr bool kRtHasProtobuf =
true;
82#if __has_include(<google/protobuf/stubs/common.h>)
83#include <google/protobuf/stubs/common.h>
86#include <google/protobuf/message_lite.h>
89#ifndef GOOGLE_PROTOBUF_VERSION
90#define GOOGLE_PROTOBUF_VERSION 0
93[[maybe_unused]]
static constexpr bool kRtHasProtobuf =
false;
109#if __has_include(<flatbuffers/flatbuffers.h>)
110[[maybe_unused]]
static constexpr bool kRtHasFlatbuffers =
true;
112#include <flatbuffers/flatbuffers.h>
114[[maybe_unused]]
static constexpr bool kRtHasFlatbuffers =
false;
118template <
typename ReturnT,
typename T>
119[[maybe_unused]]
static const ReturnT* GetRoot(
const T&) {
142 template <
typename T>
194template <Type TypeT,
typename T>
195inline static constexpr SchemaType get_schema_type() noexcept {
202 if constexpr (RealType::kZerocopyTypes) {
208 return RealType::get_schema_type();
222inline static constexpr SchemaType get_schema_type() noexcept {
225 return get_schema_type<kType, T>();
228template <Type TypeT,
typename T>
231 using NamedType = std::remove_pointer_t<RealType>;
236 return "vlink::DynamicData";
238 return RealType::get_serialized_type();
240#if GOOGLE_PROTOBUF_VERSION >= 6030000
241 return std::string(RealType{}.GetTypeName());
243 return RealType{}.GetTypeName();
246#if GOOGLE_PROTOBUF_VERSION >= 6030000
247 return std::string(std::remove_pointer_t<T>().GetTypeName());
249 return std::remove_pointer_t<T>().GetTypeName();
252 using TableType =
typename RealType::TableType;
254 return TableType::GetFullyQualifiedName();
261 using TableType =
typename RealType::Table;
263 return TableType::GetFullyQualifiedName();
270 using TableType = std::remove_pointer_t<T>;
272 return TableType::GetFullyQualifiedName();
294template <Type TypeT,
typename T>
302 }
else if constexpr (TypeT ==
kCdrType) {
304 return RealType::getCdrSerializedSize(
deref(src));
310 return deref(src).ByteSizeLong();
312 return deref(src).ByteSize();
316 return src->ByteSizeLong();
318 return src->ByteSize();
323 return src.fbb_.GetSize();
328 return deref(src).get_serialized_size();
354template <Type TypeT,
typename T>
356 [[maybe_unused]] uint8_t offset) {
363 }
else if constexpr (TypeT ==
kCdrType) {
369 size_t target_size = RealType::getCdrSerializedSize(
deref(src));
371 if (des.
size() != target_size) {
375 VLOG_W(
"Serializer: FastBuffer serialize is not supported without dds(v3).");
381#if FASTCDR_VERSION_MAJOR >= 2
387 deref(src).serialize(cdr);
392 size_t target_size =
deref(src).ByteSizeLong();
394 if (des.
size() != target_size) {
398 size_t target_size =
deref(src).ByteSize();
400 if (des.
size() != target_size) {
407 VLOG_T(
"Serializer: Protobuf serialize failed.");
413 size_t target_size = src->ByteSizeLong();
415 if (des.
size() != target_size) {
419 size_t target_size = src->ByteSize();
421 if (des.
size() != target_size) {
428 VLOG_T(
"Serializer: Protobuf ptr serialize failed.");
436 src.fbb_.Finish(
const_cast<T&
>(src).Finish());
441 des =
Bytes::deep_copy(src.fbb_.GetBufferPointer(), src.fbb_.GetSize(), offset);
446 const_cast<RealType&
>(
deref(src)) >> des;
453 des =
Bytes::deep_copy(
reinterpret_cast<const uint8_t*
>(src), std::strlen(src), offset);
455 thread_local std::stringstream ss;
460 const std::string& str = ss.str();
461 des =
Bytes::deep_copy(
reinterpret_cast<const uint8_t*
>(str.data()), str.size(), offset);
463 const auto* src_ptr =
reinterpret_cast<const uint8_t*
>(&
deref(src));
467 VLOG_T(
"Serializer: Standard ptr does not support offset.");
470 const auto* src_ptr =
reinterpret_cast<const uint8_t*
>(src);
487template <Type TypeT,
typename T>
496 }
else if constexpr (TypeT ==
kCdrType) {
499 VLOG_T(
"Serializer: Fastcdr src is not ptr.");
507#if FASTCDR_VERSION_MAJOR >= 2
513 deref(des).deserialize(cdr);
515 VLOG_W(
"Serializer: FastBuffer deserialize is not supported without dds(v3).");
522 VLOG_T(
"Serializer: Protobuf deserialize failed.");
531 VLOG_T(
"Serializer: Protobuf ptr deserialize failed.");
538 deref(des) = RealType{};
544 VLOG_T(
"Serializer: Flatbuffers table deserialize failed.");
548 auto target = flatbuffers::GetRoot<typename RealType::TableType>(src.
data());
549 target->UnPackTo(&
deref(des));
555 VLOG_T(
"Serializer: Flatbuffers ptr verify failed.");
559 des =
const_cast<T
>(flatbuffers::GetRoot<std::remove_pointer_t<T>>(src.
data()));
562 VLOG_T(
"Serializer: Flatbuffers ptr deserialize failed.");
569 deref(des) = std::string(
reinterpret_cast<const char*
>(src.
data()), src.
size());
571 deref(des) = std::string();
575 des =
reinterpret_cast<const char*
>(src.
data());
580 thread_local std::stringstream ss;
582 ss.str(std::string(
reinterpret_cast<const char*
>(src.
data()), src.
size()));
589 VLOG_T(
"Serializer: Standard layout deserialize failed.");
593 if VLIKELY (!src.
empty() && src.
size() ==
sizeof(std::remove_pointer_t<T>)) {
594 des =
reinterpret_cast<T
>(
const_cast<uint8_t*
>(src.
data()));
596 VLOG_T(
"Serializer: Standard ptr layout deserialize failed.");
613template <
typename SrcT,
typename DesT>
614inline bool convert(
const SrcT& src, DesT& des) {
618 des.shallow_copy(src);
628inline constexpr auto&
deref(
const T& t)
noexcept {
630 return *
const_cast<T&
>(t).get();
632 return const_cast<T&
>(t);
638 return std::is_same_v<T, Bytes>;
659 return kRtHasProtobuf &&
VLINK_HAS_MEMBER(RealType, SerializeToArray(0, 0)) &&
665 return kRtHasProtobuf && std::is_pointer_v<T> &&
VLINK_HAS_MEMBER(std::remove_pointer_t<T>, SerializeToArray(0, 0)) &&
672 return kRtHasFlatbuffers && std::is_base_of_v<flatbuffers::NativeTable, RealType>;
682 return kRtHasFlatbuffers && std::is_pointer_v<T> && std::is_base_of_v<flatbuffers::Table, std::remove_pointer_t<T>>;
694 return std::is_same_v<RealType, std::string>;
699 return !std::is_same_v<T, std::string> && std::is_constructible_v<std::string, T>;
711 return !std::is_pointer_v<RealType> && std::is_trivial_v<RealType> && std::is_standard_layout_v<RealType>;
716 return std::is_pointer_v<T> && std::is_trivial_v<std::remove_pointer_t<T>> &&
717 std::is_standard_layout_v<std::remove_pointer_t<T>>;
Definition serializer-inl.h:59
Type
Definition serializer-inl.h:61
@ DDS_CDR
Definition serializer-inl.h:62
Definition serializer-inl.h:66
Type
Definition serializer-inl.h:68
@ DEFAULT_ENDIAN
Definition serializer-inl.h:70
@ DDS_CDR
Definition serializer-inl.h:69
Cdr(FastBuffer &, int, int)
Definition serializer-inl.h:73
Definition serializer-inl.h:54
FastBuffer(char *, size_t)
Definition serializer-inl.h:56
Versatile 128-byte byte buffer with SBO, five ownership modes and compression helpers.
Definition bytes.h:113
bool empty() const noexcept
Returns true if the buffer is empty (no data pointer and size == 0).
Definition bytes.h:880
bool is_loaned() const noexcept
Returns true if this object holds an iceoryx loaned chunk.
Definition bytes.h:878
static Bytes shallow_copy_ptr(void *data) noexcept
Wraps an opaque pointer without associating a byte size.
static Bytes shallow_copy(uint8_t *data, size_t size) noexcept
Creates a non-owning Bytes alias pointing to an external mutable buffer.
bool is_ptr() const noexcept
Returns true if the object holds only an opaque pointer (size == 0 and not owner).
Definition bytes.h:898
size_t size() const noexcept
Returns the number of usable bytes (excluding the prefix offset region).
Definition bytes.h:868
uint8_t * data() noexcept
Returns a pointer to the start of the user data region (after the prefix offset).
Definition bytes.h:860
static Bytes create(size_t size, uint8_t offset=0) noexcept
Creates an owned Bytes buffer of the given size.
static Bytes deep_copy(uint8_t *data, size_t size, uint8_t offset=0) noexcept
Creates an owned deep copy of an external mutable buffer.
T * to_ptr() const noexcept
Reinterprets the backing buffer as a pointer to T.
Definition bytes.h:901
String, number, hash and formatting utility functions.
Global singleton logger with three output styles and pluggable backends.
#define VLOG_W(...)
Definition logger.h:852
#define VLOG_T(...)
Definition logger.h:846
#define VUNLIKELY(...)
Shorthand alias for VLINK_UNLIKELY. Hints that the expression is unlikely true.
Definition macros.h:302
#define VLIKELY(...)
Shorthand alias for VLINK_LIKELY. Hints that the expression is likely true.
Definition macros.h:297
Compile-time type-name and enum-name detection utilities.
Compile-time type detection and codec dispatch for VLink messages.
Definition serializer-inl.h:52
Definition serializer-inl.h:116
Definition serializer-inl.h:95
constexpr bool contains_substring(std::string_view sv, std::string_view needle) noexcept
Checks whether a string_view contains a given substring.
Definition helpers.h:415
VLINK_EXPORT void replace_string(std::string &str, const std::string &from, const std::string &to) noexcept
Replaces all occurrences of from in str with to in-place.
constexpr bool is_support() noexcept
Checks at compile time whether the nameof library can produce a type name for T.
Definition name_detector.h:137
constexpr std::string_view get() noexcept
Returns the unqualified compile-time name of type T as a std::string_view.
Definition name_detector.h:146
constexpr bool is_proto_ptr_type() noexcept
Returns true if T is a raw pointer to a Protobuf message.
Definition serializer-inl.h:664
constexpr bool is_dynamic_type() noexcept
Returns true if T is a VLink dynamic data type.
Definition serializer-inl.h:642
constexpr bool is_chars_type() noexcept
Returns true if T is constructible from std::string (but is not std::string).
Definition serializer-inl.h:698
constexpr Type get_type_of() noexcept
Deduces the Type enumerator for T at compile time.
Definition serializer-inl.h:160
Type
Identifies the serialisation codec to use for a given C++ type.
Definition serializer.h:123
@ kStandardPtrType
Pointer to trivial standard-layout struct (POD pointer).
Definition serializer.h:138
@ kCustomType
User-defined via operator>>(Bytes&) / operator<<(const Bytes&).
Definition serializer.h:127
@ kDynamicType
Dynamic typed data via is_vlink_dynamic_data().
Definition serializer.h:126
@ kFlatTableType
FlatBuffers NativeTable (object API).
Definition serializer.h:131
@ kCharsType
char* / string literal.
Definition serializer.h:135
@ kUnknownType
Unsupported type – is_supported() returns false.
Definition serializer.h:124
@ kBytesType
Bytes – raw byte pass-through.
Definition serializer.h:125
@ kProtoPtrType
Protobuf raw pointer (Arena-managed).
Definition serializer.h:130
@ kStandardType
Trivial standard-layout struct (POD value).
Definition serializer.h:137
@ kStringType
std::string – UTF-8 text.
Definition serializer.h:134
@ kFlatBuilderType
FlatBuffers builder (fbb_ + Finish()).
Definition serializer.h:133
@ kStreamType
Stream-serialisable via operator<<(std::stringstream).
Definition serializer.h:136
@ kProtoType
Protobuf value (MessageLite derived).
Definition serializer.h:129
@ kCdrType
FastDDS CDR via serialize(Cdr&) / deserialize(Cdr&).
Definition serializer.h:128
@ kFlatPtrType
Pointer to flatbuffers::Table (zero-copy read).
Definition serializer.h:132
constexpr auto & deref(const T &t) noexcept
Dereferences a value, unwrapping shared_ptr if necessary.
Definition serializer-inl.h:628
constexpr bool is_supported(Type type) noexcept
Returns true when type identifies a supported serializer.
Definition serializer-inl.h:157
constexpr bool is_custom_type() noexcept
Returns true if T provides custom operator>> / operator<<.
Definition serializer-inl.h:686
bool deserialize(const Bytes &src, T &des, TransportType transport)
Deserializes src bytes into des with explicit type and transport hints.
Definition serializer-inl.h:488
size_t get_serialized_size(const T &src) noexcept
Returns the exact serialised byte size for a given src value.
Definition serializer-inl.h:295
bool convert(const SrcT &src, DesT &des)
Converts between two types where at least one is Bytes.
Definition serializer-inl.h:614
constexpr bool is_cdr_type() noexcept
Returns true if T is a FastDDS CDR-serialisable type.
Definition serializer-inl.h:648
constexpr bool is_proto_type() noexcept
Returns true if T is a Protobuf message value type.
Definition serializer-inl.h:657
std::string get_serialized_type() noexcept
Returns the serialisation type name string for T with explicit TypeT.
Definition serializer-inl.h:229
bool serialize(const T &src, Bytes &des, TransportType transport, uint8_t offset)
Serializes src into des with explicit type and transport hints.
Definition serializer-inl.h:355
constexpr bool is_flat_ptr_type() noexcept
Returns true if T is a raw pointer to a flatbuffers::Table.
Definition serializer-inl.h:681
constexpr bool is_flat_builder_type() noexcept
Returns true if T is a FlatBuffers builder type.
Definition serializer-inl.h:676
constexpr bool is_standard_ptr_type() noexcept
Returns true if T is a pointer to a trivial standard-layout type.
Definition serializer-inl.h:715
constexpr bool is_standard_type() noexcept
Returns true if T is a trivial standard-layout value type (POD).
Definition serializer-inl.h:709
constexpr bool is_flat_table_type() noexcept
Returns true if T is a FlatBuffers NativeTable type.
Definition serializer-inl.h:670
constexpr bool is_string_type() noexcept
Returns true if T is std::string.
Definition serializer-inl.h:692
constexpr bool is_bytes_type() noexcept
Returns true if T is exactly Bytes.
Definition serializer-inl.h:637
constexpr bool is_stream_type() noexcept
Returns true if T supports both operator<< and operator>> with std::stringstream.
Definition serializer-inl.h:703
SchemaType
Coarse runtime schema family used by discovery, bag metadata, and proxy routing.
Definition types.h:184
@ kZeroCopy
Decode using VLink zero-copy message structs.
Definition types.h:187
@ kProtobuf
Decode using the Protocol Buffers stack.
Definition types.h:188
@ kFlatbuffers
Decode using the FlatBuffers stack.
Definition types.h:189
@ kRaw
Treat the payload as opaque/raw bytes.
Definition types.h:186
TransportType
Enumeration of all supported transport backends.
Definition types.h:107
@ kUnknown
Unknown or unsupported transport.
Definition types.h:108
@ kDds
Fast-DDS RTPS (dds://).
Definition types.h:113
Compile-time type-detection and serialisation utilities for VLink messages.
Definition serializer-inl.h:123
void Finish() const
Definition serializer-inl.h:132
const uint8_t * GetCurrentBufferPointer() const
Definition serializer-inl.h:128
void PushBytes(const uint8_t *, size_t)
Definition serializer-inl.h:130
const uint8_t * GetBufferPointer() const
Definition serializer-inl.h:126
size_t GetSize() const
Definition serializer-inl.h:124
Definition serializer-inl.h:137
Definition serializer-inl.h:135
Definition serializer-inl.h:139
bool VerifyBuffer(void *)
Definition serializer-inl.h:143
Verifier(const uint8_t *, size_t)
Definition serializer-inl.h:140
Definition serializer-inl.h:97
static T * Create(Arena *)
Definition serializer-inl.h:99
A type trait that always evaluates to std::false_type.
Definition traits.h:109
Detects whether type T is a std::shared_ptr specialization.
Definition traits.h:224
Detects whether OT supports << and >> stream operators with T.
Definition traits.h:191
T Type
Definition traits.h:242
Compile-time type-trait utilities used internally by VLink.
#define VLINK_HAS_MEMBER(T, member)
Macro Definitions.
Definition traits.h:350