From b498f4f4d91ff09120bfce6a10f7362fc40a011a Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Thu, 20 Feb 2025 12:33:59 +0000 Subject: Add support for more RPC data types. --- include/modules/rpc.h | 69 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 9 deletions(-) (limited to 'include') diff --git a/include/modules/rpc.h b/include/modules/rpc.h index 092f98f59..35db305c7 100644 --- a/include/modules/rpc.h +++ b/include/modules/rpc.h @@ -10,11 +10,69 @@ #include "httpd.h" +#include + +class RPCBlock +{ +public: + /** Represents possible types of RPC value. */ + using RPCValue = std::variant; + + /** Retrieves the list of RPC replies. */ + inline const auto &GetReplies() const { return this->replies; } + + template + inline RPCBlock &Reply(const Anope::string &key, const Stringable &value) + { + this->replies.emplace(key, Anope::ToString(value)); + return *this; + } + + inline RPCBlock &ReplyBlock(const Anope::string &key) + { + auto it = this->replies.emplace(key, RPCBlock()); + return std::get(it.first->second); + } + + inline RPCBlock &ReplyBool(const Anope::string &key, bool value) + { + this->replies.emplace(key, value); + return *this; + } + + inline RPCBlock &ReplyFloat(const Anope::string &key, double value) + { + this->replies.emplace(key, value); + return *this; + } + + inline RPCBlock &ReplyInt(const Anope::string &key, int64_t value) + { + this->replies.emplace(key, value); + return *this; + } + + inline RPCBlock &ReplyNull(const Anope::string &key) + { + this->replies.emplace(key, nullptr); + return *this; + } + + inline RPCBlock &ReplyUInt(const Anope::string &key, uint64_t value) + { + this->replies.emplace(key, value); + return *this; + } + +private: + Anope::map replies; +}; + class RPCRequest final + : public RPCBlock { private: std::optional> error; - std::map replies; public: Anope::string name; @@ -32,14 +90,7 @@ public: this->error.emplace(errcode, errstr); } - inline void Reply(const Anope::string &dname, const Anope::string &ddata) - { - this->replies.emplace(dname, ddata); - } - - inline const auto &GetError() { return this->error; } - - inline const auto &GetReplies() { return this->replies; } + inline const auto &GetError() const { return this->error; } }; class RPCServiceInterface; -- cgit