From bdafd8f2011fe035d1fbab2dbab01c9fbce4d5b8 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Wed, 11 Apr 2012 04:10:56 +0000 Subject: [PATCH] Reset to the platform-specific default option values rather than the global defaults Originally committed to SVN as r6692. --- aegisub/libaegisub/common/option.cpp | 2 +- aegisub/libaegisub/common/option_visit.cpp | 83 ++++++---------------- aegisub/libaegisub/common/option_visit.h | 9 ++- 3 files changed, 31 insertions(+), 63 deletions(-) diff --git a/aegisub/libaegisub/common/option.cpp b/aegisub/libaegisub/common/option.cpp index 50502a872..77f477060 100644 --- a/aegisub/libaegisub/common/option.cpp +++ b/aegisub/libaegisub/common/option.cpp @@ -122,7 +122,7 @@ void Options::LoadConfig(std::istream& stream, bool ignore_errors) { LOG_E("option/load") << "json::Exception: " << e.what(); } - ConfigVisitor config_visitor(values, "", ignore_errors); + ConfigVisitor config_visitor(values, "", ignore_errors, !ignore_errors); config_root.Accept(config_visitor); } diff --git a/aegisub/libaegisub/common/option_visit.cpp b/aegisub/libaegisub/common/option_visit.cpp index a1216e7a8..abe19f207 100644 --- a/aegisub/libaegisub/common/option_visit.cpp +++ b/aegisub/libaegisub/common/option_visit.cpp @@ -26,19 +26,20 @@ #ifndef LAGI_PRE #include #include -#include #endif #include #include #include +#include namespace agi { -ConfigVisitor::ConfigVisitor(OptionValueMap &val, const std::string &member_name, bool ignore_errors) +ConfigVisitor::ConfigVisitor(OptionValueMap &val, const std::string &member_name, bool ignore_errors, bool replace) : values(val) , name(member_name) , ignore_errors(ignore_errors) +, replace(replace) { } @@ -57,13 +58,13 @@ void ConfigVisitor::Visit(const json::Object& object) { name += "/"; for (; index != index_end; ++index) { - ConfigVisitor config_visitor(values, name + index->first, ignore_errors); + ConfigVisitor config_visitor(values, name + index->first, ignore_errors, replace); index->second.Accept(config_visitor); } } template -OptionValue *ConfigVisitor::ReadArray(json::Array const& src, std::string const& array_type, void (OptionValueType::*set_list)(const std::vector&)) { +OptionValue *ConfigVisitor::ReadArray(json::Array const& src, std::string const& array_type, void (OptionValueType::*)(const std::vector&)) { std::vector arr; arr.reserve(src.size()); @@ -82,9 +83,7 @@ OptionValue *ConfigVisitor::ReadArray(json::Array const& src, std::string const& arr.push_back(obj.begin()->second); } - OptionValueType *ret = new OptionValueType(name); - (ret->*set_list)(arr); - return ret; + return new OptionValueType(name, arr); } void ConfigVisitor::Visit(const json::Array& array) { @@ -145,63 +144,25 @@ void ConfigVisitor::AddOptionValue(OptionValue* opt) { return; } - if (!values.count(name)) { + if (!values.count(name)) + values[name] = opt; + else if (replace) { + delete values[name]; values[name] = opt; - return; } - - // Ensure than opt is deleted at the end of this function even if the Set - // method throws - std::auto_ptr auto_opt(opt); - - try { - switch (opt->GetType()) { - case OptionValue::Type_String: - values[name]->SetString(opt->GetString()); - break; - - case OptionValue::Type_Int: - values[name]->SetInt(opt->GetInt()); - break; - - case OptionValue::Type_Double: - values[name]->SetDouble(opt->GetDouble()); - break; - - case OptionValue::Type_Colour: - values[name]->SetColour(opt->GetColour()); - break; - - case OptionValue::Type_Bool: - values[name]->SetBool(opt->GetBool()); - break; - - case OptionValue::Type_List_String: - values[name]->SetListString(opt->GetListString()); - break; - - case OptionValue::Type_List_Int: - values[name]->SetListInt(opt->GetListInt()); - break; - - case OptionValue::Type_List_Double: - values[name]->SetListDouble(opt->GetListDouble()); - break; - - case OptionValue::Type_List_Colour: - values[name]->SetListColour(opt->GetListColour()); - break; - - case OptionValue::Type_List_Bool: - values[name]->SetListBool(opt->GetListBool()); - break; + else { + try { + // Ensure than opt is deleted at the end of this function even if the Set + // method throws + agi::scoped_ptr auto_opt(opt); + values[name]->Set(opt); + } + catch (agi::OptionValueError const& e) { + if (ignore_errors) + LOG_E("option/load/config_visitor") << "Error loading option from user configuration: " << e.GetChainedMessage(); + else + throw; } - } - catch (agi::OptionValueError const& e) { - if (ignore_errors) - LOG_E("option/load/config_visitor") << "Error loading option from user configuration: " << e.GetChainedMessage(); - else - throw; } } } // namespace agi diff --git a/aegisub/libaegisub/common/option_visit.h b/aegisub/libaegisub/common/option_visit.h index 918953e0f..943896204 100644 --- a/aegisub/libaegisub/common/option_visit.h +++ b/aegisub/libaegisub/common/option_visit.h @@ -35,9 +35,16 @@ DEFINE_SIMPLE_EXCEPTION_NOINNER(OptionJsonValueSingle, OptionJsonValueError, "op DEFINE_SIMPLE_EXCEPTION_NOINNER(OptionJsonValueNull, OptionJsonValueError, "options/value") class ConfigVisitor : public json::ConstVisitor { + /// Option map being populated OptionValueMap &values; + /// Option name prefix to add to read names std::string name; + /// Log errors rather than throwing them, for when loading user config files + /// (as a bad user config file shouldn't make the program fail to start) bool ignore_errors; + /// Replace existing options rather than changing their value, so that the + /// default value is changed to the new one + bool replace; template void Error(const char *message); @@ -47,7 +54,7 @@ class ConfigVisitor : public json::ConstVisitor { void AddOptionValue(OptionValue* opt); public: - ConfigVisitor(OptionValueMap &val, const std::string &member_name, bool ignore_errors = false); + ConfigVisitor(OptionValueMap &val, const std::string &member_name, bool ignore_errors = false, bool replace = false); void Visit(const json::Array& array); void Visit(const json::Object& object);