BsFileSerializer.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 bs
  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] object Object to encode.
  23. * @param[in] params Optional parameters to be passed to the serialization callbacks on the objects being
  24. * serialized.
  25. */
  26. void encode(IReflectable* object, const UnorderedMap<String, UINT64>& params = UnorderedMap<String, UINT64>());
  27. private:
  28. /** Called by the binary serializer whenever the buffer gets full. */
  29. UINT8* flushBuffer(UINT8* bufferStart, UINT32 bytesWritten, UINT32& newBufferSize);
  30. std::ofstream mOutputStream;
  31. UINT8* mWriteBuffer;
  32. static const UINT32 WRITE_BUFFER_SIZE = 2048;
  33. };
  34. /** Decodes objects from the specified file using the RTTI system. */
  35. class BS_UTILITY_EXPORT FileDecoder
  36. {
  37. public:
  38. FileDecoder(const Path& fileLocation);
  39. /**
  40. * Deserializes an IReflectable object by reading the binary data at the provided file location.
  41. *
  42. * @param[in] params Optional parameters to be passed to the serialization callbacks on the objects being
  43. * serialized.
  44. */
  45. SPtr<IReflectable> decode(const UnorderedMap<String, UINT64>& params = UnorderedMap<String, UINT64>());
  46. /** Skips over than object in the file. Calling decode() will decode the next object. */
  47. void skip();
  48. private:
  49. SPtr<DataStream> mInputStream;
  50. };
  51. /** @} */
  52. }