fix: fix ConfigManager::StringSetting issues

- use yycc_u8string_view in default value and Set function instead of const yycc_char8_t*
- fix value check error in Set function.
This commit is contained in:
yyc12345 2024-07-07 21:09:51 +08:00
parent 4f1e2447d0
commit d27a66e770

View File

@ -101,7 +101,7 @@ namespace YYCC::ConfigManager {
virtual ~NumberSetting() {} virtual ~NumberSetting() {}
_Ty Get() const { return m_Data; } _Ty Get() const { return m_Data; }
bool Set(_Ty new_data) { bool Set(_Ty new_data) {
// validate data // validate data
if (m_Constraint.IsValid() && !m_Constraint.m_CheckFct(new_data)) if (m_Constraint.IsValid() && !m_Constraint.m_CheckFct(new_data))
return false; return false;
@ -137,21 +137,17 @@ namespace YYCC::ConfigManager {
class StringSetting : public AbstractSetting { class StringSetting : public AbstractSetting {
public: public:
StringSetting(const yycc_char8_t* name, const yycc_char8_t* default_value, Constraint<yycc_u8string> constraint = Constraint<yycc_u8string> {}) : StringSetting(const yycc_char8_t* name, const yycc_u8string_view& default_value, Constraint<yycc_u8string_view> constraint = Constraint<yycc_u8string_view> {}) :
AbstractSetting(name), m_Data(), m_DefaultData(), m_Constraint(constraint) { AbstractSetting(name), m_Data(), m_DefaultData(), m_Constraint(constraint) {
if (default_value != nullptr) { m_Data = default_value;
m_Data = default_value; m_DefaultData = default_value;
m_DefaultData = default_value;
}
} }
virtual ~StringSetting() {} virtual ~StringSetting() {}
const yycc_u8string& Get() const { return m_Data; } const yycc_u8string& Get() const { return m_Data; }
bool Set(const yycc_char8_t* new_data) { bool Set(const yycc_u8string_view& new_data) {
// check data validation // check data validation
if (new_data == nullptr) if (m_Constraint.IsValid() && !m_Constraint.m_CheckFct(new_data))
return false;
if (m_Constraint.IsValid() && !m_Constraint.m_CheckFct(m_Data))
return false; return false;
// assign data // assign data
m_Data = new_data; m_Data = new_data;
@ -194,7 +190,7 @@ namespace YYCC::ConfigManager {
} }
yycc_u8string m_Data, m_DefaultData; yycc_u8string m_Data, m_DefaultData;
Constraint<yycc_u8string> m_Constraint; Constraint<yycc_u8string_view> m_Constraint;
}; };
#pragma endregion #pragma endregion