[ox/mc,ox/oc] Fix write for ox::Array, cleanup

This commit is contained in:
Gary Talent 2023-12-04 21:43:06 -06:00
parent 402f54b3e3
commit 0d606643f5
3 changed files with 41 additions and 110 deletions

View File

@ -43,39 +43,30 @@ class MetalClawWriter {
constexpr ~MetalClawWriter() noexcept = default;
constexpr Error field(const char*, CommonPtrWith<int8_t> auto *val) noexcept;
constexpr Error field(const char*, CommonPtrWith<int16_t> auto *val) noexcept;
constexpr Error field(const char*, CommonPtrWith<int32_t> auto *val) noexcept;
constexpr Error field(const char*, CommonPtrWith<int64_t> auto *val) noexcept;
constexpr Error field(const char*, const int8_t *val) noexcept;
constexpr Error field(const char*, const int16_t *val) noexcept;
constexpr Error field(const char*, const int32_t *val) noexcept;
constexpr Error field(const char*, const int64_t *val) noexcept;
constexpr Error field(const char*, CommonPtrWith<uint8_t> auto *val) noexcept;
constexpr Error field(const char*, CommonPtrWith<uint16_t> auto *val) noexcept;
constexpr Error field(const char*, CommonPtrWith<uint32_t> auto *val) noexcept;
constexpr Error field(const char*, CommonPtrWith<uint64_t> auto *val) noexcept;
constexpr Error field(const char*, const uint8_t *val) noexcept;
constexpr Error field(const char*, const uint16_t *val) noexcept;
constexpr Error field(const char*, const uint32_t *val) noexcept;
constexpr Error field(const char*, const uint64_t *val) noexcept;
constexpr Error field(const char*, CommonPtrWith<bool> auto *val) noexcept;
constexpr Error field(const char*, const bool *val) noexcept;
template<typename T>
constexpr Error field(const char*, T *val, std::size_t len) noexcept;
constexpr Error field(const char*, const T *val, std::size_t len) noexcept;
template<typename T>
constexpr Error field(const char *name, const HashMap<String, T> *val) noexcept;
template<typename T>
constexpr Error field(const char*, HashMap<String, T> *val) noexcept;
template<std::size_t SmallStringSize>
constexpr Error field(const char*, const BasicString<SmallStringSize> *val) noexcept;
template<std::size_t L>
constexpr Error field(const char*, const BString<L> *val) noexcept;
template<std::size_t SmallStringSize>
constexpr Error field(const char*, BasicString<SmallStringSize> *val) noexcept;
template<std::size_t L>
constexpr Error field(const char*, BString<L> *val) noexcept;
constexpr Error fieldCString(const char *name, const char *const*val, std::size_t buffLen) noexcept;
constexpr Error fieldCString(const char *name, const char **val) noexcept;
@ -84,20 +75,8 @@ class MetalClawWriter {
constexpr Error fieldCString(const char *name, const char *val, std::size_t len) noexcept;
constexpr Error fieldCString(const char *name, char **val, std::size_t buffLen) noexcept {
return fieldCString(name, const_cast<const char**>(val), buffLen);
}
constexpr Error fieldCString(const char *name, char **val) noexcept {
return fieldCString(name, const_cast<const char**>(val));
}
constexpr Error fieldCString(const char *name, char *val, std::size_t buffLen) noexcept {
return fieldCString(name, const_cast<const char*>(val), buffLen);
}
template<typename T>
constexpr Error field(const char*, T *val) noexcept;
constexpr Error field(const char*, const T *val) noexcept;
template<typename U, bool force = false>
constexpr Error field(const char*, UnionView<U, force> val) noexcept;
@ -159,47 +138,47 @@ constexpr MetalClawWriter<Writer>::MetalClawWriter(Writer &writer, int unionIdx)
}
template<Writer_c Writer>
constexpr Error MetalClawWriter<Writer>::field(const char*, CommonPtrWith<int8_t> auto *val) noexcept {
constexpr Error MetalClawWriter<Writer>::field(const char*, const int8_t *val) noexcept {
return appendInteger(*val);
}
template<Writer_c Writer>
constexpr Error MetalClawWriter<Writer>::field(const char*, CommonPtrWith<int16_t> auto *val) noexcept {
constexpr Error MetalClawWriter<Writer>::field(const char*, const int16_t *val) noexcept {
return appendInteger(*val);
}
template<Writer_c Writer>
constexpr Error MetalClawWriter<Writer>::field(const char*, CommonPtrWith<int32_t> auto *val) noexcept {
constexpr Error MetalClawWriter<Writer>::field(const char*, const int32_t *val) noexcept {
return appendInteger(*val);
}
template<Writer_c Writer>
constexpr Error MetalClawWriter<Writer>::field(const char*, CommonPtrWith<int64_t> auto *val) noexcept {
constexpr Error MetalClawWriter<Writer>::field(const char*, const int64_t *val) noexcept {
return appendInteger(*val);
}
template<Writer_c Writer>
constexpr Error MetalClawWriter<Writer>::field(const char*, CommonPtrWith<uint8_t> auto *val) noexcept {
constexpr Error MetalClawWriter<Writer>::field(const char*, const uint8_t *val) noexcept {
return appendInteger(*val);
}
template<Writer_c Writer>
constexpr Error MetalClawWriter<Writer>::field(const char*, CommonPtrWith<uint16_t> auto *val) noexcept {
constexpr Error MetalClawWriter<Writer>::field(const char*, const uint16_t *val) noexcept {
return appendInteger(*val);
}
template<Writer_c Writer>
constexpr Error MetalClawWriter<Writer>::field(const char*, CommonPtrWith<uint32_t> auto *val) noexcept {
constexpr Error MetalClawWriter<Writer>::field(const char*, const uint32_t *val) noexcept {
return appendInteger(*val);
}
template<Writer_c Writer>
constexpr Error MetalClawWriter<Writer>::field(const char*, CommonPtrWith<uint64_t> auto *val) noexcept {
constexpr Error MetalClawWriter<Writer>::field(const char*, const uint64_t *val) noexcept {
return appendInteger(*val);
}
template<Writer_c Writer>
constexpr Error MetalClawWriter<Writer>::field(const char*, CommonPtrWith<bool> auto *val) noexcept {
constexpr Error MetalClawWriter<Writer>::field(const char*, const bool *val) noexcept {
if (m_unionIdx == -1 || m_unionIdx == m_field) {
oxReturnError(m_fieldPresence.set(static_cast<std::size_t>(m_field), *val));
}
@ -230,18 +209,6 @@ constexpr Error MetalClawWriter<Writer>::field(const char *name, const BString<L
return fieldCString(name, val->data(), val->cap());
}
template<Writer_c Writer>
template<std::size_t SmallStringSize>
constexpr Error MetalClawWriter<Writer>::field(const char *name, BasicString<SmallStringSize> *val) noexcept {
return field(name, const_cast<const BasicString<SmallStringSize>*>(val));
}
template<Writer_c Writer>
template<std::size_t L>
constexpr Error MetalClawWriter<Writer>::field(const char *name, BString<L> *val) noexcept {
return fieldCString(name, val->data(), val->cap());
}
template<Writer_c Writer>
constexpr Error MetalClawWriter<Writer>::fieldCString(const char*, const char *const*val, std::size_t) noexcept {
bool fieldSet = false;
@ -287,7 +254,7 @@ constexpr Error MetalClawWriter<Writer>::fieldCString(const char*, const char *v
template<Writer_c Writer>
template<typename T>
constexpr Error MetalClawWriter<Writer>::field(const char*, T *val) noexcept {
constexpr Error MetalClawWriter<Writer>::field(const char*, const T *val) noexcept {
if constexpr(isVector_v<T> || isArray_v<T>) {
return field(nullptr, val->data(), val->size());
} else {
@ -323,7 +290,7 @@ constexpr Error MetalClawWriter<Writer>::field(const char*, UnionView<U, force>
template<Writer_c Writer>
template<typename T>
constexpr Error MetalClawWriter<Writer>::field(const char*, T *val, std::size_t len) noexcept {
constexpr Error MetalClawWriter<Writer>::field(const char*, const T *val, std::size_t len) noexcept {
bool fieldSet = false;
if (len && (m_unionIdx == -1 || m_unionIdx == m_field)) {
// write the length
@ -334,7 +301,7 @@ constexpr Error MetalClawWriter<Writer>::field(const char*, T *val, std::size_t
oxReturnError(handler.template setTypeInfo<T>("List", 0, {}, static_cast<std::size_t>(len)));
// write the array
for (std::size_t i = 0; i < len; i++) {
oxReturnError(handler.field("", &val[i]));
oxReturnError(handler.field("", const_cast<const T*>(&val[i])));
}
oxReturnError(writer.finalize());
fieldSet = true;
@ -381,12 +348,6 @@ constexpr Error MetalClawWriter<Writer>::field(const char*, const HashMap<String
return OxError(0);
}
template<Writer_c Writer>
template<typename T>
constexpr Error MetalClawWriter<Writer>::field(const char *name, HashMap<String, T> *val) noexcept {
return field(name, const_cast<const HashMap<String, T>*>(val));
}
template<Writer_c Writer>
template<typename T>
constexpr ox::Error MetalClawWriter<Writer>::setTypeInfo(
@ -421,7 +382,7 @@ 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(auto const&val, std::size_t buffReserveSz = 2 * units::KB) noexcept {
Buffer buff(buffReserveSz);
BufferWriter bw(&buff, 0);
oxReturnError(writeMC(bw, val));
@ -429,7 +390,7 @@ Result<Buffer> writeMC(const auto &val, std::size_t buffReserveSz = 2 * units::K
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, auto const&val, std::size_t *sizeOut = nullptr) noexcept {
CharBuffWriter bw(buff, buffLen);
oxReturnError(writeMC(bw, val));
if (sizeOut) {

View File

@ -26,18 +26,10 @@ Error OrganicClawWriter::fieldCString(const char *key, const char *const*val, in
return OxError(0);
}
Error OrganicClawWriter::fieldCString(const char *key, char **val, int len) noexcept {
return fieldCString(key, const_cast<const char**>(val), len);
}
Error OrganicClawWriter::fieldCString(const char *key, const char *const*val) noexcept {
return fieldCString(key, const_cast<const char**>(val), {});
}
Error OrganicClawWriter::fieldCString(const char *key, char **val) noexcept {
return fieldCString(key, const_cast<const char**>(val), {});
}
Error OrganicClawWriter::field(const char *key, const UUID *uuid) noexcept {
const auto uuidStr = uuid->toString();
if (targetValid() && uuidStr.len()) {

View File

@ -36,7 +36,7 @@ class OrganicClawWriter {
explicit OrganicClawWriter(Json::Value json, int unionIdx = -1) noexcept;
Error field(const char *key, CommonPtrWith<int8_t> auto *val) noexcept {
Error field(const char *key, const int8_t *val) noexcept {
if (*val) {
value(key) = *val;
}
@ -44,7 +44,7 @@ class OrganicClawWriter {
return OxError(0);
}
Error field(const char *key, CommonPtrWith<int16_t> auto *val) noexcept {
Error field(const char *key, const int16_t *val) noexcept {
if (*val) {
value(key) = *val;
}
@ -52,7 +52,7 @@ class OrganicClawWriter {
return OxError(0);
}
Error field(const char *key, CommonPtrWith<int32_t> auto *val) noexcept {
Error field(const char *key, const int32_t *val) noexcept {
if (*val) {
value(key) = *val;
}
@ -60,7 +60,7 @@ class OrganicClawWriter {
return OxError(0);
}
Error field(const char *key, CommonPtrWith<int64_t> auto *val) noexcept {
Error field(const char *key, const int64_t *val) noexcept {
if (*val) {
value(key) = *val;
}
@ -69,7 +69,7 @@ class OrganicClawWriter {
}
Error field(const char *key, CommonPtrWith<uint8_t> auto *val) noexcept {
Error field(const char *key, const uint8_t *val) noexcept {
if (*val) {
value(key) = *val;
}
@ -77,7 +77,7 @@ class OrganicClawWriter {
return OxError(0);
}
Error field(const char *key, CommonPtrWith<uint16_t> auto *val) noexcept {
Error field(const char *key, const uint16_t *val) noexcept {
if (targetValid() && *val) {
value(key) = *val;
}
@ -85,7 +85,7 @@ class OrganicClawWriter {
return OxError(0);
}
Error field(const char *key, CommonPtrWith<uint32_t> auto *val) noexcept {
Error field(const char *key, const uint32_t *val) noexcept {
if (targetValid() && *val) {
value(key) = *val;
}
@ -93,7 +93,7 @@ class OrganicClawWriter {
return OxError(0);
}
Error field(const char *key, CommonPtrWith<uint64_t> auto *val) noexcept {
Error field(const char *key, const uint64_t *val) noexcept {
if (targetValid() && *val) {
value(key) = *val;
}
@ -101,7 +101,7 @@ class OrganicClawWriter {
return OxError(0);
}
Error field(const char *key, CommonPtrWith<bool> auto *val) noexcept {
Error field(const char *key, const bool *val) noexcept {
if (targetValid() && *val) {
value(key) = *val;
}
@ -109,9 +109,6 @@ class OrganicClawWriter {
return {};
}
template<typename T>
Error field(const char*, T *val, std::size_t len) noexcept;
template<typename U, bool force = true>
Error field(const char*, UnionView<U, force> val) noexcept;
@ -134,11 +131,6 @@ class OrganicClawWriter {
return {};
}
template<typename T>
Error field(const char *key, HashMap<String, T> *val) noexcept {
return field(key, const_cast<const HashMap<String, T>*>(val));
}
template<std::size_t L>
Error field(const char *key, const BString<L> *val) noexcept {
if (targetValid() && val->len()) {
@ -148,15 +140,6 @@ class OrganicClawWriter {
return {};
}
template<std::size_t L>
Error field(const char *key, BString<L> *val) noexcept {
if (targetValid() && val->len()) {
value(key) = val->c_str();
}
++m_fieldIt;
return {};
}
template<std::size_t L>
Error field(const char *key, const BasicString<L> *val) noexcept {
if (targetValid() && val->len()) {
@ -166,23 +149,17 @@ class OrganicClawWriter {
return OxError(0);
}
template<std::size_t L>
Error field(const char *key, BasicString<L> *val) noexcept {
return field(key, const_cast<const BasicString<L>*>(val));
}
Error fieldCString(const char*, const char *const*val, int len) noexcept;
Error fieldCString(const char *name, char **val, int len) noexcept;
Error fieldCString(const char *name, const char *const*val) noexcept;
Error fieldCString(const char *name, char **val) noexcept;
Error field(const char *key, const UUID *uuid) noexcept;
template<typename T>
Error field(const char*, T *val) noexcept;
Error field(const char*, const T *val, std::size_t len) noexcept;
template<typename T>
Error field(const char*, const T *val) noexcept;
template<typename T>
constexpr ox::Error setTypeInfo(
@ -200,6 +177,7 @@ class OrganicClawWriter {
return {};
}
[[nodiscard]]
static constexpr auto opType() noexcept {
return OpType::Write;
}
@ -216,7 +194,7 @@ class OrganicClawWriter {
};
template<typename T>
Error OrganicClawWriter::field(const char *key, T *val, std::size_t len) noexcept {
Error OrganicClawWriter::field(const char *key, const T *val, std::size_t len) noexcept {
if (targetValid() && len) {
OrganicClawWriter w((Json::Value(Json::arrayValue)));
ModelHandlerInterface handler{&w};
@ -230,7 +208,7 @@ Error OrganicClawWriter::field(const char *key, T *val, std::size_t len) noexcep
}
template<typename T>
Error OrganicClawWriter::field(const char *key, T *val) noexcept {
Error OrganicClawWriter::field(const char *key, const T *val) noexcept {
if constexpr(isVector_v<T> || isArray_v<T>) {
return field(key, val->data(), val->size());
} else if (val && targetValid()) {