[ox] Correct bad bit_cast uses and improve constexpr-ness

This commit is contained in:
2021-11-28 21:03:29 -06:00
parent 22f08f83c5
commit 1f24912ddd
35 changed files with 247 additions and 214 deletions

View File

@@ -12,11 +12,6 @@
namespace ox {
FileAddress::FileAddress() noexcept {
m_data.inode = 0;
m_type = FileAddressType::Inode;
}
FileAddress::FileAddress(const FileAddress &other) noexcept {
operator=(other);
}
@@ -50,6 +45,9 @@ FileAddress::~FileAddress() noexcept {
}
FileAddress &FileAddress::operator=(const FileAddress &other) noexcept {
if (this == &other) {
return *this;
}
cleanup();
m_type = other.m_type;
switch (m_type) {
@@ -71,9 +69,26 @@ FileAddress &FileAddress::operator=(const FileAddress &other) noexcept {
}
FileAddress &FileAddress::operator=(FileAddress &&other) noexcept {
if (this == &other) {
return *this;
}
cleanup();
m_type = other.m_type;
memcpy(this, &other, sizeof(*this));
switch (m_type) {
case FileAddressType::Path:
{
auto strSize = ox_strlen(other.m_data.path) + 1;
m_data.path = new char[strSize];
ox_memcpy(m_data.path, other.m_data.path, strSize);
break;
}
case FileAddressType::ConstPath:
case FileAddressType::Inode:
m_data = other.m_data;
break;
case FileAddressType::None:
break;
}
other.clear();
return *this;
}
@@ -86,10 +101,8 @@ void FileAddress::cleanup() noexcept {
}
void FileAddress::clear() noexcept {
if (m_type == FileAddressType::Path) {
m_data.path = nullptr;
m_type = FileAddressType::None;
}
m_data.path = nullptr;
m_type = FileAddressType::None;
}
}

View File

@@ -9,6 +9,7 @@
#pragma once
#include <ox/std/std.hpp>
#include <ox/model/typenamecatcher.hpp>
#include <ox/model/types.hpp>
namespace ox {
@@ -32,7 +33,7 @@ class FileAddress {
union Data {
static constexpr auto TypeName = "net.drinkingtea.ox.FileAddress.Data";
static constexpr auto Fields = 3;
char *path;
char *path = nullptr;
const char *constPath;
uint64_t inode;
};
@@ -42,7 +43,10 @@ class FileAddress {
Data m_data;
public:
FileAddress() noexcept;
constexpr FileAddress() noexcept {
m_data.inode = 0;
m_type = FileAddressType::Inode;
}
FileAddress(const FileAddress &other) noexcept;
@@ -93,7 +97,7 @@ class FileAddress {
}
}
constexpr operator bool() const noexcept {
explicit constexpr operator bool() const noexcept {
return m_type != FileAddressType::None;
}
@@ -110,6 +114,16 @@ class FileAddress {
};
template<>
constexpr const char *getModelTypeName<FileAddress::Data>() noexcept {
return FileAddress::Data::TypeName;
}
template<>
constexpr const char *getModelTypeName<FileAddress>() noexcept {
return FileAddress::TypeName;
}
template<typename T>
constexpr Error model(T *io, FileAddress::Data *obj) noexcept {
io->template setTypeInfo<FileAddress::Data>();
@@ -122,7 +136,9 @@ constexpr Error model(T *io, FileAddress::Data *obj) noexcept {
template<typename T>
constexpr Error model(T *io, FileAddress *fa) noexcept {
io->template setTypeInfo<FileAddress>();
oxReturnError(io->field("type", bit_cast<int8_t*>(&fa->m_type)));
auto type = static_cast<int8_t>(fa->m_type);
oxReturnError(io->field("type", &type));
fa->m_type = static_cast<FileAddressType>(type);
oxReturnError(io->field("data", UnionView(&fa->m_data, static_cast<int>(fa->m_type))));
return OxError(0);
}

View File

@@ -269,7 +269,7 @@ Result<const char*> FileSystemTemplate<FileStore, Directory>::directAccess(uint6
if (!data.valid()) {
return OxError(1);
}
return bit_cast<char*>(data.get());
return reinterpret_cast<char*>(data.get());
}
template<typename FileStore, typename Directory>

View File

@@ -101,7 +101,7 @@ Result<Vector<String>> PassThroughFS::ls(const char *dir) noexcept {
oxReturnError(OxError(ec.value(), "PassThroughFS: ls failed"));
for (const auto &p : di) {
const auto u8p = p.path().filename().u8string();
out.emplace_back(bit_cast<const char*>(u8p.c_str()));
out.emplace_back(reinterpret_cast<const char*>(u8p.c_str()));
}
return ox::move(out);
}

View File

@@ -95,8 +95,7 @@ Error PassThroughFS::ls(const char *dir, F cb) noexcept {
const auto di = std::filesystem::directory_iterator(m_path / stripSlash(dir), ec);
oxReturnError(OxError(ec.value(), "PassThroughFS: ls failed"));
for (auto &p : di) {
const auto u8p = p.path().filename().u8string();
oxReturnError(cb(bit_cast<const char*>(u8p.c_str()), 0));
oxReturnError(cb(p.path().filename().c_str(), 0));
}
return OxError(0);
}