From a84a829769cb4d4211fff1cbc97955c0fde05860 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 23 Nov 2023 01:34:16 -0600 Subject: [PATCH] [ox] Cleanup String/StringView conversions, MallocaPtr --- deps/ox/src/ox/clargs/clargs.cpp | 12 ++++++------ deps/ox/src/ox/clargs/clargs.hpp | 12 ++++++------ deps/ox/src/ox/event/signal.hpp | 10 +++++----- deps/ox/src/ox/fs/CMakeLists.txt | 1 - deps/ox/src/ox/fs/filesystem/directory.hpp | 2 +- deps/ox/src/ox/fs/test/CMakeLists.txt | 1 - deps/ox/src/ox/logconn/logconn.cpp | 2 +- deps/ox/src/ox/mc/read.hpp | 2 +- deps/ox/src/ox/mc/write.hpp | 2 +- deps/ox/src/ox/model/descwrite.hpp | 15 ++++++++------- deps/ox/src/ox/model/typestore.hpp | 4 ++-- deps/ox/src/ox/std/error.hpp | 12 +++++++++--- deps/ox/src/ox/std/new.hpp | 15 ++++++++++----- deps/ox/src/ox/std/span.hpp | 2 -- deps/ox/src/ox/std/string.hpp | 17 ++++++++++++++--- deps/ox/src/ox/std/trace.hpp | 2 +- deps/ox/src/ox/std/uuid.hpp | 3 +-- 17 files changed, 66 insertions(+), 48 deletions(-) diff --git a/deps/ox/src/ox/clargs/clargs.cpp b/deps/ox/src/ox/clargs/clargs.cpp index 348a9897..1fa38c32 100644 --- a/deps/ox/src/ox/clargs/clargs.cpp +++ b/deps/ox/src/ox/clargs/clargs.cpp @@ -37,32 +37,32 @@ ClArgs::ClArgs(int argc, const char **args) noexcept { } } -bool ClArgs::getBool(ox::CRStringView arg, bool defaultValue) const noexcept { +bool ClArgs::getBool(ox::CRString arg, bool defaultValue) const noexcept { auto [value, err] = m_ints.at(arg); return !err ? *value : defaultValue; } -String ClArgs::getString(ox::CRStringView arg, const char *defaultValue) const noexcept { +String ClArgs::getString(ox::CRString arg, const char *defaultValue) const noexcept { auto [value, err] = m_strings.at(arg); return !err ? *value : defaultValue; } -int ClArgs::getInt(ox::CRStringView arg, int defaultValue) const noexcept { +int ClArgs::getInt(ox::CRString arg, int defaultValue) const noexcept { auto [value, err] = m_ints.at(arg); return !err ? *value : defaultValue; } -Result ClArgs::getBool(ox::CRStringView arg) const noexcept { +Result ClArgs::getBool(ox::CRString arg) const noexcept { oxRequire(out, m_bools.at(arg)); return *out; } -Result ClArgs::getString(ox::CRStringView argName) const noexcept { +Result ClArgs::getString(ox::CRString argName) const noexcept { oxRequire(out, m_strings.at(argName)); return *out; } -Result ClArgs::getInt(ox::CRStringView arg) const noexcept { +Result ClArgs::getInt(ox::CRString arg) const noexcept { oxRequire(out, m_ints.at(arg)); return *out; } diff --git a/deps/ox/src/ox/clargs/clargs.hpp b/deps/ox/src/ox/clargs/clargs.hpp index 02943564..fa12b475 100644 --- a/deps/ox/src/ox/clargs/clargs.hpp +++ b/deps/ox/src/ox/clargs/clargs.hpp @@ -23,21 +23,21 @@ class ClArgs { ClArgs(int argc, const char **args) noexcept; [[nodiscard]] - bool getBool(ox::CRStringView arg, bool defaultValue) const noexcept; + bool getBool(ox::CRString arg, bool defaultValue) const noexcept; [[nodiscard]] - String getString(ox::CRStringView argName, const char *defaultValue) const noexcept; + String getString(ox::CRString argName, const char *defaultValue) const noexcept; [[nodiscard]] - int getInt(ox::CRStringView arg, int defaultValue) const noexcept; + int getInt(ox::CRString arg, int defaultValue) const noexcept; [[nodiscard]] - Result getBool(ox::CRStringView arg) const noexcept; + Result getBool(ox::CRString arg) const noexcept; [[nodiscard]] - Result getString(ox::CRStringView argName) const noexcept; + Result getString(ox::CRString argName) const noexcept; - Result getInt(ox::CRStringView arg) const noexcept; + Result getInt(ox::CRString arg) const noexcept; }; diff --git a/deps/ox/src/ox/event/signal.hpp b/deps/ox/src/ox/event/signal.hpp index b4adbbff..99828b20 100644 --- a/deps/ox/src/ox/event/signal.hpp +++ b/deps/ox/src/ox/event/signal.hpp @@ -245,7 +245,7 @@ class Signal { } Error call(Args... args) noexcept final { - return f(args...); + return f(ox::forward(args)...); } }; @@ -260,7 +260,7 @@ class Signal { } Error call(Args... args) noexcept final { - return (m_receiver->*(m_methodPtr))(args...); + return (m_receiver->*(m_methodPtr))(ox::forward(args)...); } void cleanup(Signal *signal) noexcept final { @@ -286,7 +286,7 @@ class Signal { } Error call(Args... args) noexcept final { - return (m_receiver->*(m_methodPtr))(args...); + return (m_receiver->*(m_methodPtr))(ox::forward(args)...); } void cleanup(Signal*) noexcept final { @@ -391,14 +391,14 @@ Error Signal::disconnectObject(const void *receiver) const noexc template void Signal::emit(Args... args) const noexcept { for (auto &f : m_slots) { - oxIgnoreError(f->call(args...)); + oxIgnoreError(f->call(ox::forward(args)...)); } } template Error Signal::emitCheckError(Args... args) const noexcept { for (auto &f : m_slots) { - oxReturnError(f->call(args...)); + oxReturnError(f->call(ox::forward(args)...)); } return OxError(0); } diff --git a/deps/ox/src/ox/fs/CMakeLists.txt b/deps/ox/src/ox/fs/CMakeLists.txt index 54a8a62c..a8d1e823 100644 --- a/deps/ox/src/ox/fs/CMakeLists.txt +++ b/deps/ox/src/ox/fs/CMakeLists.txt @@ -44,7 +44,6 @@ if(NOT OX_BARE_METAL) target_link_libraries( oxfs-tool OxFS - OxStd ) install( diff --git a/deps/ox/src/ox/fs/filesystem/directory.hpp b/deps/ox/src/ox/fs/filesystem/directory.hpp index 1e5aa185..703db4a4 100644 --- a/deps/ox/src/ox/fs/filesystem/directory.hpp +++ b/deps/ox/src/ox/fs/filesystem/directory.hpp @@ -252,7 +252,7 @@ Error Directory::write(PathIterator path, uint64_t inode64 oxTracef("ox::fs::Directory::write", "Attempting to write Directory entry: {}", name->data()); oxReturnError(val->init(inode, name->data(), val.size())); - return m_fs.write(m_inodeId, cpy, cpy->size(), static_cast(FileType::Directory)); + return m_fs.write(m_inodeId, cpy.get(), cpy->size(), static_cast(FileType::Directory)); } } diff --git a/deps/ox/src/ox/fs/test/CMakeLists.txt b/deps/ox/src/ox/fs/test/CMakeLists.txt index 6b5bb14d..d2ecc00f 100644 --- a/deps/ox/src/ox/fs/test/CMakeLists.txt +++ b/deps/ox/src/ox/fs/test/CMakeLists.txt @@ -6,7 +6,6 @@ add_executable( target_link_libraries( FSTests OxFS - OxStd OxMetalClaw ) diff --git a/deps/ox/src/ox/logconn/logconn.cpp b/deps/ox/src/ox/logconn/logconn.cpp index 7c64ea6f..b02ca82f 100644 --- a/deps/ox/src/ox/logconn/logconn.cpp +++ b/deps/ox/src/ox/logconn/logconn.cpp @@ -58,7 +58,7 @@ ox::Error LoggerConn::initConn(ox::CRStringView appName) noexcept { addr.sin_port = htons(5590); m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); oxReturnError(OxError(static_cast(connect(m_socket, reinterpret_cast(&addr), sizeof(addr))))); - return sendInit({.appName = appName}); + return sendInit({.appName = ox::BasicString<128>(appName)}); } ox::Error LoggerConn::send(const char *buff, std::size_t len) const noexcept { diff --git a/deps/ox/src/ox/mc/read.hpp b/deps/ox/src/ox/mc/read.hpp index f6252e7a..ab3c82fd 100644 --- a/deps/ox/src/ox/mc/read.hpp +++ b/deps/ox/src/ox/mc/read.hpp @@ -254,7 +254,7 @@ constexpr Error MetalClawReaderTemplate::field(const char*, HashMap::field(const char*, const HashMap ox::Error { const auto keyLen = ox_strlen(key); auto wkey = ox_malloca(keyLen + 1, char, 0); - memcpy(wkey, key.c_str(), keyLen + 1); + memcpy(wkey.get(), key.c_str(), keyLen + 1); oxReturnError(handler.fieldCString("", wkey.get(), keyLen)); oxRequireM(value, val.at(key)); return handler.field("", value); diff --git a/deps/ox/src/ox/model/descwrite.hpp b/deps/ox/src/ox/model/descwrite.hpp index 7ed0ddc4..61f7ea98 100644 --- a/deps/ox/src/ox/model/descwrite.hpp +++ b/deps/ox/src/ox/model/descwrite.hpp @@ -175,8 +175,9 @@ class TypeDescWriter { constexpr TypeDescWriter::TypeDescWriter(TypeStore *typeStore) noexcept: m_typeStore(typeStore) {} template -constexpr ox::Error TypeDescWriter::setTypeInfo(CRStringView typeName, int typeVersion, - const TypeParamPack &typeParams, std::size_t) noexcept { +constexpr ox::Error TypeDescWriter::setTypeInfo( + CRStringView typeName, int typeVersion, + const TypeParamPack &typeParams, std::size_t) noexcept { PrimitiveType pt; if constexpr(is_union_v) { pt = PrimitiveType::Union; @@ -197,7 +198,7 @@ constexpr Error TypeDescWriter::field(CRStringView name, const T*, std::size_t, constexpr typename remove_pointer::type *p = nullptr; const auto t = type(p); oxAssert(t != nullptr, "field(const char *name, T *val, std::size_t): Type not found or generated"); - m_type->fieldList.emplace_back(t, name, detail::indirectionLevels_v + 1, subscriptStack, buildTypeId(*t)); + m_type->fieldList.emplace_back(t, String(name), detail::indirectionLevels_v + 1, subscriptStack, buildTypeId(*t)); return OxError(0); } return OxError(1); @@ -208,7 +209,7 @@ constexpr Error TypeDescWriter::field(CRStringView name, UnionView val if (m_type) { const auto t = type(val); oxAssert(t != nullptr, "field(const char *name, T val): Type not found or generated"); - m_type->fieldList.emplace_back(t, name, 0, SubscriptStack{}, t->typeName); + m_type->fieldList.emplace_back(t, String(name), 0, SubscriptStack{}, t->typeName); return OxError(0); } return OxError(1); @@ -226,7 +227,7 @@ constexpr Error TypeDescWriter::field(CRStringView name, const T *val) noexcept } else { const auto t = type(val); oxAssert(t != nullptr, "field(const char *name, T *val): Type not found or generated"); - m_type->fieldList.emplace_back(t, name, 0, SubscriptStack{}, buildTypeId(*t)); + m_type->fieldList.emplace_back(t, String(name), 0, SubscriptStack{}, buildTypeId(*t)); return {}; } } @@ -237,7 +238,7 @@ template constexpr Error TypeDescWriter::fieldCString(CRStringView name, Args&&...) noexcept { constexpr auto s = ""; const auto t = type(s); - m_type->fieldList.emplace_back(t, name, 0, SubscriptStack{}, t->typeName); + m_type->fieldList.emplace_back(t, String(name), 0, SubscriptStack{}, t->typeName); return {}; } @@ -344,7 +345,7 @@ constexpr const DescriptorType *TypeDescWriter::getType(CRStringView tn, int typ oxAssert(type != nullptr, "TypeDescWriter::getType returning null DescriptorType"); return type; } else { - auto dt = ox::make_unique(tn, typeVersion, pt, typeParams); + auto dt = ox::make_unique(String(tn), typeVersion, pt, typeParams); dt->length = b; const auto out = dt.get(); const auto typeId = buildTypeId(tn, typeVersion, typeParams); diff --git a/deps/ox/src/ox/model/typestore.hpp b/deps/ox/src/ox/model/typestore.hpp index 6bb404eb..c8402708 100644 --- a/deps/ox/src/ox/model/typestore.hpp +++ b/deps/ox/src/ox/model/typestore.hpp @@ -44,11 +44,11 @@ class TypeStore { return out->get(); } - constexpr DescriptorType *getInit(const auto &typeName, int typeVersion, PrimitiveType pt, + constexpr DescriptorType *getInit(CRStringView typeName, int typeVersion, PrimitiveType pt, const TypeParamPack &typeParams) noexcept { const auto typeId = buildTypeId(typeName, typeVersion, typeParams); auto &out = m_cache[typeId]; - out = ox::make_unique(typeName, typeVersion, pt, typeParams); + out = ox::make_unique(String(typeName), typeVersion, pt, typeParams); return out.get(); } diff --git a/deps/ox/src/ox/std/error.hpp b/deps/ox/src/ox/std/error.hpp index 66efd262..5f4ba25a 100644 --- a/deps/ox/src/ox/std/error.hpp +++ b/deps/ox/src/ox/std/error.hpp @@ -25,8 +25,6 @@ class exception { #endif #include "def.hpp" -#include "defines.hpp" -#include "strongint.hpp" #include "typetraits.hpp" #include "utility.hpp" @@ -191,6 +189,13 @@ struct [[nodiscard]] Result { return value; } + constexpr auto &unwrapThrow() { + if (error) { + throw ox::Exception(error); + } + return value; + } + constexpr const auto &unwrap() const noexcept { if (error) [[unlikely]] { oxPanic(error, "Failed unwrap"); @@ -198,7 +203,8 @@ struct [[nodiscard]] Result { return value; } - constexpr ox::Result to(const auto &f) noexcept { + template + constexpr ox::Result to(const auto &f) noexcept { if (error) [[unlikely]] { return OxError(1); } diff --git a/deps/ox/src/ox/std/new.hpp b/deps/ox/src/ox/std/new.hpp index a2435b8e..ac3ed318 100644 --- a/deps/ox/src/ox/std/new.hpp +++ b/deps/ox/src/ox/std/new.hpp @@ -115,11 +115,11 @@ class MallocaPtr { return reinterpret_cast(m_val); } - constexpr const T &operator=(MallocaPtr &other) = delete; + constexpr MallocaPtr &operator=(MallocaPtr &other) = delete; - constexpr const T &operator=(const MallocaPtr &other) = delete; + constexpr MallocaPtr &operator=(const MallocaPtr &other) = delete; - constexpr const T &operator=(MallocaPtr &&other) noexcept { + constexpr MallocaPtr &operator=(MallocaPtr &&other) noexcept { if (m_onHeap && m_val) { delete[] reinterpret_cast(m_val); } @@ -127,6 +127,7 @@ class MallocaPtr { m_val = other.m_val; other.m_onHeap = false; other.m_val = nullptr; + return *this; } constexpr const T *operator->() const noexcept { @@ -137,11 +138,11 @@ class MallocaPtr { return reinterpret_cast(m_val); } - constexpr operator const T*() const noexcept { + constexpr explicit operator const T*() const noexcept { return reinterpret_cast(m_val); } - constexpr operator T*() noexcept { + constexpr explicit operator T*() noexcept { return reinterpret_cast(m_val); } @@ -153,6 +154,10 @@ class MallocaPtr { return *reinterpret_cast(m_val); } + constexpr bool operator==(std::nullptr_t) const noexcept { + return m_val == nullptr; + } + constexpr bool operator==(const MallocaPtr &other) const noexcept { return m_val == other.m_val && m_onHeap == other.m_onHeap; } diff --git a/deps/ox/src/ox/std/span.hpp b/deps/ox/src/ox/std/span.hpp index 397849c5..c6e2ffd2 100644 --- a/deps/ox/src/ox/std/span.hpp +++ b/deps/ox/src/ox/std/span.hpp @@ -10,8 +10,6 @@ #include "bit.hpp" #include "iterator.hpp" -#include "typetraits.hpp" -#include "types.hpp" #include "vector.hpp" namespace ox { diff --git a/deps/ox/src/ox/std/string.hpp b/deps/ox/src/ox/std/string.hpp index fee31ea2..de1f0b1d 100644 --- a/deps/ox/src/ox/std/string.hpp +++ b/deps/ox/src/ox/std/string.hpp @@ -13,12 +13,10 @@ #endif #include "algorithm.hpp" -#include "buffer.hpp" #include "memops.hpp" #include "serialize.hpp" #include "stringview.hpp" #include "strops.hpp" -#include "typetraits.hpp" #include "vector.hpp" namespace ox { @@ -40,7 +38,7 @@ class BasicString { constexpr BasicString(const char *str, std::size_t size) noexcept; - constexpr BasicString(CRStringView str) noexcept; + constexpr explicit BasicString(CRStringView str) noexcept; constexpr BasicString(const BasicString&) noexcept; @@ -140,6 +138,8 @@ class BasicString { constexpr BasicString operator+(Integer_c auto i) const noexcept; + constexpr BasicString operator+(CRStringView src) const noexcept; + constexpr BasicString operator+(const BasicString &src) const noexcept; constexpr bool operator==(const char *other) const noexcept; @@ -414,6 +414,17 @@ constexpr BasicString BasicString::operato return *this + str; } +template +constexpr BasicString BasicString::operator+(CRStringView src) const noexcept { + const std::size_t strLen = src.len(); + 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], src.data(), strLen + 1); + return cpy; +} + template constexpr BasicString BasicString::operator+(const BasicString &src) const noexcept { const std::size_t strLen = src.len(); diff --git a/deps/ox/src/ox/std/trace.hpp b/deps/ox/src/ox/std/trace.hpp index 13a3e66e..e3ccbee6 100644 --- a/deps/ox/src/ox/std/trace.hpp +++ b/deps/ox/src/ox/std/trace.hpp @@ -131,7 +131,7 @@ class OutStream { m_msg.file = file; m_msg.line = line; m_msg.ch = ch; - m_msg.msg = msg; + m_msg.msg = BasicString<100>(msg); } constexpr OutStream(const char *file, int line, const char *ch, const char *msg = "") noexcept { diff --git a/deps/ox/src/ox/std/uuid.hpp b/deps/ox/src/ox/std/uuid.hpp index 707aa341..96c74c94 100644 --- a/deps/ox/src/ox/std/uuid.hpp +++ b/deps/ox/src/ox/std/uuid.hpp @@ -10,11 +10,10 @@ #include "array.hpp" #include "bstring.hpp" +#include "buffer.hpp" #include "random.hpp" #include "ranges.hpp" #include "stringview.hpp" -#include "strops.hpp" -#include "trace.hpp" namespace ox {