[ox/{mc,oc}] Improve noexcept correctness

This commit is contained in:
Gary Talent 2021-07-29 22:22:44 -05:00
parent 24ca5623e8
commit 55f14eb548
8 changed files with 186 additions and 175 deletions

View File

@ -31,40 +31,40 @@ MetalClawReader::~MetalClawReader() noexcept {
} }
} }
Error MetalClawReader::field(const char*, int8_t *val) { Error MetalClawReader::field(const char*, int8_t *val) noexcept {
return readInteger(val); return readInteger(val);
} }
Error MetalClawReader::field(const char*, int16_t *val) { Error MetalClawReader::field(const char*, int16_t *val) noexcept {
return readInteger(val); return readInteger(val);
} }
Error MetalClawReader::field(const char*, int32_t *val) { Error MetalClawReader::field(const char*, int32_t *val) noexcept {
return readInteger(val); return readInteger(val);
} }
Error MetalClawReader::field(const char*, int64_t *val) { Error MetalClawReader::field(const char*, int64_t *val) noexcept {
return readInteger(val); return readInteger(val);
} }
Error MetalClawReader::field(const char*, uint8_t *val) { Error MetalClawReader::field(const char*, uint8_t *val) noexcept {
return readInteger(val); return readInteger(val);
} }
Error MetalClawReader::field(const char*, uint16_t *val) { Error MetalClawReader::field(const char*, uint16_t *val) noexcept {
return readInteger(val); return readInteger(val);
} }
Error MetalClawReader::field(const char*, uint32_t *val) { Error MetalClawReader::field(const char*, uint32_t *val) noexcept {
return readInteger(val); return readInteger(val);
} }
Error MetalClawReader::field(const char*, uint64_t *val) { Error MetalClawReader::field(const char*, uint64_t *val) noexcept {
return readInteger(val); return readInteger(val);
} }
Error MetalClawReader::field(const char*, bool *val) { Error MetalClawReader::field(const char*, bool *val) noexcept {
if (m_unionIdx == -1 || m_unionIdx == m_field) { if (m_unionIdx == -1 || m_unionIdx == m_field) {
auto valErr = m_fieldPresence.get(m_field); auto valErr = m_fieldPresence.get(m_field);
*val = valErr.value; *val = valErr.value;
@ -74,7 +74,7 @@ Error MetalClawReader::field(const char*, bool *val) {
return OxError(0); return OxError(0);
} }
Error MetalClawReader::field(const char*, SerStr val) { Error MetalClawReader::field(const char*, SerStr val) noexcept {
if (m_unionIdx == -1 || m_unionIdx == m_field) { if (m_unionIdx == -1 || m_unionIdx == m_field) {
if (m_fieldPresence.get(m_field)) { if (m_fieldPresence.get(m_field)) {
// read the length // read the length
@ -109,7 +109,7 @@ Error MetalClawReader::field(const char*, SerStr val) {
return OxError(0); return OxError(0);
} }
Result<ArrayLength> MetalClawReader::arrayLength(const char*, bool pass) { Result<ArrayLength> MetalClawReader::arrayLength(const char*, bool pass) noexcept {
if ((m_unionIdx == -1 || m_unionIdx == m_field) && m_fieldPresence.get(m_field)) { if ((m_unionIdx == -1 || m_unionIdx == m_field) && m_fieldPresence.get(m_field)) {
// read the length // read the length
if (m_buffIt >= m_buffLen) { if (m_buffIt >= m_buffLen) {
@ -125,7 +125,8 @@ Result<ArrayLength> MetalClawReader::arrayLength(const char*, bool pass) {
return OxError(1); return OxError(1);
} }
[[nodiscard]] StringLength MetalClawReader::stringLength(const char*) { [[nodiscard]]
StringLength MetalClawReader::stringLength(const char*) noexcept {
if ((m_unionIdx == -1 || m_unionIdx == m_field) && m_fieldPresence.get(m_field)) { if ((m_unionIdx == -1 || m_unionIdx == m_field) && m_fieldPresence.get(m_field)) {
// read the length // read the length
std::size_t bytesRead = 0; std::size_t bytesRead = 0;
@ -135,15 +136,15 @@ Result<ArrayLength> MetalClawReader::arrayLength(const char*, bool pass) {
return 0; return 0;
} }
MetalClawReader MetalClawReader::child(const char*, int unionIdx) { MetalClawReader MetalClawReader::child(const char*, int unionIdx) noexcept {
return MetalClawReader(m_buff + m_buffIt, m_buffLen - m_buffIt, unionIdx, this); return MetalClawReader(m_buff + m_buffIt, m_buffLen - m_buffIt, unionIdx, this);
} }
bool MetalClawReader::fieldPresent(const char*) const { bool MetalClawReader::fieldPresent(const char*) const noexcept {
return m_fieldPresence.get(m_field).value; return m_fieldPresence.get(m_field).value;
} }
bool MetalClawReader::fieldPresent(int fieldNo) const { bool MetalClawReader::fieldPresent(int fieldNo) const noexcept {
return m_fieldPresence.get(fieldNo).value; return m_fieldPresence.get(fieldNo).value;
} }

View File

@ -10,7 +10,9 @@
#include <ox/model/fieldcounter.hpp> #include <ox/model/fieldcounter.hpp>
#include <ox/model/optype.hpp> #include <ox/model/optype.hpp>
#include <ox/model/typenamecatcher.hpp>
#include <ox/model/types.hpp> #include <ox/model/types.hpp>
#include <ox/std/buffer.hpp>
#include <ox/std/byteswap.hpp> #include <ox/std/byteswap.hpp>
#include <ox/std/string.hpp> #include <ox/std/string.hpp>
#include <ox/std/trace.hpp> #include <ox/std/trace.hpp>
@ -40,86 +42,89 @@ class MetalClawReader {
~MetalClawReader() noexcept; ~MetalClawReader() noexcept;
Error field(const char*, int8_t *val); Error field(const char*, int8_t *val) noexcept;
Error field(const char*, int16_t *val); Error field(const char*, int16_t *val) noexcept;
Error field(const char*, int32_t *val); Error field(const char*, int32_t *val) noexcept;
Error field(const char*, int64_t *val); Error field(const char*, int64_t *val) noexcept;
Error field(const char*, uint8_t *val); Error field(const char*, uint8_t *val) noexcept;
Error field(const char*, uint16_t *val); Error field(const char*, uint16_t *val) noexcept;
Error field(const char*, uint32_t *val); Error field(const char*, uint32_t *val) noexcept;
Error field(const char*, uint64_t *val); Error field(const char*, uint64_t *val) noexcept;
Error field(const char*, bool *val); Error field(const char*, bool *val) noexcept;
// array handler // array handler
template<typename T> template<typename T>
Error field(const char*, T *val, std::size_t len); Error field(const char*, T *val, std::size_t len) noexcept;
// map handler // map handler
template<typename T> template<typename T>
Error field(const char*, HashMap<String, T> *val); Error field(const char*, HashMap<String, T> *val) noexcept;
// array handler, with callback to allow handling individual elements // array handler, with callback to allow handling individual elements
template<typename T, typename Handler> template<typename T, typename Handler>
Error field(const char*, Handler handler); Error field(const char*, Handler handler) noexcept;
template<typename T> template<typename T>
Error field(const char*, T *val); Error field(const char*, T *val) noexcept;
template<typename U> template<typename U>
Error field(const char*, UnionView<U> val); Error field(const char*, UnionView<U> val) noexcept;
template<std::size_t L> template<std::size_t L>
Error field(const char*, BString<L> *val); Error field(const char*, const BString<L> *val) noexcept;
Error field(const char*, SerStr val); Error field(const char*, SerStr val) noexcept;
/** /**
* Reads an array length from the current location in the buffer. * Reads an array length from the current location in the buffer.
* @param pass indicates that the parsing should iterate past the array length * @param pass indicates that the parsing should iterate past the array length
*/ */
Result<ArrayLength> arrayLength(const char *name, bool pass = true); Result<ArrayLength> arrayLength(const char *name, bool pass = true) noexcept;
/** /**
* Reads an string length from the current location in the buffer. * Reads an string length from the current location in the buffer.
*/ */
[[nodiscard]] [[nodiscard]]
StringLength stringLength(const char *name); StringLength stringLength(const char *name) noexcept;
template<typename T = std::nullptr_t> template<typename T = std::nullptr_t>
void setTypeInfo(const char *name = T::TypeName, int fields = countFields<T>()); void setTypeInfo(const char *name = getModelTypeName<T>(), int fields = countFields<T>()) noexcept;
/** /**
* Returns a MetalClawReader to parse a child object. * Returns a MetalClawReader to parse a child object.
*/ */
[[nodiscard]] [[nodiscard]]
MetalClawReader child(const char *name, int unionIdx = -1); MetalClawReader child(const char *name, int unionIdx = -1) noexcept;
/** /**
* Indicates whether or not the next field to be read is present. * Indicates whether or not the next field to be read is present.
*/ */
bool fieldPresent(const char *name) const; [[nodiscard]]
bool fieldPresent(const char *name) const noexcept;
/** /**
* Indicates whether or not the given field is present. * Indicates whether or not the given field is present.
*/ */
bool fieldPresent(int fieldNo) const; [[nodiscard]]
bool fieldPresent(int fieldNo) const noexcept;
void nextField() noexcept; void nextField() noexcept;
static constexpr auto opType() { [[nodiscard]]
static constexpr auto opType() noexcept {
return OpType::Read; return OpType::Read;
} }
private: private:
template<typename I> template<typename I>
Error readInteger(I *val); Error readInteger(I *val) noexcept;
}; };
template<typename T> template<typename T>
Error MetalClawReader::field(const char *name, T *val) { Error MetalClawReader::field(const char *name, T *val) noexcept {
if constexpr(isVector_v<T>) { if constexpr(isVector_v<T>) {
if (m_unionIdx == -1 || m_unionIdx == m_field) { if (m_unionIdx == -1 || m_unionIdx == m_field) {
// set size of val if the field is present, don't worry about it if not // set size of val if the field is present, don't worry about it if not
@ -142,7 +147,7 @@ Error MetalClawReader::field(const char *name, T *val) {
} }
template<typename U> template<typename U>
Error MetalClawReader::field(const char*, UnionView<U> val) { Error MetalClawReader::field(const char*, UnionView<U> val) noexcept {
if ((m_unionIdx == -1 || m_unionIdx == m_field) && val.get() && m_fieldPresence.get(m_field)) { if ((m_unionIdx == -1 || m_unionIdx == m_field) && val.get() && m_fieldPresence.get(m_field)) {
auto reader = child("", val.idx()); auto reader = child("", val.idx());
oxReturnError(model(&reader, val.get())); oxReturnError(model(&reader, val.get()));
@ -152,12 +157,12 @@ Error MetalClawReader::field(const char*, UnionView<U> val) {
} }
template<std::size_t L> template<std::size_t L>
Error MetalClawReader::field(const char *name, BString<L> *val) { Error MetalClawReader::field(const char *name, const BString<L> *val) noexcept {
return field(name, SerStr(val->data(), val->cap())); return field(name, SerStr(val->data(), val->cap()));
} }
template<typename I> template<typename I>
Error MetalClawReader::readInteger(I *val) { Error MetalClawReader::readInteger(I *val) noexcept {
if (m_unionIdx == -1 || m_unionIdx == m_field) { if (m_unionIdx == -1 || m_unionIdx == m_field) {
if (m_fieldPresence.get(m_field)) { if (m_fieldPresence.get(m_field)) {
std::size_t bytesRead = 0; std::size_t bytesRead = 0;
@ -179,7 +184,7 @@ Error MetalClawReader::readInteger(I *val) {
// array handler // array handler
template<typename T> template<typename T>
Error MetalClawReader::field(const char *name, T *val, std::size_t valLen) { Error MetalClawReader::field(const char *name, T *val, std::size_t valLen) noexcept {
if (m_unionIdx == -1 || m_unionIdx == m_field) { if (m_unionIdx == -1 || m_unionIdx == m_field) {
if (m_fieldPresence.get(m_field)) { if (m_fieldPresence.get(m_field)) {
// read the length // read the length
@ -208,7 +213,7 @@ Error MetalClawReader::field(const char *name, T *val, std::size_t valLen) {
} }
template<typename T> template<typename T>
Error MetalClawReader::field(const char*, HashMap<String, T> *val) { Error MetalClawReader::field(const char*, HashMap<String, T> *val) noexcept {
if (m_unionIdx == -1 || m_unionIdx == m_field) { if (m_unionIdx == -1 || m_unionIdx == m_field) {
if (m_fieldPresence.get(m_field)) { if (m_fieldPresence.get(m_field)) {
// read the length // read the length
@ -235,7 +240,7 @@ Error MetalClawReader::field(const char*, HashMap<String, T> *val) {
} }
template<typename T, typename Handler> template<typename T, typename Handler>
Error MetalClawReader::field(const char*, Handler handler) { Error MetalClawReader::field(const char*, Handler handler) noexcept {
if (m_unionIdx == -1 || m_unionIdx == m_field) { if (m_unionIdx == -1 || m_unionIdx == m_field) {
if (m_fieldPresence.get(m_field)) { if (m_fieldPresence.get(m_field)) {
// read the length // read the length
@ -261,28 +266,28 @@ Error MetalClawReader::field(const char*, Handler handler) {
} }
template<typename T> template<typename T>
void MetalClawReader::setTypeInfo(const char*, int fields) { void MetalClawReader::setTypeInfo(const char*, int fields) noexcept {
m_fields = fields; m_fields = fields;
m_buffIt = (fields / 8 + 1) - (fields % 8 == 0); m_buffIt = (fields / 8 + 1) - (fields % 8 == 0);
m_fieldPresence.setFields(fields); m_fieldPresence.setFields(fields);
m_fieldPresence.setMaxLen(m_buffIt); m_fieldPresence.setMaxLen(static_cast<int>(m_buffIt));
} }
template<typename T> template<typename T>
Error readMC(const char *buff, std::size_t buffLen, T *val) { Error readMC(const char *buff, std::size_t buffLen, T *val) noexcept {
MetalClawReader reader(bit_cast<uint8_t*>(buff), buffLen); MetalClawReader reader(bit_cast<uint8_t*>(buff), buffLen);
return model(&reader, val); return model(&reader, val);
} }
template<typename T> template<typename T>
Result<T> readMC(const char *buff, std::size_t buffLen) { Result<T> readMC(const char *buff, std::size_t buffLen) noexcept {
T val; T val;
oxReturnError(readMC(buff, buffLen, &val)); oxReturnError(readMC(buff, buffLen, &val));
return move(val); return val;
} }
template<typename T> template<typename T>
Result<T> readMC(const Vector<char> &buff) { Result<T> readMC(const Buffer &buff) noexcept {
return readMC<T>(buff.data(), buff.size()); return readMC<T>(buff.data(), buff.size());
} }

View File

@ -28,40 +28,40 @@ MetalClawWriter::~MetalClawWriter() noexcept {
} }
} }
Error MetalClawWriter::field(const char*, int8_t *val) noexcept { Error MetalClawWriter::field(const char*, const int8_t *val) noexcept {
return appendInteger(*val); return appendInteger(*val);
} }
Error MetalClawWriter::field(const char*, int16_t *val) noexcept { Error MetalClawWriter::field(const char*, const int16_t *val) noexcept {
return appendInteger(*val); return appendInteger(*val);
} }
Error MetalClawWriter::field(const char*, int32_t *val) noexcept { Error MetalClawWriter::field(const char*, const int32_t *val) noexcept {
return appendInteger(*val); return appendInteger(*val);
} }
Error MetalClawWriter::field(const char*, int64_t *val) noexcept { Error MetalClawWriter::field(const char*, const int64_t *val) noexcept {
return appendInteger(*val); return appendInteger(*val);
} }
Error MetalClawWriter::field(const char*, uint8_t *val) noexcept { Error MetalClawWriter::field(const char*, const uint8_t *val) noexcept {
return appendInteger(*val); return appendInteger(*val);
} }
Error MetalClawWriter::field(const char*, uint16_t *val) noexcept { Error MetalClawWriter::field(const char*, const uint16_t *val) noexcept {
return appendInteger(*val); return appendInteger(*val);
} }
Error MetalClawWriter::field(const char*, uint32_t *val) noexcept { Error MetalClawWriter::field(const char*, const uint32_t *val) noexcept {
return appendInteger(*val); return appendInteger(*val);
} }
Error MetalClawWriter::field(const char*, uint64_t *val) noexcept { Error MetalClawWriter::field(const char*, const uint64_t *val) noexcept {
return appendInteger(*val); return appendInteger(*val);
} }
Error MetalClawWriter::field(const char*, bool *val) noexcept { Error MetalClawWriter::field(const char*, const bool *val) noexcept {
if (m_unionIdx == -1 || m_unionIdx == m_field) { if (m_unionIdx == -1 || m_unionIdx == m_field) {
oxReturnError(m_fieldPresence.set(m_field, *val)); oxReturnError(m_fieldPresence.set(m_field, *val));
} }

View File

@ -42,23 +42,23 @@ class MetalClawWriter {
~MetalClawWriter() noexcept; ~MetalClawWriter() noexcept;
Error field(const char*, int8_t *val) noexcept; Error field(const char*, const int8_t *val) noexcept;
Error field(const char*, int16_t *val) noexcept; Error field(const char*, const int16_t *val) noexcept;
Error field(const char*, int32_t *val) noexcept; Error field(const char*, const int32_t *val) noexcept;
Error field(const char*, int64_t *val) noexcept; Error field(const char*, const int64_t *val) noexcept;
Error field(const char*, uint8_t *val) noexcept; Error field(const char*, const uint8_t *val) noexcept;
Error field(const char*, uint16_t *val) noexcept; Error field(const char*, const uint16_t *val) noexcept;
Error field(const char*, uint32_t *val) noexcept; Error field(const char*, const uint32_t *val) noexcept;
Error field(const char*, uint64_t *val) noexcept; Error field(const char*, const uint64_t *val) noexcept;
Error field(const char*, bool *val) noexcept; Error field(const char*, const bool *val) noexcept;
template<typename T> template<typename T>
Error field(const char*, T *val, std::size_t len); Error field(const char*, T *val, std::size_t len) noexcept;
template<typename T> template<typename T>
Error field(const char*, HashMap<String, T> *val); Error field(const char*, HashMap<String, T> *val) noexcept;
template<std::size_t L> template<std::size_t L>
Error field(const char*, BString<L> *val) noexcept; Error field(const char*, BString<L> *val) noexcept;
@ -66,17 +66,19 @@ class MetalClawWriter {
Error field(const char*, SerStr val) noexcept; Error field(const char*, SerStr val) noexcept;
template<typename T> template<typename T>
Error field(const char*, T *val); Error field(const char*, T *val) noexcept;
template<typename U> template<typename U>
Error field(const char*, UnionView<U> val); Error field(const char*, UnionView<U> val) noexcept;
template<typename T = std::nullptr_t> template<typename T = std::nullptr_t>
void setTypeInfo(const char *name = T::TypeName, int fields = countFields<T>()); void setTypeInfo(const char *name = T::TypeName, int fields = countFields<T>()) noexcept;
[[nodiscard]]
std::size_t size() noexcept; std::size_t size() noexcept;
static constexpr auto opType() { [[nodiscard]]
static constexpr auto opType() noexcept {
return OpType::Write; return OpType::Write;
} }
@ -92,7 +94,7 @@ Error MetalClawWriter::field(const char *name, BString<L> *val) noexcept {
} }
template<typename T> template<typename T>
Error MetalClawWriter::field(const char*, T *val) { Error MetalClawWriter::field(const char*, T *val) noexcept {
if constexpr(isVector_v<T>) { if constexpr(isVector_v<T>) {
return field(nullptr, val->data(), val->size()); return field(nullptr, val->data(), val->size());
} else { } else {
@ -112,7 +114,7 @@ Error MetalClawWriter::field(const char*, T *val) {
} }
template<typename U> template<typename U>
Error MetalClawWriter::field(const char*, UnionView<U> val) { Error MetalClawWriter::field(const char*, UnionView<U> val) noexcept {
bool fieldSet = false; bool fieldSet = false;
if (val.get() && (m_unionIdx == -1 || m_unionIdx == m_field)) { if (val.get() && (m_unionIdx == -1 || m_unionIdx == m_field)) {
MetalClawWriter writer(m_buff + m_buffIt, m_buffLen - m_buffIt, val.idx()); MetalClawWriter writer(m_buff + m_buffIt, m_buffLen - m_buffIt, val.idx());
@ -128,7 +130,7 @@ Error MetalClawWriter::field(const char*, UnionView<U> val) {
} }
template<typename T> template<typename T>
Error MetalClawWriter::field(const char*, T *val, std::size_t len) { Error MetalClawWriter::field(const char*, T *val, std::size_t len) noexcept {
bool fieldSet = false; bool fieldSet = false;
if (len && (m_unionIdx == -1 || m_unionIdx == m_field)) { if (len && (m_unionIdx == -1 || m_unionIdx == m_field)) {
@ -159,7 +161,7 @@ Error MetalClawWriter::field(const char*, T *val, std::size_t len) {
} }
template<typename T> template<typename T>
Error MetalClawWriter::field(const char*, HashMap<String, T> *val) { Error MetalClawWriter::field(const char*, HashMap<String, T> *val) noexcept {
const auto &keys = val->keys(); const auto &keys = val->keys();
const auto len = keys.size(); const auto len = keys.size();
bool fieldSet = false; bool fieldSet = false;
@ -216,7 +218,7 @@ Error MetalClawWriter::appendInteger(I val) noexcept {
} }
template<typename T> template<typename T>
void MetalClawWriter::setTypeInfo(const char*, int fields) { void MetalClawWriter::setTypeInfo(const char*, int fields) noexcept {
m_fields = fields; m_fields = fields;
m_fieldPresence.setFields(fields); m_fieldPresence.setFields(fields);
m_buffIt = m_fieldPresence.getMaxLen(); m_buffIt = m_fieldPresence.getMaxLen();
@ -224,7 +226,7 @@ void MetalClawWriter::setTypeInfo(const char*, int fields) {
} }
template<typename T> template<typename T>
Result<Buffer> writeMC(T *val) { Result<Buffer> writeMC(T *val) noexcept {
Buffer buff(10 * units::MB); Buffer buff(10 * units::MB);
MetalClawWriter writer(bit_cast<uint8_t*>(buff.data()), buff.size()); MetalClawWriter writer(bit_cast<uint8_t*>(buff.data()), buff.size());
oxReturnError(model(&writer, val)); oxReturnError(model(&writer, val));
@ -233,7 +235,7 @@ Result<Buffer> writeMC(T *val) {
} }
template<typename T> template<typename T>
Error writeMC(char *buff, std::size_t buffLen, T *val, std::size_t *sizeOut = nullptr) { Error writeMC(char *buff, std::size_t buffLen, T *val, std::size_t *sizeOut = nullptr) noexcept {
MetalClawWriter writer(bit_cast<uint8_t*>(buff), buffLen); MetalClawWriter writer(bit_cast<uint8_t*>(buff), buffLen);
auto err = model(&writer, val); auto err = model(&writer, val);
if (sizeOut) { if (sizeOut) {

View File

@ -30,12 +30,12 @@ OrganicClawReader::OrganicClawReader(const char *json, std::size_t jsonLen) {
} }
} }
OrganicClawReader::OrganicClawReader(const Json::Value &json, int unionIdx): OrganicClawReader::OrganicClawReader(const Json::Value &json, int unionIdx) noexcept:
m_json(json), m_json(json),
m_unionIdx(unionIdx) { m_unionIdx(unionIdx) {
} }
Error OrganicClawReader::field(const char *key, int8_t *val) { Error OrganicClawReader::field(const char *key, int8_t *val) noexcept {
auto err = OxError(0); auto err = OxError(0);
if (targetValid()) { if (targetValid()) {
const auto &jv = value(key); const auto &jv = value(key);
@ -51,7 +51,7 @@ Error OrganicClawReader::field(const char *key, int8_t *val) {
return err; return err;
} }
Error OrganicClawReader::field(const char *key, int16_t *val) { Error OrganicClawReader::field(const char *key, int16_t *val) noexcept {
auto err = OxError(0); auto err = OxError(0);
if (targetValid()) { if (targetValid()) {
const auto &jv = value(key); const auto &jv = value(key);
@ -67,7 +67,7 @@ Error OrganicClawReader::field(const char *key, int16_t *val) {
return err; return err;
} }
Error OrganicClawReader::field(const char *key, int32_t *val) { Error OrganicClawReader::field(const char *key, int32_t *val) noexcept {
auto err = OxError(0); auto err = OxError(0);
if (targetValid()) { if (targetValid()) {
const auto &jv = value(key); const auto &jv = value(key);
@ -83,7 +83,7 @@ Error OrganicClawReader::field(const char *key, int32_t *val) {
return err; return err;
} }
Error OrganicClawReader::field(const char *key, int64_t *val) { Error OrganicClawReader::field(const char *key, int64_t *val) noexcept {
auto err = OxError(0); auto err = OxError(0);
if (targetValid()) { if (targetValid()) {
const auto &jv = value(key); const auto &jv = value(key);
@ -100,7 +100,7 @@ Error OrganicClawReader::field(const char *key, int64_t *val) {
} }
Error OrganicClawReader::field(const char *key, uint8_t *val) { Error OrganicClawReader::field(const char *key, uint8_t *val) noexcept {
auto err = OxError(0); auto err = OxError(0);
if (targetValid()) { if (targetValid()) {
const auto &jv = value(key); const auto &jv = value(key);
@ -116,7 +116,7 @@ Error OrganicClawReader::field(const char *key, uint8_t *val) {
return err; return err;
} }
Error OrganicClawReader::field(const char *key, uint16_t *val) { Error OrganicClawReader::field(const char *key, uint16_t *val) noexcept {
auto err = OxError(0); auto err = OxError(0);
if (targetValid()) { if (targetValid()) {
const auto &jv = value(key); const auto &jv = value(key);
@ -132,7 +132,7 @@ Error OrganicClawReader::field(const char *key, uint16_t *val) {
return err; return err;
} }
Error OrganicClawReader::field(const char *key, uint32_t *val) { Error OrganicClawReader::field(const char *key, uint32_t *val) noexcept {
auto err = OxError(0); auto err = OxError(0);
if (targetValid()) { if (targetValid()) {
const auto &jv = value(key); const auto &jv = value(key);
@ -148,7 +148,7 @@ Error OrganicClawReader::field(const char *key, uint32_t *val) {
return err; return err;
} }
Error OrganicClawReader::field(const char *key, uint64_t *val) { Error OrganicClawReader::field(const char *key, uint64_t *val) noexcept {
auto err = OxError(0); auto err = OxError(0);
if (targetValid()) { if (targetValid()) {
const auto &jv = value(key); const auto &jv = value(key);
@ -164,7 +164,7 @@ Error OrganicClawReader::field(const char *key, uint64_t *val) {
return err; return err;
} }
Error OrganicClawReader::field(const char *key, bool *val) { Error OrganicClawReader::field(const char *key, bool *val) noexcept {
auto err = OxError(0); auto err = OxError(0);
if (targetValid()) { if (targetValid()) {
const auto &jv = value(key); const auto &jv = value(key);
@ -180,7 +180,7 @@ Error OrganicClawReader::field(const char *key, bool *val) {
return err; return err;
} }
Error OrganicClawReader::field(const char *key, SerStr val) { Error OrganicClawReader::field(const char *key, SerStr val) noexcept {
auto err = OxError(0); auto err = OxError(0);
const char *begin = nullptr, *end = nullptr; const char *begin = nullptr, *end = nullptr;
const auto &jv = value(key); const auto &jv = value(key);
@ -208,7 +208,7 @@ Error OrganicClawReader::field(const char *key, SerStr val) {
return err; return err;
} }
Result<std::size_t> OrganicClawReader::arrayLength(const char *key, bool) { Result<std::size_t> OrganicClawReader::arrayLength(const char *key, bool) noexcept {
const auto &jv = value(key); const auto &jv = value(key);
if (jv.empty()) { if (jv.empty()) {
return 0; return 0;
@ -219,7 +219,8 @@ Result<std::size_t> OrganicClawReader::arrayLength(const char *key, bool) {
return OxError(1, "Type mismatch"); return OxError(1, "Type mismatch");
} }
[[nodiscard]] std::size_t OrganicClawReader::stringLength(const char *key) { [[nodiscard]]
std::size_t OrganicClawReader::stringLength(const char *key) noexcept {
const char *begin = nullptr, *end = nullptr; const char *begin = nullptr, *end = nullptr;
const auto &jv = value(key); const auto &jv = value(key);
if (jv.empty()) { if (jv.empty()) {
@ -232,15 +233,15 @@ Result<std::size_t> OrganicClawReader::arrayLength(const char *key, bool) {
return OxError(1, "Type mismatch"); return OxError(1, "Type mismatch");
} }
OrganicClawReader OrganicClawReader::child(const char *key, int unionIdx) { OrganicClawReader OrganicClawReader::child(const char *key, int unionIdx) noexcept {
return OrganicClawReader(m_json[key], unionIdx); return OrganicClawReader(m_json[key], unionIdx);
} }
bool OrganicClawReader::fieldPresent(const char *key) { bool OrganicClawReader::fieldPresent(const char *key) noexcept {
return !m_json[key].empty(); return !m_json[key].empty();
} }
Json::Value &OrganicClawReader::value(const char *key) { Json::Value &OrganicClawReader::value(const char *key) noexcept {
if (m_json.isArray()) { if (m_json.isArray()) {
return m_json[m_fieldIt]; return m_json[m_fieldIt];
} else { } else {

View File

@ -12,6 +12,7 @@
#include <ox/model/fieldcounter.hpp> #include <ox/model/fieldcounter.hpp>
#include <ox/model/optype.hpp> #include <ox/model/optype.hpp>
#include <ox/model/typenamecatcher.hpp>
#include <ox/model/types.hpp> #include <ox/model/types.hpp>
#include <ox/std/buffer.hpp> #include <ox/std/buffer.hpp>
#include <ox/std/byteswap.hpp> #include <ox/std/byteswap.hpp>
@ -30,38 +31,38 @@ class OrganicClawReader {
int m_unionIdx = -1; int m_unionIdx = -1;
public: public:
OrganicClawReader() = default; OrganicClawReader() noexcept = default;
OrganicClawReader(const uint8_t *buff, std::size_t buffSize); OrganicClawReader(const uint8_t *buff, std::size_t buffSize);
OrganicClawReader(const char *json, std::size_t buffSize); OrganicClawReader(const char *json, std::size_t buffSize);
explicit OrganicClawReader(const Json::Value &json, int unionIdx = -1); explicit OrganicClawReader(const Json::Value &json, int unionIdx = -1) noexcept;
Error field(const char *key, int8_t *val); Error field(const char *key, int8_t *val) noexcept;
Error field(const char *key, int16_t *val); Error field(const char *key, int16_t *val) noexcept;
Error field(const char *key, int32_t *val); Error field(const char *key, int32_t *val) noexcept;
Error field(const char *key, int64_t *val); Error field(const char *key, int64_t *val) noexcept;
Error field(const char *key, uint8_t *val); Error field(const char *key, uint8_t *val) noexcept;
Error field(const char *key, uint16_t *val); Error field(const char *key, uint16_t *val) noexcept;
Error field(const char *key, uint32_t *val); Error field(const char *key, uint32_t *val) noexcept;
Error field(const char *key, uint64_t *val); Error field(const char *key, uint64_t *val) noexcept;
Error field(const char *key, bool *val); Error field(const char *key, bool *val) noexcept;
// array handler // array handler
template<typename T> template<typename T>
Error field(const char *key, T *val, std::size_t len); Error field(const char *key, T *val, std::size_t len) noexcept;
template<typename T> template<typename T>
Error field(const char*, HashMap<String, T> *val); Error field(const char*, HashMap<String, T> *val) noexcept;
template<typename T> template<typename T>
Error field(const char *key, T *val); Error field(const char *key, T *val) noexcept;
template<typename U> template<typename U>
Error field(const char *key, UnionView<U> val); Error field(const char *key, UnionView<U> val) noexcept;
template<std::size_t L> template<std::size_t L>
Error field(const char *key, BasicString<L> *val) noexcept; Error field(const char *key, BasicString<L> *val) noexcept;
@ -69,49 +70,48 @@ class OrganicClawReader {
template<std::size_t L> template<std::size_t L>
Error field(const char *key, BString<L> *val) noexcept; Error field(const char *key, BString<L> *val) noexcept;
Error field(const char *key, SerStr val); Error field(const char *key, SerStr val) noexcept;
/** /**
* Reads an array length from the current location in the buffer. * Reads an array length from the current location in the buffer.
* @param pass indicates that the parsing should iterate past the array length * @param pass indicates that the parsing should iterate past the array length
*/ */
Result<std::size_t> arrayLength(const char *key, bool pass = true); Result<std::size_t> arrayLength(const char *key, bool pass = true) noexcept;
/** /**
* Reads an string length from the current location in the buffer. * Reads an string length from the current location in the buffer.
*/ */
[[nodiscard]] [[nodiscard]]
std::size_t stringLength(const char *name); std::size_t stringLength(const char *name) noexcept;
template<typename T = void> template<typename T = void>
constexpr void setTypeInfo(const char* = T::TypeName, int = countFields<T>()) { constexpr void setTypeInfo(const char* = getModelTypeName<T>(), int = countFields<T>()) noexcept {
} }
/** /**
* Returns a OrganicClawReader to parse a child object. * Returns a OrganicClawReader to parse a child object.
*/ */
[[nodiscard]] [[nodiscard]]
OrganicClawReader child(const char *key, int unionIdx = -1); OrganicClawReader child(const char *key, int unionIdx = -1) noexcept;
// compatibility stub // compatibility stub
constexpr void nextField() noexcept {} constexpr void nextField() noexcept {}
bool fieldPresent(const char *key); bool fieldPresent(const char *key) noexcept;
static constexpr auto opType() { static constexpr auto opType() noexcept {
return OpType::Read; return OpType::Read;
} }
private: private:
Json::Value &value(const char *key) noexcept;
Json::Value &value(const char *key);
bool targetValid() noexcept; bool targetValid() noexcept;
}; };
template<typename T> template<typename T>
Error OrganicClawReader::field(const char *key, T *val) { Error OrganicClawReader::field(const char *key, T *val) noexcept {
auto err = OxError(0); auto err = OxError(0);
if constexpr(isVector_v<T>) { if constexpr(isVector_v<T>) {
return field(key, val->data(), val->size()); return field(key, val->data(), val->size());
@ -129,7 +129,7 @@ Error OrganicClawReader::field(const char *key, T *val) {
} }
template<typename U> template<typename U>
Error OrganicClawReader::field(const char *key, UnionView<U> val) { Error OrganicClawReader::field(const char *key, UnionView<U> val) noexcept {
auto err = OxError(0); auto err = OxError(0);
if (targetValid()) { if (targetValid()) {
const auto &jv = value(key); const auto &jv = value(key);
@ -162,13 +162,13 @@ Error OrganicClawReader::field(const char *key, BasicString<L> *val) noexcept {
} }
template<std::size_t L> template<std::size_t L>
Error OrganicClawReader::field(const char *name, BString<L> *val) noexcept { Error OrganicClawReader::field(const char *key, BString<L> *val) noexcept {
return field(name, SerStr(val->data(), val->cap())); return field(key, SerStr(val->data(), val->cap()));
} }
// array handler // array handler
template<typename T> template<typename T>
Error OrganicClawReader::field(const char *key, T *val, std::size_t valLen) { Error OrganicClawReader::field(const char *key, T *val, std::size_t valLen) noexcept {
const auto &srcVal = value(key); const auto &srcVal = value(key);
auto srcSize = srcVal.size(); auto srcSize = srcVal.size();
if (srcSize > valLen) { if (srcSize > valLen) {
@ -182,7 +182,7 @@ Error OrganicClawReader::field(const char *key, T *val, std::size_t valLen) {
} }
template<typename T> template<typename T>
Error OrganicClawReader::field(const char *key, HashMap<String, T> *val) { Error OrganicClawReader::field(const char *key, HashMap<String, T> *val) noexcept {
const auto &srcVal = value(key); const auto &srcVal = value(key);
auto keys = srcVal.getMemberNames(); auto keys = srcVal.getMemberNames();
auto srcSize = srcVal.size(); auto srcSize = srcVal.size();
@ -214,19 +214,19 @@ Error readOC(const char *json, std::size_t jsonSize, T *val) noexcept {
} }
template<typename T> template<typename T>
Result<T> readOC(const char *json, std::size_t jsonLen) { Result<T> readOC(const char *json, std::size_t jsonLen) noexcept {
T val; T val;
oxReturnError(readOC(json, jsonLen, &val)); oxReturnError(readOC(json, jsonLen, &val));
return move(val); return move(val);
} }
template<typename T> template<typename T>
Result<T> readOC(const char *json) { Result<T> readOC(const char *json) noexcept {
return readOC<T>(json, ox_strlen(json)); return readOC<T>(json, ox_strlen(json));
} }
template<typename T> template<typename T>
Result<T> readOC(const Buffer &buff) { Result<T> readOC(const Buffer &buff) noexcept {
return readOC<T>(buff.data(), buff.size()); return readOC<T>(buff.data(), buff.size());
} }

View File

@ -10,15 +10,15 @@
namespace ox { namespace ox {
OrganicClawWriter::OrganicClawWriter(int unionIdx): m_unionIdx(unionIdx) { OrganicClawWriter::OrganicClawWriter(int unionIdx) noexcept: m_unionIdx(unionIdx) {
} }
OrganicClawWriter::OrganicClawWriter(Json::Value json, int unionIdx): OrganicClawWriter::OrganicClawWriter(Json::Value json, int unionIdx) noexcept:
m_json(json), m_json(json),
m_unionIdx(unionIdx) { m_unionIdx(unionIdx) {
} }
Error OrganicClawWriter::field(const char *key, int8_t *val) { Error OrganicClawWriter::field(const char *key, const int8_t *val) noexcept {
if (*val) { if (*val) {
value(key) = *val; value(key) = *val;
} }
@ -26,7 +26,7 @@ Error OrganicClawWriter::field(const char *key, int8_t *val) {
return OxError(0); return OxError(0);
} }
Error OrganicClawWriter::field(const char *key, int16_t *val) { Error OrganicClawWriter::field(const char *key, const int16_t *val) noexcept {
if (*val) { if (*val) {
value(key) = *val; value(key) = *val;
} }
@ -34,7 +34,7 @@ Error OrganicClawWriter::field(const char *key, int16_t *val) {
return OxError(0); return OxError(0);
} }
Error OrganicClawWriter::field(const char *key, int32_t *val) { Error OrganicClawWriter::field(const char *key, const int32_t *val) noexcept {
if (*val) { if (*val) {
value(key) = *val; value(key) = *val;
} }
@ -42,7 +42,7 @@ Error OrganicClawWriter::field(const char *key, int32_t *val) {
return OxError(0); return OxError(0);
} }
Error OrganicClawWriter::field(const char *key, int64_t *val) { Error OrganicClawWriter::field(const char *key, const int64_t *val) noexcept {
if (*val) { if (*val) {
value(key) = *val; value(key) = *val;
} }
@ -51,7 +51,7 @@ Error OrganicClawWriter::field(const char *key, int64_t *val) {
} }
Error OrganicClawWriter::field(const char *key, uint8_t *val) { Error OrganicClawWriter::field(const char *key, const uint8_t *val) noexcept {
if (*val) { if (*val) {
value(key) = *val; value(key) = *val;
} }
@ -59,7 +59,7 @@ Error OrganicClawWriter::field(const char *key, uint8_t *val) {
return OxError(0); return OxError(0);
} }
Error OrganicClawWriter::field(const char *key, uint16_t *val) { Error OrganicClawWriter::field(const char *key, const uint16_t *val) noexcept {
if (targetValid() && *val) { if (targetValid() && *val) {
value(key) = *val; value(key) = *val;
} }
@ -67,7 +67,7 @@ Error OrganicClawWriter::field(const char *key, uint16_t *val) {
return OxError(0); return OxError(0);
} }
Error OrganicClawWriter::field(const char *key, uint32_t *val) { Error OrganicClawWriter::field(const char *key, const uint32_t *val) noexcept {
if (targetValid() && *val) { if (targetValid() && *val) {
value(key) = *val; value(key) = *val;
} }
@ -75,7 +75,7 @@ Error OrganicClawWriter::field(const char *key, uint32_t *val) {
return OxError(0); return OxError(0);
} }
Error OrganicClawWriter::field(const char *key, uint64_t *val) { Error OrganicClawWriter::field(const char *key, const uint64_t *val) noexcept {
if (targetValid() && *val) { if (targetValid() && *val) {
value(key) = *val; value(key) = *val;
} }
@ -83,7 +83,7 @@ Error OrganicClawWriter::field(const char *key, uint64_t *val) {
return OxError(0); return OxError(0);
} }
Error OrganicClawWriter::field(const char *key, bool *val) { Error OrganicClawWriter::field(const char *key, const bool *val) noexcept {
if (targetValid() && *val) { if (targetValid() && *val) {
value(key) = *val; value(key) = *val;
} }
@ -91,7 +91,7 @@ Error OrganicClawWriter::field(const char *key, bool *val) {
return OxError(0); return OxError(0);
} }
Error OrganicClawWriter::field(const char *key, String *val) { Error OrganicClawWriter::field(const char *key, const String *val) noexcept {
if (targetValid() && val->len()) { if (targetValid() && val->len()) {
value(key) = val->c_str(); value(key) = val->c_str();
} }
@ -99,7 +99,7 @@ Error OrganicClawWriter::field(const char *key, String *val) {
return OxError(0); return OxError(0);
} }
Error OrganicClawWriter::field(const char *key, SerStr val) { Error OrganicClawWriter::field(const char *key, SerStr val) noexcept {
if (targetValid() && val.len()) { if (targetValid() && val.len()) {
value(key) = val.c_str(); value(key) = val.c_str();
} }
@ -107,7 +107,7 @@ Error OrganicClawWriter::field(const char *key, SerStr val) {
return OxError(0); return OxError(0);
} }
Json::Value &OrganicClawWriter::value(const char *key) { Json::Value &OrganicClawWriter::value(const char *key) noexcept {
if (m_json.isArray()) { if (m_json.isArray()) {
return m_json[m_fieldIt]; return m_json[m_fieldIt];
} else { } else {

View File

@ -13,6 +13,7 @@
#include <ox/model/fieldcounter.hpp> #include <ox/model/fieldcounter.hpp>
#include <ox/model/optype.hpp> #include <ox/model/optype.hpp>
#include <ox/model/types.hpp> #include <ox/model/types.hpp>
#include <ox/model/typenamecatcher.hpp>
#include <ox/std/buffer.hpp> #include <ox/std/buffer.hpp>
#include <ox/std/hashmap.hpp> #include <ox/std/hashmap.hpp>
#include <ox/std/string.hpp> #include <ox/std/string.hpp>
@ -22,7 +23,7 @@ namespace ox {
class OrganicClawWriter { class OrganicClawWriter {
template<typename T> template<typename T>
friend Result<Buffer> writeOC(T *val); friend Result<Buffer> writeOC(T *val) noexcept;
protected: protected:
Json::Value m_json; Json::Value m_json;
@ -30,62 +31,62 @@ class OrganicClawWriter {
int m_unionIdx = -1; int m_unionIdx = -1;
public: public:
OrganicClawWriter(int unionIdx = -1); explicit OrganicClawWriter(int unionIdx = -1) noexcept;
OrganicClawWriter(Json::Value json, int unionIdx = -1); explicit OrganicClawWriter(Json::Value json, int unionIdx = -1) noexcept;
Error field(const char*, int8_t *val); Error field(const char*, const int8_t *val) noexcept;
Error field(const char*, int16_t *val); Error field(const char*, const int16_t *val) noexcept;
Error field(const char*, int32_t *val); Error field(const char*, const int32_t *val) noexcept;
Error field(const char*, int64_t *val); Error field(const char*, const int64_t *val) noexcept;
Error field(const char*, uint8_t *val); Error field(const char*, const uint8_t *val) noexcept;
Error field(const char*, uint16_t *val); Error field(const char*, const uint16_t *val) noexcept;
Error field(const char*, uint32_t *val); Error field(const char*, const uint32_t *val) noexcept;
Error field(const char*, uint64_t *val); Error field(const char*, const uint64_t *val) noexcept;
Error field(const char*, bool *val); Error field(const char*, const bool *val) noexcept;
template<typename T> template<typename T>
Error field(const char*, T *val, std::size_t len); Error field(const char*, T *val, std::size_t len) noexcept;
template<typename U> template<typename U>
Error field(const char*, UnionView<U> val); Error field(const char*, UnionView<U> val) noexcept;
template<typename T> template<typename T>
Error field(const char*, HashMap<String, T> *val); Error field(const char*, const HashMap<String, T> *val) noexcept;
template<std::size_t L> template<std::size_t L>
Error field(const char*, BString<L> *val); Error field(const char*, const BString<L> *val) noexcept;
Error field(const char*, String *val); Error field(const char*, const String *val) noexcept;
Error field(const char*, SerStr val); Error field(const char*, SerStr val) noexcept;
template<typename T> template<typename T>
Error field(const char*, T *val); Error field(const char*, T *val) noexcept;
template<typename T = void> template<typename T = void>
constexpr void setTypeInfo(const char* = T::TypeName, int = countFields<T>()) { constexpr void setTypeInfo(const char* = T::TypeName, int = countFields<T>()) noexcept {
} }
static constexpr auto opType() { static constexpr auto opType() noexcept {
return OpType::Write; return OpType::Write;
} }
private: private:
[[nodiscard]] [[nodiscard]]
constexpr bool targetValid() noexcept { constexpr bool targetValid() const noexcept {
return static_cast<int>(m_fieldIt) == m_unionIdx || m_unionIdx == -1; return static_cast<int>(m_fieldIt) == m_unionIdx || m_unionIdx == -1;
} }
[[nodiscard]] [[nodiscard]]
Json::Value &value(const char *key); Json::Value &value(const char *key) noexcept;
}; };
template<typename T> template<typename T>
Error OrganicClawWriter::field(const char *key, T *val, std::size_t len) { Error OrganicClawWriter::field(const char *key, T *val, std::size_t len) noexcept {
if (targetValid()) { if (targetValid()) {
OrganicClawWriter w((Json::Value(Json::arrayValue))); OrganicClawWriter w((Json::Value(Json::arrayValue)));
for (std::size_t i = 0; i < len; ++i) { for (std::size_t i = 0; i < len; ++i) {
@ -98,12 +99,12 @@ Error OrganicClawWriter::field(const char *key, T *val, std::size_t len) {
} }
template<std::size_t L> template<std::size_t L>
Error OrganicClawWriter::field(const char *key, BString<L> *val) { Error OrganicClawWriter::field(const char *key, const BString<L> *val) noexcept {
return field(key, SerStr(val->data(), val->cap())); return field(key, SerStr(val->data(), val->cap()));
} }
template<typename T> template<typename T>
Error OrganicClawWriter::field(const char *key, T *val) { Error OrganicClawWriter::field(const char *key, T *val) noexcept {
if constexpr(isVector_v<T>) { if constexpr(isVector_v<T>) {
return field(key, val->data(), val->size()); return field(key, val->data(), val->size());
} else if (val && targetValid()) { } else if (val && targetValid()) {
@ -118,7 +119,7 @@ Error OrganicClawWriter::field(const char *key, T *val) {
} }
template<typename U> template<typename U>
Error OrganicClawWriter::field(const char *key, UnionView<U> val) { Error OrganicClawWriter::field(const char *key, UnionView<U> val) noexcept {
if (targetValid()) { if (targetValid()) {
OrganicClawWriter w(val.idx()); OrganicClawWriter w(val.idx());
oxReturnError(model(&w, val.get())); oxReturnError(model(&w, val.get()));
@ -131,7 +132,7 @@ Error OrganicClawWriter::field(const char *key, UnionView<U> val) {
} }
template<typename T> template<typename T>
Error OrganicClawWriter::field(const char *key, HashMap<String, T> *val) { Error OrganicClawWriter::field(const char *key, const HashMap<String, T> *val) noexcept {
if (targetValid()) { if (targetValid()) {
const auto &keys = val->keys(); const auto &keys = val->keys();
OrganicClawWriter w; OrganicClawWriter w;
@ -146,8 +147,9 @@ Error OrganicClawWriter::field(const char *key, HashMap<String, T> *val) {
} }
template<typename T> template<typename T>
Result<Buffer> writeOC(T *val) { Result<Buffer> writeOC(T *val) noexcept {
OrganicClawWriter writer; OrganicClawWriter writer;
writer.template setTypeInfo<T>();
oxReturnError(model(&writer, val)); oxReturnError(model(&writer, val));
Json::StreamWriterBuilder jsonBuilder; Json::StreamWriterBuilder jsonBuilder;
const auto str = Json::writeString(jsonBuilder, writer.m_json); const auto str = Json::writeString(jsonBuilder, writer.m_json);