CmManagedDataBlock.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include "CmPrerequisitesUtil.h"
  3. namespace CamelotEngine
  4. {
  5. /**
  6. * @brief Data block holding an array of bytes, usually used in serialization.
  7. */
  8. class ManagedDataBlock
  9. {
  10. public:
  11. /**
  12. * @brief Constructor
  13. *
  14. * @param [in] data Array of bytes to store. Direct pointer to the provided
  15. * array will be stored, no copying will be done. Data won't be modified
  16. * unless "managed" is true.
  17. * @param size Size of the array, in bytes.
  18. * @param managed If true then the provided data array will be deleted once
  19. * it is no longer being used. When serializing it is always faster to provide direct
  20. * pointer to the data, but sometimes you need to provide a copy of the data
  21. * instead (e.g. maybe it's needed in a different format).
  22. * In that case set managed to true so it can be properly freed.
  23. */
  24. ManagedDataBlock(UINT8* data, UINT32 size, bool managed)
  25. :mData(data), mSize(size), mManaged(managed), mIsDataOwner(true)
  26. { }
  27. ManagedDataBlock(const ManagedDataBlock& source)
  28. {
  29. mData = source.mData;
  30. mSize = source.mSize;
  31. mManaged = source.mManaged;
  32. mIsDataOwner = true;
  33. source.mIsDataOwner = false;
  34. }
  35. ~ManagedDataBlock()
  36. {
  37. if(mManaged && mIsDataOwner)
  38. delete[] mData;
  39. }
  40. UINT8* getData() { return mData; }
  41. UINT32 getSize() { return mSize; }
  42. private:
  43. UINT8* mData;
  44. UINT32 mSize;
  45. bool mManaged;
  46. mutable bool mIsDataOwner;
  47. };
  48. }