Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
AnyHandler.hpp
1#pragma once
2
3#include "rpc/common/Concepts.hpp"
4#include "rpc/common/Types.hpp"
5#include "rpc/common/impl/Processors.hpp"
6
7#include <boost/json/value.hpp>
8
9#include <memory>
10
11namespace rpc {
12
20class AnyHandler final {
21public:
29 template <
30 SomeHandler HandlerType,
31 typename ProcessingStrategy = impl::DefaultProcessor<HandlerType>>
32 /* implicit */ AnyHandler(HandlerType&& handler)
33 : pimpl_{std::make_unique<Model<HandlerType, ProcessingStrategy>>(
34 std::forward<HandlerType>(handler)
35 )}
36 {
37 }
38
40 ~AnyHandler() = default;
41 AnyHandler(AnyHandler const& other) : pimpl_{other.pimpl_->clone()}
42 {
43 }
44
46 operator=(AnyHandler const& rhs)
47 {
48 AnyHandler copy{rhs};
49 pimpl_.swap(copy.pimpl_);
50 return *this;
51 }
52
53 AnyHandler(AnyHandler&&) = default;
55 operator=(AnyHandler&&) = default;
57
65 [[nodiscard]] ReturnType
66 process(boost::json::value const& value, Context const& ctx) const
67 {
68 return pimpl_->process(value, ctx);
69 }
70
71private:
72 struct Concept {
73 virtual ~Concept() = default;
74
75 [[nodiscard]] virtual ReturnType
76 process(boost::json::value const& value, Context const& ctx) const = 0;
77
78 [[nodiscard]] virtual std::unique_ptr<Concept>
79 clone() const = 0;
80 };
81
82 template <typename HandlerType, typename ProcessorType>
83 struct Model : Concept {
84 HandlerType handler;
85 ProcessorType processor;
86
87 Model(HandlerType&& handler) : handler{std::move(handler)}
88 {
89 }
90
91 [[nodiscard]] ReturnType
92 process(boost::json::value const& value, Context const& ctx) const override
93 {
94 return processor(handler, value, ctx);
95 }
96
97 [[nodiscard]] std::unique_ptr<Concept>
98 clone() const override
99 {
100 return std::make_unique<Model>(*this);
101 }
102 };
103
104private:
105 std::unique_ptr<Concept> pimpl_;
106};
107
108} // namespace rpc
A type-erased Handler that can contain any (NextGen) RPC handler class.
Definition AnyHandler.hpp:20
ReturnType process(boost::json::value const &value, Context const &ctx) const
Process incoming JSON by the stored handler.
Definition AnyHandler.hpp:66
AnyHandler(HandlerType &&handler)
Type-erases any handler class.
Definition AnyHandler.hpp:32
Specifies what a Handler type must provide.
Definition Concepts.hpp:84
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:18
Context of an RPC call.
Definition Types.hpp:99
The final return type out of RPC engine.
Definition Types.hpp:67
Definition Processors.hpp:12