From 7151b0e5bcabb0216e79e742ae13c4f429ec2011 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Sat, 27 Apr 2024 10:06:05 -0700 Subject: Commit misc work from a while ago --- src/brussel.common/PodVector.hpp | 104 +++++++++++++++++++--------------- src/brussel.common/RcPtr.hpp | 2 +- src/brussel.common/ScopedResource.hpp | 54 ++++++++++++++++++ 3 files changed, 114 insertions(+), 46 deletions(-) create mode 100644 src/brussel.common/ScopedResource.hpp (limited to 'src/brussel.common') diff --git a/src/brussel.common/PodVector.hpp b/src/brussel.common/PodVector.hpp index bd92e7d..b6ab1d6 100644 --- a/src/brussel.common/PodVector.hpp +++ b/src/brussel.common/PodVector.hpp @@ -26,20 +26,20 @@ public: mData = nullptr; } - PodVector(const PodVector& src) { + PodVector(const PodVector& src) { mSize = mCapacity = 0; mData = nullptr; operator=(src); } - PodVector& operator=(const PodVector& src) { + PodVector& operator=(const PodVector& src) { clear(); resize(src.mSize); - std::memcpy(mData, src.mData, (size_t)mSize * sizeof(T)); + std::memcpy(mData, src.mData, static_cast(mSize) * sizeof(T)); return *this; } - PodVector(PodVector&& src) { + PodVector(PodVector&& src) noexcept { mSize = src.mSize; mCapacity = src.mCapacity; mData = src.mData; @@ -48,7 +48,7 @@ public: src.mData = nullptr; } - PodVector& operator=(PodVector&& src) { + PodVector& operator=(PodVector&& src) noexcept { if (this != &src) { std::free(mData); @@ -66,18 +66,18 @@ public: std::free(mData); } - bool empty() const { return mSize == 0; } - int size() const { return mSize; } - int size_in_bytes() const { return mSize * (int)sizeof(T); } - int max_size() const { return 0x7FFFFFFF / (int)sizeof(T); } - int capacity() const { return mCapacity; } + [[nodiscard]] bool empty() const { return mSize == 0; } + [[nodiscard]] int size() const { return mSize; } + [[nodiscard]] int size_in_bytes() const { return mSize * static_cast(sizeof(T)); } + [[nodiscard]] static int max_size() { return std::numeric_limits::max() / static_cast(sizeof(T)); } + [[nodiscard]] int capacity() const { return mCapacity; } - T& operator[](int i) { + [[nodiscard]] T& operator[](int i) { assert(i >= 0 && i < mSize); return mData[i]; } - const T& operator[](int i) const { + [[nodiscard]] const T& operator[](int i) const { assert(i >= 0 && i < mSize); return mData[i]; } @@ -90,24 +90,24 @@ public: } } - T* begin() { return mData; } - const T* begin() const { return mData; } - T* end() { return mData + mSize; } - const T* end() const { return mData + mSize; } + [[nodiscard]] T* begin() { return mData; } + [[nodiscard]] const T* begin() const { return mData; } + [[nodiscard]] T* end() { return mData + mSize; } + [[nodiscard]] const T* end() const { return mData + mSize; } - T* data() { return mData; } + [[nodiscard]] T* data() { return mData; } - T& front() { + [[nodiscard]] T& front() { assert(mSize > 0); return mData[0]; } - const T& front() const { + [[nodiscard]] const T& front() const { assert(mSize > 0); return mData[0]; } - T& back() { + [[nodiscard]] T& back() { assert(mSize > 0); return mData[mSize - 1]; } @@ -117,16 +117,16 @@ public: return mData[mSize - 1]; } - void swap(PodVector& rhs) { + friend void swap(PodVector& lhs, PodVector& rhs) noexcept { int rhs_size = rhs.mSize; - rhs.mSize = mSize; - mSize = rhs_size; + rhs.mSize = lhs.mSize; + lhs.mSize = rhs_size; int rhs_cap = rhs.mCapacity; - rhs.mCapacity = mCapacity; - mCapacity = rhs_cap; + rhs.mCapacity = lhs.mCapacity; + lhs.mCapacity = rhs_cap; T* rhs_mDataTmp = rhs.mData; - rhs.mData = mData; - mData = rhs_mDataTmp; + rhs.mData = lhs.mData; + lhs.mData = rhs_mDataTmp; } int grow_capacity(int sz) const { @@ -165,9 +165,9 @@ public: /// Resize a vector to a smaller mSize, guaranteed not to cause a reallocation void reserve(int newCapacity) { if (newCapacity <= mCapacity) return; - auto tmp = (T*)std::malloc((size_t)newCapacity * sizeof(T)); + auto tmp = static_cast(std::malloc(static_cast(newCapacity) * sizeof(T))); if (mData) { - std::memcpy(tmp, mData, (size_t)mSize * sizeof(T)); + std::memcpy(tmp, mData, static_cast(mSize) * sizeof(T)); std::free(mData); } mData = tmp; @@ -201,7 +201,10 @@ public: T* erase(const T* it) { assert(it >= mData && it < mData + mSize); const ptrdiff_t off = it - mData; - std::memmove(mData + off, mData + off + 1, ((size_t)mSize - (size_t)off - 1) * sizeof(T)); + std::memmove( + mData + off, + mData + off + 1, + (static_cast(mSize) - static_cast(off) - 1) * sizeof(T)); mSize--; return mData + off; } @@ -210,15 +213,22 @@ public: assert(it >= mData && it < mData + mSize && it_last > it && it_last <= mData + mSize); const ptrdiff_t count = it_last - it; const ptrdiff_t off = it - mData; - std::memmove(mData + off, mData + off + count, ((size_t)mSize - (size_t)off - count) * sizeof(T)); - mSize -= (int)count; + std::memmove( + mData + off, + mData + off + count, + (static_cast(mSize) - static_cast(off) - count) * sizeof(T)); + mSize -= static_cast(count); return mData + off; } T* erase_unsorted(const T* it) { assert(it >= mData && it < mData + mSize); const ptrdiff_t off = it - mData; - if (it < mData + mSize - 1) std::memcpy(mData + off, mData + mSize - 1, sizeof(T)); + if (it < mData + mSize - 1) + std::memcpy( + mData + off, + mData + mSize - 1, + sizeof(T)); mSize--; return mData + off; } @@ -227,13 +237,17 @@ public: assert(it >= mData && it <= mData + mSize); const ptrdiff_t off = it - mData; if (mSize == mCapacity) reserve(grow_capacity(mSize + 1)); - if (off < (int)mSize) std::memmove(mData + off + 1, mData + off, ((size_t)mSize - (size_t)off) * sizeof(T)); + if (off < mSize) + std::memmove( + mData + off + 1, + mData + off, + (static_cast(mSize) - static_cast(off)) * sizeof(T)); std::memcpy(&mData[off], &v, sizeof(v)); mSize++; return mData + off; } - bool contains(const T& v) const { + [[nodiscard]] bool contains(const T& v) const { const T* data = mData; const T* dataEnd = mData + mSize; while (data < dataEnd) { @@ -242,7 +256,7 @@ public: return false; } - T* find(const T& v) { + [[nodiscard]] T* find(const T& v) { T* data = mData; const T* dataEnd = mData + mSize; while (data < dataEnd) @@ -253,14 +267,14 @@ public: return data; } - const T* find(const T& v) const { + [[nodiscard]] const T* find(const T& v) const { const T* data = mData; const T* dataEnd = mData + mSize; - while (data < dataEnd) + while (data < dataEnd) { if (*data == v) break; - else - ++data; + ++data; + } return data; } @@ -285,13 +299,13 @@ public: int index_from_ptr(const T* it) const { assert(it >= mData && it < mData + mSize); const ptrdiff_t off = it - mData; - return (int)off; + return off; } // Custom utility functions - std::span as_span() { return { mData, (size_t)mSize }; } - std::span as_data_span() { return { (uint8_t*)mData, (size_t)mSize * sizeof(T) }; } - std::span as_span() const { return { mData, (size_t)mSize }; } - std::span as_data_span() const { return { (uint8_t*)mData, (size_t)mSize * sizeof(T) }; } + [[nodiscard]] std::span as_span() { return { mData, static_cast(mSize) }; } + [[nodiscard]] std::span as_data_span() { return { static_cast(mData), static_cast(mSize) * sizeof(T) }; } + [[nodiscard]] std::span as_span() const { return { mData, static_cast(mSize) }; } + [[nodiscard]] std::span as_data_span() const { return { static_cast(mData), static_cast(mSize) * sizeof(T) }; } }; diff --git a/src/brussel.common/RcPtr.hpp b/src/brussel.common/RcPtr.hpp index e3e420e..72a9b86 100644 --- a/src/brussel.common/RcPtr.hpp +++ b/src/brussel.common/RcPtr.hpp @@ -98,7 +98,7 @@ public: template bool operator==(const RcPtr& ptr) const { - return mPtr == ptr.Get(); + return mPtr == ptr.get(); } T* Get() const { diff --git a/src/brussel.common/ScopedResource.hpp b/src/brussel.common/ScopedResource.hpp new file mode 100644 index 0000000..b4098eb --- /dev/null +++ b/src/brussel.common/ScopedResource.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include + +/// std::unique_ptr, but for non-pointer resources +template + requires std::is_trivial_v && std::equality_comparable +class ScopedResource { +private: + T mValue; + +public: + ScopedResource() + : mValue{ kNullValue } {} + explicit ScopedResource(T t) + : mValue{ std::move(t) } {} + + ScopedResource(const ScopedResource& that) = delete; + ScopedResource& operator=(const ScopedResource& that) = delete; + + ScopedResource(ScopedResource&& that) noexcept + : mValue(that.mValue) {} + ScopedResource& operator=(ScopedResource&& that) noexcept { + if (this == &that) + return *this; + TDeleter::DeleteObject(mValue); + mValue = that.mValue; + that.mValue = kNullValue; + return *this; + } + + ScopedResource& operator=(T newValue) { + TDeleter::DeleteObject(mValue); + mValue = newValue; + return *this; + } + + ~ScopedResource() { + TDeleter::DeleteObject(mValue); + } + + explicit operator bool() const { + return mValue != kNullValue; + } + + const T& get() const { return mValue; } + T& get() { return mValue; } + + T release() { + T tmp = mValue; + mValue = kNullValue; + return tmp; + } +}; -- cgit v1.2.3-70-g09d2