BsMemorySerializer.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. * @param object Object to encode.
  23. * @param bytesWritten Output value containing the total number of bytes it took to encode the object.
  24. * @param allocator Determines how is memory allocated. If not specified the default allocator is used.
  25. * @param shallow Determines how to handle referenced objects. If true then references will not be encoded
  26. * and will be set to null. If false then references will be encoded as well and restored
  27. * upon decoding.
  28. *
  29. * @return A buffer containing the encoded object. It is up to the user to release the buffer memory when
  30. * no longer needed.
  31. */
  32. UINT8* encode(IReflectable* object, UINT32& bytesWritten, std::function<void*(UINT32)> allocator = nullptr,
  33. bool shallow = false);
  34. /**
  35. * @brief Deserializes an IReflectable object by reading the binary data from the provided
  36. * memory location.
  37. */
  38. std::shared_ptr<IReflectable> decode(UINT8* buffer, UINT32 bufferSize);
  39. private:
  40. Vector<BufferPiece> mBufferPieces;
  41. /**
  42. * @brief Called by the binary serializer whenever the buffer gets full.
  43. */
  44. UINT8* flushBuffer(UINT8* bufferStart, UINT32 bytesWritten, UINT32& newBufferSize);
  45. /************************************************************************/
  46. /* CONSTANTS */
  47. /************************************************************************/
  48. private:
  49. static const UINT32 WRITE_BUFFER_SIZE = 16384;
  50. };
  51. }