[ox] Cleanup String/StringView conversions, MallocaPtr

This commit is contained in:
Gary Talent 2023-11-23 01:34:16 -06:00
parent 5b91ad25c2
commit a84a829769
17 changed files with 66 additions and 48 deletions

View File

@ -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<bool> ClArgs::getBool(ox::CRStringView arg) const noexcept {
Result<bool> ClArgs::getBool(ox::CRString arg) const noexcept {
oxRequire(out, m_bools.at(arg));
return *out;
}
Result<String> ClArgs::getString(ox::CRStringView argName) const noexcept {
Result<String> ClArgs::getString(ox::CRString argName) const noexcept {
oxRequire(out, m_strings.at(argName));
return *out;
}
Result<int> ClArgs::getInt(ox::CRStringView arg) const noexcept {
Result<int> ClArgs::getInt(ox::CRString arg) const noexcept {
oxRequire(out, m_ints.at(arg));
return *out;
}

View File

@ -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<bool> getBool(ox::CRStringView arg) const noexcept;
Result<bool> getBool(ox::CRString arg) const noexcept;
[[nodiscard]]
Result<String> getString(ox::CRStringView argName) const noexcept;
Result<String> getString(ox::CRString argName) const noexcept;
Result<int> getInt(ox::CRStringView arg) const noexcept;
Result<int> getInt(ox::CRString arg) const noexcept;
};

View File

@ -245,7 +245,7 @@ class Signal<Error(Args...)> {
}
Error call(Args... args) noexcept final {
return f(args...);
return f(ox::forward<Args>(args)...);
}
};
@ -260,7 +260,7 @@ class Signal<Error(Args...)> {
}
Error call(Args... args) noexcept final {
return (m_receiver->*(m_methodPtr))(args...);
return (m_receiver->*(m_methodPtr))(ox::forward<Args>(args)...);
}
void cleanup(Signal *signal) noexcept final {
@ -286,7 +286,7 @@ class Signal<Error(Args...)> {
}
Error call(Args... args) noexcept final {
return (m_receiver->*(m_methodPtr))(args...);
return (m_receiver->*(m_methodPtr))(ox::forward<Args>(args)...);
}
void cleanup(Signal*) noexcept final {
@ -391,14 +391,14 @@ Error Signal<Error(Args...)>::disconnectObject(const void *receiver) const noexc
template<class... Args>
void Signal<Error(Args...)>::emit(Args... args) const noexcept {
for (auto &f : m_slots) {
oxIgnoreError(f->call(args...));
oxIgnoreError(f->call(ox::forward<Args>(args)...));
}
}
template<class... Args>
Error Signal<Error(Args...)>::emitCheckError(Args... args) const noexcept {
for (auto &f : m_slots) {
oxReturnError(f->call(args...));
oxReturnError(f->call(ox::forward<Args>(args)...));
}
return OxError(0);
}

View File

@ -44,7 +44,6 @@ if(NOT OX_BARE_METAL)
target_link_libraries(
oxfs-tool
OxFS
OxStd
)
install(

View File

@ -252,7 +252,7 @@ Error Directory<FileStore, InodeId_t>::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<uint8_t>(FileType::Directory));
return m_fs.write(m_inodeId, cpy.get(), cpy->size(), static_cast<uint8_t>(FileType::Directory));
}
}

View File

@ -6,7 +6,6 @@ add_executable(
target_link_libraries(
FSTests
OxFS
OxStd
OxMetalClaw
)

View File

@ -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<ox::ErrorCode>(connect(m_socket, reinterpret_cast<sockaddr*>(&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 {

View File

@ -254,7 +254,7 @@ constexpr Error MetalClawReaderTemplate<Reader>::field(const char*, HashMap<Stri
auto wkey = ox_malloca(keyLen + 1, char, 0);
auto wkeyPtr = wkey.get();
oxReturnError(handler.fieldCString("", &wkeyPtr, keyLen + 1));
return handler.field("", &val[wkey.get()]);
return handler.field("", &val[wkeyPtr]);
};
for (std::size_t i = 0; i < len; ++i) {
oxReturnError(loopBody(handler, *val));

View File

@ -363,7 +363,7 @@ constexpr Error MetalClawWriter<Writer>::field(const char*, const HashMap<String
constexpr auto loopBody = [](auto &handler, auto const&key, auto const&val) -> 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);

View File

@ -175,8 +175,9 @@ class TypeDescWriter {
constexpr TypeDescWriter::TypeDescWriter(TypeStore *typeStore) noexcept: m_typeStore(typeStore) {}
template<typename T>
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<T>) {
pt = PrimitiveType::Union;
@ -197,7 +198,7 @@ constexpr Error TypeDescWriter::field(CRStringView name, const T*, std::size_t,
constexpr typename remove_pointer<T>::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<T> + 1, subscriptStack, buildTypeId(*t));
m_type->fieldList.emplace_back(t, String(name), detail::indirectionLevels_v<T> + 1, subscriptStack, buildTypeId(*t));
return OxError(0);
}
return OxError(1);
@ -208,7 +209,7 @@ constexpr Error TypeDescWriter::field(CRStringView name, UnionView<T, force> 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<typename ...Args>
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<DescriptorType>(tn, typeVersion, pt, typeParams);
auto dt = ox::make_unique<DescriptorType>(String(tn), typeVersion, pt, typeParams);
dt->length = b;
const auto out = dt.get();
const auto typeId = buildTypeId(tn, typeVersion, typeParams);

View File

@ -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<DescriptorType>(typeName, typeVersion, pt, typeParams);
out = ox::make_unique<DescriptorType>(String(typeName), typeVersion, pt, typeParams);
return out.get();
}

View File

@ -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<T> to(const auto &f) noexcept {
template<typename U = T>
constexpr ox::Result<U> to(const auto &f) noexcept {
if (error) [[unlikely]] {
return OxError(1);
}

View File

@ -115,11 +115,11 @@ class MallocaPtr {
return reinterpret_cast<T*>(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<uint8_t*>(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<T*>(m_val);
}
constexpr operator const T*() const noexcept {
constexpr explicit operator const T*() const noexcept {
return reinterpret_cast<T*>(m_val);
}
constexpr operator T*() noexcept {
constexpr explicit operator T*() noexcept {
return reinterpret_cast<T*>(m_val);
}
@ -153,6 +154,10 @@ class MallocaPtr {
return *reinterpret_cast<T*>(m_val);
}
constexpr bool operator==(std::nullptr_t) const noexcept {
return m_val == nullptr;
}
constexpr bool operator==(const MallocaPtr<T> &other) const noexcept {
return m_val == other.m_val && m_onHeap == other.m_onHeap;
}

View File

@ -10,8 +10,6 @@
#include "bit.hpp"
#include "iterator.hpp"
#include "typetraits.hpp"
#include "types.hpp"
#include "vector.hpp"
namespace ox {

View File

@ -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<SmallStringSize_v> BasicString<SmallStringSize_v>::operato
return *this + str;
}
template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::operator+(CRStringView src) const noexcept {
const std::size_t strLen = src.len();
const auto currentLen = len();
BasicString<SmallStringSize_v> 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<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::operator+(const BasicString &src) const noexcept {
const std::size_t strLen = src.len();

View File

@ -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 {

View File

@ -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 {