BsMemorySerializer.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. namespace BansheeEngine
  4. {
  5. /** @addtogroup Serialization
  6. * @{
  7. */
  8. /** Encodes/decodes an IReflectable object from/to memory. */
  9. class BS_UTILITY_EXPORT MemorySerializer
  10. {
  11. struct BufferPiece
  12. {
  13. UINT8* buffer;
  14. UINT32 size;
  15. };
  16. public:
  17. MemorySerializer();
  18. ~MemorySerializer();
  19. /**
  20. * Parses the provided object, serializes all of its data as specified by its RTTIType and returns the data in the
  21. * form of raw memory.
  22. *
  23. * @param[in] object Object to encode.
  24. * @param[in] bytesWritten Output value containing the total number of bytes it took to encode the object.
  25. * @param[in] allocator Determines how is memory allocated. If not specified the default allocator is used.
  26. * @param[in] shallow Determines how to handle referenced objects. If true then references will not be
  27. * encoded and will be set to null. If false then references will be encoded as well
  28. * and restored upon decoding.
  29. *
  30. * @return A buffer containing the encoded object. It is up to the user to release the buffer
  31. * memory when no longer needed.
  32. */
  33. UINT8* encode(IReflectable* object, UINT32& bytesWritten, std::function<void*(UINT32)> allocator = nullptr,
  34. bool shallow = false);
  35. /** Deserializes an IReflectable object by reading the binary data from the provided memory location. */
  36. std::shared_ptr<IReflectable> decode(UINT8* buffer, UINT32 bufferSize);
  37. private:
  38. Vector<BufferPiece> mBufferPieces;
  39. /** Called by the binary serializer whenever the buffer gets full. */
  40. UINT8* flushBuffer(UINT8* bufferStart, UINT32 bytesWritten, UINT32& newBufferSize);
  41. /************************************************************************/
  42. /* CONSTANTS */
  43. /************************************************************************/
  44. private:
  45. static const UINT32 WRITE_BUFFER_SIZE = 16384;
  46. };
  47. /** @} */
  48. }