From 2b579c58bbbf2a6a7070c0d0d217ad8a165bbf1b Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 20 Apr 2021 22:09:52 -0500 Subject: [PATCH] [ox/std] Add UniquePtr --- deps/ox/src/ox/std/CMakeLists.txt | 1 + deps/ox/src/ox/std/memory.hpp | 111 ++++++++++++++++++++++++++++++ deps/ox/src/ox/std/std.hpp | 1 + 3 files changed, 113 insertions(+) create mode 100644 deps/ox/src/ox/std/memory.hpp diff --git a/deps/ox/src/ox/std/CMakeLists.txt b/deps/ox/src/ox/std/CMakeLists.txt index 4a9f04a5..8180b58a 100644 --- a/deps/ox/src/ox/std/CMakeLists.txt +++ b/deps/ox/src/ox/std/CMakeLists.txt @@ -71,6 +71,7 @@ install( heapmgr.hpp math.hpp memops.hpp + memory.hpp new.hpp random.hpp std.hpp diff --git a/deps/ox/src/ox/std/memory.hpp b/deps/ox/src/ox/std/memory.hpp new file mode 100644 index 00000000..75bb997c --- /dev/null +++ b/deps/ox/src/ox/std/memory.hpp @@ -0,0 +1,111 @@ +/* + * Copyright 2015 - 2021 gary@drinkingtea.net + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include "utility.hpp" + +namespace ox { + +template +class UniquePtr { + + private: + T *m_t; + + public: + explicit constexpr UniquePtr(T *t = nullptr) noexcept: m_t(t) { + } + + constexpr UniquePtr(const UniquePtr&) = delete; + + template + constexpr UniquePtr(UniquePtr &&other) noexcept { + m_t = other.release(); + } + + ~UniquePtr() { + delete m_t; + } + + constexpr T *release() noexcept { + auto t = m_t; + m_t = nullptr; + return t; + } + + [[nodiscard]] + constexpr T *get() const noexcept { + return m_t; + } + + constexpr void reset(UniquePtr &&other = UniquePtr()) { + auto t = m_t; + m_t = other.m_t; + other.m_t = nullptr; + delete t; + } + + constexpr UniquePtr &operator=(UniquePtr &&other) { + reset(ox::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 +constexpr bool operator==(const UniquePtr p1, const UniquePtr p2) noexcept { + return p1.get() == p2.get(); +} + +template +constexpr bool operator==(const UniquePtr p1, std::nullptr_t) noexcept { + return p1.get(); +} + +template +constexpr bool operator==(std::nullptr_t, const UniquePtr p2) noexcept { + return p2.get(); +} + + +template +constexpr bool operator!=(const UniquePtr p1, const UniquePtr p2) noexcept { + return p1.get() != p2.get(); +} + +template +constexpr bool operator!=(const UniquePtr p1, std::nullptr_t) noexcept { + return !p1.get(); +} + +template +constexpr bool operator!=(std::nullptr_t, const UniquePtr p2) noexcept { + return !p2.get(); +} + + +template +[[nodiscard]] +constexpr auto make_unique(Args&&... args) noexcept { + return UniquePtr(new T(args...)); +} + +} diff --git a/deps/ox/src/ox/std/std.hpp b/deps/ox/src/ox/std/std.hpp index 873aae8b..3016bc48 100644 --- a/deps/ox/src/ox/std/std.hpp +++ b/deps/ox/src/ox/std/std.hpp @@ -20,6 +20,7 @@ #include "heapmgr.hpp" #include "math.hpp" #include "memops.hpp" +#include "memory.hpp" #include "new.hpp" #include "random.hpp" #include "stacktrace.hpp"