From fe53ea7ea1c1bebdbc728684eb086c9c2097dda4 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Mon, 24 Apr 2017 09:25:04 +0100 Subject: [PATCH] Implement as_str() and as_ref() as template as<>(). Also provide a commodity at() that is a synonym for as() but much shorter to type. --- src/list_highlight_langs.cpp | 2 +- src/pastie_response.cpp | 2 +- src/response.cpp | 8 ++++---- src/settings_bag.cpp | 18 ++++++++---------- src/settings_bag.hpp | 13 +++++++++++-- 5 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/list_highlight_langs.cpp b/src/list_highlight_langs.cpp index 5b1b184..ea01e18 100644 --- a/src/list_highlight_langs.cpp +++ b/src/list_highlight_langs.cpp @@ -24,7 +24,7 @@ namespace tawashi { HighlightLangList list_highlight_langs (const SettingsBag& parSettings) { - srchilite::LangMap lang_map(parSettings.as_str("langmap_dir"), "lang.map"); + srchilite::LangMap lang_map(parSettings.as("langmap_dir"), "lang.map"); lang_map.open(); const auto lang_range = boost::make_iterator_range(lang_map.begin(), lang_map.end()); diff --git a/src/pastie_response.cpp b/src/pastie_response.cpp index 78841c7..61c5d74 100644 --- a/src/pastie_response.cpp +++ b/src/pastie_response.cpp @@ -26,7 +26,7 @@ namespace tawashi { PastieResponse::PastieResponse (const Kakoune::SafePtr& parSettings) : Response(Response::ContentType, "text/html", "", parSettings, true), - m_langmap_dir(parSettings->as_str("langmap_dir")), + m_langmap_dir(parSettings->as("langmap_dir")), m_plain_text(false) { } diff --git a/src/response.cpp b/src/response.cpp index 3e3ce9a..f1d216c 100644 --- a/src/response.cpp +++ b/src/response.cpp @@ -59,12 +59,12 @@ namespace tawashi { if (parSettings["redis_mode"] == "inet") { return IncRedis( - parSettings.as_str("redis_server"), + parSettings.as("redis_server"), dhandy::lexical_cast(parSettings["redis_port"]) ); } else if (parSettings["redis_mode"] == "sock") { - return IncRedis(parSettings.as_str("redis_sock")); + return IncRedis(parSettings.as("redis_sock")); } else { throw std::runtime_error("Unknown setting for \"redis_mode\", valid settings are \"inet\" or \"sock\""); @@ -129,7 +129,7 @@ namespace tawashi { void Response::send() { mstch::map mustache_context { {"version", std::string{STRINGIZE(VERSION_MAJOR) "." STRINGIZE(VERSION_MINOR) "." STRINGIZE(VERSION_PATCH)}}, - {"base_uri", m_settings->as_str("base_uri")}, + {"base_uri", m_settings->as("base_uri")}, {"languages", make_mstch_langmap(*m_settings)} }; @@ -183,7 +183,7 @@ namespace tawashi { } const boost::string_ref& Response::base_uri() const { - return m_settings->as_ref("base_uri"); + return m_settings->at("base_uri"); } const std::string& Response::page_basename() const { diff --git a/src/settings_bag.cpp b/src/settings_bag.cpp index 33e1d83..d177a57 100644 --- a/src/settings_bag.cpp +++ b/src/settings_bag.cpp @@ -36,18 +36,16 @@ namespace tawashi { return m_defaults.at(parIndex); } - const boost::string_ref& SettingsBag::as_ref (boost::string_ref parIndex) const { - return (*this)[parIndex]; - } - - std::string SettingsBag::as_str (boost::string_ref parIndex) const { - auto& setting = (*this)[parIndex]; - return std::string(setting.data(), setting.size()); - } - void SettingsBag::add_default (boost::string_ref parKey, boost::string_ref parValue) { assert(m_defaults.find(parKey) == m_defaults.end()); m_defaults[parKey] = parValue; } -} //namespace tawashi + template <> + std::string SettingsBag::as (boost::string_ref parIndex) const { + auto& setting = (*this)[parIndex]; + return std::string(setting.data(), setting.size()); + } + + template std::string SettingsBag::as (boost::string_ref parIndex) const; +} //namespace tawashi diff --git a/src/settings_bag.hpp b/src/settings_bag.hpp index 6649d0d..c7f0e66 100644 --- a/src/settings_bag.hpp +++ b/src/settings_bag.hpp @@ -32,8 +32,8 @@ namespace tawashi { ~SettingsBag() noexcept; const boost::string_ref& operator[] (boost::string_ref parIndex) const; - const boost::string_ref& as_ref (boost::string_ref parIndex) const; - std::string as_str (boost::string_ref parIndex) const; + const boost::string_ref& at (boost::string_ref parIndex) const; + template T as (boost::string_ref parIndex) const; void add_default (boost::string_ref parKey, boost::string_ref parValue); private: @@ -41,4 +41,13 @@ namespace tawashi { Kakoune::SafePtr m_ini; const IniFile::KeyValueMapType* m_values; }; + + template <> + inline boost::string_ref SettingsBag::as (boost::string_ref parIndex) const { + return (*this)[parIndex]; + } + + inline const boost::string_ref& SettingsBag::at (boost::string_ref parIndex) const { + return (*this)[parIndex]; + } } //namespace tawashi