BsMemorySerializer.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "Prerequisites/BsPrerequisitesUtil.h"
  5. namespace bs
  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] buffer Previously allocated buffer to store the data in.
  43. * @param[in] bufferSize Size of the @p buffer in bytes.
  44. * @param[in] params Optional parameters to be passed to the serialization callbacks on the objects being
  45. * serialized.
  46. */
  47. SPtr<IReflectable> decode(UINT8* buffer, UINT32 bufferSize,
  48. const UnorderedMap<String, UINT64>& params = UnorderedMap<String, UINT64>());
  49. private:
  50. Vector<BufferPiece> mBufferPieces;
  51. /** Called by the binary serializer whenever the buffer gets full. */
  52. UINT8* flushBuffer(UINT8* bufferStart, UINT32 bytesWritten, UINT32& newBufferSize);
  53. /************************************************************************/
  54. /* CONSTANTS */
  55. /************************************************************************/
  56. private:
  57. static const UINT32 WRITE_BUFFER_SIZE = 16384;
  58. };
  59. /** @} */
  60. }