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

Add as_str() and as_ref() convenience methods to SettingsBag.

as_ref() does the same as operator[], but it's nicer to
type when settings bag is a pointer.
This commit is contained in:
King_DuckZ 2017-04-23 13:54:06 +01:00
parent 75674525de
commit fb4d99d62b
3 changed files with 15 additions and 9 deletions

View file

@ -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<uint16_t>(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 {

View file

@ -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;

View file

@ -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<IniFile> 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