// Copyright (C) 2019 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #ifndef QHTTPSERVERROUTER_H #define QHTTPSERVERROUTER_H #include #include #include #include #include #include #include #include QT_BEGIN_NAMESPACE class QHttpServerResponder; class QHttpServerRequest; class QHttpServerRouterRule; class QHttpServerRouterPrivate; class Q_HTTPSERVER_EXPORT QHttpServerRouter { Q_DECLARE_PRIVATE(QHttpServerRouter) Q_DISABLE_COPY_MOVE(QHttpServerRouter) public: QHttpServerRouter(); ~QHttpServerRouter(); template bool addConverter(QAnyStringView regexp) { if (!QMetaType::registerConverter()) return false; addConverter(QMetaType::fromType(), regexp); return true; } void addConverter(QMetaType metaType, QAnyStringView regexp); void removeConverter(QMetaType metaType); void clearConverters(); const QHash &converters() const; template> bool addRule(std::unique_ptr rule) { return addRuleHelper( std::move(rule), typename ViewTraits::Arguments::Indexes{}); } template> typename ViewTraits::BindableType bindCaptured(ViewHandler &&handler, const QRegularExpressionMatch &match) const { return bindCapturedImpl( std::forward(handler), match, typename ViewTraits::Arguments::CapturableIndexes{}); } bool handleRequest(const QHttpServerRequest &request, QHttpServerResponder &responder) const; private: template bool addRuleHelper(std::unique_ptr rule, QtPrivate::IndexesList) { return addRuleImpl(std::move(rule), {ViewTraits::Arguments::template metaType()...}); } bool addRuleImpl(std::unique_ptr rule, std::initializer_list metaTypes); // Implementation of C++20 std::bind_front() in C++17 template auto bind_front(F &&f, Args &&...args) const { return [f = std::forward(f), args = std::make_tuple(std::forward(args)...)](auto &&...callArgs) { return std::apply(f, std::tuple_cat(args, std::forward_as_tuple(std::forward( callArgs)...))); }; } template typename ViewTraits::BindableType bindCapturedImpl(ViewHandler &&handler, const QRegularExpressionMatch &match, QtPrivate::IndexesList) const { return bind_front( std::forward(handler), QVariant(match.captured(Cx + 1)) .value::CleanType>()...); } std::unique_ptr d_ptr; }; QT_END_NAMESPACE #endif // QHTTPSERVERROUTER_H