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