BsMemorySerializer.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. /**
  9. * @brief Encodes/decodes an IReflectable object from/to memory.
  10. */
  11. class BS_UTILITY_EXPORT MemorySerializer
  12. {
  13. struct BufferPiece
  14. {
  15. UINT8* buffer;
  16. UINT32 size;
  17. };
  18. public:
  19. MemorySerializer();
  20. ~MemorySerializer();
  21. /**
  22. * @brief Parses the provided object, serializes all of its data as specified by its
  23. * RTTIType and returns the data in the form of raw memory.
  24. *
  25. * All memory is allocated using the provided "allocator". If not specified the default
  26. * allocator is used.
  27. */
  28. UINT8* encode(IReflectable* object, UINT32& bytesWritten, std::function<void*(UINT32)> allocator = nullptr);
  29. /**
  30. * @brief Deserializes an IReflectable object by reading the binary data from the provided
  31. * memory location.
  32. */
  33. std::shared_ptr<IReflectable> decode(UINT8* buffer, UINT32 bufferSize);
  34. private:
  35. Vector<BufferPiece> mBufferPieces;
  36. /**
  37. * @brief Called by the binary serializer whenever the buffer gets full.
  38. */
  39. UINT8* flushBuffer(UINT8* bufferStart, int bytesWritten, UINT32& newBufferSize);
  40. /************************************************************************/
  41. /* CONSTANTS */
  42. /************************************************************************/
  43. private:
  44. static const UINT32 WRITE_BUFFER_SIZE = 2048;
  45. };
  46. }