1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2024-11-27 00:43:47 +00:00

Implement as_str() and as_ref() as template as<>().

Also provide a commodity at() that is a synonym
for as<boost::string_ref>() but much shorter to type.
This commit is contained in:
King_DuckZ 2017-04-24 09:25:04 +01:00
parent 34e69d0b52
commit fe53ea7ea1
5 changed files with 25 additions and 18 deletions

View file

@ -24,7 +24,7 @@
namespace tawashi { namespace tawashi {
HighlightLangList list_highlight_langs (const SettingsBag& parSettings) { HighlightLangList list_highlight_langs (const SettingsBag& parSettings) {
srchilite::LangMap lang_map(parSettings.as_str("langmap_dir"), "lang.map"); srchilite::LangMap lang_map(parSettings.as<std::string>("langmap_dir"), "lang.map");
lang_map.open(); lang_map.open();
const auto lang_range = boost::make_iterator_range(lang_map.begin(), lang_map.end()); const auto lang_range = boost::make_iterator_range(lang_map.begin(), lang_map.end());

View file

@ -26,7 +26,7 @@
namespace tawashi { namespace tawashi {
PastieResponse::PastieResponse (const Kakoune::SafePtr<SettingsBag>& parSettings) : PastieResponse::PastieResponse (const Kakoune::SafePtr<SettingsBag>& parSettings) :
Response(Response::ContentType, "text/html", "", parSettings, true), Response(Response::ContentType, "text/html", "", parSettings, true),
m_langmap_dir(parSettings->as_str("langmap_dir")), m_langmap_dir(parSettings->as<std::string>("langmap_dir")),
m_plain_text(false) m_plain_text(false)
{ {
} }

View file

@ -59,12 +59,12 @@ namespace tawashi {
if (parSettings["redis_mode"] == "inet") { if (parSettings["redis_mode"] == "inet") {
return IncRedis( return IncRedis(
parSettings.as_str("redis_server"), parSettings.as<std::string>("redis_server"),
dhandy::lexical_cast<uint16_t>(parSettings["redis_port"]) dhandy::lexical_cast<uint16_t>(parSettings["redis_port"])
); );
} }
else if (parSettings["redis_mode"] == "sock") { else if (parSettings["redis_mode"] == "sock") {
return IncRedis(parSettings.as_str("redis_sock")); return IncRedis(parSettings.as<std::string>("redis_sock"));
} }
else { else {
throw std::runtime_error("Unknown setting for \"redis_mode\", valid settings are \"inet\" or \"sock\""); throw std::runtime_error("Unknown setting for \"redis_mode\", valid settings are \"inet\" or \"sock\"");
@ -129,7 +129,7 @@ namespace tawashi {
void Response::send() { void Response::send() {
mstch::map mustache_context { mstch::map mustache_context {
{"version", std::string{STRINGIZE(VERSION_MAJOR) "." STRINGIZE(VERSION_MINOR) "." STRINGIZE(VERSION_PATCH)}}, {"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<std::string>("base_uri")},
{"languages", make_mstch_langmap(*m_settings)} {"languages", make_mstch_langmap(*m_settings)}
}; };
@ -183,7 +183,7 @@ namespace tawashi {
} }
const boost::string_ref& Response::base_uri() const { 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 { const std::string& Response::page_basename() const {

View file

@ -36,18 +36,16 @@ namespace tawashi {
return m_defaults.at(parIndex); 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) { void SettingsBag::add_default (boost::string_ref parKey, boost::string_ref parValue) {
assert(m_defaults.find(parKey) == m_defaults.end()); assert(m_defaults.find(parKey) == m_defaults.end());
m_defaults[parKey] = parValue; 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<std::string> (boost::string_ref parIndex) const;
} //namespace tawashi

View file

@ -32,8 +32,8 @@ namespace tawashi {
~SettingsBag() noexcept; ~SettingsBag() noexcept;
const boost::string_ref& operator[] (boost::string_ref parIndex) const; const boost::string_ref& operator[] (boost::string_ref parIndex) const;
const boost::string_ref& as_ref (boost::string_ref parIndex) const; const boost::string_ref& at (boost::string_ref parIndex) const;
std::string as_str (boost::string_ref parIndex) const; template <typename T> T as (boost::string_ref parIndex) const;
void add_default (boost::string_ref parKey, boost::string_ref parValue); void add_default (boost::string_ref parKey, boost::string_ref parValue);
private: private:
@ -41,4 +41,13 @@ namespace tawashi {
Kakoune::SafePtr<IniFile> m_ini; Kakoune::SafePtr<IniFile> m_ini;
const IniFile::KeyValueMapType* m_values; 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 } //namespace tawashi