Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/server.h
| Show First 20 Lines • Show All 139 Lines • ▼ Show 20 Lines | void RPCRunLater(const std::string &name, std::function<void(void)> func, | ||||
| int64_t nSeconds); | int64_t nSeconds); | ||||
| typedef UniValue (*rpcfn_type)(Config &config, | 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 { | ||||
| private: | |||||
| union { | |||||
| rpcfn_type fn; | |||||
| const_rpcfn_type cfn; | |||||
| } actor; | |||||
| bool useConstConfig; | |||||
deadalnix: Please keep the struct layout as before. The order in which you declare fields in C++ is… | |||||
| public: | public: | ||||
| std::string category; | std::string category; | ||||
| std::string name; | std::string name; | ||||
| rpcfn_type actor; | |||||
| bool okSafeMode; | bool okSafeMode; | ||||
| 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}, argNames{std::move(_argNames)} { | ||||
| actor.fn = _actor; | |||||
| useConstConfig = false; | |||||
| } | |||||
| /** | /** | ||||
| * 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. | ||||
deadalnixUnsubmitted Not Done Inline ActionsPlease explains why? This is obviously not obvious because this was wrong in the past and required a fair amount of digging to get there. deadalnix: Please explains why? This is obviously not obvious because this was wrong in the past and… | |||||
| * understand type variance. As a result, we need to do the dirty job | |||||
| * ourselves. | |||||
| */ | */ | ||||
| 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}, argNames{std::move(_argNames)} { | ||||
| okSafeMode{_okSafeMode}, argNames{std::move(_argNames)} {} | actor.cfn = _actor; | ||||
| useConstConfig = true; | |||||
| } | |||||
| 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 | |||||
Please keep the struct layout as before. The order in which you declare fields in C++ is significant as it changes how the structure is represented in memory.