1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
3 | #pragma once |
4 | |
5 | #include "Prerequisites/BsPrerequisitesUtil.h" |
6 | |
7 | namespace bs |
8 | { |
9 | struct SerializationContext; |
10 | |
11 | /** @addtogroup Serialization |
12 | * @{ |
13 | */ |
14 | |
15 | /** Encodes/decodes an IReflectable object from/to memory. */ |
16 | class BS_UTILITY_EXPORT MemorySerializer |
17 | { |
18 | struct BufferPiece |
19 | { |
20 | UINT8* buffer; |
21 | UINT32 size; |
22 | }; |
23 | |
24 | public: |
25 | MemorySerializer() = default; |
26 | ~MemorySerializer() = default; |
27 | |
28 | /** |
29 | * Parses the provided object, serializes all of its data as specified by its RTTIType and returns the data in the |
30 | * form of raw memory. |
31 | * |
32 | * @param[in] object Object to encode. |
33 | * @param[in] bytesWritten Output value containing the total number of bytes it took to encode the object. |
34 | * @param[in] allocator Determines how is memory allocated. If not specified the default allocator is used. |
35 | * @param[in] shallow Determines how to handle referenced objects. If true then references will not be |
36 | * encoded and will be set to null. If false then references will be encoded as well |
37 | * and restored upon decoding. |
38 | * @param[in] context Optional object that will be passed along to all serialized objects through |
39 | * their serialization callbacks. Can be used for controlling serialization, |
40 | * maintaining state or sharing information between objects during |
41 | * serialization. |
42 | * |
43 | * @return A buffer containing the encoded object. It is up to the user to release the buffer |
44 | * memory when no longer needed. |
45 | */ |
46 | UINT8* encode(IReflectable* object, UINT32& bytesWritten, std::function<void*(UINT32)> allocator = nullptr, |
47 | bool shallow = false, SerializationContext* context = nullptr); |
48 | |
49 | /** |
50 | * Deserializes an IReflectable object by reading the binary data from the provided memory location. |
51 | * |
52 | * @param[in] buffer Previously allocated buffer to store the data in. |
53 | * @param[in] bufferSize Size of the @p buffer in bytes. |
54 | * @param[in] context Optional object that will be passed along to all deserialized objects through |
55 | * their deserialization callbacks. Can be used for controlling deserialization, |
56 | * maintaining state or sharing information between objects during |
57 | * deserialization. |
58 | */ |
59 | SPtr<IReflectable> decode(UINT8* buffer, UINT32 bufferSize, SerializationContext* context = nullptr); |
60 | |
61 | private: |
62 | Vector<BufferPiece> mBufferPieces; |
63 | |
64 | /** Called by the binary serializer whenever the buffer gets full. */ |
65 | UINT8* flushBuffer(UINT8* bufferStart, UINT32 bytesWritten, UINT32& newBufferSize); |
66 | |
67 | /************************************************************************/ |
68 | /* CONSTANTS */ |
69 | /************************************************************************/ |
70 | private: |
71 | static constexpr const UINT32 WRITE_BUFFER_SIZE = 16384; |
72 | }; |
73 | |
74 | /** @} */ |
75 | } |
76 | |