mrpc/templates/cpp_server_json.rs.cpp

122 lines
3.1 KiB
C++

@use crate::data::RPC;
@use crate::generators::cpp_s::*;
@(rpc: &RPC)
template<typename T>
void json_get(const rapidjson::Value &j, const char *key, T &v);
template<typename T>
void json_get_inner(const rapidjson::Value&, T &v) = delete;
template<>
inline void json_get_inner(const rapidjson::Value &member, std::string &v) @{
if (!member.IsString())
throw std::exception@{@};
v = member.GetString();
@}
@for i in [8, 16, 32] {
template<>
inline void json_get_inner(const rapidjson::Value &member, std::int@(i)_t &v) @{
if (!member.IsInt())
throw std::exception@{@};
v = member.GetInt();
@}
template<>
inline void json_get_inner(const rapidjson::Value &member, std::uint@(i)_t& v) @{
if (!member.IsUint())
throw std::exception@{@};
v = member.GetUint();
@}
}
template<>
inline void json_get_inner(const rapidjson::Value &member, std::int64_t &v) @{
if (!member.IsInt64())
throw std::exception@{@};
v = member.GetInt64();
@}
template<>
inline void json_get_inner(const rapidjson::Value &member, std::uint64_t& v) @{
if (!member.IsUint64())
throw std::exception@{@};
v = member.GetUint64();
@}
template<>
inline void json_get_inner(const rapidjson::Value &member, bool &v) @{
if (!member.IsBool())
throw std::exception@{@};
v = member.GetBool();
@}
template<>
inline void json_get_inner(const rapidjson::Value &member, double &v) @{
if (!member.IsDouble())
throw std::exception@{@};
v = member.GetDouble();
@}
template<typename T>
inline void json_get_inner(const rapidjson::Value &member, std::optional<T> &v) @{
if (member.IsNull())
v = std::nullopt;
else @{
T t;
json_get_inner<T>(member, t);
v = std::move(t);
@}
@}
template<typename T>
inline void json_get_inner(const rapidjson::Value &member, std::vector<T> &v) @{
if (!member.IsArray())
throw std::exception@{@};
for (const auto &j : member.GetArray()) @{
T t;
json_get_inner<T>(j, t);
v.push_back(std::move(t));
@}
@}
@for s in &rpc.structs {
template<>
inline void json_get_inner(const rapidjson::Value &__j, mrpc::@s.name &v) @{
using namespace mrpc;
@for f in &s.fields { json_get<@ty_to_str(&f.ty)>(__j, "@f.name", v.@f.name);
}@}
}
@for e in &rpc.enums {
template<>
inline void json_get_inner(const rapidjson::Value &j, mrpc::@e.name &v) @{
json_get_inner<std::uint64_t>(j, (std::uint64_t&)v);
@}
mrpc::MRPCJWriter& operator >>(const mrpc::@e.name &v, mrpc::MRPCJWriter &w) @{
w.Uint64((std::uint64_t)v);
return w;
@}
}
template<typename T>
inline void json_get(const rapidjson::Value &j, const char *key, T &v) @{
auto member = j.FindMember(key);
if (member == j.MemberEnd())
throw std::exception@{@};
json_get_inner(member->value, v);
@}
namespace mrpc @{
@for s in &rpc.structs {
MRPCJWriter& @(s.name)::operator >>(MRPCJWriter &__w) const @{
__w.StartObject();
@for f in &s.fields { __w.Key("@f.name");
@json_write(&f)
} __w.EndObject();
return __w;
@}
@(s.name)& @(s.name)::operator <<(const rapidjson::Value &__j) @{ json_get_inner<@(s.name)>(__j, *this); return *this; @}
}