CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Deserializer.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Forward.hpp"
11
12#include "Serialization.hpp"
13
16
21
24
25namespace CeresEngine {
26
27#define CE_DESERIALIZER_PRIMITIVE_TYPES(F) \
28 F(float, Float) \
29 F(double, Double) \
30 F(UInt8, UInt8) \
31 F(UInt16, UInt16) \
32 F(UInt32, UInt32) \
33 F(UInt64, UInt64) \
34 F(Int8, Int8) \
35 F(Int16, Int16) \
36 F(Int32, Int32) \
37 F(Int64, Int64) \
38 F(bool, Boolean)
39
46 public:
47 virtual ~IDeserializer() = default;
48
49 public:
58 };
59
67
73 class Deserializer : public IDeserializer {
74 public:
77
80
81 public:
84
87
88 public: // Deserializer interface
91
94
115
121 friend class Deserializer;
122
125
128
135
137 const bool isInplaceSupported = false;
138
142 const void* const existingValueRaw = nullptr;
143
146 void cancelInplace() { const_cast<const void*&>(existingValueRaw) = nullptr; }
147 };
148
153 struct Array final {
154 friend class Deserializer;
155
158
161 };
162
165 struct Map final {
166 friend class Deserializer;
167
170
173 };
174
175 public:
183 [[nodiscard]] virtual Box decode(Box&& existingObject = {}) = 0;
184
185 protected: // Decoding callbacks
197
202
211
219 void endObjectProperty(Object& object, const Property& property, Box&& value);
220
226
238
245
249 void arrayElement(const Array& array, Box&& value);
250
255
262
267 void mapElement(const Map& map, Box&& key, Box&& value);
268
273 };
274
275} // namespace CeresEngine
A value type that can hold any alongside it's type information.
Definition Box.hpp:40
Represents a reflected property from metadata defined by the class.
Definition Class.hpp:176
The deserializer class has basic support for reflection-based deserializers.
Definition Deserializer.hpp:73
Box binary(InputStream &stream, Box &&existingObject)
Decodes a binary input stream into a MetaValue object.
void arrayElement(const Array &array, Box &&value)
Notifies the deserializer that an array element was deserialized.
Optional< Box > deserialize() final
Deserializes a value from a stream of bytes read from dataStream.
Box endArray(Array &array)
Notifies the deserializer that an array has ended deserialization.
~Deserializer() noexcept override
Destroys the deserializer.
DeserializerState state
An object that stores context for the deserializer.
Definition Deserializer.hpp:79
void endObjectProperty(Object &object, const Property &property, Box &&value)
Notifies the deserializer that a property has ended deserialization.
Box endObject(Object &object)
Notifies the deserializer that an object has ended deserialization.
Box endMap(Map &map)
Notifies the deserializer that a map has ended deserialization.
Property beginObjectProperty(const Object &object, const SerializedPropertyMetadata &metadata)
Notifies the deserializer that a new property is starting deserialization.
const SerializationContext & context
A shared context that contains a context for the deserializer.
Definition Deserializer.hpp:76
Deserializer(const SerializationContext &context=SerializationContext::getDefault())
Creates a new deserializer with a custom context.
Object beginObject(const SerializedObjectMetadata &metadata, Box &&existingObject={})
Notifies the deserializer that a new object is starting deserialization.
Box objectReference(SerializedObjectID referenceID)
Notifies the deserializer that an object reference was deserialized.
Array beginArray(const SerializedArrayMetadata &metadata, Box &&existingObject={})
Notifies the deserializer that an array is starting deserialization.
virtual Box decode(Box &&existingObject={})=0
Starts decoding from the deserializer implementation.
Map beginMap(const SerializedMapMetadata &metadata, Box &&existingObject={})
Notifies the deserializer that a map is starting deserialization.
virtual void resetState()
Resets the deserializer state.
void mapElement(const Map &map, Box &&key, Box &&value)
Notifies the deserializer that a map element was deserialized.
A class that represents state for a deserializer.
Definition Deserializer.hpp:61
HashMap< SerializedObjectID, Box > deserializedObjects
A map of deserialized objects.
Definition Deserializer.hpp:65
The deserializer is responsible from taking a stream of bytes and make a valid C++ object from it.
Definition Deserializer.hpp:45
virtual ~IDeserializer()=default
virtual Optional< Box > deserialize()=0
Deserializes a value from a stream of bytes read from dataStream.
A stream that provides read-only stream functionality.
Definition Stream.hpp:210
Definition Optional.hpp:17
A context that is shared between multiple serializer and deserializer instances.
Definition Serialization.hpp:196
static const SerializationContext & getDefault()
Gets the default serialization context to be used in case the user doesn't provide one himself.
A base class for serializer and deserializer states.
Definition Serialization.hpp:402
Definition Application.hpp:19
std::unordered_map< Key, T, Hash, KeyEqual, ScopedAllocatorAdaptor< StdAllocator< Pair< const Key, T >, RawAllocator > > > HashMap
HashMap is an associative container that contains key-value pairs with unique keys.
Definition Map.hpp:33
UInt32 SerializedObjectID
Definition Forward.hpp:24
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
A context struct that contains information for the decoding of an array.
Definition Deserializer.hpp:153
const SerializedArrayMetadata metadata
The serialized array metadata.
Definition Deserializer.hpp:157
Box value
The object value.
Definition Deserializer.hpp:160
A context struct that contains information for the decoding of a map.
Definition Deserializer.hpp:165
Box value
The object value.
Definition Deserializer.hpp:172
const SerializedMapMetadata metadata
The serialized array metadata.
Definition Deserializer.hpp:169
A context struct that contains information for the decoding of an object.
Definition Deserializer.hpp:100
HashMap< String, Box > forgottenProperties
A map that contains properties that were serialized with the object but are no longer on the object.
Definition Deserializer.hpp:113
const SerializedObjectMetadata metadata
The serialized object metadata.
Definition Deserializer.hpp:104
Box value
The object value.
Definition Deserializer.hpp:107
A context struct that contains information for the decoding of a property.
Definition Deserializer.hpp:120
const void *const existingValueRaw
The raw value of the existing value.
Definition Deserializer.hpp:142
const bool isInplaceSupported
Determines if the deserialization was performed inplace.
Definition Deserializer.hpp:137
const ClassProperty *const property
The meta property being reflected.
Definition Deserializer.hpp:127
const SerializedPropertyMetadata metadata
The serialized property metadata.
Definition Deserializer.hpp:124
void cancelInplace()
Cancels the inplace construction in case the implementation cannot support it.
Definition Deserializer.hpp:146
Box existingValue
The value to perform in-place deserialization with.
Definition Deserializer.hpp:134
A structure that holds metadata for a serialized array.
Definition Serialization.hpp:123
A structure that holds metadata for a serialized map.
Definition Serialization.hpp:148
A struct that holds metadata for a serialized object.
Definition Serialization.hpp:56
A structure that holds metadata for a serialized property.
Definition Serialization.hpp:85