mrpc/templates/cpp_server.rs.hxx

71 lines
1.9 KiB
C++
Raw Normal View History

@use itertools::Itertools;
@use crate::data::RPC;
@use crate::generators::cpp_s::*;
@(rpc: &RPC)
#pragma once
#ifndef MRPC_GEN_H
#define MRPC_GEN_H
#include <memory>
#include <string>
#include <vector>
#include <optional>
#include <cstdint>
#include <cmath>
#include <corvusoft/restbed/byte.hpp>
2023-10-02 07:50:27 +00:00
#define RAPIDJSON_HAS_STDSTRING 1
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include <rapidjson/document.h>
namespace restbed @{
class Resource;
class Session;
@}
namespace mrpc @{
using MRPCJWriter = rapidjson::Writer<rapidjson::StringBuffer>;
@for e in &rpc.enums {
enum struct @e.name : std::uint64_t @{
@e.values.iter().map(|(k,v)| format!("{k} = {v}")).join(",\n ")
@};
}
@for s in &rpc.structs {@get_struct_generics(s)struct @s.name;
}
@for s in &rpc.structs {
@get_struct_generics(s)struct @s.name @{
@for f in &s.fields { @ty_to_str(&f.ty) @f.name;
}
2023-10-02 07:50:27 +00:00
MRPCJWriter& operator >>(MRPCJWriter&) const;
@(s.name)& operator <<(const rapidjson::Value&);
@};}
@if streams_required(rpc).len() > 0 {
struct MRPCStreamImpl @{
void close() const noexcept;
bool is_open() const noexcept;
protected:
explicit MRPCStreamImpl(const std::shared_ptr<restbed::Session> &conn);
std::shared_ptr<restbed::Session> conn;
@};
template<typename T>
struct MRPCStream final : MRPCStreamImpl @{
explicit MRPCStream(const std::shared_ptr<restbed::Session> &conn) : MRPCStreamImpl(conn) @{@}
void send(const T &v) const noexcept;
@};
@for s in streams_required(rpc) {template struct MRPCStream<@(s)>;
}}
struct MRPCServer @{
MRPCServer() = delete;
explicit MRPCServer(std::shared_ptr<restbed::Resource>&);
private:
@for s in &rpc.services {@for m in &s.methods { virtual @method_ret(m) @(s.name)_@(m.name)(@method_args(m)) = 0;
}}
virtual void msg_handler(std::shared_ptr<restbed::Session>, const restbed::Bytes&) final;
@};
@}
#endif // MRPC_GEN_H