Issue #77 Simplify compare functions

This commit is contained in:
Mat Sutcliffe
2020-08-24 00:53:35 +01:00
parent 6703922de5
commit 84d8d747ad
2 changed files with 31 additions and 67 deletions

View File

@@ -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

View File

@@ -11,31 +11,46 @@
#ifndef BLACKMISC_COMPAREFUNCTIONS_H
#define BLACKMISC_COMPAREFUNCTIONS_H
#include "blackmisc/blackmiscexport.h"
#include <QDateTime>
#include <QtGlobal>
#include <type_traits>
template<typename Enum>
class QFlags;
namespace BlackMisc
{
namespace Compare
{
//! Compare bool
BLACKMISC_EXPORT int compare(bool a, bool b);
//! Compare arithmetic values
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
BLACKMISC_EXPORT int compare(int a, int b);
//! Compare enumerators
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
BLACKMISC_EXPORT int compare(uint a, uint b);
//! Compare QFlags
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
BLACKMISC_EXPORT int compare(qint64 a, qint64 b);
//! Compare double
BLACKMISC_EXPORT int compare(double a, double b);
//! Compare double
BLACKMISC_EXPORT int compare(const QDateTime &a, const QDateTime &b);
//! Compare dates
inline int compare(const QDateTime &a, const QDateTime &b)
{
return compare(a.toMSecsSinceEpoch(), b.toMSecsSinceEpoch());
}
} // ns
} // ns