From 2ccd182d2e6689877eba911452eaa8a06e82ac0b Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sun, 23 Feb 2025 02:34:05 +0000 Subject: Add support for RPC arrays, simplify the RPC objects. --- include/modules/rpc.h | 120 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 96 insertions(+), 24 deletions(-) (limited to 'include') diff --git a/include/modules/rpc.h b/include/modules/rpc.h index c9f9200a7..3af6e81c5 100644 --- a/include/modules/rpc.h +++ b/include/modules/rpc.h @@ -14,13 +14,15 @@ namespace RPC { + class Array; class Event; class Map; class Request; class ServiceInterface; + class Value; /** Represents possible types of RPC value. */ - using Value = std::variant; + using ValueUnion = std::variant; /** Represents standard RPC errors from the JSON-RPC and XML-RPC specifications. */ enum Error @@ -35,57 +37,103 @@ namespace RPC }; } +class RPC::Array final +{ +private: + std::vector replies; + +public: + /** Retrieves the list of RPC replies. */ + inline const auto &GetReplies() const { return this->replies; } + + template + inline Array &Reply(const T &t) + { + this->replies.emplace_back(RPC::Value(t)); + return *this; + } + + inline Array &ReplyArray(); + + inline Map &ReplyMap(); +}; + class RPC::Map { +private: + Anope::map replies; + public: /** Retrieves the list of RPC replies. */ inline const auto &GetReplies() const { return this->replies; } - template - inline Map &Reply(const Anope::string &key, const Stringable &value) + template + inline Map &Reply(const Anope::string &key, const T &t) { - this->replies.emplace(key, Anope::ToString(value)); + this->replies.emplace(key, RPC::Value(t)); return *this; } - inline Map &ReplyMap(const Anope::string &key) + inline Array &ReplyArray(const Anope::string &key); + + inline Map &ReplyMap(const Anope::string &key); +}; + +class RPC::Value final +{ +private: + RPC::ValueUnion value; + +public: + explicit Value(const ValueUnion &v) + : value(v) { - auto it = this->replies.emplace(key, Map()); - return std::get(it.first->second); } - inline Map &ReplyBool(const Anope::string &key, bool value) + explicit Value(const Array &a) + : value(a) { - this->replies.emplace(key, value); - return *this; } - inline Map &ReplyFloat(const Anope::string &key, double value) + explicit Value(const Map &m) + : value(m) { - this->replies.emplace(key, value); - return *this; } - inline Map &ReplyInt(const Anope::string &key, int64_t value) + explicit Value(std::nullptr_t) + : value(nullptr) { - this->replies.emplace(key, value); - return *this; } - inline Map &ReplyNull(const Anope::string &key) + explicit Value(bool b) + : value(b) { - this->replies.emplace(key, nullptr); - return *this; } - inline Map &ReplyUInt(const Anope::string &key, uint64_t value) + Value(double d) + : value(d) { - this->replies.emplace(key, value); - return *this; } -private: - Anope::map replies; + Value(int64_t i) + : value(i) + { + } + + Value(uint64_t u) + : value(u) + { + } + + template + Value(const T &t) + : value(Anope::ToString(t)) + { + } + + inline auto &Get() { return this->value; } + + inline const auto &Get() const { return this->value; } }; class RPC::Request final @@ -147,3 +195,27 @@ public: virtual void Reply(Request &request) = 0; }; + +inline RPC::Array &RPC::Array::ReplyArray() +{ + auto &reply = this->replies.emplace_back(RPC::Array()); + return std::get(reply.Get()); +} + +inline RPC::Map &RPC::Array::ReplyMap() +{ + auto &reply = this->replies.emplace_back(RPC::Map()); + return std::get(reply.Get()); +} + +inline RPC::Array &RPC::Map::ReplyArray(const Anope::string &key) +{ + auto it = this->replies.emplace(key, RPC::Array()); + return std::get(it.first->second.Get()); +} + +inline RPC::Map &RPC::Map::ReplyMap(const Anope::string &key) +{ + auto it = this->replies.emplace(key, RPC::Map()); + return std::get(it.first->second.Get()); +} -- cgit