mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-15 01:05:35 +08:00
Issue #77 Simplify compare functions
This commit is contained in:
@@ -1,51 +0,0 @@
|
|||||||
/* Copyright (C) 2015
|
|
||||||
* swift Project Community / Contributors
|
|
||||||
*
|
|
||||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
|
||||||
* directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated,
|
|
||||||
* or distributed except according to the terms contained in the LICENSE file.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "blackmisc/comparefunctions.h"
|
|
||||||
|
|
||||||
namespace BlackMisc
|
|
||||||
{
|
|
||||||
namespace Compare
|
|
||||||
{
|
|
||||||
int compare(bool a, bool b)
|
|
||||||
{
|
|
||||||
if ((a && b) || (!a && !b)) return 0;
|
|
||||||
if (a && !b) return 1;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int compare(int a, int b)
|
|
||||||
{
|
|
||||||
if (a == b) return 0;
|
|
||||||
return a < b ? -1 : 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int compare(uint a, uint b)
|
|
||||||
{
|
|
||||||
if (a == b) return 0;
|
|
||||||
return a < b ? -1 : 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int compare(qint64 a, qint64 b)
|
|
||||||
{
|
|
||||||
if (a == b) return 0;
|
|
||||||
return a < b ? -1 : 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int compare(double a, double b)
|
|
||||||
{
|
|
||||||
if (qFuzzyCompare(a, b)) return 0;
|
|
||||||
return a < b ? -1 : 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int compare(const QDateTime &a, const QDateTime &b)
|
|
||||||
{
|
|
||||||
return Compare::compare(a.toMSecsSinceEpoch(), b.toMSecsSinceEpoch());
|
|
||||||
}
|
|
||||||
} // ns
|
|
||||||
} // ns
|
|
||||||
@@ -11,31 +11,46 @@
|
|||||||
#ifndef BLACKMISC_COMPAREFUNCTIONS_H
|
#ifndef BLACKMISC_COMPAREFUNCTIONS_H
|
||||||
#define BLACKMISC_COMPAREFUNCTIONS_H
|
#define BLACKMISC_COMPAREFUNCTIONS_H
|
||||||
|
|
||||||
#include "blackmisc/blackmiscexport.h"
|
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QtGlobal>
|
#include <type_traits>
|
||||||
|
|
||||||
|
template<typename Enum>
|
||||||
|
class QFlags;
|
||||||
|
|
||||||
namespace BlackMisc
|
namespace BlackMisc
|
||||||
{
|
{
|
||||||
namespace Compare
|
namespace Compare
|
||||||
{
|
{
|
||||||
//! Compare bool
|
//! Compare arithmetic values
|
||||||
BLACKMISC_EXPORT int compare(bool a, bool b);
|
template <typename T, std::enable_if_t<std::is_arithmetic<T>::value, int> = 0>
|
||||||
|
int compare(T a, T b)
|
||||||
|
{
|
||||||
|
if (a < b) { return -1; }
|
||||||
|
if (b < a) { return 1; }
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
//! Compare int
|
//! Compare enumerators
|
||||||
BLACKMISC_EXPORT int compare(int a, int b);
|
template <typename T, std::enable_if_t<std::is_enum<T>::value, int> = 0>
|
||||||
|
int compare(T a, T b)
|
||||||
|
{
|
||||||
|
using UT = std::underlying_type_t<T>;
|
||||||
|
return compare(static_cast<UT>(a), static_cast<UT>(b));
|
||||||
|
}
|
||||||
|
|
||||||
//! Compare uint
|
//! Compare QFlags
|
||||||
BLACKMISC_EXPORT int compare(uint a, uint b);
|
template <typename T>
|
||||||
|
int compare(QFlags<T> a, QFlags<T> b)
|
||||||
|
{
|
||||||
|
using UT = typename QFlags<T>::Int;
|
||||||
|
return compare(static_cast<UT>(a), static_cast<UT>(b));
|
||||||
|
}
|
||||||
|
|
||||||
//! Compare qint64
|
//! Compare dates
|
||||||
BLACKMISC_EXPORT int compare(qint64 a, qint64 b);
|
inline int compare(const QDateTime &a, const QDateTime &b)
|
||||||
|
{
|
||||||
//! Compare double
|
return compare(a.toMSecsSinceEpoch(), b.toMSecsSinceEpoch());
|
||||||
BLACKMISC_EXPORT int compare(double a, double b);
|
}
|
||||||
|
|
||||||
//! Compare double
|
|
||||||
BLACKMISC_EXPORT int compare(const QDateTime &a, const QDateTime &b);
|
|
||||||
} // ns
|
} // ns
|
||||||
} // ns
|
} // ns
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user