VectorBuffer.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../IO/AbstractFile.h"
  5. namespace Urho3D
  6. {
  7. /// Dynamically sized buffer that can be read and written to as a stream.
  8. class URHO3D_API VectorBuffer : public AbstractFile
  9. {
  10. public:
  11. /// Construct an empty buffer.
  12. VectorBuffer();
  13. /// Construct from another buffer.
  14. explicit VectorBuffer(const Vector<byte>& data);
  15. /// Construct from a memory area.
  16. VectorBuffer(const void* data, i32 size);
  17. /// Construct from a stream.
  18. VectorBuffer(Deserializer& source, i32 size);
  19. /// Read bytes from the buffer. Return number of bytes actually read.
  20. i32 Read(void* dest, i32 size) override;
  21. /// Set position from the beginning of the buffer. Return actual new position.
  22. i64 Seek(i64 position) override;
  23. /// Write bytes to the buffer. Return number of bytes actually written.
  24. i32 Write(const void* data, i32 size) override;
  25. /// Set data from another buffer.
  26. void SetData(const Vector<byte>& data);
  27. /// Set data from a memory area.
  28. void SetData(const void* data, i32 size);
  29. /// Set data from a stream.
  30. void SetData(Deserializer& source, i32 size);
  31. /// Reset to zero size.
  32. void Clear();
  33. /// Set size.
  34. void Resize(i32 size);
  35. /// Return data.
  36. const byte* GetData() const { return size_ ? &buffer_[0] : nullptr; }
  37. /// Return non-const data.
  38. byte* GetModifiableData() { return size_ ? &buffer_[0] : nullptr; }
  39. /// Return the buffer.
  40. const Vector<byte>& GetBuffer() const { return buffer_; }
  41. private:
  42. /// Dynamic data buffer.
  43. Vector<byte> buffer_;
  44. };
  45. }