From 762804905afe0cb4e2b108d68d55cc551203bfe1 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 16 Feb 2023 01:36:20 -0600 Subject: [PATCH] [ox] Make UUID serializable, and make serialize as a string in OC --- deps/ox/src/ox/oc/read.cpp | 6 ++++++ deps/ox/src/ox/oc/read.hpp | 3 +++ deps/ox/src/ox/oc/write.cpp | 9 +++++++++ deps/ox/src/ox/oc/write.hpp | 7 +++++-- deps/ox/src/ox/std/bstring.hpp | 9 +++++++++ deps/ox/src/ox/std/uuid.hpp | 11 ++++++++++- 6 files changed, 42 insertions(+), 3 deletions(-) diff --git a/deps/ox/src/ox/oc/read.cpp b/deps/ox/src/ox/oc/read.cpp index 8fbd922e..40e56f9a 100644 --- a/deps/ox/src/ox/oc/read.cpp +++ b/deps/ox/src/ox/oc/read.cpp @@ -290,6 +290,12 @@ Error OrganicClawReader::fieldCString(const char *key, char **val, std::size_t b return err; } +Error OrganicClawReader::field(const char *key, UUID *val) noexcept { + UUIDStr str; + oxReturnError(field(key, &str)); + return UUID::fromString(str).moveTo(val); +} + Result OrganicClawReader::arrayLength(const char *key, bool) noexcept { const auto &jv = value(key); if (jv.empty()) { diff --git a/deps/ox/src/ox/oc/read.hpp b/deps/ox/src/ox/oc/read.hpp index e29cfe22..828e933f 100644 --- a/deps/ox/src/ox/oc/read.hpp +++ b/deps/ox/src/ox/oc/read.hpp @@ -21,6 +21,7 @@ #include #include #include +#include namespace ox { @@ -79,6 +80,8 @@ class OrganicClawReader { Error fieldCString(const char *key, char **val, std::size_t buffLen) noexcept; + Error field(const char *key, UUID *val) noexcept; + /** * Reads an array length from the current location in the buffer. * @param pass indicates that the parsing should iterate past the array length diff --git a/deps/ox/src/ox/oc/write.cpp b/deps/ox/src/ox/oc/write.cpp index 52daa63b..5dcbda9c 100644 --- a/deps/ox/src/ox/oc/write.cpp +++ b/deps/ox/src/ox/oc/write.cpp @@ -46,6 +46,15 @@ Error OrganicClawWriter::fieldCString(const char *key, char **val) noexcept { return fieldCString(key, const_cast(val), {}); } +Error OrganicClawWriter::field(const char *key, const UUID *uuid) noexcept { + const auto uuidStr = uuid->toString(); + if (targetValid() && uuidStr.len()) { + value(key) = uuidStr.c_str(); + } + ++m_fieldIt; + return {}; +} + Json::Value &OrganicClawWriter::value(const char *key) noexcept { if (m_json.isArray()) { return m_json[m_fieldIt]; diff --git a/deps/ox/src/ox/oc/write.hpp b/deps/ox/src/ox/oc/write.hpp index 1801a6eb..05cd333d 100644 --- a/deps/ox/src/ox/oc/write.hpp +++ b/deps/ox/src/ox/oc/write.hpp @@ -18,6 +18,7 @@ #include #include #include +#include namespace ox { @@ -105,7 +106,7 @@ class OrganicClawWriter { value(key) = *val; } ++m_fieldIt; - return OxError(0); + return {}; } template @@ -130,7 +131,7 @@ class OrganicClawWriter { value(key) = w.m_json; } ++m_fieldIt; - return OxError(0); + return {}; } template @@ -172,6 +173,8 @@ class OrganicClawWriter { Error fieldCString(const char *name, char **val) noexcept; + Error field(const char *key, const UUID *uuid) noexcept; + template Error field(const char*, T *val) noexcept; diff --git a/deps/ox/src/ox/std/bstring.hpp b/deps/ox/src/ox/std/bstring.hpp index 4d9b8e31..a228e3df 100644 --- a/deps/ox/src/ox/std/bstring.hpp +++ b/deps/ox/src/ox/std/bstring.hpp @@ -64,6 +64,10 @@ class BString { constexpr Error append(const char *str, std::size_t strLen) noexcept; + [[nodiscard]] + constexpr const char *data() const noexcept; + + [[nodiscard]] constexpr char *data() noexcept; [[nodiscard]] @@ -228,6 +232,11 @@ constexpr Error BString::append(const char *str, std::size_t strLen) no return err; } +template +constexpr const char *BString::data() const noexcept { + return static_cast(m_buff); +} + template constexpr char *BString::data() noexcept { return static_cast(m_buff); diff --git a/deps/ox/src/ox/std/uuid.hpp b/deps/ox/src/ox/std/uuid.hpp index aa5d711a..0e227e63 100644 --- a/deps/ox/src/ox/std/uuid.hpp +++ b/deps/ox/src/ox/std/uuid.hpp @@ -90,7 +90,9 @@ constexpr ox::BString<2> toHex(uint8_t v) noexcept { } class UUID { - private: + template + friend constexpr Error model(T *io, ox::CommonPtrWith auto *obj) noexcept; + protected: static bool s_seeded; static Random s_rand; ox::Array m_value; @@ -158,4 +160,11 @@ class UUID { } }; +template +constexpr Error model(T *io, ox::CommonPtrWith auto *obj) noexcept { + io->template setTypeInfo(); + oxReturnError(io->field("value", &obj->m_value)); + return {}; +} + }