mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-11 12:22:39 -03:00
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.
This commit is contained in:
parent
fa8ec00061
commit
faaeb2b0b3
3 changed files with 39 additions and 2 deletions
|
@ -8,6 +8,7 @@
|
|||
|
||||
#include <amount.h>
|
||||
#include <rpc/request.h>
|
||||
#include <rpc/util.h>
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
|
@ -85,6 +86,7 @@ void RPCUnsetTimerInterface(RPCTimerInterface *iface);
|
|||
void RPCRunLater(const std::string& name, std::function<void()> 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<std::string> 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<const char*> args)
|
||||
: CRPCCommand(category, name,
|
||||
|
@ -117,7 +130,7 @@ public:
|
|||
};
|
||||
|
||||
/**
|
||||
* Bitcoin RPC command dispatcher.
|
||||
* RPC command dispatcher.
|
||||
*/
|
||||
class CRPCTable
|
||||
{
|
||||
|
|
|
@ -418,7 +418,11 @@ struct Sections {
|
|||
};
|
||||
|
||||
RPCHelpMan::RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> 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<RPCArg> 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<std::string> RPCHelpMan::GetArgNames() const
|
||||
{
|
||||
std::vector<std::string> ret;
|
||||
for (const auto& arg : m_args) {
|
||||
ret.emplace_back(arg.m_names);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string RPCHelpMan::ToString() const
|
||||
{
|
||||
std::string ret;
|
||||
|
|
|
@ -326,8 +326,14 @@ class RPCHelpMan
|
|||
{
|
||||
public:
|
||||
RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples);
|
||||
using RPCMethodImpl = std::function<UniValue(const RPCHelpMan&, const JSONRPCRequest&)>;
|
||||
RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> 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<std::string> GetArgNames() const;
|
||||
|
||||
const std::string m_name;
|
||||
|
||||
private:
|
||||
const RPCMethodImpl m_fun;
|
||||
const std::string m_description;
|
||||
const std::vector<RPCArg> m_args;
|
||||
const RPCResults m_results;
|
||||
|
|
Loading…
Reference in a new issue