Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
AnyHandler.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "rpc/common/Concepts.hpp"
23#include "rpc/common/Types.hpp"
24#include "rpc/common/impl/Processors.hpp"
25
26#include <boost/json/value.hpp>
27
28#include <memory>
29
30namespace rpc {
31
39class AnyHandler final {
40public:
48 template <
49 SomeHandler HandlerType,
50 typename ProcessingStrategy = impl::DefaultProcessor<HandlerType>>
51 /* implicit */ AnyHandler(HandlerType&& handler)
52 : pimpl_{std::make_unique<Model<HandlerType, ProcessingStrategy>>(
53 std::forward<HandlerType>(handler)
54 )}
55 {
56 }
57
59 ~AnyHandler() = default;
60 AnyHandler(AnyHandler const& other) : pimpl_{other.pimpl_->clone()}
61 {
62 }
63
65 operator=(AnyHandler const& rhs)
66 {
67 AnyHandler copy{rhs};
68 pimpl_.swap(copy.pimpl_);
69 return *this;
70 }
71
72 AnyHandler(AnyHandler&&) = default;
74 operator=(AnyHandler&&) = default;
76
84 [[nodiscard]] ReturnType
85 process(boost::json::value const& value, Context const& ctx) const
86 {
87 return pimpl_->process(value, ctx);
88 }
89
90private:
91 struct Concept {
92 virtual ~Concept() = default;
93
94 [[nodiscard]] virtual ReturnType
95 process(boost::json::value const& value, Context const& ctx) const = 0;
96
97 [[nodiscard]] virtual std::unique_ptr<Concept>
98 clone() const = 0;
99 };
100
101 template <typename HandlerType, typename ProcessorType>
102 struct Model : Concept {
103 HandlerType handler;
104 ProcessorType processor;
105
106 Model(HandlerType&& handler) : handler{std::move(handler)}
107 {
108 }
109
110 [[nodiscard]] ReturnType
111 process(boost::json::value const& value, Context const& ctx) const override
112 {
113 return processor(handler, value, ctx);
114 }
115
116 [[nodiscard]] std::unique_ptr<Concept>
117 clone() const override
118 {
119 return std::make_unique<Model>(*this);
120 }
121 };
122
123private:
124 std::unique_ptr<Concept> pimpl_;
125};
126
127} // namespace rpc
A type-erased Handler that can contain any (NextGen) RPC handler class.
Definition AnyHandler.hpp:39
ReturnType process(boost::json::value const &value, Context const &ctx) const
Process incoming JSON by the stored handler.
Definition AnyHandler.hpp:85
AnyHandler(HandlerType &&handler)
Type-erases any handler class.
Definition AnyHandler.hpp:51
Specifies what a Handler type must provide.
Definition Concepts.hpp:103
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:37
Context of an RPC call.
Definition Types.hpp:118
The final return type out of RPC engine.
Definition Types.hpp:86
Definition Processors.hpp:31