BsManagedDataBlock.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #pragma once
  5. #include "BsPrerequisitesUtil.h"
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * @brief 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.
  12. * Data will be automatically freed once the last copy is destroyed.
  13. */
  14. class BS_UTILITY_EXPORT ManagedDataBlock
  15. {
  16. public:
  17. /**
  18. * @brief Constructor
  19. *
  20. * @param data Array of bytes to store. Direct pointer to the provided
  21. * array will be stored, no copying will be done.
  22. * @param size Size of the array, in bytes.
  23. * @param deallocator Deallocator that will be used for freeing the data. If null, the default
  24. * deallocator will be used.
  25. */
  26. ManagedDataBlock(UINT8* data, UINT32 size, std::function<void(UINT8*)> deallocator = nullptr);
  27. /**
  28. * @brief Constructor that will automatically allocate an internal buffer of the specified size.
  29. * Copying ManagedDataBlock transfers ownership of the buffer to the copy of the buffer.
  30. * Buffer is deleted when the latest copy is deleted.
  31. *
  32. * @param size The size of the data in bytes.
  33. */
  34. ManagedDataBlock(UINT32 size);
  35. ManagedDataBlock(const ManagedDataBlock& source);
  36. ~ManagedDataBlock();
  37. UINT8* getData() { return mData; }
  38. UINT32 getSize() { return mData ? mSize : 0; }
  39. private:
  40. UINT8* mData;
  41. UINT32 mSize;
  42. bool mManaged;
  43. std::function<void(UINT8*)> mDeallocator;
  44. mutable bool mIsDataOwner;
  45. };
  46. }