Cbeam
Loading...
Searching...
No Matches
traits.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2025 acrion innovations GmbH
3Authors: Stefan Zipproth, s.zipproth@acrion.ch
4
5This file is part of Cbeam, see https://github.com/acrion/cbeam and https://cbeam.org
6
7Cbeam is offered under a commercial and under the AGPL license.
8For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
9
10AGPL licensing:
11
12Cbeam is free software: you can redistribute it and/or modify
13it under the terms of the GNU Affero General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17Cbeam is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU Affero General Public License for more details.
21
22You should have received a copy of the GNU Affero General Public License
23along with Cbeam. If not, see <https://www.gnu.org/licenses/>.
24*/
25
26#pragma once
27
28#include <cbeam/container/buffer.hpp> // for cbeam::container::buffer
29
30#include <cstdint> // for uint8_t
31#include <cstring> // for memcpy, std::memcpy
32
33#include <type_traits> // for std::is_standard_layout, std::is_trivial
34
36{
42 using serialized_object = void*;
43
50 template <typename T>
51 struct traits
52 {
53 static_assert(std::is_standard_layout<T>::value && std::is_trivial<T>::value,
54 "A specialization of cbeam::serialization::traits must be implemented for types that are not both standard-layout and trivial.");
55
63 static inline void serialize(const T& val, container::buffer& stream)
64 {
65 // implementation for types that are both standard-layout and trivial
66 stream.append(reinterpret_cast<const char*>(&val), sizeof(val));
67 }
68
77 static inline void deserialize(serialized_object& it, T& val)
78 {
79 // implementation for types that are both standard-layout and trivial
80 uint8_t* local_it = reinterpret_cast<uint8_t*>(it);
81 std::memcpy(&val, local_it, sizeof(val));
82 local_it += sizeof(val);
83 it = local_it;
84 }
85 };
86}
Manages memory a byte buffer, offering dynamic appending. This class is designed for scenarios where ...
Definition buffer.hpp:50
virtual void append(const void *buffer_to_append, const std::size_t length_of_buffer)
append the given buffer to the end of the current buffer. If there is no current buffer yet,...
Definition buffer.hpp:96
Implements traits-based serialization for complex data types, including standard containers and custo...
Definition direct.hpp:38
void * serialized_object
Represents a serialized value in memory.
Definition traits.hpp:42
Defines the traits required for serializing and deserializing objects of type T.
Definition traits.hpp:52
static void serialize(const T &val, container::buffer &stream)
Required to serialize an object of type T into a shared_buffer stream.
Definition traits.hpp:63
static void deserialize(serialized_object &it, T &val)
Required to deserialize an object of type T from a serialized memory block, incrementing the iterator...
Definition traits.hpp:77