[ox] Make OC and Claw writers take const values

This commit is contained in:
Gary Talent 2023-06-08 21:38:02 -05:00
parent 23054c9f6f
commit 5a021043f4
6 changed files with 15 additions and 14 deletions

View File

@ -158,7 +158,7 @@ static std::map<std::string_view, ox::Error(*)()> tests = {
testIn.Struct.Bool = false;
testIn.Struct.Int = 300;
testIn.Struct.String = "Test String 2";
const auto [buff, err] = ox::writeMC(&testIn);
const auto [buff, err] = ox::writeMC(testIn);
oxAssert(err, "writeClaw failed");
oxAssert(ox::readMC(buff.data(), buff.size(), &testOut), "readClaw failed");
//std::cout << testIn.Union.Int << "|" << testOut.Union.Int << "|\n";

View File

@ -72,7 +72,7 @@ constexpr int getTypeVersion(const T *t) noexcept {
}
template<typename T>
ox::Error writeClawHeader(Writer_c auto &writer, T *t, ClawFormat fmt) noexcept {
ox::Error writeClawHeader(Writer_c auto &writer, const T *t, ClawFormat fmt) noexcept {
switch (fmt) {
case ClawFormat::Metal:
oxReturnError(write(&writer, "M2;"));
@ -96,7 +96,7 @@ ox::Error writeClawHeader(Writer_c auto &writer, T *t, ClawFormat fmt) noexcept
}
Result<Buffer> writeClaw(
auto &t,
const auto &t,
ClawFormat fmt = ClawFormat::Metal,
std::size_t buffReserveSz = 2 * units::KB) noexcept {
Buffer out(buffReserveSz);

View File

@ -45,7 +45,7 @@ class LoggerConn: public trace::Logger {
ox::Error send(trace::MsgId msgId, const auto &msg) noexcept {
ox::Array<char, 10 * ox::units::KB> buff;
std::size_t sz = 0;
oxReturnError(ox::writeMC(&buff[0], buff.size(), &msg, &sz));
oxReturnError(ox::writeMC(&buff[0], buff.size(), msg, &sz));
//std::printf("sz: %lu\n", sz);
oxRequire(szBuff, serialize(static_cast<uint32_t>(sz)));
std::unique_lock buffLk(m_buffMut);

View File

@ -113,7 +113,8 @@ std::map<ox::String, ox::Error(*)()> tests = {
static constexpr size_t buffLen = 1024;
char buff[buffLen];
TestStruct ts;
oxReturnError(ox::writeMC(buff, buffLen, &ts));
oxReturnError(ox::writeMC(buff, buffLen, ts));
oxReturnError(ox::writeMC(ts));
return OxError(0);
}
},
@ -138,7 +139,7 @@ std::map<ox::String, ox::Error(*)()> tests = {
testIn.Struct.Int = 300;
testIn.Struct.BString = "Test String 2";
// run tests
const auto [buff, err] = ox::writeMC(&testIn);
const auto [buff, err] = ox::writeMC(testIn);
oxAssert(err, "writeMC failed");
oxAssert(ox::readMC(buff.data(), buff.size(), &testOut), "readMC failed");
//std::cout << testIn.Union.Int << "|" << testOut.Union.Int << "|\n";
@ -306,7 +307,7 @@ std::map<ox::String, ox::Error(*)()> tests = {
testIn.Struct.BString = "Test String 2";
testIn.unionIdx = 1;
testIn.Union.Int = 93;
oxAssert(ox::writeMC(dataBuff.data(), dataBuff.size(), &testIn), "Data generation failed");
oxAssert(ox::writeMC(dataBuff.data(), dataBuff.size(), testIn), "Data generation failed");
ox::TypeStore typeStore;
const auto [type, typeErr] = ox::buildTypeDef(&typeStore, &testIn);
oxAssert(typeErr, "Descriptor write failed");
@ -358,7 +359,7 @@ std::map<ox::String, ox::Error(*)()> tests = {
testIn.Struct.Bool = false;
testIn.Struct.Int = 300;
testIn.Struct.BString = "Test String 2";
oxAssert(ox::writeMC(dataBuff, dataBuffLen, &testIn), "Data generation failed");
oxAssert(ox::writeMC(dataBuff, dataBuffLen, testIn), "Data generation failed");
ox::TypeStore typeStore;
const auto [type, typeErr] = ox::buildTypeDef(&typeStore, &testIn);
oxAssert(typeErr, "Descriptor write failed");

View File

@ -414,17 +414,17 @@ Result<Buffer> writeMC(Writer_c auto &writer, const auto &val) noexcept {
return {};
}
Result<Buffer> writeMC(const auto *val, std::size_t buffReserveSz = 2 * units::KB) noexcept {
Result<Buffer> writeMC(const auto &val, std::size_t buffReserveSz = 2 * units::KB) noexcept {
Buffer buff(buffReserveSz);
BufferWriter bw(&buff, 0);
oxReturnError(writeMC(bw, *val));
oxReturnError(writeMC(bw, val));
buff.resize(bw.tellp());
return buff;
}
Error writeMC(char *buff, std::size_t buffLen, const auto *val, std::size_t *sizeOut = nullptr) noexcept {
Error writeMC(char *buff, std::size_t buffLen, const auto &val, std::size_t *sizeOut = nullptr) noexcept {
CharBuffWriter bw(buff, buffLen);
oxReturnError(writeMC(bw, *val));
oxReturnError(writeMC(bw, val));
if (sizeOut) {
*sizeOut = bw.tellp();
}

View File

@ -24,7 +24,7 @@ namespace ox {
class OrganicClawWriter {
friend Result<Buffer> writeOC(auto &val) noexcept;
friend Result<Buffer> writeOC(const auto &val) noexcept;
protected:
Json::Value m_json;
@ -248,7 +248,7 @@ Error OrganicClawWriter::field(const char *key, UnionView<U, force> val) noexcep
return OxError(0);
}
Result<Buffer> writeOC(auto &val) noexcept {
Result<Buffer> writeOC(const auto &val) noexcept {
OrganicClawWriter writer;
ModelHandlerInterface handler(&writer);
oxReturnError(model(&handler, &val));