1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtNetwork module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QHOSTINFO_H
41#define QHOSTINFO_H
42
43#include <QtNetwork/qtnetworkglobal.h>
44#include <QtCore/qlist.h>
45#include <QtCore/qscopedpointer.h>
46#include <QtNetwork/qhostaddress.h>
47
48QT_BEGIN_NAMESPACE
49
50
51class QObject;
52class QHostInfoPrivate;
53
54class Q_NETWORK_EXPORT QHostInfo
55{
56public:
57 enum HostInfoError {
58 NoError,
59 HostNotFound,
60 UnknownError
61 };
62
63 explicit QHostInfo(int lookupId = -1);
64 QHostInfo(const QHostInfo &d);
65 QHostInfo(QHostInfo &&other) noexcept : d_ptr(qExchange(other.d_ptr, nullptr)) {}
66 QHostInfo &operator=(const QHostInfo &d);
67 QHostInfo &operator=(QHostInfo &&other) noexcept { swap(other); return *this; }
68 ~QHostInfo();
69
70 void swap(QHostInfo &other) noexcept { qSwap(d_ptr, other.d_ptr); }
71
72 QString hostName() const;
73 void setHostName(const QString &name);
74
75 QList<QHostAddress> addresses() const;
76 void setAddresses(const QList<QHostAddress> &addresses);
77
78 HostInfoError error() const;
79 void setError(HostInfoError error);
80
81 QString errorString() const;
82 void setErrorString(const QString &errorString);
83
84 void setLookupId(int id);
85 int lookupId() const;
86
87 static int lookupHost(const QString &name, QObject *receiver, const char *member);
88 static void abortHostLookup(int lookupId);
89
90 static QHostInfo fromName(const QString &name);
91 static QString localHostName();
92 static QString localDomainName();
93
94#ifdef Q_CLANG_QDOC
95 template<typename Functor>
96 static int lookupHost(const QString &name, Functor functor);
97 template<typename Functor>
98 static int lookupHost(const QString &name, const QObject *context, Functor functor);
99#else
100 // lookupHost to a QObject slot
101 template <typename Func>
102 static inline int lookupHost(const QString &name,
103 const typename QtPrivate::FunctionPointer<Func>::Object *receiver,
104 Func slot)
105 {
106 typedef QtPrivate::FunctionPointer<Func> SlotType;
107
108 typedef QtPrivate::FunctionPointer<void (*)(QHostInfo)> SignalType;
109 static_assert(int(SignalType::ArgumentCount) >= int(SlotType::ArgumentCount),
110 "The slot requires more arguments than the signal provides.");
111 static_assert((QtPrivate::CheckCompatibleArguments<typename SignalType::Arguments,
112 typename SlotType::Arguments>::value),
113 "Signal and slot arguments are not compatible.");
114 static_assert((QtPrivate::AreArgumentsCompatible<typename SlotType::ReturnType,
115 typename SignalType::ReturnType>::value),
116 "Return type of the slot is not compatible "
117 "with the return type of the signal.");
118
119 auto slotObj = new QtPrivate::QSlotObject<Func, typename SlotType::Arguments, void>(slot);
120 return lookupHostImpl(name, receiver, slotObj, nullptr);
121 }
122
123 // lookupHost to a callable (without context)
124 template <typename Func>
125 static inline typename std::enable_if<!QtPrivate::FunctionPointer<Func>::IsPointerToMemberFunction &&
126 !std::is_same<const char *, Func>::value, int>::type
127 lookupHost(const QString &name, Func slot)
128 {
129 return lookupHost(name, nullptr, std::move(slot));
130 }
131
132 // lookupHost to a functor or function pointer (with context)
133 template <typename Func1>
134 static inline typename std::enable_if<!QtPrivate::FunctionPointer<Func1>::IsPointerToMemberFunction &&
135 !std::is_same<const char*, Func1>::value, int>::type
136 lookupHost(const QString &name, QObject *context, Func1 slot)
137 {
138 typedef QtPrivate::FunctionPointer<Func1> SlotType;
139
140 static_assert(int(SlotType::ArgumentCount) <= 1,
141 "The slot must not require more than one argument");
142
143 auto slotObj = new QtPrivate::QFunctorSlotObject<Func1, 1,
144 typename QtPrivate::List<QHostInfo>,
145 void>(std::move(slot));
146 return lookupHostImpl(name, context, slotObj, nullptr);
147 }
148#endif // Q_QDOC
149
150private:
151 QHostInfoPrivate *d_ptr;
152 Q_DECLARE_PRIVATE(QHostInfo)
153
154 static int lookupHostImpl(const QString &name,
155 const QObject *receiver,
156 QtPrivate::QSlotObjectBase *slotObj,
157 const char *member);
158
159 friend QHostInfo Q_NETWORK_EXPORT qt_qhostinfo_lookup(const QString &name, QObject *receiver,
160 const char *member, bool *valid, int *id);
161};
162
163Q_DECLARE_SHARED(QHostInfo)
164
165QT_END_NAMESPACE
166
167Q_DECLARE_METATYPE(QHostInfo)
168
169#endif // QHOSTINFO_H
170