diff options
author | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2023-08-07 14:53:42 +0200 |
---|---|---|
committer | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2023-08-24 10:44:45 +0200 |
commit | c00000df1605788acadceb90c22ae9f00db8a9dc (patch) | |
tree | 547e525ac6821f751bcca2dd9534043aa75fc382 /src/rpc/util.h | |
parent | cd5d2f5f0938ad4f8737caf4e7c501101818fd36 (diff) | |
download | bitcoin-c00000df1605788acadceb90c22ae9f00db8a9dc.tar.xz |
rpc: Add MaybeArg() and Arg() default helper
Diffstat (limited to 'src/rpc/util.h')
-rw-r--r-- | src/rpc/util.h | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/rpc/util.h b/src/rpc/util.h index ac0b2c088e..392540ffad 100644 --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -6,6 +6,7 @@ #define BITCOIN_RPC_UTIL_H #include <addresstype.h> +#include <consensus/amount.h> #include <node/transaction.h> #include <outputtype.h> #include <protocol.h> @@ -14,13 +15,29 @@ #include <rpc/request.h> #include <script/script.h> #include <script/sign.h> +#include <uint256.h> #include <univalue.h> #include <util/check.h> +#include <cstddef> +#include <cstdint> +#include <functional> +#include <initializer_list> +#include <map> +#include <optional> #include <string> +#include <type_traits> +#include <utility> #include <variant> #include <vector> +class JSONRPCRequest; +enum ServiceFlags : uint64_t; +enum class OutputType; +enum class TransactionError; +struct FlatSigningProvider; +struct bilingual_str; + static constexpr bool DEFAULT_RPC_DOC_CHECK{ #ifdef RPC_DOC_CHECK true @@ -383,6 +400,44 @@ public: RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples, RPCMethodImpl fun); UniValue HandleRequest(const JSONRPCRequest& request) const; + /** + * Helper to get a request argument. + * This function only works during m_fun(), i.e. it should only be used in + * RPC method implementations. The helper internally checks whether the + * user-passed argument isNull() and parses (from JSON) and returns the + * user-passed argument, or the default value derived from the RPCArg + * documention, or a falsy value if no default was given. + * + * Use Arg<Type>(i) to get the argument or its default value. Otherwise, + * use MaybeArg<Type>(i) to get the optional argument or a falsy value. + * + * The Type passed to this helper must match the corresponding + * RPCArg::Type. + */ + template <typename R> + auto Arg(size_t i) const + { + // Return argument (required or with default value). + if constexpr (std::is_integral_v<R> || std::is_floating_point_v<R>) { + // Return numbers by value. + return ArgValue<R>(i); + } else { + // Return everything else by reference. + return ArgValue<const R&>(i); + } + } + template <typename R> + auto MaybeArg(size_t i) const + { + // Return optional argument (without default). + if constexpr (std::is_integral_v<R> || std::is_floating_point_v<R>) { + // Return numbers by value, wrapped in optional. + return ArgValue<std::optional<R>>(i); + } else { + // Return other types by pointer. + return ArgValue<const R*>(i); + } + } std::string ToString() const; /** Return the named args that need to be converted from string to another JSON type */ UniValue GetArgMap() const; @@ -399,6 +454,9 @@ private: const std::vector<RPCArg> m_args; const RPCResults m_results; const RPCExamples m_examples; + mutable const JSONRPCRequest* m_req{nullptr}; // A pointer to the request for the duration of m_fun() + template <typename R> + R ArgValue(size_t i) const; }; /** |