Cbeam
Loading...
Searching...
No Matches
xpod.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
29#include <cbeam/error/runtime_error.hpp> // for cbeam::error:runtime_error
30#include <cbeam/memory/pointer.hpp> // for cbeam::memory::pointer
31#include <cbeam/serialization/traits.hpp> // for cbeam::serialization::serialized_object
32
33#include <cassert> // for assert
34#include <cstdint> // for uint8_t
35#include <cstring> // for memcpy, std::memcpy
36
37#include <string> // for std::string
38#include <variant> // for std::get
39
40namespace cbeam::container
41{
42 class buffer;
43}
44
46{
54 template <>
55 struct traits<container::xpod::type>
56 {
67 static inline void serialize(const container::xpod::type& val, container::buffer& stream)
68 {
69 assert(val.index() >= 0 && val.index() <= 0xff);
70
71 char valIndex = (char)val.index();
72
73 stream.append(&valIndex, 1);
74
75 switch (val.index())
76 {
78 {
79 const auto intValue = std::get<container::xpod::type_index::integer>(val);
80 stream.append(reinterpret_cast<const char*>(&intValue), sizeof(intValue));
81 break;
82 }
84 {
85 const auto doubleValue = std::get<container::xpod::type_index::number>(val);
86 stream.append(reinterpret_cast<const char*>(&doubleValue), sizeof(doubleValue));
87 break;
88 }
90 {
91 const auto boolValue = std::get<container::xpod::type_index::boolean>(val);
92 stream.append(reinterpret_cast<const char*>(&boolValue), sizeof(boolValue));
93 break;
94 }
96 {
97 // For pointers, we serialize them as a string in hex representation.
98 const auto strValue = (std::string)std::get<container::xpod::type_index::pointer>(val);
99 const auto strLen = strValue.size();
100 stream.append(reinterpret_cast<const char*>(&strLen), sizeof(strLen));
101 stream.append(strValue.data(), strLen);
102 break;
103 }
105 {
106 const auto strValue = std::get<container::xpod::type_index::string>(val);
107 const auto strLen = strValue.size();
108 stream.append(reinterpret_cast<const char*>(&strLen), sizeof(strLen));
109 stream.append(strValue.data(), strLen);
110 break;
111 }
112 default:
113 break;
114 }
115 }
116
128 {
129 uint8_t* localIt = (uint8_t*)it;
130
131 const unsigned long valIndex = *localIt++;
132 switch (valIndex)
133 {
135 {
136 long long intValue;
137 std::memcpy(&intValue, localIt, sizeof(intValue));
138 val = intValue;
139 localIt += sizeof(intValue);
140 break;
141 }
143 {
144 double doubleValue;
145 std::memcpy(&doubleValue, localIt, sizeof(doubleValue));
146 val = doubleValue;
147 localIt += sizeof(doubleValue);
148 break;
149 }
151 {
152 bool boolValue;
153 std::memcpy(&boolValue, localIt, sizeof(boolValue));
154 val = boolValue;
155 localIt += sizeof(boolValue);
156 break;
157 }
159 {
160 size_t strLen;
161 std::memcpy(&strLen, localIt, sizeof(strLen));
162 localIt += sizeof(strLen);
163 val = cbeam::memory::pointer(std::string(localIt, localIt + strLen));
164 localIt += strLen;
165 break;
166 }
168 {
169 size_t strLen;
170 std::memcpy(&strLen, localIt, sizeof(strLen));
171 localIt += sizeof(strLen);
172 val = std::string(localIt, localIt + strLen);
173 localIt += strLen;
174 break;
175 }
176 default:
177 throw cbeam::error::runtime_error("cbeam::serialization::deserialize: invalid ByteStream");
178 break;
179 }
180
181 it = localIt;
182 }
183 };
184}
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
A Cbeam-specific runtime error that also acts like std::runtime_error.
Definition runtime_error.hpp:46
The pointer class is one of the types supported by container::xpod::type.
Definition pointer.hpp:46
constexpr std::size_t number
Definition xpod.hpp:51
constexpr std::size_t boolean
Definition xpod.hpp:52
constexpr std::size_t pointer
Definition xpod.hpp:53
constexpr std::size_t string
Definition xpod.hpp:54
constexpr std::size_t integer
Definition xpod.hpp:50
std::variant< long long, double, bool, memory::pointer, std::string > type
A variant designed for basic data types. memory::pointer is used in place of void* to provide additio...
Definition xpod.hpp:46
Offers advanced container types with unique approaches to stability and interprocess sharing....
Definition buffer.hpp:44
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
static void deserialize(serialized_object &it, container::xpod::type &val)
Deserialize an xpod::type variant from the buffer pointed to by it.
Definition xpod.hpp:127
static void serialize(const container::xpod::type &val, container::buffer &stream)
Serialize the given xpod::type variant into the buffer stream.
Definition xpod.hpp:67
Defines the traits required for serializing and deserializing objects of type T.
Definition traits.hpp:52