Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#ifndef QSOCKETABSTRACTION_P_H
5#define QSOCKETABSTRACTION_P_H
6
7#include <private/qtnetworkglobal_p.h>
8
9#include <QtNetwork/qabstractsocket.h>
10#include <QtNetwork/qlocalsocket.h>
11
12#include <QtCore/qxpfunctional.h>
13
14//
15// W A R N I N G
16// -------------
17//
18// This file is not part of the Qt API. It exists for the convenience
19// of the Network Access API. This header file may change from
20// version to version without notice, or even be removed.
21//
22// We mean it.
23//
24
26
27// Helper functions to deal with a QIODevice that is either a socket or a local
28// socket.
30template <typename Fn, typename... Args>
31auto visit(Fn &&fn, QIODevice *socket, Args &&...args)
32{
33 if (auto *s = qobject_cast<QAbstractSocket *>(socket))
34 return std::forward<Fn>(fn)(s, std::forward<Args>(args)...);
35 if (auto *s = qobject_cast<QLocalSocket *>(socket))
36 return std::forward<Fn>(fn)(s, std::forward<Args>(args)...);
37 Q_UNREACHABLE();
38}
39
40// Since QLocalSocket's LocalSocketState's values are defined as being equal
41// to some of QAbstractSocket's SocketState's values, we can use the superset
42// of the two as the return type.
44{
45 auto getState = [](auto *s) {
46 using T = std::remove_pointer_t<decltype(s)>;
47 if constexpr (std::is_same_v<T, QAbstractSocket>) {
48 return s->state();
49 } else if constexpr (std::is_same_v<T, QLocalSocket>) {
51 return static_cast<QAbstractSocket::SocketState>(st);
52 }
53 Q_UNREACHABLE();
54 };
55 return visit(getState, device);
56}
57
58// Same as for socketState(), but for the errors
60{
61 auto getError = [](auto *s) {
62 using T = std::remove_pointer_t<decltype(s)>;
63 if constexpr (std::is_same_v<T, QAbstractSocket>) {
64 return s->error();
65 } else if constexpr (std::is_same_v<T, QLocalSocket>) {
67 return static_cast<QAbstractSocket::SocketError>(st);
68 }
69 Q_UNREACHABLE();
70 };
71 return visit(getError, device);
72}
73
75{
76 auto getPeerName = [](auto *s) {
77 using T = std::remove_pointer_t<decltype(s)>;
78 if constexpr (std::is_same_v<T, QAbstractSocket>) {
79 return s->peerName();
80 } else if constexpr (std::is_same_v<T, QLocalSocket>) {
81 return s->serverName();
82 }
83 Q_UNREACHABLE();
84 };
85 return visit(getPeerName, device);
86}
87} // namespace QSocketAbstraction
88
90
91#endif // QSOCKETABSTRACTION_P_H
IOBluetoothDevice * device
SocketState
This enum describes the different states in which a socket can be.
SocketError
This enum describes the socket errors that can occur.
\inmodule QtCore \reentrant
Definition qiodevice.h:34
LocalSocketState
This enum describes the different states in which a socket can be.
LocalSocketError
The LocalServerError enumeration represents the errors that can occur.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
auto visit(Fn &&fn, QIODevice *socket, Args &&...args)
QString socketPeerName(QIODevice *device)
QAbstractSocket::SocketError socketError(QIODevice *device)
QAbstractSocket::SocketState socketState(QIODevice *device)
Combined button and popup list for selecting options.
GLdouble s
[6]
Definition qopenglext.h:235
QTcpSocket * socket
[1]
QJSValueList args