BsManagedDataBlock.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. namespace BansheeEngine
  4. {
  5. /** @addtogroup Serialization
  6. * @{
  7. */
  8. /**
  9. * Data block holding an array of bytes, usually used in serialization.
  10. *
  11. * Ownership of the data blocked is passed to the latest copy of the ManagedDataBlock. Data will be automatically
  12. * freed once the last copy is destroyed.
  13. */
  14. class BS_UTILITY_EXPORT ManagedDataBlock
  15. {
  16. public:
  17. /**
  18. * Constructor
  19. *
  20. * @param[in] data Array of bytes to store. Direct pointer to the provided array will be stored,
  21. * no copying will be done.
  22. * @param[in] size Size of the array, in bytes.
  23. * @param[in] deallocator Deallocator that will be used for freeing the data. If null, the default deallocator
  24. * will be used.
  25. */
  26. ManagedDataBlock(UINT8* data, UINT32 size, std::function<void(UINT8*)> deallocator = nullptr);
  27. /**
  28. * Constructor that will automatically allocate an internal buffer of the specified size. Copying ManagedDataBlock
  29. * transfers ownership of the buffer to the copy of the buffer. Buffer is deleted when the latest copy is deleted.
  30. *
  31. * @param[in] size The size of the data in bytes.
  32. */
  33. ManagedDataBlock(UINT32 size);
  34. ManagedDataBlock(const ManagedDataBlock& source);
  35. ~ManagedDataBlock();
  36. UINT8* getData() { return mData; }
  37. UINT32 getSize() { return mData ? mSize : 0; }
  38. private:
  39. UINT8* mData;
  40. UINT32 mSize;
  41. bool mManaged;
  42. std::function<void(UINT8*)> mDeallocator;
  43. mutable bool mIsDataOwner;
  44. };
  45. /** @} */
  46. }