aboutsummaryrefslogtreecommitdiff
path: root/zencore/include
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2021-09-01 11:35:53 +0200
committerStefan Boberg <[email protected]>2021-09-01 11:35:53 +0200
commit551636500b210a302f1a868b889eb82b5c87c2ac (patch)
treee62884ff8a1af88ca8c446e24e3a72735c9e76ca /zencore/include
parentCompactBinary: Added explicit operator bool for array and object types (diff)
downloadzen-551636500b210a302f1a868b889eb82b5c87c2ac.tar.xz
zen-551636500b210a302f1a868b889eb82b5c87c2ac.zip
SharedBuffer: MakeOwned now returns a buffer instead of operating in-place
CL15713705
Diffstat (limited to 'zencore/include')
-rw-r--r--zencore/include/zencore/sharedbuffer.h47
1 files changed, 26 insertions, 21 deletions
diff --git a/zencore/include/zencore/sharedbuffer.h b/zencore/include/zencore/sharedbuffer.h
index 2c10c4469..b30f09d50 100644
--- a/zencore/include/zencore/sharedbuffer.h
+++ b/zencore/include/zencore/sharedbuffer.h
@@ -28,9 +28,9 @@ public:
ZENCORE_API explicit UniqueBuffer(IoBufferCore* Owner);
- 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; }
+ [[nodiscard]] void* GetData() { return m_Buffer ? m_Buffer->MutableDataPointer() : nullptr; }
+ [[nodiscard]] const void* GetData() const { return m_Buffer ? m_Buffer->DataPointer() : nullptr; }
+ [[nodiscard]] size_t GetSize() const { return m_Buffer ? m_Buffer->DataBytes() : 0; }
operator MutableMemoryView() { return GetMutableView(); }
operator MemoryView() const { return GetView(); }
@@ -45,14 +45,14 @@ public:
/** Reset this to null. */
ZENCORE_API void Reset();
- inline MutableMemoryView GetMutableView() { return MutableMemoryView(GetData(), GetSize()); }
- inline MemoryView GetView() const { return MemoryView(GetData(), GetSize()); }
+ [[nodiscard]] inline MutableMemoryView GetMutableView() { return MutableMemoryView(GetData(), GetSize()); }
+ [[nodiscard]] 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);
+ [[nodiscard]] ZENCORE_API static UniqueBuffer Alloc(uint64_t Size);
/** Make a non-owned view of the input. */
- ZENCORE_API static UniqueBuffer MakeMutableView(void* DataPtr, uint64_t Size);
+ [[nodiscard]] ZENCORE_API static UniqueBuffer MakeMutableView(void* DataPtr, uint64_t Size);
/**
* Convert this to an immutable shared buffer, leaving this null.
@@ -81,7 +81,7 @@ public:
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
+ [[nodiscard]] const void* GetData() const
{
if (m_Buffer)
{
@@ -90,7 +90,7 @@ public:
return nullptr;
}
- size_t GetSize() const
+ [[nodiscard]] size_t GetSize() const
{
if (m_Buffer)
{
@@ -105,13 +105,15 @@ public:
m_Buffer->SetIsImmutable(true);
}
- 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; }
+ /** Returns a buffer that is owned, by cloning if not owned. */
+ [[nodiscard]] ZENCORE_API SharedBuffer MakeOwned() const&;
+ [[nodiscard]] ZENCORE_API SharedBuffer MakeOwned() &&;
+
+ [[nodiscard]] bool IsOwned() const { return m_Buffer && m_Buffer->IsOwned(); }
+ [[nodiscard]] inline bool IsNull() const { return !m_Buffer; }
inline void Reset() { m_Buffer = nullptr; }
- MemoryView GetView() const
+ [[nodiscard]] MemoryView GetView() const
{
if (m_Buffer)
{
@@ -122,9 +124,12 @@ public:
return MemoryView();
}
}
+ operator MemoryView() const { return GetView(); }
+
+ /** Returns true if this points to a buffer owner. */
+ [[nodiscard]] inline explicit operator bool() const { return !IsNull(); }
- operator MemoryView() const { return GetView(); }
- inline IoBuffer AsIoBuffer() const { return IoBuffer(m_Buffer); }
+ [[nodiscard]] inline IoBuffer AsIoBuffer() const { return IoBuffer(m_Buffer); }
SharedBuffer& operator=(UniqueBuffer&& Rhs)
{
@@ -135,15 +140,15 @@ public:
std::strong_ordering operator<=>(const SharedBuffer& Rhs) const = default;
/** Make a non-owned view of the input */
- inline static SharedBuffer MakeView(MemoryView View) { return MakeView(View.GetData(), View.GetSize()); }
+ [[nodiscard]] inline static SharedBuffer MakeView(MemoryView View) { return MakeView(View.GetData(), View.GetSize()); }
/** Make a non-owned view of the input */
- ZENCORE_API static SharedBuffer MakeView(const void* Data, uint64_t Size);
+ [[nodiscard]] ZENCORE_API static SharedBuffer MakeView(const void* Data, uint64_t Size);
/** Make a non-owned view of the input */
- ZENCORE_API static SharedBuffer MakeView(MemoryView View, SharedBuffer OuterBuffer);
+ [[nodiscard]] ZENCORE_API static SharedBuffer MakeView(MemoryView View, SharedBuffer OuterBuffer);
/** Make an owned clone of the buffer */
- ZENCORE_API SharedBuffer Clone();
+ [[nodiscard]] ZENCORE_API SharedBuffer Clone();
/** Make an owned clone of the memory in the input view */
- ZENCORE_API static SharedBuffer Clone(MemoryView View);
+ [[nodiscard]] ZENCORE_API static SharedBuffer Clone(MemoryView View);
private:
RefPtr<IoBufferCore> m_Buffer;