Mercurial > minori
diff src/core/date.cc @ 187:9613d72b097e
*: multiple performance improvements
like marking `static const` when it makes sense...
date: change old stupid heap-based method to a structure which should
make copying the thing actually make a copy.
also many performance-based changes, like removing the std::tie
dependency and forward-declaring nlohmann json
*: replace every instance of QString::fromUtf8 to Strings::ToQString.
the main difference is that our function will always convert exactly
what is in the string, while some other times it would only convert
up to the nearest NUL byte
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 06 Dec 2023 13:43:54 -0500 |
parents | 9b10175be389 |
children | f0ff06a45c42 |
line wrap: on
line diff
--- a/src/core/date.cc Wed Dec 06 11:47:59 2023 -0500 +++ b/src/core/date.cc Wed Dec 06 13:43:54 2023 -0500 @@ -1,14 +1,17 @@ #include "core/date.h" #include "core/json.h" + #include <QDate> #include <QDebug> + #include <algorithm> -#include <cstdint> -#include <tuple> /* An implementation of AniList's "fuzzy date" */ -#define CLAMP(x, low, high) (std::max(low, std::min(high, x))) +template<typename T> +bool CLAMP(T x, T low, T high) { + return std::max(low, std::min(high, x)); +} Date::Date() { } @@ -31,7 +34,7 @@ Date::Date(const nlohmann::json& json) { /* NOTE: this constructor is made for use with - AniList FussyDate-style JSON. In the future, some other + AniList FuzzyDate-style JSON. In the future, some other methods may be parsed and whatnot if necessary. */ if (json.contains("year") && json.at("year").is_number()) SetYear(json.at("year").get<unsigned int>()); @@ -42,58 +45,50 @@ } void Date::VoidYear() { - year.reset(); + year.Void(); } void Date::VoidMonth() { - month.reset(); + month.Void(); } void Date::VoidDay() { - day.reset(); + day.Void(); } void Date::SetYear(unsigned int y) { - year.reset(new unsigned int(y)); + year.Set(y); } void Date::SetMonth(unsigned int m) { - month.reset(new unsigned int(CLAMP(m, 1U, 12U))); + month.Set(CLAMP(m, 1U, 12U)); } void Date::SetDay(unsigned int d) { - day.reset(new unsigned int(CLAMP(d, 1U, 31U))); + day.Set(CLAMP(d, 1U, 31U)); } unsigned int Date::GetYear() const { - unsigned int* ptr = year.get(); - if (ptr != nullptr) - return *year; - return -1; + return year.Get(); } unsigned int Date::GetMonth() const { - unsigned int* ptr = month.get(); - if (ptr != nullptr) - return *month; - return -1; + return month.Get(); } unsigned int Date::GetDay() const { - unsigned int* ptr = day.get(); - if (ptr != nullptr) - return *day; - return -1; + return day.Get(); } bool Date::IsValid() const { - return year.get() && month.get() && day.get(); + return year.Enabled() && month.Enabled() && day.Enabled(); } bool Date::operator<(const Date& other) const { - unsigned int y = GetYear(), m = GetMonth(), d = GetDay(); - unsigned int o_y = other.GetYear(), o_m = other.GetMonth(), o_d = other.GetDay(); - return std::tie(y, m, d) < std::tie(o_y, o_m, o_d); + const unsigned int y = GetYear(), m = GetMonth(), d = GetDay(); + const unsigned int o_y = other.GetYear(), o_m = other.GetMonth(), o_d = other.GetDay(); + + return (y < o_y && m < o_m && d < o_d); } bool Date::operator>(const Date& other) const { @@ -109,26 +104,30 @@ } QDate Date::GetAsQDate() const { - /* QDates don't support "missing" values, for good reason. */ - if (IsValid()) - return QDate(*year, *month, *day); - else - return QDate(); + /* QDate doesn't support "missing" values (for good reason), + * 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); } nlohmann::json Date::GetAsAniListJson() const { nlohmann::json result = {}; - if (year.get()) - result["year"] = *year; + + if (year.Enabled()) + result["year"] = year.Get(); else result["year"] = nullptr; - if (month.get()) - result["month"] = *month; + + if (month.Enabled()) + result["month"] = month.Get(); else result["month"] = nullptr; - if (day.get()) - result["day"] = *day; + + if (day.Enabled()) + result["day"] = day.Get(); else result["day"] = nullptr; + return result; }