changeset 196:f0ff06a45c42

date: use std::optional for values
author Paper <mrpapersonic@gmail.com>
date Thu, 07 Dec 2023 16:28:11 -0500
parents 975a3f0965e2
children c4ca035c565d
files include/core/date.h src/core/date.cc src/sys/osx/dark_theme.cc src/sys/osx/filesystem.cc src/sys/win32/dark_theme.cc
diffstat 5 files changed, 40 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/include/core/date.h	Thu Dec 07 11:14:01 2023 -0500
+++ b/include/core/date.h	Thu Dec 07 16:28:11 2023 -0500
@@ -3,6 +3,8 @@
 
 #include "json/json_fwd.hpp"
 
+#include <optional>
+
 class QDate;
 
 class Date {
@@ -30,23 +32,9 @@
 		bool operator>=(const Date& other) const;
 
 	private:
-		/* this implementation sucks and we should really use a struct instead */
-		template<typename T>
-		struct OptionalNumber {
-			public:
-				T Get() const { return enabled ? num : 0; }
-				bool Enabled() const { return enabled; }
-				void Set(T n) { num = n; enabled = true; }
-				void Void() { num = 0; enabled = false; }
-
-			protected:
-				T num = 0;
-				bool enabled = false;
-		};
-
-		OptionalNumber<unsigned int> year;
-		OptionalNumber<unsigned int> month;
-		OptionalNumber<unsigned int> day;
+		std::optional<unsigned int> year;
+		std::optional<unsigned int> month;
+		std::optional<unsigned int> day;
 };
 
 #endif // __core__date_h
