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 <SomeHandler HandlerType, typename ProcessingStrategy = impl::DefaultProcessor<HandlerType>>
49 /* implicit */ AnyHandler(HandlerType&& handler)
50 : pimpl_{std::make_unique<Model<HandlerType, ProcessingStrategy>>(std::forward<HandlerType>(handler))}
51 {
52 }
53
55 ~AnyHandler() = default;
56 AnyHandler(AnyHandler const& other) : pimpl_{other.pimpl_->clone()}
57 {
58 }
59
61 operator=(AnyHandler const& rhs)
62 {
63 AnyHandler copy{rhs};
64 pimpl_.swap(copy.pimpl_);
65 return *this;
66 }
67
68 AnyHandler(AnyHandler&&) = default;
70 operator=(AnyHandler&&) = default;
80 [[nodiscard]] ReturnType
81 process(boost::json::value const& value, Context const& ctx) const
82 {
83 return pimpl_->process(value, ctx);
84 }
85
86private:
87 struct Concept {
88 virtual ~Concept() = default;
89
90 [[nodiscard]] virtual ReturnType
91 process(boost::json::value const& value, Context const& ctx) const = 0;
92
93 [[nodiscard]] virtual std::unique_ptr<Concept>
94 clone() const = 0;
95 };
96
97 template <typename HandlerType, typename ProcessorType>
98 struct Model : Concept {
99 HandlerType handler;
100 ProcessorType processor;
101
102 Model(HandlerType&& handler) : handler{std::move(handler)}
103 {
104 }
105
106 [[nodiscard]] ReturnType
107 process(boost::json::value const& value, Context const& ctx) const override
108 {
109 return processor(handler, value, ctx);
110 }
111
112 [[nodiscard]] std::unique_ptr<Concept>
113 clone() const override
114 {
115 return std::make_unique<Model>(*this);
116 }
117 };
118
119private:
120 std::unique_ptr<Concept> pimpl_;
121};
122
123} // 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:81
AnyHandler(HandlerType &&handler)
Type-erases any handler class.
Definition AnyHandler.hpp:49
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:36
Context of an RPC call.
Definition Types.hpp:118
The final return type out of RPC engine.
Definition Types.hpp:86