From 79e82e2489f9635ece213da9003f49c667e04ae9 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Mon, 24 Apr 2017 18:55:04 +0100 Subject: [PATCH] Do the str to int conversion in the SettingsBag. --- src/response.cpp | 5 ++--- src/settings_bag.cpp | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/response.cpp b/src/response.cpp index f1d216c..d967e9a 100644 --- a/src/response.cpp +++ b/src/response.cpp @@ -22,7 +22,6 @@ #include "duckhandy/stringize.h" #include "pathname/pathname.hpp" #include "list_highlight_langs.hpp" -#include "duckhandy/lexical_cast.hpp" #include #include #include @@ -60,7 +59,7 @@ namespace tawashi { if (parSettings["redis_mode"] == "inet") { return IncRedis( parSettings.as("redis_server"), - dhandy::lexical_cast(parSettings["redis_port"]) + parSettings.as("redis_port") ); } else if (parSettings["redis_mode"] == "sock") { @@ -136,7 +135,7 @@ namespace tawashi { if (m_redis) { m_redis->wait_for_connect(); auto batch = m_redis->make_batch(); - batch.select(dhandy::lexical_cast((*m_settings)["redis_db"])); + batch.select(m_settings->as("redis_db")); batch.client_setname("tawashi_v" STRINGIZE(VERSION_MAJOR) "." STRINGIZE(VERSION_MINOR) "." STRINGIZE(VERSION_PATCH)); batch.throw_if_failed(); } diff --git a/src/settings_bag.cpp b/src/settings_bag.cpp index d177a57..f4b7d9c 100644 --- a/src/settings_bag.cpp +++ b/src/settings_bag.cpp @@ -16,7 +16,11 @@ */ #include "settings_bag.hpp" +#include "duckhandy/lexical_cast.hpp" +#include #include +#include +#include namespace tawashi { SettingsBag::SettingsBag (const Kakoune::SafePtr& parIni) : @@ -43,9 +47,38 @@ namespace tawashi { template <> std::string SettingsBag::as (boost::string_ref parIndex) const { - auto& setting = (*this)[parIndex]; + auto& setting = this->at(parIndex); return std::string(setting.data(), setting.size()); } + template <> + bool SettingsBag::as (boost::string_ref parIndex) const { + auto& setting = this->at(parIndex); + if (setting == "true" or setting == "yes" or setting == "1" or setting == "on") { + return true; + } + else if (setting == "false" or setting == "no" or setting == "0" or setting == "off") { + return false; + } + else { + std::ostringstream oss; + oss << "Bad conversion: can't convert \"" << setting << "\" to bool"; + throw std::runtime_error(oss.str()); + } + } + + template <> + uint16_t SettingsBag::as (boost::string_ref parIndex) const { + return dhandy::lexical_cast(this->at(parIndex)); + } + + template <> + int SettingsBag::as (boost::string_ref parIndex) const { + return dhandy::lexical_cast(this->at(parIndex)); + } + template std::string SettingsBag::as (boost::string_ref parIndex) const; + template bool SettingsBag::as (boost::string_ref parIndex) const; + template uint16_t SettingsBag::as (boost::string_ref parIndex) const; + template int SettingsBag::as (boost::string_ref parIndex) const; } //namespace tawashi