From 3fa247e3d4aa0df49414a0fa44b37101d780e2e7 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 23 Mar 2024 16:52:43 -0500 Subject: [PATCH] [ox/std] Add AnyPtr --- deps/ox/src/ox/std/anyptr.hpp | 73 +++++++++++++++++++++++++++++++++++ deps/ox/src/ox/std/array.hpp | 1 - deps/ox/src/ox/std/span.hpp | 1 + deps/ox/src/ox/std/std.hpp | 1 + 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 deps/ox/src/ox/std/anyptr.hpp diff --git a/deps/ox/src/ox/std/anyptr.hpp b/deps/ox/src/ox/std/anyptr.hpp new file mode 100644 index 00000000..3a8729f1 --- /dev/null +++ b/deps/ox/src/ox/std/anyptr.hpp @@ -0,0 +1,73 @@ + +#pragma once + +#include "array.hpp" +#include "span.hpp" + +namespace ox { + +class AnyPtr { + private: + struct WrapBase { + virtual ~WrapBase() = default; + virtual WrapBase *copyTo(ox::Span s) noexcept = 0; + virtual operator bool() const noexcept = 0; + }; + + template + struct Wrap: public WrapBase { + T *data{}; + constexpr Wrap(T *pData) noexcept: data(pData) { + } + inline WrapBase *copyTo(ox::Span s) noexcept override { + return new(s.data()) Wrap{data}; + } + constexpr operator bool() const noexcept override { + return data != nullptr; + } + }; + + WrapBase *m_wrapPtr{}; + ox::Array)> m_wrapData; + public: + constexpr AnyPtr() noexcept = default; + template + inline AnyPtr(T *ptr) noexcept { + m_wrapPtr = new(m_wrapData.data()) Wrap(ptr); + } + inline AnyPtr(AnyPtr const&other) noexcept { + if (other) { + m_wrapPtr = other.m_wrapPtr->copyTo(m_wrapData); + } + } + template + inline AnyPtr &operator=(T *ptr) noexcept { + m_wrapPtr = new(m_wrapData.data()) Wrap(ptr); + return *this; + } + inline AnyPtr &operator=(AnyPtr const&ptr) noexcept { + if (this != &ptr) { + if (ptr) { + m_wrapPtr = ptr.m_wrapPtr->copyTo(m_wrapData); + } else { + m_wrapPtr = nullptr; + } + } + return *this; + } + constexpr operator bool() const noexcept { + return m_wrapPtr && *m_wrapPtr; + } + template + [[nodiscard]] + constexpr T *get() const noexcept { +#ifdef OX_BARE_METAL + auto const out = static_cast*>(m_wrapPtr); +#else + auto const out = dynamic_cast*>(m_wrapPtr); +#endif + return out->data; + } +}; + +} \ No newline at end of file diff --git a/deps/ox/src/ox/std/array.hpp b/deps/ox/src/ox/std/array.hpp index b18efc2b..bef35d09 100644 --- a/deps/ox/src/ox/std/array.hpp +++ b/deps/ox/src/ox/std/array.hpp @@ -13,7 +13,6 @@ #include "initializerlist.hpp" #include "iterator.hpp" #include "math.hpp" -#include "memory.hpp" #include "new.hpp" #include "types.hpp" #include "utility.hpp" diff --git a/deps/ox/src/ox/std/span.hpp b/deps/ox/src/ox/std/span.hpp index 1d0af4fb..da9272f8 100644 --- a/deps/ox/src/ox/std/span.hpp +++ b/deps/ox/src/ox/std/span.hpp @@ -8,6 +8,7 @@ #pragma once +#include "array.hpp" #include "bit.hpp" #include "iterator.hpp" #include "vector.hpp" diff --git a/deps/ox/src/ox/std/std.hpp b/deps/ox/src/ox/std/std.hpp index 70b110ea..0cf050d3 100644 --- a/deps/ox/src/ox/std/std.hpp +++ b/deps/ox/src/ox/std/std.hpp @@ -8,6 +8,7 @@ #pragma once +#include "anyptr.hpp" #include "array.hpp" #include "assert.hpp" #include "bit.hpp"