BsManagedDataBlock.h 1.5 KB

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