BsFileSerializer.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisitesUtil.h"
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Serialization
  8. * @{
  9. */
  10. // TODO - Low priority. Eventually I'll want to generalize BinarySerializer to Serializer class, then I can make this class accept
  11. // a generic Serializer interface so it may write both binary, plain-text or some other form of data.
  12. /** Encodes the provided object to the specified file using the RTTI system. */
  13. class BS_UTILITY_EXPORT FileEncoder
  14. {
  15. public:
  16. FileEncoder(const Path& fileLocation);
  17. ~FileEncoder();
  18. /**
  19. * Parses the provided object, serializes all of its data as specified by its RTTIType and saves the serialized
  20. * data to the provided file location.
  21. *
  22. * @param[in] params Optional parameters to be passed to the serialization callbacks on the objects being
  23. * serialized.
  24. */
  25. void encode(IReflectable* object, const UnorderedMap<String, UINT64>& params = UnorderedMap<String, UINT64>());
  26. private:
  27. /** Called by the binary serializer whenever the buffer gets full. */
  28. UINT8* flushBuffer(UINT8* bufferStart, UINT32 bytesWritten, UINT32& newBufferSize);
  29. std::ofstream mOutputStream;
  30. UINT8* mWriteBuffer;
  31. static const UINT32 WRITE_BUFFER_SIZE = 2048;
  32. };
  33. /** Decodes objects from the specified file using the RTTI system. */
  34. class BS_UTILITY_EXPORT FileDecoder
  35. {
  36. public:
  37. FileDecoder(const Path& fileLocation);
  38. /**
  39. * Deserializes an IReflectable object by reading the binary data at the provided file location.
  40. *
  41. * @param[in] params Optional parameters to be passed to the serialization callbacks on the objects being
  42. * serialized.
  43. */
  44. SPtr<IReflectable> decode(const UnorderedMap<String, UINT64>& params = UnorderedMap<String, UINT64>());
  45. /** Skips over than object in the file. Calling decode() will decode the next object. */
  46. void skip();
  47. private:
  48. SPtr<DataStream> mInputStream;
  49. };
  50. /** @} */
  51. }