86 #include <string_view>
89 #include "../serializer.h"
146 template <u
int8_t SizeT,
typename T>
147 DynamicData& load(
const char (&type)[SizeT],
const T& t);
160 template <
typename T>
169 template <
typename T>
170 [[nodiscard]] T as()
const;
183 [[nodiscard]]
const std::string_view&
get_type()
const;
207 [[nodiscard]]
static constexpr
bool is_vlink_dynamic_data();
212 [[nodiscard]]
static constexpr uint8_t get_offset();
232 void refresh_type_view(
size_t type_size) noexcept;
234 static constexpr uint8_t kOffset{20};
236 std::string_view type_;
243 template <u
int8_t SizeT,
typename T>
245 static_assert(SizeT <
get_offset(),
"DynamicData name out of size.");
246 static_assert(!std::is_same_v<DynamicData, T>,
"DynamicData can not serialize self.");
247 static_assert(!Serializer::is_cdr_type<T>(),
"DynamicData can not serialize cdr data.");
254 type_ = std::string_view();
255 VLOG_F(
"DynamicData serialize failed.");
259 data_ = std::move(next_data);
261 if constexpr (SizeT > 0) {
262 std::memcpy(data_.
real_data(), type, SizeT);
264 if constexpr (SizeT < kOffset) {
265 std::memset(data_.
real_data() + SizeT, 0, kOffset - SizeT);
268 size_t name_len = SizeT;
270 if (name_len > 0 && type[SizeT - 1] ==
'\0') {
274 type_ = std::string_view(
reinterpret_cast<const char*
>(data_.
real_data()), name_len);
276 std::memset(data_.
real_data(), 0, kOffset);
277 type_ = std::string_view();
283 template <
typename T>
285 static_assert(!std::is_same_v<DynamicData, T>,
"DynamicData can not deserialize self.");
286 static_assert(!Serializer::is_cdr_type<T>(),
"DynamicData can not deserialize cdr data.");
295 template <
typename T>
300 t = std::make_shared<typename T::element_type>();
304 VLOG_F(
"DynamicData deserialize failed.");
Fixed-size 128-byte buffer holder with SBO, five ownership modes and integrated codecs.
Definition: bytes.h:120
bool empty() const noexcept
Reports whether the buffer is logically empty.
Definition: bytes.h:861
uint8_t * real_data() noexcept
Returns a mutable pointer to the very beginning of the backing buffer.
Definition: bytes.h:845
Type-erased wrapper that pairs an inline type name with a serialised payload.
Definition: dynamic_data.h:102
bool operator>>(Bytes &bytes) const noexcept
Emits the container as a wire-format Bytes blob.
bool operator==(const DynamicData &target) const
Byte-equality comparison.
static constexpr uint8_t get_offset()
Returns the byte offset at which the serialised payload begins (= kOffset).
Definition: dynamic_data.h:313
bool operator!=(const DynamicData &target) const
Byte-inequality comparison.
T as() const
Default-constructs a T (or shared_ptr<T>) and decodes the payload into it.
Definition: dynamic_data.h:296
DynamicData & operator=(const DynamicData &target)
Copy-assignment that rebinds the local type view to point into the new buffer.
DynamicData(DynamicData &&target) noexcept
Adopts the source buffer and rebinds the type view to point at this object's storage.
DynamicData & operator=(DynamicData &&target) noexcept
Move-assignment that rebinds the local type view and empties the source.
bool is_empty() const
Returns whether the internal buffer is empty.
DynamicData(const DynamicData &target)
Copies the buffer and rebinds the local type view to point into the new buffer.
DynamicData & load(const char(&type)[SizeT], const T &t)
Serialises t under the type tag type into the internal buffer.
Definition: dynamic_data.h:244
bool convert(T &t) const
Deserialises the stored payload into t.
Definition: dynamic_data.h:284
DynamicData()
Builds an empty container with no buffer and no type tag.
bool operator<<(const Bytes &bytes) noexcept
Reconstructs the container from a wire-format Bytes blob.
static constexpr bool is_vlink_dynamic_data()
Compile-time trait used by Serializer to detect DynamicData specialisations.
Definition: dynamic_data.h:311
const Bytes & get_data() const
Returns the underlying Bytes buffer, including the embedded type name.
const std::string_view & get_type() const
Returns the embedded type name as a view into the internal buffer.
#define VLOG_F(...)
Definition: logger.h:791
#define VUNLIKELY(...)
Short alias for VLINK_UNLIKELY.
Definition: macros.h:289
#define VLINK_EXPORT
Definition: macros.h:81
bool convert(const SrcT &src, DesT &des)
Converts between two types where at least one side is Bytes.
Definition: serializer-inl.h:681
@ kUnknown
Unknown or unsupported transport.
Detects whether T is (or derives from) a std::shared_ptr specialisation.
Definition: traits.h:193