[ox/oc] Add option for writeOC to return a string

This commit is contained in:
Gary Talent 2024-09-27 02:31:08 -05:00
parent 5373b63cca
commit 396fecab5b

View File

@ -24,7 +24,9 @@ namespace ox {
class OrganicClawWriter { class OrganicClawWriter {
friend Result<Buffer> writeOC(const auto &val) noexcept; template<typename T>
friend Result<T> writeOC(const auto &val) noexcept
requires(ox::is_same_v<T, ox::Buffer> || ox::is_same_v<T, ox::String>);
protected: protected:
Json::Value m_json{Json::Value(Json::objectValue)}; Json::Value m_json{Json::Value(Json::objectValue)};
@ -247,14 +249,17 @@ Error OrganicClawWriter::field(const char *key, UnionView<U, force> val) noexcep
return OxError(0); return OxError(0);
} }
Result<Buffer> writeOC(const auto &val) noexcept { template<typename T = ox::Buffer>
Result<T> writeOC(const auto &val) noexcept
requires(ox::is_same_v<T, ox::Buffer> || ox::is_same_v<T, ox::String>) {
OrganicClawWriter writer; OrganicClawWriter writer;
ModelHandlerInterface<OrganicClawWriter, OpType::Write> handler(&writer); ModelHandlerInterface<OrganicClawWriter, OpType::Write> handler(&writer);
oxReturnError(model(&handler, &val)); oxReturnError(model(&handler, &val));
Json::StreamWriterBuilder jsonBuilder; Json::StreamWriterBuilder const jsonBuilder;
const auto str = Json::writeString(jsonBuilder, writer.m_json); const auto str = Json::writeString(jsonBuilder, writer.m_json);
Buffer buff(str.size() + 1); Result<T> buff;
memcpy(buff.data(), str.c_str(), str.size() + 1); buff.value.resize(str.size() + ox::is_same_v<T, ox::Buffer>);
memcpy(buff.value.data(), str.data(), str.size() + 1);
return buff; return buff;
} }