diff --git a/deps/ox/src/ox/clargs/clargs.hpp b/deps/ox/src/ox/clargs/clargs.hpp index 6e564fb9..15fae765 100644 --- a/deps/ox/src/ox/clargs/clargs.hpp +++ b/deps/ox/src/ox/clargs/clargs.hpp @@ -9,11 +9,10 @@ #pragma once #include +#include namespace ox { -class String; - class ClArgs { private: HashMap m_bools; diff --git a/deps/ox/src/ox/std/CMakeLists.txt b/deps/ox/src/ox/std/CMakeLists.txt index 1e69cd3f..f6acb383 100644 --- a/deps/ox/src/ox/std/CMakeLists.txt +++ b/deps/ox/src/ox/std/CMakeLists.txt @@ -12,6 +12,7 @@ endif() target_compile_definitions( OxTraceHook PUBLIC + $<$:OX_BARE_METAL> $<$:OX_USE_STDLIB> $<$:OX_NODEBUG> ) diff --git a/deps/ox/src/ox/std/error.hpp b/deps/ox/src/ox/std/error.hpp index 9bd5b1cb..73f3e094 100644 --- a/deps/ox/src/ox/std/error.hpp +++ b/deps/ox/src/ox/std/error.hpp @@ -126,8 +126,8 @@ constexpr void oxIgnoreError(ox::Error) noexcept {} #define oxReturnError(x) if (const auto _ox_error = ox::detail::toError(x)) [[unlikely]] return _ox_error #define oxThrowError(x) if (const auto _ox_error = ox::detail::toError(x)) [[unlikely]] throw _ox_error #else -#define oxReturnError(x) if (const auto _ox_error = ox::detail::toError(x)) return _ox_error -#define oxThrowError(x) if (const auto _ox_error = ox::detail::toError(x)) throw _ox_error +#define oxReturnError(err) if (const auto _ox_error = ox::detail::toError(err)) return _ox_error +#define oxThrowError(err) if (const auto _ox_error = ox::detail::toError(err)) throw _ox_error #endif #define oxConcatImpl(a, b) a##b #define oxConcat(a, b) oxConcatImpl(a, b) diff --git a/deps/ox/src/ox/std/stacktrace.cpp b/deps/ox/src/ox/std/stacktrace.cpp index f1f9f9a2..6fc21044 100644 --- a/deps/ox/src/ox/std/stacktrace.cpp +++ b/deps/ox/src/ox/std/stacktrace.cpp @@ -11,17 +11,14 @@ #include #include -#include #if __has_include() #include #endif #endif -#include "error.hpp" #include "string.hpp" #include "trace.hpp" -#include "units.hpp" #include "utility.hpp" #include "vector.hpp" diff --git a/deps/ox/src/ox/std/string.cpp b/deps/ox/src/ox/std/string.cpp index cb15f9b1..4e1685db 100644 --- a/deps/ox/src/ox/std/string.cpp +++ b/deps/ox/src/ox/std/string.cpp @@ -7,172 +7,6 @@ */ #include "string.hpp" -#include "types.hpp" namespace ox { - -String &String::operator=(const char *str) noexcept { - std::size_t strLen = ox_strlen(str) + 1; - m_buff.resize(strLen + 1); - memcpy(m_buff.data(), str, strLen); - // make sure last element is a null terminator - m_buff[m_buff.size() - 1] = 0; - return *this; -} - -String &String::operator=(char *str) noexcept { - return *this = static_cast(str); -} - -String &String::operator=(char c) noexcept { - char str[] = {c, 0}; - return this->operator=(str); -} - -String &String::operator=(int i) noexcept { - return this->operator=(static_cast(i)); -} - -String &String::operator=(int64_t i) noexcept { - char str[65] = {}; - ox_itoa(i, str); - return this->operator=(str); -} - -String &String::operator=(uint64_t i) noexcept { - char str[65] = {}; - ox_itoa(i, str); - return this->operator=(str); -} - -String &String::operator=(const String &src) noexcept { - return *this = src.c_str(); -} - -String &String::operator=(String &&src) noexcept { - m_buff = move(src.m_buff); - return *this; -} - -String &String::operator+=(const char *str) noexcept { - std::size_t strLen = ox_strlen(str); - oxIgnoreError(append(str, strLen)); - return *this; -} - -String &String::operator+=(char *str) noexcept { - return *this += static_cast(str); -} - -String &String::operator+=(char c) noexcept { - const char str[] = {c, 0}; - return this->operator+=(str); -} - -String &String::operator+=(int i) noexcept { - return this->operator+=(static_cast(i)); -} - -String &String::operator+=(int64_t i) noexcept { - char str[65] = {}; - ox_itoa(i, str); - return this->operator+=(str); -} - -String &String::operator+=(uint64_t i) noexcept { - char str[65] = {}; - ox_itoa(i, str); - return this->operator+=(str); -} - -String &String::operator+=(const String &src) noexcept { - return *this += src.c_str(); -} - -String String::operator+(const char *str) const noexcept { - const std::size_t strLen = ox_strlen(str); - const auto currentLen = len(); - String cpy(currentLen + strLen); - cpy.m_buff.resize(m_buff.size() + strLen); - memcpy(&cpy.m_buff[0], m_buff.data(), currentLen); - memcpy(&cpy.m_buff[currentLen], str, strLen); - // make sure last element is a null terminator - cpy.m_buff[currentLen + strLen] = 0; - return move(cpy); -} - -String String::operator+(char *str) const noexcept { - return *this + static_cast(str); -} - -String String::operator+(char c) const noexcept { - const char str[] = {c, 0}; - return *this + str; -} - -String String::operator+(int i) const noexcept { - return this->operator+(static_cast(i)); -} - -String String::operator+(int64_t i) const noexcept { - char str[65] = {}; - ox_itoa(i, str); - return *this + str; -} - -String String::operator+(uint64_t i) const noexcept { - char str[65] = {}; - ox_itoa(i, str); - return *this + str; -} - -String String::operator+(const String &src) const noexcept { - return *this + src.c_str(); -} - -bool String::operator==(const String &other) const noexcept { - bool retval = true; - std::size_t i = 0; - while (i < m_buff.size() && (m_buff[i] || other.m_buff[i])) { - if (m_buff[i] != other.m_buff[i]) { - retval = false; - break; - } - i++; - } - return retval; -} - -bool String::operator!=(const String &other) const noexcept { - return !operator==(other); -} - -char String::operator[](std::size_t i) const noexcept { - return m_buff[i]; -} - -char &String::operator[](std::size_t i) noexcept { - return m_buff[i]; -} - -String String::substr(std::size_t pos) const noexcept { - return m_buff.data() + pos; -} - -bool String::endsWith(const char *ending) const noexcept { - const auto endingLen = ox_strlen(ending); - return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending) == 0; -} - -bool String::endsWith(const String &ending) const noexcept { - const auto endingLen = ending.len(); - return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending.c_str()) == 0; -} - -std::size_t String::bytes() const noexcept { - std::size_t i; - for (i = 0; i < m_buff.size() && m_buff[i]; i++); - return i + 1; // add one for null terminator -} - } diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index 1aa9e2c8..00d1acf8 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -20,70 +20,71 @@ namespace ox { -class String { +template +class BasicString { private: - Buffer m_buff; + Vector m_buff; public: - constexpr String() noexcept; + constexpr BasicString() noexcept; - constexpr explicit String(std::size_t cap) noexcept; + constexpr explicit BasicString(std::size_t cap) noexcept; - constexpr String(const char *str) noexcept; + constexpr BasicString(const char *str) noexcept; - constexpr String(const char *str, std::size_t size) noexcept; + constexpr BasicString(const char *str, std::size_t size) noexcept; - constexpr String(const String&) noexcept; + constexpr BasicString(const BasicString&) noexcept; - constexpr String(String&&) noexcept; + constexpr BasicString(BasicString&&) noexcept; - String &operator=(const char *str) noexcept; + BasicString &operator=(const char *str) noexcept; - String &operator=(char *str) noexcept; + BasicString &operator=(char *str) noexcept; - String &operator=(char c) noexcept; + BasicString &operator=(char c) noexcept; - String &operator=(int i) noexcept; + BasicString &operator=(int i) noexcept; - String &operator=(int64_t i) noexcept; + BasicString &operator=(int64_t i) noexcept; - String &operator=(uint64_t i) noexcept; + BasicString &operator=(uint64_t i) noexcept; - String &operator=(const String &src) noexcept; + BasicString &operator=(const BasicString &src) noexcept; - String &operator=(String &&src) noexcept; + BasicString &operator=(BasicString &&src) noexcept; - String &operator+=(const char *str) noexcept; + constexpr BasicString &operator+=(const char *str) noexcept; - String &operator+=(char *str) noexcept; + constexpr BasicString &operator+=(char *str) noexcept; - String &operator+=(char c) noexcept; + constexpr BasicString &operator+=(char c) noexcept; - String &operator+=(int i) noexcept; + constexpr BasicString &operator+=(int i) noexcept; - String &operator+=(int64_t i) noexcept; + constexpr BasicString &operator+=(int64_t i) noexcept; - String &operator+=(uint64_t i) noexcept; + constexpr BasicString &operator+=(uint64_t i) noexcept; - String &operator+=(const String &src) noexcept; + constexpr BasicString &operator+=(const BasicString &src) noexcept; - String operator+(const char *str) const noexcept; + BasicString operator+(const char *str) const noexcept; - String operator+(char *str) const noexcept; + BasicString operator+(char *str) const noexcept; - String operator+(char c) const noexcept; + BasicString operator+(char c) const noexcept; - String operator+(int i) const noexcept; + BasicString operator+(int i) const noexcept; - String operator+(int64_t i) const noexcept; + BasicString operator+(int64_t i) const noexcept; - String operator+(uint64_t i) const noexcept; + BasicString operator+(uint64_t i) const noexcept; - String operator+(const String &src) const noexcept; + BasicString operator+(const BasicString &src) const noexcept; - bool operator==(const String &other) const noexcept; + bool operator==(const BasicString &other) const noexcept; - bool operator!=(const String &other) const noexcept; + bool operator!=(const BasicString &other) const noexcept; char operator[](std::size_t i) const noexcept; @@ -100,13 +101,13 @@ class String { } [[nodiscard]] - String substr(std::size_t pos) const noexcept; + BasicString substr(std::size_t pos) const noexcept; [[nodiscard]] bool endsWith(const char *other) const noexcept; [[nodiscard]] - bool endsWith(const String &other) const noexcept; + bool endsWith(const BasicString &other) const noexcept; [[nodiscard]] constexpr const char *data() const noexcept { @@ -144,7 +145,8 @@ class String { }; -constexpr String::String() noexcept { +template +constexpr BasicString::BasicString() noexcept { if (m_buff.size()) { m_buff[0] = 0; } else { @@ -152,12 +154,14 @@ constexpr String::String() noexcept { } } -constexpr String::String(std::size_t cap) noexcept { +template +constexpr BasicString::BasicString(std::size_t cap) noexcept { m_buff.resize(cap + 1); m_buff[0] = 0; } -constexpr String::String(const char *str) noexcept { +template +constexpr BasicString::BasicString(const char *str) noexcept { if (m_buff.size()) { m_buff[0] = 0; } else { @@ -166,29 +170,227 @@ constexpr String::String(const char *str) noexcept { *this = str; } -constexpr String::String(const char *str, std::size_t size) noexcept { +template +constexpr BasicString::BasicString(const char *str, std::size_t size) noexcept { m_buff.resize(size + 1); memcpy(m_buff.data(), str, size); m_buff[size] = 0; } -constexpr String::String(const String &other) noexcept { +template +constexpr BasicString::BasicString(const BasicString &other) noexcept { m_buff = other.m_buff; } -constexpr String::String(String &&other) noexcept { +template +constexpr BasicString::BasicString(BasicString &&other) noexcept { m_buff = move(other.m_buff); } -constexpr std::size_t String::len() const noexcept { +template +BasicString &BasicString::operator=(const char *str) noexcept { + std::size_t strLen = ox_strlen(str) + 1; + m_buff.resize(strLen + 1); + memcpy(m_buff.data(), str, strLen); + // make sure last element is a null terminator + m_buff[m_buff.size() - 1] = 0; + return *this; +} + +template +BasicString &BasicString::operator=(char *str) noexcept { + return *this = static_cast(str); +} + +template +BasicString &BasicString::operator=(char c) noexcept { + char str[] = {c, 0}; + return this->operator=(str); +} + +template +BasicString &BasicString::operator=(int i) noexcept { + return this->operator=(static_cast(i)); +} + +template +BasicString &BasicString::operator=(int64_t i) noexcept { + char str[65] = {}; + ox_itoa(i, str); + return this->operator=(str); +} + +template +BasicString &BasicString::operator=(uint64_t i) noexcept { + char str[65] = {}; + ox_itoa(i, str); + return this->operator=(str); +} + +template +BasicString &BasicString::operator=(const BasicString &src) noexcept { + return *this = src.c_str(); +} + +template +BasicString &BasicString::operator=(BasicString &&src) noexcept { + m_buff = move(src.m_buff); + return *this; +} + +template +constexpr BasicString &BasicString::operator+=(const char *str) noexcept { + std::size_t strLen = ox_strlen(str); + oxIgnoreError(append(str, strLen)); + return *this; +} + +template +constexpr BasicString &BasicString::operator+=(char *str) noexcept { + return *this += static_cast(str); +} + +template +constexpr BasicString &BasicString::operator+=(char c) noexcept { + const char str[] = {c, 0}; + return this->operator+=(str); +} + +template +constexpr BasicString &BasicString::operator+=(int i) noexcept { + return this->operator+=(static_cast(i)); +} + +template +constexpr BasicString &BasicString::operator+=(int64_t i) noexcept { + char str[65] = {}; + ox_itoa(i, str); + return this->operator+=(str); +} + +template +constexpr BasicString &BasicString::operator+=(uint64_t i) noexcept { + char str[65] = {}; + ox_itoa(i, str); + return this->operator+=(str); +} + +template +constexpr BasicString &BasicString::operator+=(const BasicString &src) noexcept { + return *this += src.c_str(); +} + +template +BasicString BasicString::operator+(const char *str) const noexcept { + const std::size_t strLen = ox_strlen(str); + const auto currentLen = len(); + BasicString cpy(currentLen + strLen); + cpy.m_buff.resize(m_buff.size() + strLen); + memcpy(&cpy.m_buff[0], m_buff.data(), currentLen); + memcpy(&cpy.m_buff[currentLen], str, strLen); + // make sure last element is a null terminator + cpy.m_buff[currentLen + strLen] = 0; + return move(cpy); +} + +template +BasicString BasicString::operator+(char *str) const noexcept { + return *this + static_cast(str); +} + +template +BasicString BasicString::operator+(char c) const noexcept { + const char str[] = {c, 0}; + return *this + str; +} + +template +BasicString BasicString::operator+(int i) const noexcept { + return this->operator+(static_cast(i)); +} + +template +BasicString BasicString::operator+(int64_t i) const noexcept { + char str[65] = {}; + ox_itoa(i, str); + return *this + str; +} + +template +BasicString BasicString::operator+(uint64_t i) const noexcept { + char str[65] = {}; + ox_itoa(i, str); + return *this + str; +} + +template +BasicString BasicString::operator+(const BasicString &src) const noexcept { + return *this + src.c_str(); +} + +template +bool BasicString::operator==(const BasicString &other) const noexcept { + bool retval = true; + std::size_t i = 0; + while (i < m_buff.size() && (m_buff[i] || other.m_buff[i])) { + if (m_buff[i] != other.m_buff[i]) { + retval = false; + break; + } + i++; + } + return retval; +} + +template +bool BasicString::operator!=(const BasicString &other) const noexcept { + return !operator==(other); +} + +template +char BasicString::operator[](std::size_t i) const noexcept { + return m_buff[i]; +} + +template +char &BasicString::operator[](std::size_t i) noexcept { + return m_buff[i]; +} + +template +BasicString BasicString::substr(std::size_t pos) const noexcept { + return m_buff.data() + pos; +} + +template +bool BasicString::endsWith(const char *ending) const noexcept { + const auto endingLen = ox_strlen(ending); + return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending) == 0; +} + +template +bool BasicString::endsWith(const BasicString &ending) const noexcept { + const auto endingLen = ending.len(); + return len() >= endingLen && ox_strcmp(data() + (len() - endingLen), ending.c_str()) == 0; +} + +template +std::size_t BasicString::bytes() const noexcept { + std::size_t i; + for (i = 0; i < m_buff.size() && m_buff[i]; i++); + return i + 1; // add one for null terminator +} + +template +constexpr std::size_t BasicString::len() const noexcept { std::size_t length = 0; - for (std::size_t i = 0; i < m_buff.size(); i++) { - uint8_t b = static_cast(m_buff[i]); + for (const auto c : m_buff) { + auto b = static_cast(c); if (b) { if ((b & 128) == 0) { // normal ASCII character - length++; + ++length; } else if ((b & (256 << 6)) == (256 << 6)) { // start of UTF-8 character - length++; + ++length; } } else { break; @@ -197,4 +399,6 @@ constexpr std::size_t String::len() const noexcept { return length; } +using String = BasicString<0>; + } diff --git a/deps/ox/src/ox/std/trace.hpp b/deps/ox/src/ox/std/trace.hpp index cf18e9a8..16bd1b68 100644 --- a/deps/ox/src/ox/std/trace.hpp +++ b/deps/ox/src/ox/std/trace.hpp @@ -15,6 +15,7 @@ #include "bstring.hpp" #include "fmt.hpp" #include "hashmap.hpp" +#include "string.hpp" #include "units.hpp" extern "C" { @@ -35,7 +36,7 @@ struct TraceMsg { int line = 0; uint64_t time = 0; const char *ch = ""; - BString msg; + BasicString<100> msg; }; template @@ -102,7 +103,53 @@ class OutStream { } template - constexpr OutStream &operator<<(const T &v) noexcept { + constexpr OutStream &operator<<(const typename enable_if>::type &v) noexcept { + if (m_msg.msg.len()) { + m_msg.msg += m_delimiter; + } + if constexpr(T(-1) < 0) { + m_msg.msg += static_cast(v); + } else { + m_msg.msg += static_cast(v); + } + return *this; + } + + constexpr OutStream &operator<<(char *v) noexcept { + if (m_msg.msg.len()) { + m_msg.msg += m_delimiter; + } + m_msg.msg += v; + return *this; + } + + constexpr OutStream &operator<<(const char *v) noexcept { + if (m_msg.msg.len()) { + m_msg.msg += m_delimiter; + } + m_msg.msg += v; + return *this; + } + + template + constexpr OutStream &operator<<(const BasicString &v) noexcept { + if (m_msg.msg.len()) { + m_msg.msg += m_delimiter; + } + m_msg.msg += v.c_str(); + return *this; + } + + template + constexpr OutStream &operator<<(const BString &v) noexcept { + if (m_msg.msg.len()) { + m_msg.msg += m_delimiter; + } + m_msg.msg += v.c_str(); + return *this; + } + + constexpr OutStream &operator<<(char v) noexcept { if (m_msg.msg.len()) { m_msg.msg += m_delimiter; } @@ -156,7 +203,7 @@ class NullStream { }; -#ifdef DEBUG +#if defined(DEBUG) && !defined(OX_BARE_METAL) using TraceStream = OutStream; #else using TraceStream = NullStream; diff --git a/deps/ox/src/ox/std/vector.hpp b/deps/ox/src/ox/std/vector.hpp index 37732886..f093d0a3 100644 --- a/deps/ox/src/ox/std/vector.hpp +++ b/deps/ox/src/ox/std/vector.hpp @@ -17,8 +17,61 @@ namespace ox { +namespace detail { + +template +struct SmallVector { + private: + AllocAlias m_data[Size] = {}; + + protected: + constexpr void initItems(T **items, std::size_t cap) noexcept { + if (cap <= Size) { + *items = bit_cast(m_data); + } else { + *items = bit_cast(new AllocAlias[cap]); + } + } + + constexpr void moveItemsFrom(T **items, SmallVector &src, const std::size_t count, const std::size_t cap) noexcept { + if (cap <= Size) { + const auto dstItems = bit_cast(m_data); + const auto srcItems = bit_cast(src.m_data); + for (auto i = 0u; i < count; ++i) { + dstItems[i] = move(srcItems[i]); + } + *items = bit_cast(m_data); + } + } + + constexpr void clearItems(AllocAlias *items) noexcept { + if (items != m_data) { + delete[] items; + } + } + +}; + template -class Vector { +struct SmallVector { + protected: + constexpr void initItems(T **items, std::size_t cap) noexcept { + *items = bit_cast(new AllocAlias[cap]); + } + + constexpr void moveItemsFrom(T**, SmallVector&, const std::size_t, const std::size_t) noexcept { + } + + constexpr void clearItems(AllocAlias *items) noexcept { + delete[] items; + } + +}; + +} + +template +class Vector: detail::SmallVector { public: using value_type = T; @@ -232,45 +285,46 @@ class Vector { }; -template -Vector::Vector(std::size_t size) noexcept { +template +Vector::Vector(std::size_t size) noexcept { m_size = size; m_cap = m_size; - m_items = bit_cast(new AllocAlias[m_cap]); - for (std::size_t i = 0; i < size; i++) { + this->initItems(&m_items, m_cap); + for (std::size_t i = 0; i < size; ++i) { m_items[i] = {}; } } -template -Vector::Vector(const Vector &other) { +template +Vector::Vector(const Vector &other) { m_size = other.m_size; m_cap = other.m_cap; - m_items = bit_cast(new AllocAlias[m_cap]); - for (std::size_t i = 0; i < m_size; i++) { + this->initItems(&m_items, other.m_cap); + for (std::size_t i = 0; i < m_size; ++i) { m_items[i] = move(other.m_items[i]); } } -template -Vector::Vector(Vector &&other) noexcept { +template +Vector::Vector(Vector &&other) noexcept { m_size = other.m_size; m_cap = other.m_cap; m_items = other.m_items; + this->moveItemsFrom(&m_items, other, m_size, m_cap); other.m_size = 0; other.m_cap = 0; other.m_items = nullptr; } -template -Vector::~Vector() { +template +Vector::~Vector() { clear(); - delete[] bit_cast*>(m_items); + this->clearItems(bit_cast*>(m_items)); m_items = nullptr; } -template -bool Vector::operator==(const Vector &other) const { +template +bool Vector::operator==(const Vector &other) const { if (m_size != other.m_size) { return false; } @@ -282,14 +336,15 @@ bool Vector::operator==(const Vector &other) const { return true; } -template -constexpr Vector &Vector::operator=(const Vector &other) { +template +constexpr Vector &Vector::operator=(const Vector &other) { if (this != &other) { clear(); - delete[] bit_cast*>(m_items); + this->clearItems(bit_cast*>(m_items)); + m_items = nullptr; m_size = other.m_size; m_cap = other.m_cap; - m_items = bit_cast(new AllocAlias[m_cap]); + this->initItems(&m_items, other.m_cap); for (std::size_t i = 0; i < m_size; i++) { m_items[i] = other.m_items[i]; } @@ -297,14 +352,15 @@ constexpr Vector &Vector::operator=(const Vector &other) { return *this; } -template -constexpr Vector &Vector::operator=(Vector &&other) noexcept { +template +constexpr Vector &Vector::operator=(Vector &&other) noexcept { if (this != &other) { clear(); - delete[] bit_cast*>(m_items); + this->clearItems(bit_cast*>(m_items)); m_size = other.m_size; m_cap = other.m_cap; m_items = other.m_items; + this->moveItemsFrom(&m_items, other, m_size, m_cap); other.m_size = 0; other.m_cap = 0; other.m_items = nullptr; @@ -312,18 +368,18 @@ constexpr Vector &Vector::operator=(Vector &&other) noexcept { return *this; } -template -constexpr T &Vector::operator[](std::size_t i) noexcept { +template +constexpr T &Vector::operator[](std::size_t i) noexcept { return m_items[i]; } -template -constexpr const T &Vector::operator[](std::size_t i) const noexcept { +template +constexpr const T &Vector::operator[](std::size_t i) const noexcept { return m_items[i]; } -template -Result Vector::front() noexcept { +template +Result Vector::front() noexcept { if (!m_size) { AllocAlias v; return {*bit_cast(&v), OxError(1)}; @@ -331,8 +387,8 @@ Result Vector::front() noexcept { return m_items[0]; } -template -Result Vector::front() const noexcept { +template +Result Vector::front() const noexcept { if (!m_size) { AllocAlias v; return {*bit_cast(&v), OxError(1)}; @@ -340,8 +396,8 @@ Result Vector::front() const noexcept { return m_items[0]; } -template -Result Vector::back() noexcept { +template +Result Vector::back() noexcept { if (!m_size) { AllocAlias v; return {*bit_cast(&v), OxError(1)}; @@ -349,8 +405,8 @@ Result Vector::back() noexcept { return m_items[m_size - 1]; } -template -Result Vector::back() const noexcept { +template +Result Vector::back() const noexcept { if (!m_size) { AllocAlias v; return {*bit_cast(&v), OxError(1)}; @@ -358,18 +414,18 @@ Result Vector::back() const noexcept { return m_items[m_size - 1]; } -template -constexpr std::size_t Vector::size() const noexcept { +template +constexpr std::size_t Vector::size() const noexcept { return m_size; } -template -bool Vector::empty() const noexcept { +template +bool Vector::empty() const noexcept { return !m_size; } -template -void Vector::clear() { +template +void Vector::clear() { if constexpr(is_class()) { for (std::size_t i = 0; i < m_size; ++i) { m_items[i].~T(); @@ -378,8 +434,8 @@ void Vector::clear() { m_size = 0; } -template -constexpr void Vector::resize(std::size_t size) { +template +constexpr void Vector::resize(std::size_t size) { if (m_cap < size) { expandCap(size); } @@ -395,8 +451,8 @@ constexpr void Vector::resize(std::size_t size) { m_size = size; } -template -bool Vector::contains(const T &v) const { +template +bool Vector::contains(const T &v) const { for (std::size_t i = 0; i < m_size; i++) { if (m_items[i] == v) { return true; @@ -405,8 +461,8 @@ bool Vector::contains(const T &v) const { return false; } -template -void Vector::insert(std::size_t pos, const T &val) { +template +void Vector::insert(std::size_t pos, const T &val) { // TODO: insert should ideally have its own expandCap if (m_size == m_cap) { expandCap(m_cap ? m_cap * 2 : 100); @@ -418,9 +474,9 @@ void Vector::insert(std::size_t pos, const T &val) { ++m_size; } -template +template template -void Vector::emplace_back(Args&&... args) { +void Vector::emplace_back(Args&&... args) { if (m_size == m_cap) { expandCap(m_cap ? m_cap * 2 : 100); } @@ -428,8 +484,8 @@ void Vector::emplace_back(Args&&... args) { ++m_size; } -template -void Vector::push_back(const T &item) { +template +void Vector::push_back(const T &item) { if (m_size == m_cap) { expandCap(m_cap ? m_cap * 2 : 100); } @@ -437,14 +493,14 @@ void Vector::push_back(const T &item) { ++m_size; } -template -void Vector::pop_back() { +template +void Vector::pop_back() { --m_size; m_items[m_size].~T(); } -template -Error Vector::erase(std::size_t pos) { +template +Error Vector::erase(std::size_t pos) { if (pos >= m_size) { return OxError(1); } @@ -455,21 +511,21 @@ Error Vector::erase(std::size_t pos) { return OxError(0); } -template -Error Vector::unordered_erase(std::size_t pos) { +template +Error Vector::unordered_erase(std::size_t pos) { if (pos >= m_size) { return OxError(1); } - m_size--; + --m_size; m_items[pos] = move(m_items[m_size]); return OxError(0); } -template -void Vector::expandCap(std::size_t cap) { +template +void Vector::expandCap(std::size_t cap) { auto oldItems = m_items; m_cap = cap; - m_items = bit_cast(new AllocAlias[m_cap]); + this->initItems(&m_items, cap); if (oldItems) { // move over old items const auto itRange = cap > m_size ? m_size : cap; for (std::size_t i = 0; i < itRange; i++) { @@ -478,7 +534,7 @@ void Vector::expandCap(std::size_t cap) { for (std::size_t i = itRange; i < m_cap; i++) { new (&m_items[i]) T; } - delete[] bit_cast*>(oldItems); + this->clearItems(bit_cast*>(oldItems)); } }