1// Copyright (C) 2020 Mikhail Svetkin <mikhail.svetkin@gmail.com>
2// Copyright (C) 2019 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4
5#ifndef QHTTPSERVERVIEWTRAITS_H
6#define QHTTPSERVERVIEWTRAITS_H
7
8#include <QtHttpServer/qhttpserverviewtraits_impl.h>
9
10QT_BEGIN_NAMESPACE
11
12class QHttpServerRequest;
13class QHttpServerResponse;
14
15namespace QtPrivate {
16
17template <typename ViewHandler, bool DisableStaticAssert>
18struct AfterRequestViewTraitsHelper : ViewTraits<ViewHandler, DisableStaticAssert> {
19 using VTraits = ViewTraits<ViewHandler, DisableStaticAssert>;
20 using FunctionTraits = typename VTraits::FTraits;
21
22 static_assert(DisableStaticAssert ||
23 FunctionTraits::ArgumentCount == 2 ||
24 FunctionTraits::ArgumentCount == 1,
25 "ViewHandler arguments error: "
26 "afterRequest can only accept QHttpServerResponse and QHttpServerRequest");
27
28 static_assert(DisableStaticAssert ||
29 std::is_same<typename FunctionTraits::ReturnType,
30 QHttpServerResponse>::value,
31 "ViewHandler return type error: "
32 "Return type can only be QHttpServerResponse");
33
34 template<int I>
35 struct ArgumentChecker {
36 using IsRequest = typename VTraits::template Special<I, const QHttpServerRequest &>;
37 static_assert(IsRequest::AssertCondition,
38 "ViewHandler arguments error: "
39 "QHttpServerRequest can only be passed as a const reference");
40
41 using IsResponse = typename VTraits::template Special<I, QHttpServerResponse &&>;
42 static_assert(IsResponse::AssertCondition,
43 "ViewHandler arguments error: "
44 "QHttpServerResponse can only be passed as an rvalue reference");
45
46 using IsSpecial = CheckAny<IsRequest, IsResponse>;
47
48 static constexpr bool Valid = IsSpecial::Valid;
49 static constexpr bool StaticAssert = IsSpecial::StaticAssert;
50 };
51
52 struct Arguments {
53 template<int ... I>
54 struct ArgumentsReturn {
55 template<int Idx>
56 using Arg = ArgumentChecker<Idx>;
57 static constexpr bool Valid = (Arg<I>::Valid && ...);
58 static constexpr bool StaticAssert = (Arg<I>::StaticAssert && ...);
59 using Last = Arg<FunctionTraits::ArgumentIndexMax>;
60 static constexpr std::size_t Count = FunctionTraits::ArgumentCount;
61 };
62
63 template<int ... I>
64 static constexpr ArgumentsReturn<I...> eval(QtPrivate::IndexesList<I...>) noexcept
65 {
66 return ArgumentsReturn<I...>{};
67 }
68 };
69};
70
71} // namespace QtPrivate
72
73template <typename ViewHandler, bool DisableStaticAssert = false>
74struct QHttpServerAfterRequestViewTraits
75{
76 using Helpers = typename QtPrivate::AfterRequestViewTraitsHelper<ViewHandler, DisableStaticAssert>;
77 using Arguments = decltype(Helpers::Arguments::eval(typename Helpers::ArgumentIndexes{}));
78};
79
80QT_END_NAMESPACE
81
82#endif // QHTTPSERVERVIEWTRAITS_H
83

source code of qthttpserver/src/httpserver/qhttpserverviewtraits.h