blob: 35753b7b19be9a1e08a865364f84b8ebb7154104 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous08bbe112023-04-06 13:10:02 -07003#pragma once
4#include "baserule.hpp"
5#include "ruleparametertraits.hpp"
6#include "websocket.hpp"
7
8#include <boost/beast/http/verb.hpp>
9
10#include <functional>
11#include <limits>
12#include <string>
13#include <type_traits>
14
15namespace crow
16{
17namespace detail
18{
Ed Tanous08bbe112023-04-06 13:10:02 -070019
20template <typename Func, typename... ArgsWrapped>
21struct Wrapped
Ed Tanous1017ef82023-06-27 09:52:58 -070022{};
23
24template <typename Func, typename... ArgsWrapped>
25struct Wrapped<Func, std::tuple<ArgsWrapped...>>
Ed Tanous08bbe112023-04-06 13:10:02 -070026{
Ed Tanous1017ef82023-06-27 09:52:58 -070027 explicit Wrapped(Func f) : handler(std::move(f)) {}
Ed Tanous08bbe112023-04-06 13:10:02 -070028
Ed Tanouscfe3bc02023-06-26 12:47:24 -070029 std::function<void(ArgsWrapped...)> handler;
Ed Tanous08bbe112023-04-06 13:10:02 -070030
31 void operator()(const Request& req,
32 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
33 const std::vector<std::string>& params)
34 {
Ed Tanouscfe3bc02023-06-26 12:47:24 -070035 if constexpr (sizeof...(ArgsWrapped) == 2)
36 {
37 handler(req, asyncResp);
38 }
39 else if constexpr (sizeof...(ArgsWrapped) == 3)
40 {
41 handler(req, asyncResp, params[0]);
42 }
43 else if constexpr (sizeof...(ArgsWrapped) == 4)
44 {
45 handler(req, asyncResp, params[0], params[1]);
46 }
47 else if constexpr (sizeof...(ArgsWrapped) == 5)
48 {
49 handler(req, asyncResp, params[0], params[1], params[2]);
50 }
51 else if constexpr (sizeof...(ArgsWrapped) == 6)
52 {
53 handler(req, asyncResp, params[0], params[1], params[2], params[3]);
54 }
55 else if constexpr (sizeof...(ArgsWrapped) == 7)
56 {
57 handler(req, asyncResp, params[0], params[1], params[2], params[3],
58 params[4]);
59 }
Ed Tanous08bbe112023-04-06 13:10:02 -070060 }
61};
Ed Tanous08bbe112023-04-06 13:10:02 -070062} // namespace detail
63
64class DynamicRule : public BaseRule, public RuleParameterTraits<DynamicRule>
65{
66 public:
67 explicit DynamicRule(const std::string& ruleIn) : BaseRule(ruleIn) {}
68
69 void validate() override
70 {
71 if (!erasedHandler)
72 {
73 throw std::runtime_error("no handler for url " + rule);
74 }
75 }
76
77 void handle(const Request& req,
78 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
79 const std::vector<std::string>& params) override
80 {
81 erasedHandler(req, asyncResp, params);
82 }
83
84 template <typename Func>
85 void operator()(Func f)
86 {
87 using boost::callable_traits::args_t;
Ed Tanous1017ef82023-06-27 09:52:58 -070088 erasedHandler = detail::Wrapped<Func, args_t<Func>>(std::move(f));
Ed Tanous08bbe112023-04-06 13:10:02 -070089 }
90
91 private:
92 std::function<void(const Request&,
93 const std::shared_ptr<bmcweb::AsyncResp>&,
94 const std::vector<std::string>&)>
95 erasedHandler;
96};
97
98} // namespace crow