[ox/std] Add ox::ignore

(synced from af7c89564c)
This commit is contained in:
2024-04-09 22:40:20 -05:00
parent 8314bcd260
commit 5c605c3479
6 changed files with 16 additions and 104 deletions
+1
View File
@@ -103,6 +103,7 @@ install(
hardware.hpp
hashmap.hpp
heapmgr.hpp
ignore.hpp
iterator.hpp
math.hpp
memops.hpp
-102
View File
@@ -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<char> s) noexcept = 0;
virtual constexpr operator bool() const noexcept = 0;
};
template<typename T>
struct Wrap: public WrapBase {
T *data{};
constexpr Wrap(T *pData) noexcept: data(pData) {
}
constexpr WrapBase *copyTo(ox::Span<char> 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<char, sizeof(Wrap<void*>)> m_wrapData;
public:
constexpr AnyPtr() noexcept = default;
template<typename T>
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<typename T>
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<typename T>
[[nodiscard]]
constexpr T *get() const noexcept {
#ifdef OX_BARE_METAL
return static_cast<Wrap<T>*>(m_wrapPtr)->data;
#else
return dynamic_cast<Wrap<T>*>(m_wrapPtr)->data;
#endif
}
};
}
+1
View File
@@ -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"
+13
View File
@@ -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;
}
-1
View File
@@ -8,7 +8,6 @@
#pragma once
#include "array.hpp"
#include "bit.hpp"
#include "iterator.hpp"
#include "vector.hpp"
+1 -1
View File
@@ -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"