1 | // Copyright (C) 2016 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 QHOSTINFO_H |
5 | #define QHOSTINFO_H |
6 | |
7 | #include <QtNetwork/qtnetworkglobal.h> |
8 | #include <QtCore/qlist.h> |
9 | #include <QtCore/qscopedpointer.h> |
10 | #include <QtNetwork/qhostaddress.h> |
11 | |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | |
15 | class QObject; |
16 | class QHostInfoPrivate; |
17 | |
18 | class Q_NETWORK_EXPORT QHostInfo |
19 | { |
20 | public: |
21 | enum HostInfoError { |
22 | NoError, |
23 | HostNotFound, |
24 | UnknownError |
25 | }; |
26 | |
27 | explicit QHostInfo(int lookupId = -1); |
28 | QHostInfo(const QHostInfo &d); |
29 | QHostInfo(QHostInfo &&other) noexcept : d_ptr(std::exchange(obj&: other.d_ptr, new_val: nullptr)) {} |
30 | QHostInfo &operator=(const QHostInfo &d); |
31 | QHostInfo &operator=(QHostInfo &&other) noexcept { swap(other); return *this; } |
32 | ~QHostInfo(); |
33 | |
34 | void swap(QHostInfo &other) noexcept { qt_ptr_swap(lhs&: d_ptr, rhs&: other.d_ptr); } |
35 | |
36 | QString hostName() const; |
37 | void setHostName(const QString &name); |
38 | |
39 | QList<QHostAddress> addresses() const; |
40 | void setAddresses(const QList<QHostAddress> &addresses); |
41 | |
42 | HostInfoError error() const; |
43 | void setError(HostInfoError error); |
44 | |
45 | QString errorString() const; |
46 | void setErrorString(const QString &errorString); |
47 | |
48 | void setLookupId(int id); |
49 | int lookupId() const; |
50 | |
51 | static int lookupHost(const QString &name, QObject *receiver, const char *member); |
52 | static void abortHostLookup(int lookupId); |
53 | |
54 | static QHostInfo fromName(const QString &name); |
55 | static QString localHostName(); |
56 | static QString localDomainName(); |
57 | |
58 | #ifdef Q_QDOC |
59 | template<typename Functor> |
60 | static int lookupHost(const QString &name, const QObject *context, Functor functor); |
61 | #else |
62 | // lookupHost to a callable (with context) |
63 | template <typename Functor> |
64 | static inline int lookupHost(const QString &name, |
65 | const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver, |
66 | Functor &&func) |
67 | { |
68 | using Prototype = void(*)(QHostInfo); |
69 | QtPrivate::AssertCompatibleFunctions<Prototype, Functor>(); |
70 | return lookupHostImpl(name, receiver, |
71 | slotObj: QtPrivate::makeCallableObject<Prototype>(std::forward<Functor>(func)), |
72 | member: nullptr); |
73 | } |
74 | #endif // Q_QDOC |
75 | |
76 | // lookupHost to a callable (without context) |
77 | template <typename Functor> |
78 | static inline int lookupHost(const QString &name, Functor &&slot) |
79 | { |
80 | return lookupHost(name, nullptr, std::forward<Functor>(slot)); |
81 | } |
82 | |
83 | private: |
84 | QHostInfoPrivate *d_ptr; |
85 | Q_DECLARE_PRIVATE(QHostInfo) |
86 | |
87 | static int lookupHostImpl(const QString &name, |
88 | const QObject *receiver, |
89 | QtPrivate::QSlotObjectBase *slotObj, |
90 | const char *member); |
91 | |
92 | friend QHostInfo Q_NETWORK_EXPORT qt_qhostinfo_lookup(const QString &name, QObject *receiver, |
93 | const char *member, bool *valid, int *id); |
94 | }; |
95 | |
96 | Q_DECLARE_SHARED(QHostInfo) |
97 | |
98 | QT_END_NAMESPACE |
99 | |
100 | QT_DECL_METATYPE_EXTERN(QHostInfo, Q_NETWORK_EXPORT) |
101 | |
102 | #endif // QHOSTINFO_H |
103 | |