/src/qtbase/src/network/access/qsocketabstraction_p.h
Line | Count | Source |
1 | | // Copyright (C) 2024 The Qt Company Ltd. |
2 | | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | | // Qt-Security score:significant reason:default |
4 | | |
5 | | #ifndef QSOCKETABSTRACTION_P_H |
6 | | #define QSOCKETABSTRACTION_P_H |
7 | | |
8 | | #include <private/qtnetworkglobal_p.h> |
9 | | |
10 | | #include <QtNetwork/qabstractsocket.h> |
11 | | #if QT_CONFIG(localserver) |
12 | | # include <QtNetwork/qlocalsocket.h> |
13 | | #endif |
14 | | |
15 | | #include <QtCore/qxpfunctional.h> |
16 | | |
17 | | // |
18 | | // W A R N I N G |
19 | | // ------------- |
20 | | // |
21 | | // This file is not part of the Qt API. It exists for the convenience |
22 | | // of the Network Access API. This header file may change from |
23 | | // version to version without notice, or even be removed. |
24 | | // |
25 | | // We mean it. |
26 | | // |
27 | | |
28 | | QT_BEGIN_NAMESPACE |
29 | | |
30 | | // Helper functions to deal with a QIODevice that is either a socket or a local |
31 | | // socket. |
32 | | namespace QSocketAbstraction { |
33 | | template <typename Fn, typename... Args> |
34 | | auto visit(Fn &&fn, QIODevice *socket, Args &&...args) |
35 | 0 | { |
36 | 0 | if (auto *s = qobject_cast<QAbstractSocket *>(socket)) |
37 | 0 | return std::forward<Fn>(fn)(s, std::forward<Args>(args)...); |
38 | 0 | #if QT_CONFIG(localserver) |
39 | 0 | if (auto *s = qobject_cast<QLocalSocket *>(socket)) |
40 | 0 | return std::forward<Fn>(fn)(s, std::forward<Args>(args)...); |
41 | 0 | #endif |
42 | 0 | Q_UNREACHABLE(); |
43 | 0 | } Unexecuted instantiation: auto QSocketAbstraction::visit<QSocketAbstraction::socketState(QIODevice*)::{lambda(auto:1*)#1}&>(QSocketAbstraction::socketState(QIODevice*)::{lambda(auto:1*)#1}&, QIODevice*)Unexecuted instantiation: auto QSocketAbstraction::visit<QSocketAbstraction::socketPeerName(QIODevice*)::{lambda(auto:1*)#1}&>(QSocketAbstraction::socketPeerName(QIODevice*)::{lambda(auto:1*)#1}&, QIODevice*)Unexecuted instantiation: auto QSocketAbstraction::visit<QSocketAbstraction::socketError(QIODevice*)::{lambda(auto:1*)#1}&>(QSocketAbstraction::socketError(QIODevice*)::{lambda(auto:1*)#1}&, QIODevice*)Unexecuted instantiation: qhttpnetworkconnectionchannel.cpp:auto QSocketAbstraction::visit<QHttpNetworkConnectionChannel::init()::$_0>(QHttpNetworkConnectionChannel::init()::$_0&&, QIODevice*) Unexecuted instantiation: qhttpnetworkconnectionchannel.cpp:auto QSocketAbstraction::visit<QHttpNetworkConnectionChannel::abort()::$_0&>(QHttpNetworkConnectionChannel::abort()::$_0&, QIODevice*) |
44 | | |
45 | | // Since QLocalSocket's LocalSocketState's values are defined as being equal |
46 | | // to some of QAbstractSocket's SocketState's values, we can use the superset |
47 | | // of the two as the return type. |
48 | | inline QAbstractSocket::SocketState socketState(QIODevice *device) |
49 | 0 | { |
50 | 0 | auto getState = [](auto *s) { |
51 | 0 | using T = std::remove_pointer_t<decltype(s)>; |
52 | 0 | if constexpr (std::is_same_v<T, QAbstractSocket>) { |
53 | 0 | return s->state(); |
54 | 0 | #if QT_CONFIG(localserver) |
55 | 0 | } else if constexpr (std::is_same_v<T, QLocalSocket>) { |
56 | 0 | QLocalSocket::LocalSocketState st = s->state(); |
57 | 0 | return static_cast<QAbstractSocket::SocketState>(st); |
58 | 0 | #endif |
59 | 0 | } |
60 | 0 | Q_UNREACHABLE(); |
61 | 0 | }; Unexecuted instantiation: auto QSocketAbstraction::socketState(QIODevice*)::{lambda(auto:1*)#1}::operator()<QAbstractSocket>(QAbstractSocket*) constUnexecuted instantiation: auto QSocketAbstraction::socketState(QIODevice*)::{lambda(auto:1*)#1}::operator()<QLocalSocket>(QLocalSocket*) const |
62 | 0 | return visit(getState, device); |
63 | 0 | } |
64 | | |
65 | | // Same as for socketState(), but for the errors |
66 | | inline QAbstractSocket::SocketError socketError(QIODevice *device) |
67 | 0 | { |
68 | 0 | auto getError = [](auto *s) { |
69 | 0 | using T = std::remove_pointer_t<decltype(s)>; |
70 | 0 | if constexpr (std::is_same_v<T, QAbstractSocket>) { |
71 | 0 | return s->error(); |
72 | 0 | #if QT_CONFIG(localserver) |
73 | 0 | } else if constexpr (std::is_same_v<T, QLocalSocket>) { |
74 | 0 | QLocalSocket::LocalSocketError st = s->error(); |
75 | 0 | return static_cast<QAbstractSocket::SocketError>(st); |
76 | 0 | #endif |
77 | 0 | } |
78 | 0 | Q_UNREACHABLE(); |
79 | 0 | }; Unexecuted instantiation: auto QSocketAbstraction::socketError(QIODevice*)::{lambda(auto:1*)#1}::operator()<QAbstractSocket>(QAbstractSocket*) constUnexecuted instantiation: auto QSocketAbstraction::socketError(QIODevice*)::{lambda(auto:1*)#1}::operator()<QLocalSocket>(QLocalSocket*) const |
80 | 0 | return visit(getError, device); |
81 | 0 | } |
82 | | |
83 | | inline QString socketPeerName(QIODevice *device) |
84 | 0 | { |
85 | 0 | auto getPeerName = [](auto *s) { |
86 | 0 | using T = std::remove_pointer_t<decltype(s)>; |
87 | 0 | if constexpr (std::is_same_v<T, QAbstractSocket>) { |
88 | 0 | return s->peerName(); |
89 | 0 | #if QT_CONFIG(localserver) |
90 | 0 | } else if constexpr (std::is_same_v<T, QLocalSocket>) { |
91 | 0 | return s->serverName(); |
92 | 0 | #endif |
93 | 0 | } |
94 | | Q_UNREACHABLE(); |
95 | 0 | }; Unexecuted instantiation: auto QSocketAbstraction::socketPeerName(QIODevice*)::{lambda(auto:1*)#1}::operator()<QAbstractSocket>(QAbstractSocket*) constUnexecuted instantiation: auto QSocketAbstraction::socketPeerName(QIODevice*)::{lambda(auto:1*)#1}::operator()<QLocalSocket>(QLocalSocket*) const |
96 | 0 | return visit(getPeerName, device); |
97 | 0 | } |
98 | | } // namespace QSocketAbstraction |
99 | | |
100 | | QT_END_NAMESPACE |
101 | | |
102 | | #endif // QSOCKETABSTRACTION_P_H |