From b225120e7220c61bb46800e39206175f9e2608e0 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 8 Sep 2021 14:05:34 +0200
Subject: [PATCH] MISC: Make things constexpr when possible

---
 src/Lib/HostOsInfo.cc  | 18 ++++++++-------
 src/Lib/HostOsInfo.hh  | 51 ++++++++++++++++++++++++++----------------
 src/UI/Theme/Theme.cc  |  9 +++++---
 src/VivyApplication.hh |  1 +
 4 files changed, 49 insertions(+), 30 deletions(-)

diff --git a/src/Lib/HostOsInfo.cc b/src/Lib/HostOsInfo.cc
index 48f5bc6d..b6628fea 100644
--- a/src/Lib/HostOsInfo.cc
+++ b/src/Lib/HostOsInfo.cc
@@ -1,10 +1,13 @@
 #include "HostOsInfo.hh"
 
 #if !defined(QT_NO_OPENGL) && defined(QT_GUI_LIB)
+#define VIVY_OPENGL 1
 #include <QOpenGLContext>
+#else
+#define VIVY_OPENGL 0
 #endif
 
-#ifdef Q_OS_WIN
+#if VIVY_WIN32
 #include <qt_windows.h>
 #endif
 
@@ -13,7 +16,7 @@ using namespace Vivy::Utils;
 Qt::CaseSensitivity HostOsInfo::m_overrideFileNameCaseSensitivity = Qt::CaseSensitive;
 bool HostOsInfo::m_useOverrideFileNameCaseSensitivity             = false;
 
-#ifdef Q_OS_WIN
+#if VIVY_WIN32
 static WORD
 hostProcessorArchitecture()
 {
@@ -26,7 +29,7 @@ hostProcessorArchitecture()
 HostOsInfo::HostArchitecture
 HostOsInfo::hostArchitecture()
 {
-#ifdef Q_OS_WIN
+#if VIVY_WIN32
     static const WORD processorArchitecture = hostProcessorArchitecture();
     switch (processorArchitecture) {
     case PROCESSOR_ARCHITECTURE_AMD64: return HostOsInfo::HostArchitectureAMD64;
@@ -54,16 +57,15 @@ HostOsInfo::unsetOverrideFileNameCaseSensitivity()
 }
 
 bool
-HostOsInfo::canCreateOpenGLContext(QString *errorMessage)
+HostOsInfo::canCreateOpenGLContext([[maybe_unused]] QString *errorMessage)
 {
-#if defined(QT_NO_OPENGL) || !defined(QT_GUI_LIB)
-    Q_UNUSED(errorMessage)
-    return false;
-#else
+#if VIVY_OPENGL
     static const bool canCreate = QOpenGLContext().create();
     if (!canCreate)
         *errorMessage =
             QCoreApplication::translate("Utils::HostOsInfo", "Cannot create OpenGL context.");
     return canCreate;
+#else
+    return false;
 #endif
 }
diff --git a/src/Lib/HostOsInfo.hh b/src/Lib/HostOsInfo.hh
index 97b28896..af00e94b 100644
--- a/src/Lib/HostOsInfo.hh
+++ b/src/Lib/HostOsInfo.hh
@@ -4,27 +4,47 @@
 
 #ifdef Q_OS_WIN
 #define VIVY_HOST_EXE_SUFFIX VIVY_WIN_EXE_SUFFIX
+#define VIVY_WIN32           1
 #else
 #define VIVY_HOST_EXE_SUFFIX ""
+#define VIVY_WIN32           0
 #endif // Q_OS_WIN
 
+// Detect MacOS
+#if defined(Q_OS_DARWIN) || defined(Q_OS_MACOS)
+#define VIVY_MACOS 1
+#else
+#define VIVY_MACOS 0
+#endif
+
+// Detect Linux or UNIX
+#if defined(Q_OS_LINUX)
+#define VIVY_LINUX 1
+#define VIVY_UNIX  1
+#elif defined(Q_OS_UNIX)
+#define VIVY_LINUX 0
+#define VIVY_UNIX  1
+#else
+#define VIVY_LINUX 0
+#define VIVY_UNIX  0
+#endif
+
 namespace Vivy::Utils
 {
 class HostOsInfo {
 public:
     static constexpr OsType hostOs()
     {
-#if defined(Q_OS_WIN)
-        return OsTypeWindows;
-#elif defined(Q_OS_LINUX)
-        return OsTypeLinux;
-#elif defined(Q_OS_MAC)
-        return OsTypeMac;
-#elif defined(Q_OS_UNIX)
-        return OsTypeOtherUnix;
-#else
-        return OsTypeOther;
-#endif
+        if constexpr (VIVY_WIN32)
+            return OsTypeWindows;
+        else if constexpr (VIVY_LINUX)
+            return OsTypeLinux;
+        else if constexpr (VIVY_MACOS)
+            return OsTypeMac;
+        else if constexpr (VIVY_UNIX)
+            return OsTypeOtherUnix;
+        else
+            return OsTypeOther;
     }
 
     enum HostArchitecture {
@@ -39,14 +59,7 @@ public:
     static constexpr bool isWindowsHost() { return hostOs() == OsTypeWindows; }
     static constexpr bool isLinuxHost() { return hostOs() == OsTypeLinux; }
     static constexpr bool isMacHost() { return hostOs() == OsTypeMac; }
-    static constexpr bool isAnyUnixHost()
-    {
-#ifdef Q_OS_UNIX
-        return true;
-#else
-        return false;
-#endif
-    }
+    static constexpr bool isAnyUnixHost() { return VIVY_UNIX; }
 
     static QString withExecutableSuffix(const QString &executable)
     {
diff --git a/src/UI/Theme/Theme.cc b/src/UI/Theme/Theme.cc
index 60d5a103..31c53f16 100644
--- a/src/UI/Theme/Theme.cc
+++ b/src/UI/Theme/Theme.cc
@@ -50,11 +50,14 @@ systemUsesDarkMode() noexcept
         const auto setting =
             QSettings(regkey, QSettings::NativeFormat).value("AppsUseLightTheme").toInt(&ok);
         return ok && setting == 0;
-    } else if (Utils::HostOsInfo::isMacHost()) {
+    }
+
+    else if constexpr (Utils::HostOsInfo::isMacHost())
         return false;
-    } else if (Utils::HostOsInfo::isLinuxHost()) {
+
+    else if constexpr (Utils::HostOsInfo::isLinuxHost())
         return true;
-    }
+
     return false;
 }
 
diff --git a/src/VivyApplication.hh b/src/VivyApplication.hh
index d0a25b8b..4d67f0ec 100644
--- a/src/VivyApplication.hh
+++ b/src/VivyApplication.hh
@@ -42,6 +42,7 @@
 #endif
 
 #include "Lib/Log.hh"
+#include "Lib/HostOsInfo.hh"
 #include "UI/Theme/Theme.hh"
 
 namespace Vivy
-- 
GitLab