From 0c4eb7a8618101365e95bb890e10450b84c89d39 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 5 Mar 2013 19:15:38 +0100 Subject: [PATCH] simplify how string -> option conversion are written, free functions instead of template specialization --- src/option_manager.cc | 55 ++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/src/option_manager.cc b/src/option_manager.cc index 477deab6..03a5ed9d 100644 --- a/src/option_manager.cc +++ b/src/option_manager.cc @@ -6,6 +6,28 @@ namespace Kakoune { +namespace +{ + +String option_to_string(const String& opt) { return opt; } +void option_from_string(const String& str, String& opt) { opt = str; } + +String option_to_string(int opt) { return int_to_str(opt); } +void option_from_string(const String& str, int& opt) { opt = str_to_int(str); } + +String option_to_string(bool opt) { return opt ? "true" : "false"; } +void option_from_string(const String& str, bool& opt) +{ + if (str == "true" or str == "yes") + opt = true; + else if (str == "false" or str == "no") + opt = false; + else + throw runtime_error("boolean values are either true, yes, false or no"); +} + +} + template class TypedOption : public Option { @@ -23,8 +45,16 @@ public: } const T& get() const { return m_value; } - String get_as_string() const override; - void set_from_string(const String& str) override; + String get_as_string() const override + { + return option_to_string(m_value); + } + void set_from_string(const String& str) override + { + T val; + option_from_string(str, val); + set(val); + } Option* clone(OptionManager& manager) const override { @@ -40,35 +70,16 @@ Option::Option(OptionManager& manager, String name) template const T& Option::get() const { return dynamic_cast*>(this)->get(); } template void Option::set(const T& val) { return dynamic_cast*>(this)->set(val); } -// TypedOption specializations; -template<> String TypedOption::get_as_string() const { return m_value; } -template<> void TypedOption::set_from_string(const String& str) { set(str); } -template class TypedOption; template const String& Option::get() const; template void Option::set(const String&); -// TypedOption specializations; -template<> String TypedOption::get_as_string() const { return int_to_str(m_value); } -template<> void TypedOption::set_from_string(const String& str) { set(str_to_int(str)); } -template class TypedOption; template const int& Option::get() const; template void Option::set(const int&); -// TypedOption specializations; -template<> String TypedOption::get_as_string() const { return m_value ? "true" : "false"; } -template<> void TypedOption::set_from_string(const String& str) -{ - if (str == "true" or str == "yes") - m_value = true; - else if (str == "false" or str == "no") - m_value = false; - else - throw runtime_error("boolean values are either true, yes, false or no"); -} -template class TypedOption; template const bool& Option::get() const; template void Option::set(const bool&); + OptionManager::OptionManager(OptionManager& parent) : m_parent(&parent) {