[ox/std] Delete UniquePtr copy operators
This commit is contained in:
parent
5e2e459650
commit
dfd6670dfe
123
deps/ox/src/ox/std/memory.hpp
vendored
123
deps/ox/src/ox/std/memory.hpp
vendored
@ -63,6 +63,117 @@ struct DefaultDelete {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename T, typename Deleter = DefaultDelete>
|
||||
class SharedPtr {
|
||||
|
||||
private:
|
||||
T *m_t = nullptr;
|
||||
int *m_refCnt = nullptr;
|
||||
|
||||
public:
|
||||
explicit constexpr SharedPtr(T *t = nullptr) noexcept: m_t(t), m_refCnt(new int) {
|
||||
}
|
||||
|
||||
constexpr SharedPtr(SharedPtr &other) {
|
||||
m_t = other.m_t;
|
||||
m_refCnt = other.m_refCnt;
|
||||
++*m_refCnt;
|
||||
}
|
||||
|
||||
constexpr SharedPtr(const SharedPtr&) = delete;
|
||||
|
||||
template<typename U>
|
||||
constexpr SharedPtr(SharedPtr<U> &&other) noexcept {
|
||||
m_t = other.m_t;
|
||||
m_refCnt = other.m_refCnt;
|
||||
other.m_refCnt = nullptr;
|
||||
}
|
||||
|
||||
~SharedPtr() {
|
||||
if (m_refCnt) {
|
||||
--*m_refCnt;
|
||||
if (!*m_refCnt) {
|
||||
Deleter()(m_t);
|
||||
safeDelete(m_refCnt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr T *get() const noexcept {
|
||||
return m_t;
|
||||
}
|
||||
|
||||
constexpr T *reset() noexcept {
|
||||
if (m_refCnt) {
|
||||
--*m_refCnt;
|
||||
if (!*m_refCnt) {
|
||||
safeDelete(m_refCnt);
|
||||
Deleter()(m_t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
constexpr void reset(U *other) {
|
||||
reset();
|
||||
m_t = other;
|
||||
m_refCnt = new int(1);
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
constexpr SharedPtr &operator=(SharedPtr<U> &&other) {
|
||||
reset(std::move(other));
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr T *operator->() const noexcept {
|
||||
return m_t;
|
||||
}
|
||||
|
||||
constexpr T &operator*() const noexcept {
|
||||
return *m_t;
|
||||
}
|
||||
|
||||
constexpr operator bool() const noexcept {
|
||||
return m_t;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
constexpr bool operator==(const SharedPtr<T> &p1, const SharedPtr<T> &p2) noexcept {
|
||||
return p1.get() == p2.get();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr bool operator==(const SharedPtr<T> &p1, std::nullptr_t) noexcept {
|
||||
return p1.get();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr bool operator==(std::nullptr_t, const SharedPtr<T> &p2) noexcept {
|
||||
return p2.get();
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
constexpr bool operator!=(const SharedPtr<T> &p1, const SharedPtr<T> &p2) noexcept {
|
||||
return p1.get() != p2.get();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr bool operator!=(const SharedPtr<T> &p1, std::nullptr_t) noexcept {
|
||||
return !p1.get();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr bool operator!=(std::nullptr_t, const SharedPtr<T> &p2) noexcept {
|
||||
return !p2.get();
|
||||
}
|
||||
|
||||
|
||||
template<typename T, typename Deleter = DefaultDelete>
|
||||
class UniquePtr {
|
||||
|
||||
@ -82,7 +193,7 @@ class UniquePtr {
|
||||
m_t = other.release();
|
||||
}
|
||||
|
||||
~UniquePtr() {
|
||||
constexpr ~UniquePtr() {
|
||||
Deleter()(m_t);
|
||||
}
|
||||
|
||||
@ -104,6 +215,16 @@ class UniquePtr {
|
||||
Deleter()(t);
|
||||
}
|
||||
|
||||
constexpr UniquePtr &operator=(const UniquePtr<T> &other) = delete;
|
||||
|
||||
template<typename U>
|
||||
constexpr UniquePtr &operator=(const UniquePtr<U> &other) = delete;
|
||||
|
||||
constexpr UniquePtr &operator=(UniquePtr<T> &&other) {
|
||||
reset(std::move(other));
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
constexpr UniquePtr &operator=(UniquePtr<U> &&other) {
|
||||
reset(std::move(other));
|
||||
|
Loading…
Reference in New Issue
Block a user