Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/commandfactory.h
- This file was added.
// Copyright (c) 2018 The Bitcoin developers | |||||
// Distributed under the MIT software license, see the accompanying | |||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | |||||
#ifndef BITCOIN_RPC_COMMAND_FACTORY_H | |||||
#define BITCOIN_RPC_COMMAND_FACTORY_H | |||||
#include "rpc/commandname.h" | |||||
#include "rpc/jsonrpcrequest.h" | |||||
#include "rpc/parameter.h" | |||||
#include <boost/noncopyable.hpp> | |||||
#include <memory> | |||||
#include <string> | |||||
#include <univalue.h> | |||||
/** | |||||
* Abstract class to define RPC command factories. | |||||
* Each factory is a singleton that is instantiated with the necessary | |||||
* dependencies at application init time. | |||||
*/ | |||||
class RPCCommandFactory : public boost::noncopyable { | |||||
private: | |||||
/** | |||||
* Subclasses of RPCCommandFactory should store dependencies as private | |||||
* member variables. | |||||
*/ | |||||
public: | |||||
RPCCommandFactory() {} | |||||
virtual ~RPCCommandFactory() {} | |||||
/** | |||||
* Returns the command name. | |||||
*/ | |||||
virtual RPCCommandName GetName() const = 0; | |||||
/** | |||||
* Returns a new list of parameter definitions for the RPC command that is | |||||
* generated by this factory. | |||||
*/ | |||||
virtual std::vector<std::unique_ptr<RPCParameterBase>> | |||||
MakeParameters() const = 0; | |||||
/** | |||||
* Instantiates a new RPCCommand with the parameter values received via | |||||
* jsonRequest, if they are valid. If the parameters are not valid, | |||||
* MakeCommand should return nullptr. | |||||
*/ | |||||
std::unique_ptr<RPCCommand> | |||||
MakeCommand(const JSONRPCRequest &jsonRequest) const { | |||||
std::unique_ptr<RPCCommand> command = NewCommand(jsonRequest); | |||||
if (command) { | |||||
if (!command->ValidateAndSaveParameters(jsonRequest)) { | |||||
return nullptr; | |||||
} | |||||
} | |||||
return command; | |||||
} | |||||
/** | |||||
* Instantiates a new RPCCommand to be used for parsing and validation of a | |||||
* particular request. | |||||
* Returns nullptr in case of any errors. | |||||
*/ | |||||
virtual std::unique_ptr<RPCCommand> | |||||
NewCommand(const JSONRPCRequest &jsonRequest) const = 0; | |||||
}; | |||||
#endif // BITCOIN_RPC_COMMAND_FACTORY_H |