diff --git a/src/ox/std/CMakeLists.txt b/src/ox/std/CMakeLists.txt index 07b965bcd..f6ba8673d 100644 --- a/src/ox/std/CMakeLists.txt +++ b/src/ox/std/CMakeLists.txt @@ -103,6 +103,7 @@ install( hardware.hpp hashmap.hpp heapmgr.hpp + ignore.hpp iterator.hpp math.hpp memops.hpp diff --git a/src/ox/std/anyptr.hpp b/src/ox/std/anyptr.hpp deleted file mode 100644 index ac11e4845..000000000 --- a/src/ox/std/anyptr.hpp +++ /dev/null @@ -1,102 +0,0 @@ - -#pragma once - -#include "assert.hpp" -#include "array.hpp" -#include "def.hpp" -#include "span.hpp" - -namespace ox { - -class AnyPtr { - private: - struct WrapBase { - virtual constexpr ~WrapBase() = default; - virtual constexpr WrapBase *copyTo(ox::Span s) noexcept = 0; - virtual constexpr operator bool() const noexcept = 0; - }; - - template - struct Wrap: public WrapBase { - T *data{}; - constexpr Wrap(T *pData) noexcept: data(pData) { - } - constexpr WrapBase *copyTo(ox::Span s) noexcept override { - oxAssert(s.size() >= sizeof(Wrap), "too small buffer"); - if (std::is_constant_evaluated()) { - return new Wrap(data); - } else { - 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 - constexpr AnyPtr(T *ptr) noexcept { - if (std::is_constant_evaluated()) { - m_wrapPtr = new Wrap(ptr); - } else { - m_wrapPtr = new(m_wrapData.data()) Wrap(ptr); - } - } - - constexpr AnyPtr(AnyPtr const&other) noexcept { - if (other) { - m_wrapPtr = other.m_wrapPtr->copyTo(m_wrapData); - } - } - - constexpr ~AnyPtr() noexcept { - if (std::is_constant_evaluated()) { - ox::safeDelete(m_wrapPtr); - } - } - - template - constexpr AnyPtr &operator=(T *ptr) noexcept { - if (std::is_constant_evaluated()) { - ox::safeDelete(m_wrapPtr); - m_wrapPtr = new Wrap(ptr); - } else { - m_wrapPtr = new(m_wrapData.data()) Wrap(ptr); - } - return *this; - } - - constexpr AnyPtr &operator=(AnyPtr const&ptr) noexcept { - if (this != &ptr) { - if (ptr) { - ox::safeDelete(m_wrapPtr); - 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 - return static_cast*>(m_wrapPtr)->data; -#else - return dynamic_cast*>(m_wrapPtr)->data; -#endif - } -}; - -} \ No newline at end of file diff --git a/src/ox/std/array.hpp b/src/ox/std/array.hpp index bef35d091..b18efc2b9 100644 --- a/src/ox/std/array.hpp +++ b/src/ox/std/array.hpp @@ -13,6 +13,7 @@ #include "initializerlist.hpp" #include "iterator.hpp" #include "math.hpp" +#include "memory.hpp" #include "new.hpp" #include "types.hpp" #include "utility.hpp" diff --git a/src/ox/std/ignore.hpp b/src/ox/std/ignore.hpp new file mode 100644 index 000000000..771cb7e40 --- /dev/null +++ b/src/ox/std/ignore.hpp @@ -0,0 +1,13 @@ +/* + * Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved. + */ + +#pragma once + +namespace ox { + +inline constexpr struct { + constexpr void operator=(auto&&) const noexcept {} +} ignore; + +} \ No newline at end of file diff --git a/src/ox/std/span.hpp b/src/ox/std/span.hpp index da9272f81..1d0af4fbc 100644 --- a/src/ox/std/span.hpp +++ b/src/ox/std/span.hpp @@ -8,7 +8,6 @@ #pragma once -#include "array.hpp" #include "bit.hpp" #include "iterator.hpp" #include "vector.hpp" diff --git a/src/ox/std/std.hpp b/src/ox/std/std.hpp index 0cf050d38..c77eacce9 100644 --- a/src/ox/std/std.hpp +++ b/src/ox/std/std.hpp @@ -8,7 +8,6 @@ #pragma once -#include "anyptr.hpp" #include "array.hpp" #include "assert.hpp" #include "bit.hpp" @@ -26,6 +25,7 @@ #include "hardware.hpp" #include "hashmap.hpp" #include "heapmgr.hpp" +#include "ignore.hpp" #include "iterator.hpp" #include "math.hpp" #include "memops.hpp"