| 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 | /** @addtogroup Serialization |
| 10 | * @{ |
| 11 | */ |
| 12 | |
| 13 | struct SerializationContext; |
| 14 | |
| 15 | /** Encodes the provided object to the specified file using the RTTI system. */ |
| 16 | class BS_UTILITY_EXPORT FileEncoder |
| 17 | { |
| 18 | public: |
| 19 | FileEncoder(const Path& fileLocation); |
| 20 | ~FileEncoder(); |
| 21 | |
| 22 | /** |
| 23 | * Parses the provided object, serializes all of its data as specified by its RTTIType and saves the serialized |
| 24 | * data to the provided file location. |
| 25 | * |
| 26 | * @param[in] object Object to encode. |
| 27 | * @param[in] context Optional object that will be passed along to all serialized objects through |
| 28 | * their serialization callbacks. Can be used for controlling serialization, |
| 29 | * maintaining state or sharing information between objects during |
| 30 | * serialization. |
| 31 | */ |
| 32 | void encode(IReflectable* object, SerializationContext* context = nullptr); |
| 33 | |
| 34 | private: |
| 35 | /** Called by the binary serializer whenever the buffer gets full. */ |
| 36 | UINT8* flushBuffer(UINT8* bufferStart, UINT32 bytesWritten, UINT32& newBufferSize); |
| 37 | |
| 38 | std::ofstream mOutputStream; |
| 39 | UINT8* mWriteBuffer = nullptr; |
| 40 | |
| 41 | static const UINT32 WRITE_BUFFER_SIZE = 2048; |
| 42 | }; |
| 43 | |
| 44 | /** Decodes objects from the specified file using the RTTI system. */ |
| 45 | class BS_UTILITY_EXPORT FileDecoder |
| 46 | { |
| 47 | public: |
| 48 | FileDecoder(const Path& fileLocation); |
| 49 | |
| 50 | /** |
| 51 | * Deserializes an IReflectable object by reading the binary data at the provided file location. |
| 52 | * |
| 53 | * @param[in] context Optional object that will be passed along to all deserialized objects through |
| 54 | * their deserialization callbacks. Can be used for controlling deserialization, |
| 55 | * maintaining state or sharing information between objects during |
| 56 | * deserialization. |
| 57 | */ |
| 58 | SPtr<IReflectable> decode(SerializationContext* context = nullptr); |
| 59 | |
| 60 | /** Gets the size in bytes of the next object in the file. Returns 0 if no next object. */ |
| 61 | UINT32 getSize() const; |
| 62 | |
| 63 | /** Skips over than object in the file. Calling decode() will decode the next object. */ |
| 64 | void skip(); |
| 65 | |
| 66 | private: |
| 67 | SPtr<DataStream> mInputStream; |
| 68 | }; |
| 69 | |
| 70 | /** @} */ |
| 71 | } |
| 72 | |