2
0

BsMemorySerializer.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. * @param[in] params Optional parameters to be passed to the serialization callbacks on the objects being
  32. * serialized.
  33. *
  34. * @return A buffer containing the encoded object. It is up to the user to release the buffer
  35. * memory when no longer needed.
  36. */
  37. UINT8* encode(IReflectable* object, UINT32& bytesWritten, std::function<void*(UINT32)> allocator = nullptr,
  38. bool shallow = false, const UnorderedMap<String, UINT64>& params = UnorderedMap<String, UINT64>());
  39. /**
  40. * Deserializes an IReflectable object by reading the binary data from the provided memory location.
  41. *
  42. * @param[in] params Optional parameters to be passed to the serialization callbacks on the objects being
  43. * serialized.
  44. */
  45. SPtr<IReflectable> decode(UINT8* buffer, UINT32 bufferSize,
  46. const UnorderedMap<String, UINT64>& params = UnorderedMap<String, UINT64>());
  47. private:
  48. Vector<BufferPiece> mBufferPieces;
  49. /** Called by the binary serializer whenever the buffer gets full. */
  50. UINT8* flushBuffer(UINT8* bufferStart, UINT32 bytesWritten, UINT32& newBufferSize);
  51. /************************************************************************/
  52. /* CONSTANTS */
  53. /************************************************************************/
  54. private:
  55. static const UINT32 WRITE_BUFFER_SIZE = 16384;
  56. };
  57. /** @} */
  58. }