swift
comparefunctions.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #ifndef SWIFT_MISC_COMPAREFUNCTIONS_H
7 #define SWIFT_MISC_COMPAREFUNCTIONS_H
8 
9 #include <type_traits>
10 
11 #include <QDateTime>
12 #include <QFlags>
13 
14 namespace swift::misc::Compare
15 {
17  template <typename T, std::enable_if_t<std::is_arithmetic_v<T>, int> = 0>
18  int compare(T a, T b)
19  {
20  if (a < b) { return -1; }
21  if (b < a) { return 1; }
22  return 0;
23  }
24 
26  template <typename T, std::enable_if_t<std::is_enum_v<T>, int> = 0>
27  int compare(T a, T b)
28  {
29  using UT = std::underlying_type_t<T>;
30  return compare(static_cast<UT>(a), static_cast<UT>(b));
31  }
32 
34  template <typename T>
35  int compare(QFlags<T> a, QFlags<T> b)
36  {
37  using UT = typename QFlags<T>::Int;
38  return compare(static_cast<UT>(a), static_cast<UT>(b));
39  }
40 
42  inline int compare(const QDateTime &a, const QDateTime &b)
43  {
44  return compare(a.toMSecsSinceEpoch(), b.toMSecsSinceEpoch());
45  }
46 } // namespace swift::misc::Compare
47 
48 #endif // SWIFT_MISC_COMPAREFUNCTIONS_H
int compare(T a, T b)
Compare arithmetic values.