From 84d8d747ad207a5d71cbbb460056413f7bd7c3ac Mon Sep 17 00:00:00 2001 From: Mat Sutcliffe Date: Mon, 24 Aug 2020 00:53:35 +0100 Subject: [PATCH] Issue #77 Simplify compare functions --- src/blackmisc/comparefunctions.cpp | 51 ------------------------------ src/blackmisc/comparefunctions.h | 47 +++++++++++++++++---------- 2 files changed, 31 insertions(+), 67 deletions(-) delete mode 100644 src/blackmisc/comparefunctions.cpp diff --git a/src/blackmisc/comparefunctions.cpp b/src/blackmisc/comparefunctions.cpp deleted file mode 100644 index 619791dca..000000000 --- a/src/blackmisc/comparefunctions.cpp +++ /dev/null @@ -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 diff --git a/src/blackmisc/comparefunctions.h b/src/blackmisc/comparefunctions.h index 004cb326f..5f6c05539 100644 --- a/src/blackmisc/comparefunctions.h +++ b/src/blackmisc/comparefunctions.h @@ -11,31 +11,46 @@ #ifndef BLACKMISC_COMPAREFUNCTIONS_H #define BLACKMISC_COMPAREFUNCTIONS_H -#include "blackmisc/blackmiscexport.h" #include -#include +#include + +template +class QFlags; namespace BlackMisc { namespace Compare { - //! Compare bool - BLACKMISC_EXPORT int compare(bool a, bool b); + //! Compare arithmetic values + template ::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 ::value, int> = 0> + int compare(T a, T b) + { + using UT = std::underlying_type_t; + return compare(static_cast(a), static_cast(b)); + } - //! Compare uint - BLACKMISC_EXPORT int compare(uint a, uint b); + //! Compare QFlags + template + int compare(QFlags a, QFlags b) + { + using UT = typename QFlags::Int; + return compare(static_cast(a), static_cast(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