6 #ifndef KAGOME_CHAIN_BASE_REQUEST_HPP 7 #define KAGOME_CHAIN_BASE_REQUEST_HPP 9 #include <jsonrpc-lean/request.h> 18 template <
size_t I,
size_t Max,
typename Tuple,
typename F>
19 constexpr
void loop(Tuple &t, F &&f) {
20 static_assert(I <= Max,
"Invalid expression!");
21 if constexpr (!std::equal_to<size_t>()(I, Max)) {
22 std::forward<F>(f)(I, std::get<I>(t));
23 loop<I + 1, Max>(t, std::forward<F>(f));
31 template <
typename ResultType,
typename... ArgumentTypes>
34 using Params = std::tuple<ArgumentTypes...>;
44 virtual outcome::result<ResultType>
execute() = 0;
52 outcome::result<void>
init(
const jsonrpc::Request::Parameters ¶ms) {
53 if (params.size() <=
sizeof...(ArgumentTypes)) {
54 loop<0,
sizeof...(ArgumentTypes)>(params_,
55 [&](
const auto ix,
auto &dst) {
56 if (ix < params.size())
59 return outcome::success();
61 throw jsonrpc::InvalidParametersFault(
"Incorrect number of params");
66 typename std::tuple_element<I, decltype(params_)>::type & {
67 static_assert(I < std::tuple_size<decltype(params_)>::value,
74 void loadValue(std::optional<T> &dst,
const jsonrpc::Value &src) {
84 template <
typename SequenceContainer,
85 typename =
typename SequenceContainer::value_type,
86 typename =
typename SequenceContainer::iterator>
87 void loadValue(SequenceContainer &dst,
const jsonrpc::Value &src) {
88 if (!src.IsNil() && src.IsArray()) {
89 for (
auto &v : src.AsArray()) {
90 typename SequenceContainer::value_type t;
92 dst.insert(dst.end(), std::move(t));
95 throw jsonrpc::InvalidParametersFault(
"invalid argument type");
100 std::enable_if_t<std::is_integral_v<T>,
void>
loadValue(
101 T &dst,
const jsonrpc::Value &src) {
102 if (not src.IsInteger32() and not src.IsInteger64()) {
103 throw jsonrpc::InvalidParametersFault(
"invalid argument type");
105 auto num = src.AsInteger64();
106 if constexpr (std::is_signed_v<T>) {
107 if (num < std::numeric_limits<T>::min()
108 or num > std::numeric_limits<T>::max()) {
109 throw jsonrpc::InvalidParametersFault(
"invalid argument value");
113 or static_cast<uint64_t>(num) > std::numeric_limits<T>::max()) {
114 throw jsonrpc::InvalidParametersFault(
"invalid argument value");
121 if (not src.IsBoolean()) {
122 throw jsonrpc::InvalidParametersFault(
"invalid argument type");
124 dst = src.AsBoolean();
127 void loadValue(std::string &dst,
const jsonrpc::Value &src) {
128 if (not src.IsString()) {
129 throw jsonrpc::InvalidParametersFault(
"invalid argument type");
131 dst = src.AsString();
137 #endif // KAGOME_CHAIN_BASE_REQUEST_HPP std::tuple< ArgumentTypes... > Params
void loadValue(std::string &dst, const jsonrpc::Value &src)
RequestType & operator=(const RequestType &)=delete
std::enable_if_t< std::is_integral_v< T >, void > loadValue(T &dst, const jsonrpc::Value &src)
void loadValue(SequenceContainer &dst, const jsonrpc::Value &src)
void loadValue(bool &dst, const jsonrpc::Value &src)
void loadValue(std::optional< T > &dst, const jsonrpc::Value &src)
virtual ~RequestType()=default
virtual outcome::result< ResultType > execute()=0
auto getParam() -> typename std::tuple_element< I, decltype(params_)>::type &
constexpr void loop(Tuple &t, F &&f)
outcome::result< void > init(const jsonrpc::Request::Parameters ¶ms)