Sane C++ Libraries
C++ Platform Abstraction Libraries
SerializationBinaryTypeErased.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "Internal/SerializationBinaryTypeErasedReadVersioned.h"
5#include "Internal/SerializationBinaryTypeErasedReadWriteExact.h"
6namespace SC
7{
8
12
15
18{
25 template <typename T>
26 [[nodiscard]] static bool write(const T& object, Vector<uint8_t>& buffer, size_t* numberOfWrites = nullptr)
27 {
28 SerializationBinaryBufferWriter binaryBuffer(buffer);
29 SerializationBinaryTypeErasedWriteExact writer;
30 if (not writer.write(object, binaryBuffer))
31 return false;
32 if (numberOfWrites)
33 *numberOfWrites = binaryBuffer.numberOfOperations;
34 return true;
35 }
36
43 template <typename T>
44 [[nodiscard]] static bool loadExact(T& object, Span<const uint8_t> buffer, size_t* numberOfReads = nullptr)
45 {
46 SerializationBinaryBufferReader bufferReader(buffer);
47 SerializationBinaryTypeErasedReadExact reader;
48 if (not reader.loadExact(object, bufferReader))
49 return false;
50 if (numberOfReads)
51 *numberOfReads = bufferReader.numberOfOperations;
52 return bufferReader.positionIsAtEnd();
53 }
62 template <typename T>
63 [[nodiscard]] static bool loadVersioned(T& object, Span<const uint8_t> buffer,
65 SerializationBinaryOptions options = {}, size_t* numberOfReads = nullptr)
66 {
67 SerializationBinaryTypeErasedReadVersioned loader;
68
69 SerializationSchema serializationSchema(schema);
70 serializationSchema.options = options;
71 SerializationBinaryBufferReader readerBuffer(buffer);
72 if (not loader.loadVersioned(object, readerBuffer, serializationSchema))
73 return false;
74 if (numberOfReads)
75 *numberOfReads = readerBuffer.numberOfOperations;
76 return readerBuffer.positionIsAtEnd();
77 }
78};
80} // namespace SC
Conversion options for the binary versioned deserializer.
Definition: SerializationBinaryOptions.h:12
Loads or writes binary data with its associated reflection schema from or into a C++ object.
Definition: SerializationBinaryTypeErased.h:18
static bool write(const T &object, Vector< uint8_t > &buffer, size_t *numberOfWrites=nullptr)
Writes object T to a buffer.
Definition: SerializationBinaryTypeErased.h:26
static bool loadVersioned(T &object, 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: SerializationBinaryTypeErased.h:63
static bool loadExact(T &object, Span< const uint8_t > buffer, size_t *numberOfReads=nullptr)
Reads object T from a buffer, assuming no versioning changes.
Definition: SerializationBinaryTypeErased.h:44
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:24
A contiguous sequence of heap allocated elements.
Definition: Vector.h:51