From eaf824ca73241014a280b1bd787d8f83e772dd05 Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Sun, 12 Aug 2018 10:02:32 +0200 Subject: Use emplace result for get,set of settings --- common/settings.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'common/settings.h') diff --git a/common/settings.h b/common/settings.h index 8ced6560..e1f1166a 100644 --- a/common/settings.h +++ b/common/settings.h @@ -34,10 +34,11 @@ class Settings { try { IdString id = ctx->id(name); - if (ctx->settings.find(id) != ctx->settings.end()) { - return boost::lexical_cast(ctx->settings[id]); + auto pair = ctx->settings.emplace(id, std::to_string(defaultValue)); + if (!pair.second) { + return boost::lexical_cast(pair.first->second); } - ctx->settings.emplace(id, std::to_string(defaultValue)); + } catch (boost::bad_lexical_cast &) { log_error("Problem reading setting %s, using default value\n", name); } @@ -47,11 +48,10 @@ class Settings template void set(const char *name, T value) { IdString id = ctx->id(name); - if (ctx->settings.find(id) != ctx->settings.end()) { - ctx->settings[id] = value; - return; - } - ctx->settings.emplace(id, std::to_string(value)); + auto pair = ctx->settings.emplace(id, std::to_string(value)); + if (!pair.second) { + ctx->settings[pair.first->first] = value; + } } private: -- cgit v1.2.3