[ox] Make Result copyTo and moveTo take refs

This commit is contained in:
Gary Talent 2023-12-14 22:26:17 -06:00
parent 935099f8d4
commit d31938ba4f
5 changed files with 27 additions and 11 deletions

View File

@ -58,7 +58,7 @@ class TypeStore {
if (!std::is_constant_evaluated()) { if (!std::is_constant_evaluated()) {
oxRequireM(dt, loadDescriptor(typeId)); oxRequireM(dt, loadDescriptor(typeId));
for (auto &f : dt->fieldList) { for (auto &f : dt->fieldList) {
oxReturnError(this->getLoad(f.typeId).moveTo(&f.type)); oxReturnError(this->getLoad(f.typeId).moveTo(f.type));
} }
auto &out = m_cache[typeId]; auto &out = m_cache[typeId];
out = std::move(dt); out = std::move(dt);

View File

@ -265,7 +265,7 @@ Error OrganicClawReader::fieldCString(const char *key, char **val, std::size_t b
Error OrganicClawReader::field(const char *key, UUID *val) noexcept { Error OrganicClawReader::field(const char *key, UUID *val) noexcept {
UUIDStr str; UUIDStr str;
oxReturnError(field(key, &str)); oxReturnError(field(key, &str));
return UUID::fromString(str).moveTo(val); return UUID::fromString(str).moveTo(*val);
} }
Result<std::size_t> OrganicClawReader::arrayLength(const char *key, bool) noexcept { Result<std::size_t> OrganicClawReader::arrayLength(const char *key, bool) noexcept {

View File

@ -205,7 +205,7 @@ const std::map<ox::StringView, ox::Error(*)()> tests = {
testIn.Struct.String = "Test String 2"; testIn.Struct.String = "Test String 2";
testIn.unionIdx = 1; testIn.unionIdx = 1;
testIn.Union.Int = 93; testIn.Union.Int = 93;
oxAssert(ox::writeOC(testIn).moveTo(&dataBuff), "Data generation failed"); oxAssert(ox::writeOC(testIn).moveTo(dataBuff), "Data generation failed");
ox::TypeStore typeStore; ox::TypeStore typeStore;
auto type = ox::buildTypeDef(&typeStore, &testIn); auto type = ox::buildTypeDef(&typeStore, &testIn);
oxAssert(type.error, "Descriptor write failed"); oxAssert(type.error, "Descriptor write failed");

View File

@ -199,7 +199,7 @@ constexpr ox::Error Preloader<PlatSpec>::field(CRStringView, const ox::BasicStri
const auto restore = m_writer.tellp(); const auto restore = m_writer.tellp();
std::size_t a = 0; std::size_t a = 0;
if (sz && sz >= SmallStringSize) { if (sz && sz >= SmallStringSize) {
oxReturnError(ox::allocate(&m_writer, sz).moveTo(&a)); oxReturnError(ox::allocate(&m_writer, sz).moveTo(a));
} else { } else {
a = restore; a = restore;
} }

View File

@ -165,23 +165,39 @@ struct [[nodiscard]] Result {
return error == 0; return error == 0;
} }
constexpr Error copyTo(type *val) const noexcept { constexpr Error copyTo(type &val) const & noexcept {
if (!error) [[likely]] {
*val = value;
}
return error;
}
constexpr Error copyTo(type &val) const && noexcept {
if (!error) [[likely]] {
*val = value;
}
return error;
}
constexpr Error copyTo(type &val) & noexcept {
*val = value; *val = value;
return error; return error;
} }
constexpr Error copyTo(type *val) noexcept { constexpr Error copyTo(type &val) && noexcept {
*val = value;
return error;
}
constexpr Error moveTo(type *val) noexcept {
if (!error) [[likely]] { if (!error) [[likely]] {
*val = std::move(value); *val = std::move(value);
} }
return error; return error;
} }
constexpr Error moveTo(type &val) noexcept {
if (!error) [[likely]] {
val = std::move(value);
}
return error;
}
constexpr T &unwrap() & noexcept { constexpr T &unwrap() & noexcept {
if (error) { if (error) {
oxPanic(error, "Failed unwrap"); oxPanic(error, "Failed unwrap");