[PowerRename]Save data from the last run outside of the settings file (#28861)

* [PowerRename] Sync settings before saving last window size

* address review comments
This commit is contained in:
Andrey Nekrasov
2023-10-24 22:49:09 +02:00
committed by GitHub
parent b5bd19f3f5
commit ef6b17c5d1
5 changed files with 142 additions and 64 deletions

View File

@@ -70,4 +70,40 @@ namespace json
{
return value; // identity function overload for convenience
}
template<typename T, typename D = std::optional<T>>
requires std::constructible_from<std::optional<T>, D>
void get(const json::JsonObject& o, const wchar_t* name, T& destination, D default_value = std::nullopt)
{
try
{
if constexpr (std::is_same_v<T, bool>)
{
destination = o.GetNamedBoolean(name);
}
else if constexpr (std::is_arithmetic_v<T>)
{
destination = static_cast<T>(o.GetNamedNumber(name));
}
else if constexpr (std::is_same_v<T, std::wstring>)
{
destination = o.GetNamedString(name);
}
else if constexpr (std::is_same_v<T, json::JsonObject>)
{
destination = o.GetNamedObject(name);
}
else
{
static_assert(std::bool_constant<std::is_same_v<T, T&>>::value, "Unsupported type");
}
}
catch (...)
{
std::optional<T> maybe_default{ std::move(default_value) };
if (maybe_default.has_value())
destination = std::move(*maybe_default);
}
}
}