Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsocketabstraction_p.h
Go to the documentation of this file.
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
28QT_BEGIN_NAMESPACE
29
30// Helper functions to deal with a QIODevice that is either a socket or a local
31// socket.
33template <typename Fn, typename... Args>
34auto visit(Fn &&fn, QIODevice *socket, Args &&...args)
35{
36 if (auto *s = qobject_cast<QAbstractSocket *>(socket))
37 return std::forward<Fn>(fn)(s, std::forward<Args>(args)...);
38#if QT_CONFIG(localserver)
39 if (auto *s = qobject_cast<QLocalSocket *>(socket))
40 return std::forward<Fn>(fn)(s, std::forward<Args>(args)...);
41#endif
42 Q_UNREACHABLE();
43}
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.
48inline QAbstractSocket::SocketState socketState(QIODevice *device)
49{
50 auto getState = [](auto *s) {
51 using T = std::remove_pointer_t<decltype(s)>;
52 if constexpr (std::is_same_v<T, QAbstractSocket>) {
53 return s->state();
54#if QT_CONFIG(localserver)
55 } else if constexpr (std::is_same_v<T, QLocalSocket>) {
56 QLocalSocket::LocalSocketState st = s->state();
57 return static_cast<QAbstractSocket::SocketState>(st);
58#endif
59 }
60 Q_UNREACHABLE();
61 };
62 return visit(getState, device);
63}
64
65// Same as for socketState(), but for the errors
66inline QAbstractSocket::SocketError socketError(QIODevice *device)
67{
68 auto getError = [](auto *s) {
69 using T = std::remove_pointer_t<decltype(s)>;
70 if constexpr (std::is_same_v<T, QAbstractSocket>) {
71 return s->error();
72#if QT_CONFIG(localserver)
73 } else if constexpr (std::is_same_v<T, QLocalSocket>) {
74 QLocalSocket::LocalSocketError st = s->error();
75 return static_cast<QAbstractSocket::SocketError>(st);
76#endif
77 }
78 Q_UNREACHABLE();
79 };
80 return visit(getError, device);
81}
82
83inline QString socketPeerName(QIODevice *device)
84{
85 auto getPeerName = [](auto *s) {
86 using T = std::remove_pointer_t<decltype(s)>;
87 if constexpr (std::is_same_v<T, QAbstractSocket>) {
88 return s->peerName();
89#if QT_CONFIG(localserver)
90 } else if constexpr (std::is_same_v<T, QLocalSocket>) {
91 return s->serverName();
92#endif
93 }
94 Q_UNREACHABLE();
95 };
96 return visit(getPeerName, device);
97}
98} // namespace QSocketAbstraction
99
100QT_END_NAMESPACE
101
102#endif // QSOCKETABSTRACTION_P_H
auto visit(Fn &&fn, QIODevice *socket, Args &&...args)
QString socketPeerName(QIODevice *device)
QAbstractSocket::SocketError socketError(QIODevice *device)
QAbstractSocket::SocketState socketState(QIODevice *device)