CryMemFile.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #ifndef CRYINCLUDE_EDITOR_UTIL_CRYMEMFILE_H
  9. #define CRYINCLUDE_EDITOR_UTIL_CRYMEMFILE_H
  10. #pragma once
  11. #include <QBuffer>
  12. // derived class to get correct memory allocation/deallocation with custom memory manager - and to avoid memory leaks from calling Detach()
  13. class CCryMemFile
  14. : public QBuffer
  15. {
  16. public: // ---------------------------------------------------------------
  17. CCryMemFile()
  18. : QBuffer(&m_lpBuffer)
  19. {
  20. open(QIODevice::WriteOnly);
  21. }
  22. CCryMemFile(char* lpBuffer, int nBufferSize)
  23. : QBuffer(&m_lpBuffer), m_lpBuffer(lpBuffer, nBufferSize)
  24. {
  25. open(QIODevice::WriteOnly);
  26. }
  27. virtual ~CCryMemFile()
  28. {
  29. close(); // call Close() to make sure the Free() is using my v-table
  30. }
  31. qulonglong GetPosition() const
  32. {
  33. return QBuffer::pos();
  34. }
  35. qulonglong GetLength() const
  36. {
  37. return QBuffer::size();
  38. }
  39. void Write(const void* lpBuf, unsigned int nCount)
  40. {
  41. QBuffer::write(reinterpret_cast<const char*>(lpBuf), nCount);
  42. }
  43. // only for temporary use
  44. void* GetMemPtr() const
  45. {
  46. return const_cast<char*>(m_lpBuffer.data());
  47. }
  48. void Close()
  49. {
  50. QBuffer::close();
  51. }
  52. char* Detach()
  53. {
  54. assert(0); // dangerous - most likely we cause memory leak - better use GetMemPtr
  55. return 0;
  56. }
  57. private:
  58. QByteArray m_lpBuffer;
  59. };
  60. #endif // CRYINCLUDE_EDITOR_UTIL_CRYMEMFILE_H