-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.h
58 lines (49 loc) · 2.09 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* This file is part of Osavul.
*
* Osavul is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Osavul is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Osavul. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OSAVUL_TYPES_H__
#define OSAVUL_TYPES_H__
#include <QtNetwork/QHostAddress>
namespace Settings
{
class PreferredIPVersion
{
public:
PreferredIPVersion(QAbstractSocket::NetworkLayerProtocol prefer_ = QAbstractSocket::IPv4Protocol)
: prefer(prefer_)
{ }
PreferredIPVersion(int prefer_)
: prefer(QAbstractSocket::NetworkLayerProtocol(prefer_))
{ }
bool v4() const { return prefer == QAbstractSocket::IPv4Protocol; }
bool v6() const { return prefer == QAbstractSocket::IPv6Protocol; }
bool any() const { return prefer == QAbstractSocket::UnknownNetworkLayerProtocol; }
explicit operator QAbstractSocket::NetworkLayerProtocol() const { return prefer; }
operator int() const { return int(prefer); }
operator QVariant() { return QVariant(int(prefer)); }
// note: returns false if any is preferred
bool isPreferredType(const QAbstractSocket &addr)
{
return addr.state() >= QAbstractSocket::ConnectingState && addr.peerAddress().protocol() == prefer;
}
protected:
QAbstractSocket::NetworkLayerProtocol prefer;
};
const PreferredIPVersion preferredIPVersion_v4 = QAbstractSocket::IPv4Protocol;
const PreferredIPVersion preferredIPVersion_v6 = QAbstractSocket::IPv6Protocol;
const PreferredIPVersion preferredIPVersion_any = QAbstractSocket::UnknownNetworkLayerProtocol;
}
#endif