summaryrefslogtreecommitdiff
path: root/basic_functions.h
blob: be58d0d574180fbcf34526ce220b775ab1821849 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#pragma once

// cpp
#include <limits>

// qt
#include <QHash>
#include <QHostAddress>
#include <QJsonObject>
#include <QLineF>
#include <QMetaEnum>
#include <QNetworkInterface>
#include <QPointF>
#include <QVector3D>

quint16 CalculateChecksum(quint16* usBuf, int size);

quint16 bit_reverse_word(quint16 value);

quint8 reverse(quint8 b);

const QJsonObject point2json(const QPointF& point);

const QPointF pointFromJson(const QJsonObject& json);

const QJsonObject line2json(const QLineF& line);

const QLineF lineFromJson(const QJsonObject& json);

const QJsonValue real2Json(const qreal& real);

qreal realFromJson(const QJsonValue& json);

template <typename T> static const QString enumToString(const T& value)
{
    return QMetaEnum::fromType<T>().valueToKey(value);
}

template <typename T> static const T stringToEnum(const QString& name)
{
    return T(QMetaEnum::fromType<T>().keyToValue(name.toStdString().c_str()));
}

Q_DECL_CONST_FUNCTION uint qHash(const QVector3D& v) noexcept;

template <typename T> static constexpr T min()
{
    return std::numeric_limits<T>::min();
}

template <typename T> static constexpr T max()
{
    return std::numeric_limits<T>::max();
}

template <typename T> static constexpr T lowest()
{
    return std::numeric_limits<T>::lowest();
}

#define INI_ENUM_FROM_JSON(type, name, default)                                \
    m_##name(json.contains(#name) ? stringToEnum<type>(#name) : default)

template <
    typename T,
    typename retT,
    retT (T::*func)() const,
    Qt::SortOrder order>
Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline bool comparator(
    const T& a,
    const T& b
) Q_DECL_NOTHROW
{
    if constexpr (order == Qt::AscendingOrder)
    {
        return ((&a)->*func)() < ((&b)->*func)();
    }
    else
    {
        return ((&a)->*func)() > ((&b)->*func)();
    }
}

bool copyDir(const QString& src, const QString& dst);

/*!
 * \brief getLocalNetworkIntefaceBySubnet - get local address from the same
 * subnet as remote
 * \param remote - remote address
 * \return valid local address on success, QHostAddress() otherwise
 */
QHostAddress getLocalNetworkIntefaceBySubnet(const QHostAddress& remote);