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, Buffer& 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 char> 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 char> buffer, Span<const Reflection::TypeInfo> schema,
64 SerializationBinaryOptions options = {}, size_t* numberOfReads = nullptr)
65 {
66 SerializationBinaryTypeErasedReadVersioned loader;
67
68 SerializationSchema serializationSchema(schema);
69 serializationSchema.options = options;
70 SerializationBinaryBufferReader readerBuffer(buffer);
71 if (not loader.loadVersioned(object, readerBuffer, serializationSchema))
72 return false;
73 if (numberOfReads)
74 *numberOfReads = readerBuffer.numberOfOperations;
75 return readerBuffer.positionIsAtEnd();
76 }
77};
79} // namespace SC
An heap allocated byte buffer that can optionally use an inline buffer.
Definition: Buffer.h:28
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, Buffer &buffer, size_t *numberOfWrites=nullptr)
Writes object T to a buffer.
Definition: SerializationBinaryTypeErased.h:26
static bool loadExact(T &object, Span< const char > buffer, size_t *numberOfReads=nullptr)
Reads object T from a buffer, assuming no versioning changes.
Definition: SerializationBinaryTypeErased.h:44
static bool loadVersioned(T &object, Span< const char > 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
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:32