diff options
| author | Stefan Boberg <[email protected]> | 2021-05-24 13:19:21 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-05-24 13:19:21 +0200 |
| commit | 5cf5fd50b34dec7be0362b35046ffd44a3d58983 (patch) | |
| tree | b24065a931acfbb1158e30520c6fd267b3736289 /zencore/include | |
| parent | Added some functionality to support CompositeBuffer implementation (diff) | |
| download | zen-5cf5fd50b34dec7be0362b35046ffd44a3d58983.tar.xz zen-5cf5fd50b34dec7be0362b35046ffd44a3d58983.zip | |
Added functionality to SharedBuffer/UniqueBuffer to support CompositeBuffer implementation
Most importantly, UniqueBuffer may now be "null", whereas previously it would never be.
Diffstat (limited to 'zencore/include')
| -rw-r--r-- | zencore/include/zencore/sharedbuffer.h | 48 |
1 files changed, 33 insertions, 15 deletions
diff --git a/zencore/include/zencore/sharedbuffer.h b/zencore/include/zencore/sharedbuffer.h index fcf4eec83..5b950ee38 100644 --- a/zencore/include/zencore/sharedbuffer.h +++ b/zencore/include/zencore/sharedbuffer.h @@ -20,20 +20,30 @@ namespace zen { class UniqueBuffer { public: - UniqueBuffer(const UniqueBuffer&) = delete; + UniqueBuffer() = default; + UniqueBuffer(UniqueBuffer&&) = default; + UniqueBuffer& operator=(UniqueBuffer&&) = default; + UniqueBuffer(const UniqueBuffer&) = delete; UniqueBuffer& operator=(const UniqueBuffer&) = delete; - UniqueBuffer() = delete; ZENCORE_API explicit UniqueBuffer(IoBufferCore* Owner); - void* GetData() { return m_Buffer->MutableDataPointer(); } - const void* GetData() const { return m_Buffer->DataPointer(); } - size_t GetSize() const { return m_Buffer->DataBytes(); } - operator MutableMemoryView() { return GetMutableView(); } - operator MemoryView() const { return GetView(); } + void* GetData() { return m_Buffer ? m_Buffer->MutableDataPointer() : nullptr; } + const void* GetData() const { return m_Buffer ? m_Buffer->DataPointer() : nullptr; } + size_t GetSize() const { return m_Buffer ? m_Buffer->DataBytes() : 0; } - inline MutableMemoryView GetMutableView() { return MutableMemoryView(m_Buffer->MutableDataPointer(), m_Buffer->DataBytes()); } - inline MemoryView GetView() const { return MemoryView(m_Buffer->MutableDataPointer(), m_Buffer->DataBytes()); } + operator MutableMemoryView() { return GetMutableView(); } + operator MemoryView() const { return GetView(); } + + /** + * Returns true if this does not point to a buffer owner. + * + * A null buffer is always owned, materialized, and empty. + */ + [[nodiscard]] inline bool IsNull() const { return m_Buffer.IsNull(); } + + inline MutableMemoryView GetMutableView() { return MutableMemoryView(GetData(), GetSize()); } + inline MemoryView GetView() const { return MemoryView(GetData(), GetSize()); } /** Make an uninitialized owned buffer of the specified size. */ ZENCORE_API static UniqueBuffer Alloc(uint64_t Size); @@ -41,8 +51,15 @@ public: /** Make a non-owned view of the input. */ ZENCORE_API static UniqueBuffer MakeMutableView(void* DataPtr, uint64_t Size); + /** + * Convert this to an immutable shared buffer, leaving this null. + * + * Steals the buffer owner from the unique buffer. + */ + [[nodiscard]] ZENCORE_API SharedBuffer MoveToShared(); + private: - // This may never be null + // This may be null, for a default constructed UniqueBuffer only RefPtr<IoBufferCore> m_Buffer; friend class SharedBuffer; @@ -59,6 +76,7 @@ public: inline explicit SharedBuffer(IoBufferCore* Owner) : m_Buffer(Owner) {} ZENCORE_API explicit SharedBuffer(IoBuffer&& Buffer) : m_Buffer(std::move(Buffer.m_Core)) {} ZENCORE_API explicit SharedBuffer(const IoBuffer& Buffer) : m_Buffer(Buffer.m_Core) {} + ZENCORE_API explicit SharedBuffer(RefPtr<IoBufferCore>&& Owner) : m_Buffer(std::move(Owner)) {} const void* GetData() const { @@ -84,11 +102,11 @@ public: m_Buffer->SetIsImmutable(true); } - ZENCORE_API void MakeOwned(); - bool IsOwned() const { return m_Buffer && m_Buffer->IsOwned(); } - inline explicit operator bool() const { return m_Buffer; } - inline bool IsNull() const { return !m_Buffer; } - inline void Reset() { m_Buffer = nullptr; } + ZENCORE_API SharedBuffer& MakeOwned(); + bool IsOwned() const { return m_Buffer && m_Buffer->IsOwned(); } + inline explicit operator bool() const { return m_Buffer; } + inline bool IsNull() const { return !m_Buffer; } + inline void Reset() { m_Buffer = nullptr; } MemoryView GetView() const { |