diff options
author | Sadie Powell <sadie@witchery.services> | 2025-02-23 02:34:05 +0000 |
---|---|---|
committer | Sadie Powell <sadie@witchery.services> | 2025-02-24 03:07:22 +0000 |
commit | 2ccd182d2e6689877eba911452eaa8a06e82ac0b (patch) | |
tree | d460bb3ea66ba981f2bd9092371d8a351ed9615a /include/modules | |
parent | ee08b3e8804074c73245ca24c3663e43158eb790 (diff) |
Add support for RPC arrays, simplify the RPC objects.
Diffstat (limited to 'include/modules')
-rw-r--r-- | include/modules/rpc.h | 120 |
1 files changed, 96 insertions, 24 deletions
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<Map, Anope::string, std::nullptr_t, bool, double, int64_t, uint64_t>; + using ValueUnion = std::variant<Array, Map, Anope::string, std::nullptr_t, bool, double, int64_t, uint64_t>; /** 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<Value> replies; + +public: + /** Retrieves the list of RPC replies. */ + inline const auto &GetReplies() const { return this->replies; } + + template <typename T> + 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<Value> replies; + public: /** Retrieves the list of RPC replies. */ inline const auto &GetReplies() const { return this->replies; } - template <typename Stringable> - inline Map &Reply(const Anope::string &key, const Stringable &value) + template <typename T> + 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<Map>(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<Value> replies; + Value(int64_t i) + : value(i) + { + } + + Value(uint64_t u) + : value(u) + { + } + + template <typename T> + 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<RPC::Array>(reply.Get()); +} + +inline RPC::Map &RPC::Array::ReplyMap() +{ + auto &reply = this->replies.emplace_back(RPC::Map()); + return std::get<RPC::Map>(reply.Get()); +} + +inline RPC::Array &RPC::Map::ReplyArray(const Anope::string &key) +{ + auto it = this->replies.emplace(key, RPC::Array()); + return std::get<RPC::Array>(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<RPC::Map>(it.first->second.Get()); +} |