--- a/src/core/date.cc	Thu Dec 07 11:14:01 2023 -0500
+++ b/src/core/date.cc	Thu Dec 07 16:28:11 2023 -0500
@@ -45,43 +45,43 @@
 }
 
 void Date::VoidYear() {
-	year.Void();
+	year.reset();
 }
 
 void Date::VoidMonth() {
-	month.Void();
+	month.reset();
 }
 
 void Date::VoidDay() {
-	day.Void();
+	day.reset();
 }
 
 void Date::SetYear(unsigned int y) {
-	year.Set(y);
+	year = y;
 }
 
 void Date::SetMonth(unsigned int m) {
-	month.Set(CLAMP(m, 1U, 12U));
+	month = CLAMP(m, 1U, 12U);
 }
 
 void Date::SetDay(unsigned int d) {
-	day.Set(CLAMP(d, 1U, 31U));
+	day = CLAMP(d, 1U, 31U);
 }
 
 unsigned int Date::GetYear() const {
-	return year.Get();
+	return year.value_or(-1);
 }
 
 unsigned int Date::GetMonth() const {
-	return month.Get();
+	return month.value_or(-1);
 }
 
 unsigned int Date::GetDay() const {
-	return day.Get();
+	return day.value_or(-1);
 }
 
 bool Date::IsValid() const {
-	return year.Enabled() && month.Enabled() && day.Enabled();
+	return year.has_value() && month.has_value() && day.has_value();
 }
 
 bool Date::operator<(const Date& other) const {
@@ -108,24 +108,24 @@
 	 * so we do our best and return what we can.
 	*/
 
-	return QDate(year.Enabled() ? year.Get() : 2000, month.Enabled() ? month.Get() : 1, day.Enabled() ? day.Get() : 1);
+	return QDate(year.value_or(2000), month.value_or(1), day.value_or(1));
 }
 
 nlohmann::json Date::GetAsAniListJson() const {
 	nlohmann::json result = {};
 
-	if (year.Enabled())
-		result["year"] = year.Get();
+	if (year.has_value())
+		result["year"] = year.value();
 	else
 		result["year"] = nullptr;
 
-	if (month.Enabled())
-		result["month"] = month.Get();
+	if (month.has_value())
+		result["month"] = month.value();
 	else
 		result["month"] = nullptr;
 
-	if (day.Enabled())
-		result["day"] = day.Get();
+	if (day.has_value())
+		result["day"] = day.value();
 	else
 		result["day"] = nullptr;
 
--- a/src/sys/osx/dark_theme.cc	Thu Dec 07 11:14:01 2023 -0500
+++ b/src/sys/osx/dark_theme.cc	Thu Dec 07 16:28:11 2023 -0500
@@ -48,6 +48,7 @@
 		if (!RetrieveAppearanceNames())
 			return false;
 
+	// NSArray* array = @[NSAppearanceNameAqua, NSAppearanceNameDarkAqua];
 	CFArrayRef array = []() -> CFArrayRef {
 		CFStringRef refs[] = {NSAppearanceNameAqua, NSAppearanceNameDarkAqua};
 		return CFArrayCreate(NULL, reinterpret_cast<const void**>(refs), 2, &kCFTypeArrayCallBacks);
--- a/src/sys/osx/filesystem.cc	Thu Dec 07 11:14:01 2023 -0500
+++ b/src/sys/osx/filesystem.cc	Thu Dec 07 16:28:11 2023 -0500
@@ -18,22 +18,26 @@
 namespace osx {
 
 bool GetApplicationSupportDirectory(std::string& result) {
-	const CFArrayRef strings = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
+	// NSArray* strings = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, ON);
+	const CFArrayRef strings = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, ON);
 	if (!strings)
 		return false;
 
+	// NSIndex index = [strings count];
 	const CFIndex count = CFArrayGetCount(strings);
 	if (count < 1) {
 		CFRelease(strings);
 		return false;
 	}
 
+	// NSString* string = [strings objectAtIndex: 0];
 	const CFStringRef string = reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(strings, 0));
 	if (!string) {
 		CFRelease(strings);
 		return false;
 	}
 
+	// result = [string UTF8String];
 	result.resize(CFStringGetMaximumSizeForEncoding(CFStringGetLength(string), kCFStringEncodingUTF8) + 1);
 	if (!CFStringGetCString(string, &result.front(), result.length(), kCFStringEncodingUTF8)) {
 		CFRelease(strings);
--- a/src/sys/win32/dark_theme.cc	Thu Dec 07 11:14:01 2023 -0500
+++ b/src/sys/win32/dark_theme.cc	Thu Dec 07 16:28:11 2023 -0500
@@ -25,8 +25,8 @@
 			if (!library.get())
 				return E_POINTER;
 
-			/* GCC throws a fit here because C/C++ lacks a "generic" function pointer type.
-			   Ignore. */
+			// GCC throws a fit here because C/C++ lacks a "generic" function pointer type.
+			// Ignore.
 			auto set_wind_attrib = reinterpret_cast<decltype(::DwmSetWindowAttribute)*>(GetProcAddress(library.get(), "DwmSetWindowAttribute"));
 			if (!set_wind_attrib)
 				return E_POINTER;
@@ -42,21 +42,25 @@
 
 namespace win32 {
 
-/* NOTE: explicit conversion from builtin `bool` and win32 `BOOL` IS allowed. */
 bool SetTitleBarToBlack(QWidget* win, bool enabled) {
+	/* 19 and 20 are *both* DWMWA_USE_IMMERSIVE_DARK_MODE.
+	 *
+	 * It's 20 on newer versions of windows (i.e. win11 and late win10),
+	 * but it's 19 on very old versions of win10 nobody ought to be using anymore.
+	*/
+	static constexpr DWORD DWMWA_USE_IMMERSIVE_DARK_MODE_OLD = 19;
+	static constexpr DWORD DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
+
 	BOOL b = enabled;
 
-	/* MAGIC NUMBERS: 19 and 20 are both DWMWA_USE_IMMERSIVE_DARK_MODE.
-	   For clarification: it's 20 on newer versions of windows (i.e. win11 and late win10),
-	   but it's 19 on very old versions of win10 nobody ought to be using anymore. */
 	{
-		HRESULT result = dwmapi.SetWindowAttribute(reinterpret_cast<HWND>(win->winId()), 20, &b, sizeof(b));
+		HRESULT result = dwmapi.SetWindowAttribute(reinterpret_cast<HWND>(win->winId()), DWMWA_USE_IMMERSIVE_DARK_MODE, &b, sizeof(b));
 		if (result == S_OK)
 			return b;
 	}
 
 	{
-		HRESULT result = dwmapi.SetWindowAttribute(reinterpret_cast<HWND>(win->winId()), 19, &b, sizeof(b));
+		HRESULT result = dwmapi.SetWindowAttribute(reinterpret_cast<HWND>(win->winId()), DWMWA_USE_IMMERSIVE_DARK_MODE_OLD, &b, sizeof(b));
 		if (result == S_OK)
 			return b;
 	}