BsMemorySerializer.h 1.4 KB

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