Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/parameter.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_PARAMETER_H | |||||
#define BITCOIN_RPC_PARAMETER_H | |||||
#include <string> | |||||
#include <univalue.h> | |||||
class RPCParameterBase { | |||||
private: | |||||
std::string name; | |||||
std::string description; | |||||
public: | |||||
RPCParameterBase(std::string nameIn, std::string descriptionIn) | |||||
: name(nameIn), description(descriptionIn){}; | |||||
virtual ~RPCParameterBase() = default; | |||||
std::string GetName() const { return name; } | |||||
std::string GetDescription() const { return description; } | |||||
/** | |||||
* Validate()'s input for this parameter. For example, this function | |||||
* could be defined to only accept a certain range for integer values. | |||||
* Validate() should return true if the input is valid, false otherwise. | |||||
*/ | |||||
virtual bool Validate(UniValue input) const = 0; | |||||
}; | |||||
/** | |||||
* Abstract class to define RPC parameters. | |||||
* Any class derived from RPCParameter may be used by multiple RPCCommands. | |||||
*/ | |||||
template <class ParameterType> class RPCParameter : public RPCParameterBase { | |||||
private: | |||||
ParameterType value; | |||||
public: | |||||
RPCParameter(std::string nameIn, std::string descriptionIn) | |||||
: RPCParameterBase(nameIn, descriptionIn), value(nullptr){}; | |||||
/** | |||||
* GetValue() returns the value validated by Validate(). | |||||
* Returns nullptr if Validate() was never called or returned false. | |||||
*/ | |||||
virtual ParameterType GetValue() const = 0; | |||||
}; | |||||
#endif // BITCOIN_RPC_PARAMETER_H |