From faaeb2b0b347b40ce456a951eec5e820587e5b02 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Fri, 26 Jun 2020 12:16:22 -0400 Subject: [PATCH] rpc: Add CRPCCommand constructor which takes RPCHelpMan This allows the constructor to ask the rpc manager for the name of the rpc method or the rpc argument names instead of having it manually passed in. --- src/rpc/server.h | 15 ++++++++++++++- src/rpc/util.cpp | 14 ++++++++++++++ src/rpc/util.h | 12 +++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/rpc/server.h b/src/rpc/server.h index d7a04ff6e8b..1c587ae88f2 100644 --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -85,6 +86,7 @@ void RPCUnsetTimerInterface(RPCTimerInterface *iface); void RPCRunLater(const std::string& name, std::function func, int64_t nSeconds); typedef UniValue(*rpcfn_type)(const JSONRPCRequest& jsonRequest); +typedef RPCHelpMan (*RpcMethodFnType)(); class CRPCCommand { @@ -101,6 +103,17 @@ public: { } + //! Simplified constructor taking plain RpcMethodFnType function pointer. + CRPCCommand(std::string category, std::string name_in, RpcMethodFnType fn, std::vector args_in) + : CRPCCommand( + category, + fn().m_name, + [fn](const JSONRPCRequest& request, UniValue& result, bool) { result = fn().HandleRequest(request); return true; }, + fn().GetArgNames(), + intptr_t(fn)) + { + } + //! Simplified constructor taking plain rpcfn_type function pointer. CRPCCommand(const char* category, const char* name, rpcfn_type fn, std::initializer_list args) : CRPCCommand(category, name, @@ -117,7 +130,7 @@ public: }; /** - * Bitcoin RPC command dispatcher. + * RPC command dispatcher. */ class CRPCTable { diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp index d476feb9625..fbd51784a72 100644 --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -418,7 +418,11 @@ struct Sections { }; RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector args, RPCResults results, RPCExamples examples) + : RPCHelpMan{std::move(name), std::move(description), std::move(args), std::move(results), std::move(examples), nullptr} {} + +RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector args, RPCResults results, RPCExamples examples, RPCMethodImpl fun) : m_name{std::move(name)}, + m_fun{std::move(fun)}, m_description{std::move(description)}, m_args{std::move(args)}, m_results{std::move(results)}, @@ -467,6 +471,16 @@ bool RPCHelpMan::IsValidNumArgs(size_t num_args) const } return num_required_args <= num_args && num_args <= m_args.size(); } + +std::vector RPCHelpMan::GetArgNames() const +{ + std::vector ret; + for (const auto& arg : m_args) { + ret.emplace_back(arg.m_names); + } + return ret; +} + std::string RPCHelpMan::ToString() const { std::string ret; diff --git a/src/rpc/util.h b/src/rpc/util.h index 53dce2c3972..49f603c8a1f 100644 --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -326,8 +326,14 @@ class RPCHelpMan { public: RPCHelpMan(std::string name, std::string description, std::vector args, RPCResults results, RPCExamples examples); + using RPCMethodImpl = std::function; + RPCHelpMan(std::string name, std::string description, std::vector args, RPCResults results, RPCExamples examples, RPCMethodImpl fun); std::string ToString() const; + UniValue HandleRequest(const JSONRPCRequest& request) + { + return m_fun(*this, request); + } /** If the supplied number of args is neither too small nor too high */ bool IsValidNumArgs(size_t num_args) const; /** @@ -340,8 +346,12 @@ public: } } -private: + std::vector GetArgNames() const; + const std::string m_name; + +private: + const RPCMethodImpl m_fun; const std::string m_description; const std::vector m_args; const RPCResults m_results;