Sane C++ Libraries
C++ Platform Abstraction Libraries
SerializationBinary.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "Internal/SerializationBinaryBuffer.h"
5#include "Internal/SerializationBinaryReadVersioned.h"
6#include "Internal/SerializationBinaryReadWriteExact.h"
7
8#include "SerializationBinaryOptions.h"
9
10namespace SC
11{
18
21
24{
41
49 template <typename T>
50 [[nodiscard]] static bool write(T& value, Vector<uint8_t>& buffer, size_t* numberOfWrites = nullptr)
51 {
52 SerializationBinaryBufferWriter writer(buffer);
53 using Writer = Serialization::SerializerBinaryReadWriteExact<SerializationBinaryBufferWriter, T>;
54 if (not Writer::serialize(value, writer))
55 return false;
56 if (numberOfWrites)
57 *numberOfWrites = writer.numberOfOperations;
58 return true;
59 }
60
76
95 template <typename T>
96 [[nodiscard]] static bool loadExact(T& value, Span<const uint8_t> buffer, size_t* numberOfReads = nullptr)
97 {
98 SerializationBinaryBufferReader bufferReader(buffer);
99 using Reader = Serialization::SerializerBinaryReadWriteExact<SerializationBinaryBufferReader, T>;
100 if (not Reader::serialize(value, bufferReader))
101 return false;
102 if (numberOfReads)
103 *numberOfReads = bufferReader.numberOfOperations;
104 return bufferReader.positionIsAtEnd();
105 }
106
108 //
127
142 template <typename T>
143 [[nodiscard]] static bool loadVersioned(T& value, Span<const uint8_t> buffer,
145 SerializationBinaryOptions options = {}, size_t* numberOfReads = nullptr)
146 {
147 SerializationBinaryBufferReader readerBuffer(buffer);
148 using Reader = Serialization::SerializerBinaryReadVersioned<SerializationBinaryBufferReader, T>;
149 SerializationSchema versionSchema(schema);
150 versionSchema.options = options;
151 if (not Reader::readVersioned(value, readerBuffer, versionSchema))
152 return false;
153 if (numberOfReads)
154 *numberOfReads = readerBuffer.numberOfOperations;
155 return readerBuffer.positionIsAtEnd();
156 }
157
163 template <typename T>
164 [[nodiscard]] static bool writeWithSchema(T& value, Vector<uint8_t>& buffer, size_t* numberOfWrites = nullptr)
165 {
166 constexpr auto typeInfos = Reflection::Schema::template compile<T>().typeInfos;
167 constexpr uint32_t numInfos = typeInfos.size;
168 static_assert(alignof(Reflection::TypeInfo) == sizeof(uint32_t), "Alignof TypeInfo");
169 // Implying same endianness when reading here
170 SC_TRY(buffer.append(Span<const uint8_t>::reinterpret_bytes(&numInfos, sizeof(numInfos))));
171 SC_TRY(buffer.append(
172 Span<const uint8_t>::reinterpret_bytes(typeInfos.values, typeInfos.size * sizeof(Reflection::TypeInfo))));
173 return write(value, buffer, numberOfWrites);
174 }
175
181 template <typename T>
182 [[nodiscard]] static bool loadVersionedWithSchema(T& value, Span<const uint8_t> buffer,
183 SerializationBinaryOptions options = {},
184 size_t* numberOfReads = nullptr)
185 {
186 uint32_t numInfos = 0;
187 // Read number of type info
188 Span<const uint8_t> numInfosSlice;
189 SC_TRY(buffer.sliceStartLength(0, sizeof(numInfos), numInfosSlice));
190 memcpy(&numInfos, numInfosSlice.data(), sizeof(numInfos));
191
192 // Cast first part of the buffer to a Span of TypeInfo.
193 // It's possible as we've been serializing it with correct alignment (currently 32 bits).
194 static_assert(alignof(Reflection::TypeInfo) == sizeof(uint32_t), "Alignof TypeInfo");
195 Span<const uint8_t> typeInfos;
196 SC_TRY(buffer.sliceStartLength(sizeof(numInfos), numInfos * sizeof(Reflection::TypeInfo), typeInfos));
198
199 constexpr auto sourceSchema = Reflection::Schema::template compile<T>().typeInfos;
200
201 // Get the slice of bytes where actual serialization data has been written by writeWithSchema
202 Span<const uint8_t> serializedDataSlice;
203 SC_TRY(buffer.sliceStart(sizeof(numInfos) + numInfos * sizeof(Reflection::TypeInfo), serializedDataSlice));
204 if (sourceSchema.equals(serializedSchema))
205 {
206 // If the serialized schema matches current object schema, we can run the fast "loadExact" path
207 return loadExact(value, serializedDataSlice);
208 }
209 else
210 {
211 // The two schemas differs, so resort to the "slower" versioned loader that tries to match field order
212 return loadVersioned(value, serializedDataSlice, serializedSchema, options, numberOfReads);
213 }
214 }
215};
216
218
219} // namespace SC
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition: PrimitiveTypes.h:38
#define SC_TRY(expression)
Checks the value of the given expression and if failed, returns this value to caller.
Definition: Result.h:48
[reflectionSnippet3]
Definition: Reflection.h:64
Loads or writes binary data with its associated reflection schema from or into a C++ object.
Definition: SerializationBinary.h:24
static bool loadVersioned(T &value, Span< const uint8_t > buffer, Span< const Reflection::TypeInfo > schema, SerializationBinaryOptions options={}, size_t *numberOfReads=nullptr)
Deserialize object T from a Binary buffer with a reflection schema not matching T schema.
Definition: SerializationBinary.h:143
static bool writeWithSchema(T &value, Vector< uint8_t > &buffer, size_t *numberOfWrites=nullptr)
Writes the reflection schema of object T followed by contents of object T to a binary buffer.
Definition: SerializationBinary.h:164
static bool write(T &value, Vector< uint8_t > &buffer, size_t *numberOfWrites=nullptr)
Writes object T to a binary buffer.
Definition: SerializationBinary.h:50
static bool loadExact(T &value, Span< const uint8_t > buffer, size_t *numberOfReads=nullptr)
Loads object T from binary buffer as written by SerializationBinary::write.
Definition: SerializationBinary.h:96
static bool loadVersionedWithSchema(T &value, Span< const uint8_t > buffer, SerializationBinaryOptions options={}, size_t *numberOfReads=nullptr)
Loads object T using the schema information that has been prepended by SerializationBinary::writeWith...
Definition: SerializationBinary.h:182
Conversion options for the binary versioned deserializer.
Definition: SerializationBinaryOptions.h:12
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:21
constexpr const Type * data() const
Returns pointer to first element of the span.
Definition: Span.h:89
constexpr bool sliceStart(SizeType offsetInElements, Span &destination) const
Creates another Span, starting at an offset in elements from current Span, until end.
Definition: Span.h:117
Span< T > reinterpret_as_array_of() const
Reinterprets the current span as an array of the specified type.
Definition: Span.h:74
constexpr bool sliceStartLength(SizeType offsetInElements, SizeType lengthInElements, Span &destination) const
Creates another Span, starting at an offset in elements from current Span of specified length.
Definition: Span.h:134
A contiguous sequence of heap allocated elements.
Definition: Vector.h:51
bool append(Span< const T > data)
Appends a range of items copying them at the end of vector.
Definition: Vector.h:520