BsFileSerializer.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #pragma once
  5. #include "BsPrerequisitesUtil.h"
  6. namespace BansheeEngine
  7. {
  8. // TODO - Low priority. Eventually I'll want to generalize BinarySerializer to Serializer class, then I can make this class accept
  9. // a generic Serializer interface so it may write both binary, plain-text or some other form of data.
  10. /**
  11. * @brief Encodes/decodes an IReflectable object from/to a file.
  12. */
  13. class BS_UTILITY_EXPORT FileSerializer
  14. {
  15. public:
  16. FileSerializer();
  17. ~FileSerializer();
  18. /**
  19. * @brief Parses the provided object, serializes all of its data as specified by its
  20. * RTTIType and saves the serialized data to the provided file location.
  21. */
  22. void encode(IReflectable* object, const Path& fileLocation);
  23. /**
  24. * @brief Deserializes an IReflectable object by reading the binary data at
  25. * the provided file location.
  26. */
  27. std::shared_ptr<IReflectable> decode(const Path& fileLocation);
  28. private:
  29. std::ofstream mOutputStream;
  30. UINT8* mWriteBuffer;
  31. std::ifstream mInputStream;
  32. /**
  33. * @brief Called by the binary serializer whenever the buffer gets full.
  34. */
  35. UINT8* flushBuffer(UINT8* bufferStart, int bytesWritten, UINT32& newBufferSize);
  36. /************************************************************************/
  37. /* CONSTANTS */
  38. /************************************************************************/
  39. private:
  40. static const UINT32 WRITE_BUFFER_SIZE = 2048;
  41. };
  42. }