BsMemorySerializer.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisitesUtil.h"
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Serialization
  8. * @{
  9. */
  10. /** Encodes/decodes an IReflectable object from/to memory. */
  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. * Parses the provided object, serializes all of its data as specified by its RTTIType and returns the data in the
  23. * form of raw memory.
  24. *
  25. * @param[in] object Object to encode.
  26. * @param[in] bytesWritten Output value containing the total number of bytes it took to encode the object.
  27. * @param[in] allocator Determines how is memory allocated. If not specified the default allocator is used.
  28. * @param[in] shallow Determines how to handle referenced objects. If true then references will not be
  29. * encoded and will be set to null. If false then references will be encoded as well
  30. * and restored upon decoding.
  31. *
  32. * @return A buffer containing the encoded object. It is up to the user to release the buffer
  33. * memory when no longer needed.
  34. */
  35. UINT8* encode(IReflectable* object, UINT32& bytesWritten, std::function<void*(UINT32)> allocator = nullptr,
  36. bool shallow = false);
  37. /** Deserializes an IReflectable object by reading the binary data from the provided memory location. */
  38. std::shared_ptr<IReflectable> decode(UINT8* buffer, UINT32 bufferSize);
  39. private:
  40. Vector<BufferPiece> mBufferPieces;
  41. /** Called by the binary serializer whenever the buffer gets full. */
  42. UINT8* flushBuffer(UINT8* bufferStart, UINT32 bytesWritten, UINT32& newBufferSize);
  43. /************************************************************************/
  44. /* CONSTANTS */
  45. /************************************************************************/
  46. private:
  47. static const UINT32 WRITE_BUFFER_SIZE = 16384;
  48. };
  49. /** @} */
  50. }