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_{
34 std::make_unique<Model<HandlerType, ProcessingStrategy>>(
35 std::forward<HandlerType>(handler)
36 ),
37 }
38 {
39 }
40
42 ~AnyHandler() = default;
43 AnyHandler(AnyHandler const& other) : pimpl_{other.pimpl_->clone()}
44 {
45 }
46
48 operator=(AnyHandler const& rhs)
49 {
50 AnyHandler copy{rhs};
51 pimpl_.swap(copy.pimpl_);
52 return *this;
53 }
54
55 AnyHandler(AnyHandler&&) = default;
57 operator=(AnyHandler&&) = default;
59
67 [[nodiscard]] ReturnType
68 process(boost::json::value const& value, Context const& ctx) const
69 {
70 return pimpl_->process(value, ctx);
71 }
72
73private:
74 struct Concept {
75 virtual ~Concept() = default;
76
77 [[nodiscard]] virtual ReturnType
78 process(boost::json::value const& value, Context const& ctx) const = 0;
79
80 [[nodiscard]] virtual std::unique_ptr<Concept>
81 clone() const = 0;
82 };
83
84 template <typename HandlerType, typename ProcessorType>
85 struct Model : Concept {
86 HandlerType handler;
87 ProcessorType processor;
88
89 Model(HandlerType&& handler) : handler{std::move(handler)}
90 {
91 }
92
93 [[nodiscard]] ReturnType
94 process(boost::json::value const& value, Context const& ctx) const override
95 {
96 return processor(handler, value, ctx);
97 }
98
99 [[nodiscard]] std::unique_ptr<Concept>
100 clone() const override
101 {
102 return std::make_unique<Model>(*this);
103 }
104 };
105
106private:
107 std::unique_ptr<Concept> pimpl_;
108};
109
110} // 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:68
AnyHandler(HandlerType &&handler)
Type-erases any handler class.
Definition AnyHandler.hpp:32
Specifies what a Handler type must provide.
Definition Concepts.hpp:71
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:17
Context of an RPC call.
Definition Types.hpp:102
The final return type out of RPC engine.
Definition Types.hpp:68
Definition Processors.hpp:14