95 lines
2.5 KiB
C++
95 lines
2.5 KiB
C++
@use crate::data::RPC;
|
|
@use crate::generators::cpp_s::*;
|
|
|
|
@(rpc: &RPC)
|
|
@for (ty, jty) in JSON_INNER_IMPLS {
|
|
inline @(ty)& operator<<(@ty &v, const rapidjson::Value &j) @{
|
|
if (!j.Is@(jty)())
|
|
throw std::exception@{@};
|
|
v = j.Get@(jty)();
|
|
return v;
|
|
@}
|
|
inline mrpc::MRPCJWriter& operator>>(const @ty &v, mrpc::MRPCJWriter &w) @{
|
|
w.@(jty)(v);
|
|
return w;
|
|
@}}
|
|
|
|
@for e in &rpc.enums {
|
|
inline mrpc::@e.name& operator<<(mrpc::@e.name &v, const rapidjson::Value &j) @{
|
|
((std::uint64_t&)v) << j;
|
|
return v;
|
|
@}
|
|
mrpc::MRPCJWriter& operator>>(const mrpc::@e.name &v, mrpc::MRPCJWriter &w) @{
|
|
w.Uint64((std::uint64_t)v);
|
|
return w;
|
|
@}
|
|
}
|
|
|
|
template<typename T>
|
|
inline std::vector<T>& operator<<(std::vector<T> &v, const rapidjson::Value &j);
|
|
template<typename T>
|
|
inline std::optional<T>& operator<<(std::optional<T> &v, const rapidjson::Value &j) @{
|
|
if (j.IsNull())
|
|
v = std::nullopt;
|
|
else @{
|
|
T t;
|
|
t << j;
|
|
v = std::move(t);
|
|
@}
|
|
return v;
|
|
@}
|
|
|
|
template<typename T>
|
|
inline std::vector<T>& operator<<(std::vector<T> &v, const rapidjson::Value &j) @{
|
|
if (!j.IsArray())
|
|
throw std::exception@{@};
|
|
for (const auto &e : j.GetArray()) @{
|
|
T t;
|
|
t << e;
|
|
v.push_back(std::move(t));
|
|
@}
|
|
return v;
|
|
@}
|
|
|
|
template<typename T>
|
|
inline mrpc::MRPCJWriter& operator>>(const std::vector<T> &v, mrpc::MRPCJWriter &w);
|
|
template<typename T>
|
|
inline mrpc::MRPCJWriter& operator>>(const std::optional<T> &v, mrpc::MRPCJWriter &w) @{
|
|
if (v.has_value())
|
|
v.value() >> w;
|
|
else
|
|
w.Null();
|
|
return w;
|
|
@}
|
|
|
|
template<typename T>
|
|
inline mrpc::MRPCJWriter& operator>>(const std::vector<T> &v, mrpc::MRPCJWriter &w) @{
|
|
w.StartArray();
|
|
for (const auto &e : v)
|
|
e >> w;
|
|
w.EndArray();
|
|
return w;
|
|
@}
|
|
|
|
inline const rapidjson::Value& json_get(const rapidjson::Value &j, const char *key) @{
|
|
auto member = j.FindMember(key);
|
|
if (member == j.MemberEnd())
|
|
throw std::exception@{@};
|
|
return member->value;
|
|
@}
|
|
|
|
namespace mrpc @{
|
|
@for s in &rpc.structs {
|
|
@get_struct_generics(s)MRPCJWriter& @(s.name)@(generics_brace(s))::operator>>(MRPCJWriter &__w) const @{
|
|
__w.StartObject();
|
|
@for f in &s.fields { __w.Key("@f.name", @f.name.len());
|
|
@f.name >> __w;
|
|
} __w.EndObject();
|
|
return __w;
|
|
@}
|
|
@get_struct_generics(s)@(s.name)@(generics_brace(s))& @(s.name)@(generics_brace(s))::operator<<(const rapidjson::Value &__j) @{
|
|
using namespace mrpc;
|
|
@for f in &s.fields { @f.name << json_get(__j, "@f.name");
|
|
} return *this;
|
|
@}
|
|
} |