MemoryBuffer.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../IO/AbstractFile.h"
  5. namespace Urho3D
  6. {
  7. /// Memory area that can be read and written to as a stream.
  8. /// @nobind
  9. class URHO3D_API MemoryBuffer : public AbstractFile
  10. {
  11. public:
  12. /// Construct with a pointer and size.
  13. MemoryBuffer(void* data, i32 size);
  14. /// Construct as read-only with a pointer and size.
  15. MemoryBuffer(const void* data, i32 size);
  16. /// Construct from a vector, which must not go out of scope before MemoryBuffer.
  17. explicit MemoryBuffer(Vector<byte>& data);
  18. /// Construct from a read-only vector, which must not go out of scope before MemoryBuffer.
  19. explicit MemoryBuffer(const Vector<byte>& data);
  20. /// Read bytes from the memory area. Return number of bytes actually read.
  21. i32 Read(void* dest, i32 size) override;
  22. /// Set position from the beginning of the memory area. Return actual new position.
  23. i64 Seek(i64 position) override;
  24. /// Write bytes to the memory area.
  25. i32 Write(const void* data, i32 size) override;
  26. /// Return memory area.
  27. byte* GetData() { return buffer_; }
  28. /// Return whether buffer is read-only.
  29. bool IsReadOnly() { return readOnly_; }
  30. private:
  31. /// Pointer to the memory area.
  32. byte* buffer_;
  33. /// Read-only flag.
  34. bool readOnly_;
  35. };
  36. }