Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/server.h
| Show First 20 Lines • Show All 142 Lines • ▼ Show 20 Lines | typedef UniValue (*rpcfn_type)(Config &config, | ||||
| const JSONRPCRequest &jsonRequest); | const JSONRPCRequest &jsonRequest); | ||||
| typedef UniValue (*const_rpcfn_type)(const Config &config, | typedef UniValue (*const_rpcfn_type)(const Config &config, | ||||
| const JSONRPCRequest &jsonRequest); | const JSONRPCRequest &jsonRequest); | ||||
| class CRPCCommand { | class CRPCCommand { | ||||
| public: | public: | ||||
| std::string category; | std::string category; | ||||
| std::string name; | std::string name; | ||||
| rpcfn_type actor; | |||||
| bool okSafeMode; | bool okSafeMode; | ||||
| private: | |||||
| union { | |||||
| rpcfn_type fn; | |||||
| const_rpcfn_type cfn; | |||||
| } actor; | |||||
| bool useConstConfig; | |||||
| public: | |||||
| std::vector<std::string> argNames; | std::vector<std::string> argNames; | ||||
| CRPCCommand(std::string _category, std::string _name, rpcfn_type _actor, | CRPCCommand(std::string _category, std::string _name, rpcfn_type _actor, | ||||
| bool _okSafeMode, std::vector<std::string> _argNames) | bool _okSafeMode, std::vector<std::string> _argNames) | ||||
| : category{std::move(_category)}, name{std::move(_name)}, actor{_actor}, | : category{std::move(_category)}, name{std::move(_name)}, | ||||
| okSafeMode{_okSafeMode}, argNames{std::move(_argNames)} {} | okSafeMode{_okSafeMode}, useConstConfig{false}, argNames{std::move( | ||||
| _argNames)} { | |||||
| actor.fn = _actor; | |||||
| } | |||||
| /** | /** | ||||
| * It is safe to cast from void(const int*) to void(int*) but C++ do not | * There are 2 constructors depending Config is const or not, so we | ||||
| * understand type variance. As a result, we need to do the dirty job | * can call the command through the proper pointer. Casting constness | ||||
| * ourselves. | * on parameters of function is undefined behavior. | ||||
| */ | */ | ||||
| CRPCCommand(std::string _category, std::string _name, | CRPCCommand(std::string _category, std::string _name, | ||||
| const_rpcfn_type _actor, bool _okSafeMode, | const_rpcfn_type _actor, bool _okSafeMode, | ||||
| std::vector<std::string> _argNames) | std::vector<std::string> _argNames) | ||||
| : category{std::move(_category)}, name{std::move(_name)}, | : category{std::move(_category)}, name{std::move(_name)}, | ||||
| actor{reinterpret_cast<rpcfn_type>(_actor)}, | okSafeMode{_okSafeMode}, useConstConfig{true}, argNames{std::move( | ||||
| okSafeMode{_okSafeMode}, argNames{std::move(_argNames)} {} | _argNames)} { | ||||
| actor.cfn = _actor; | |||||
| } | |||||
| UniValue call(Config &config, const JSONRPCRequest &jsonRequest) const { | |||||
| return useConstConfig ? (*actor.cfn)(config, jsonRequest) | |||||
| : (*actor.fn)(config, jsonRequest); | |||||
| }; | |||||
| }; | }; | ||||
| /** | /** | ||||
| * Bitcoin RPC command dispatcher. | * Bitcoin RPC command dispatcher. | ||||
| */ | */ | ||||
| class CRPCTable { | class CRPCTable { | ||||
| private: | private: | ||||
| std::map<std::string, const CRPCCommand *> mapCommands; | std::map<std::string, const CRPCCommand *> mapCommands; | ||||
| ▲ Show 20 Lines • Show All 59 Lines • Show Last 20 Lines | |||||