| 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_IMPL_H |
| 6 | #define QHTTPSERVERVIEWTRAITS_IMPL_H |
| 7 | |
| 8 | #include <QtCore/qglobal.h> |
| 9 | #include <QtCore/qmetatype.h> |
| 10 | #include <QtCore/qnamespace.h> |
| 11 | #include <QtCore/qobjectdefs.h> |
| 12 | |
| 13 | #include <tuple> |
| 14 | #include <type_traits> |
| 15 | |
| 16 | QT_BEGIN_NAMESPACE |
| 17 | |
| 18 | namespace QtPrivate { |
| 19 | |
| 20 | template<typename ReturnT, typename... Args> |
| 21 | struct FunctionTraitsHelper |
| 22 | { |
| 23 | static constexpr const int ArgumentCount = sizeof ... (Args); |
| 24 | static constexpr const int ArgumentIndexMax = ArgumentCount - 1; |
| 25 | using ReturnType = ReturnT; |
| 26 | |
| 27 | template <int I> |
| 28 | struct Arg { |
| 29 | using Type = typename std::tuple_element<I, std::tuple<Args...>>::type; |
| 30 | |
| 31 | using CleanType = q20::remove_cvref_t<Type>; |
| 32 | |
| 33 | static constexpr bool CopyConstructible = std::is_copy_constructible_v<CleanType>; |
| 34 | }; |
| 35 | }; |
| 36 | |
| 37 | template<typename T> |
| 38 | struct FunctionTraitsImpl; |
| 39 | |
| 40 | template<typename T> |
| 41 | struct FunctionTraitsImpl : public FunctionTraitsImpl<decltype(&T::operator())> |
| 42 | { |
| 43 | }; |
| 44 | |
| 45 | template<typename ReturnT, typename... Args> |
| 46 | struct FunctionTraitsImpl<ReturnT (*)(Args...)> : public FunctionTraitsHelper<ReturnT, Args...> |
| 47 | { |
| 48 | }; |
| 49 | |
| 50 | template<typename ReturnT, typename ClassT, typename... Args> |
| 51 | struct FunctionTraitsImpl<ReturnT (ClassT::*)(Args...)> |
| 52 | : public FunctionTraitsHelper<ReturnT, Args...> |
| 53 | { |
| 54 | }; |
| 55 | |
| 56 | template<typename ReturnT, typename ClassT, typename... Args> |
| 57 | struct FunctionTraitsImpl<ReturnT (ClassT::*)(Args...) const> |
| 58 | : public FunctionTraitsHelper<ReturnT, Args...> |
| 59 | { |
| 60 | }; |
| 61 | |
| 62 | template<typename T> |
| 63 | using FunctionTraits = FunctionTraitsImpl<std::decay_t<T>>; |
| 64 | |
| 65 | template<typename ... T> |
| 66 | struct CheckAny { |
| 67 | static constexpr bool Value = (T::Value || ...); |
| 68 | static constexpr bool Valid = (T::Valid || ...); |
| 69 | static constexpr bool StaticAssert = (T::StaticAssert || ...); |
| 70 | }; |
| 71 | |
| 72 | template<typename ViewHandler, bool DisableStaticAssert> |
| 73 | struct ViewTraits { |
| 74 | using FTraits = FunctionTraits<ViewHandler>; |
| 75 | using ArgumentIndexes = typename Indexes<FTraits::ArgumentCount>::Value; |
| 76 | |
| 77 | template<int I, typename Special> |
| 78 | struct SpecialHelper { |
| 79 | using Arg = typename FTraits::template Arg<I>; |
| 80 | using CleanSpecialT = q20::remove_cvref_t<Special>; |
| 81 | |
| 82 | static constexpr bool TypeMatched = std::is_same<typename Arg::CleanType, CleanSpecialT>::value; |
| 83 | static constexpr bool TypeCVRefMatched = std::is_same<typename Arg::Type, Special>::value; |
| 84 | |
| 85 | static constexpr bool ValidPosition = |
| 86 | (I == FTraits::ArgumentIndexMax || |
| 87 | I == FTraits::ArgumentIndexMax - 1); |
| 88 | static constexpr bool ValidAll = TypeCVRefMatched && ValidPosition; |
| 89 | |
| 90 | static constexpr bool AssertCondition = |
| 91 | DisableStaticAssert || !TypeMatched || TypeCVRefMatched; |
| 92 | |
| 93 | static constexpr bool AssertConditionOrder = |
| 94 | DisableStaticAssert || !TypeMatched || ValidPosition; |
| 95 | |
| 96 | static constexpr bool StaticAssert = AssertCondition && AssertConditionOrder; |
| 97 | |
| 98 | static_assert(AssertConditionOrder, |
| 99 | "ViewHandler arguments error: " |
| 100 | "QHttpServerRequest or QHttpServerResponder" |
| 101 | " can only be the last argument" ); |
| 102 | }; |
| 103 | |
| 104 | template<int I, typename T> |
| 105 | struct Special { |
| 106 | using Helper = SpecialHelper<I, T>; |
| 107 | static constexpr bool Value = Helper::TypeMatched; |
| 108 | static constexpr bool Valid = Helper::ValidAll; |
| 109 | static constexpr bool StaticAssert = Helper::StaticAssert; |
| 110 | static constexpr bool AssertCondition = Helper::AssertCondition; |
| 111 | }; |
| 112 | }; |
| 113 | |
| 114 | } // namespace QtPrivate |
| 115 | |
| 116 | QT_END_NAMESPACE |
| 117 | |
| 118 | #endif // QHTTPSERVERVIEWTRAITS_IMPL_H |
| 119 | |