diff --git a/Source/Core/Common/StringUtil.h b/Source/Core/Common/StringUtil.h index 617aee16bc..c09d8e3732 100644 --- a/Source/Core/Common/StringUtil.h +++ b/Source/Core/Common/StringUtil.h @@ -4,6 +4,7 @@ #pragma once #include +#include #include #include #include @@ -18,22 +19,8 @@ #include #include "Common/CommonTypes.h" - -namespace detail -{ -template -constexpr bool IsBooleanEnum() -{ - if constexpr (std::is_enum_v) - { - return std::is_same_v, bool>; - } - else - { - return false; - } -} -} // namespace detail +#include "Common/EnumUtils.h" +#include "Common/TypeUtils.h" std::string StringFromFormatV(const char* format, va_list args); @@ -73,7 +60,7 @@ void TruncateToCString(std::string* s); bool TryParse(const std::string& str, bool* output); template -requires(std::is_integral_v || (std::is_enum_v && !detail::IsBooleanEnum())) +requires(std::is_integral_v || (std::is_enum_v && !Common::BooleanEnum)) bool TryParse(const std::string& str, T* output, int base = 0) { char* end_ptr = nullptr; @@ -111,8 +98,7 @@ bool TryParse(const std::string& str, T* output, int base = 0) return true; } -template -requires(detail::IsBooleanEnum()) +template bool TryParse(const std::string& str, T* output) { bool value; @@ -123,7 +109,7 @@ bool TryParse(const std::string& str, T* output) return true; } -template >* = nullptr> +template bool TryParse(std::string str, T* const output) { // Replace commas with dots. @@ -169,10 +155,9 @@ std::string ValueToString(double value); std::string ValueToString(int value); std::string ValueToString(s64 value); std::string ValueToString(bool value); -template ::value>* = nullptr> -std::string ValueToString(T value) +std::string ValueToString(Common::Enum auto value) { - return ValueToString(static_cast>(value)); + return ValueToString(Common::ToUnderlying(value)); } // Generates an hexdump-like representation of a binary data blob. @@ -180,15 +165,13 @@ std::string HexDump(const u8* data, size_t size); namespace Common { -template >* = nullptr> -std::from_chars_result FromChars(std::string_view sv, T& value, int base = 10) +std::from_chars_result FromChars(std::string_view sv, std::integral auto& value, int base = 10) { const char* const first = sv.data(); const char* const last = first + sv.size(); return std::from_chars(first, last, value, base); } -template >* = nullptr> -std::from_chars_result FromChars(std::string_view sv, T& value, +std::from_chars_result FromChars(std::string_view sv, std::floating_point auto& value, std::chars_format fmt = std::chars_format::general) { const char* const first = sv.data(); diff --git a/Source/Core/Common/TypeUtils.h b/Source/Core/Common/TypeUtils.h index 384af63b8c..0b1a2683eb 100644 --- a/Source/Core/Common/TypeUtils.h +++ b/Source/Core/Common/TypeUtils.h @@ -133,4 +133,12 @@ private: } m_value; }; +template +concept Enum = std::is_enum_v; + +template +concept TypedEnum = std::is_same_v, Underlying>; + +template +concept BooleanEnum = TypedEnum; } // namespace Common