diff --git a/src/response.cpp b/src/response.cpp index cd1b1f4..5bcfb5f 100644 --- a/src/response.cpp +++ b/src/response.cpp @@ -59,12 +59,12 @@ namespace tawashi { if (parSettings["redis_mode"] == "inet") { return IncRedis( - std::string(parSettings["redis_server"]), + parSettings.as_str("redis_server"), dhandy::lexical_cast(parSettings["redis_port"]) ); } else if (parSettings["redis_mode"] == "sock") { - return IncRedis(std::string(parSettings["redis_sock"])); + return IncRedis(parSettings.as_str("redis_sock")); } else { throw std::runtime_error("Unknown setting for \"redis_mode\", valid settings are \"inet\" or \"sock\""); @@ -127,10 +127,9 @@ namespace tawashi { } void Response::send() { - auto& base_uri = this->base_uri(); mstch::map mustache_context { {"version", std::string{STRINGIZE(VERSION_MAJOR) "." STRINGIZE(VERSION_MINOR) "." STRINGIZE(VERSION_PATCH)}}, - {"base_uri", std::string(base_uri.data(), base_uri.size())}, + {"base_uri", m_settings->as_str("base_uri")}, {"languages", make_mstch_langmap()} }; @@ -184,7 +183,7 @@ namespace tawashi { } const boost::string_ref& Response::base_uri() const { - return (*m_settings)["base_uri"]; + return m_settings->as_ref("base_uri"); } const std::string& Response::page_basename() const { diff --git a/src/settings_bag.cpp b/src/settings_bag.cpp index 697f5b2..33e1d83 100644 --- a/src/settings_bag.cpp +++ b/src/settings_bag.cpp @@ -36,6 +36,15 @@ 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; diff --git a/src/settings_bag.hpp b/src/settings_bag.hpp index 9bd5f3b..6649d0d 100644 --- a/src/settings_bag.hpp +++ b/src/settings_bag.hpp @@ -32,6 +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; void add_default (boost::string_ref parKey, boost::string_ref parValue); private: @@ -39,8 +41,4 @@ namespace tawashi { Kakoune::SafePtr m_ini; const IniFile::KeyValueMapType* m_values; }; - - inline std::string string_ref_to_string (const boost::string_ref& parIn) { - return std::string(parIn.data(), parIn.size()); - } } //namespace tawashi