magpie
Loading...
Searching...
No Matches
Route.hpp
1#pragma once
2
3#include "magpie/data/CommonData.hpp"
4#include "magpie/routing/BaseRoute.hpp"
5#include "magpie/routing/Compile.hpp"
6
7#include <vector>
8#include <tuple>
9#include <vector>
10#include <string_view>
11#include <cstddef>
12
13namespace magpie::routing {
14
15template <FixedString path, data::IsCommonData ContextType>
16struct Route : public BaseRoute<ContextType> {
17 const RouteCallback<path, ContextType> callback;
18 constexpr static size_t Size = guessParams<path>();
19 constexpr static inline auto typeTuples = getForwardableIndices<path>();
20
21 Route(
22 const RouteCallback<path, ContextType>& callback
23 ) : callback(callback) {
24
25 }
26
27 template <std::size_t... I>
28 constexpr auto typeToFuncArgs(const std::vector<std::string_view>& requestedPath, std::index_sequence<I...>) {
29 return std::tuple {
31 FixedString<typeTuples.at(I).first.size - 1>(typeTuples.at(I).first)
32 // "{string}"
33 >::convert(
34 requestedPath.at(
35 std::get<1>(typeTuples.at(I))
36 )
37 )...
38 };
39 }
40
41 virtual void invoke(
42 const std::vector<std::string_view>& requestedPath,
43 ContextType* context,
44 Request& req,
45 Response& res
46 ) override {
47 if constexpr (Size == 0) {
48 callback(
49 context,
50 std::ref(req),
51 std::ref(res)
52 );
53 } else {
54 return std::apply(
55 [&](auto&&... converted) {
56 callback(
57 context,
58 std::ref(req),
59 std::ref(res),
60 std::forward<decltype(converted)>(converted)...
61 );
62 },
63 typeToFuncArgs(
64 requestedPath,
65 std::make_index_sequence<typeTuples.size()>{}
66 )
67 );
68 }
69 }
70
71};
72
73
74}
Definition BaseRoute.hpp:16
Definition Request.hpp:11
Definition Response.hpp:13
Definition Compile.hpp:19
Definition Compile.hpp:136