From f135a6519a0b4608dc0d01ccd0abe45934c57510 Mon Sep 17 00:00:00 2001 From: Jan Olszak Date: Tue, 19 Aug 2014 12:54:43 +0200 Subject: [PATCH] Function for creating std::string keys [Bug/Feature] Function creating keys from other serializable types [Cause] N/A [Solution] N/A [Verification] Build, install, run tests Change-Id: I4581b650b85ce2d2bba35daff66dca9d5ec0d936 --- src/config/kvstore.hpp | 75 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 19 deletions(-) diff --git a/src/config/kvstore.hpp b/src/config/kvstore.hpp index 6b75c91..b01c5da 100644 --- a/src/config/kvstore.hpp +++ b/src/config/kvstore.hpp @@ -137,12 +137,30 @@ private: }; +namespace { template -void KVStore::setInternal(const std::string& key, const T& value) +std::string toString(const T& value) { std::ostringstream oss; oss << value; - setInternal(key, oss.str()); + return oss.str(); +} + +template +T fromString(const std::string& strValue) +{ + std::istringstream iss(strValue); + T value; + iss >> value; + return value; +} + +} // namespace + +template +void KVStore::setInternal(const std::string& key, const T& value) +{ + setInternal(key, toString(value)); } template @@ -150,15 +168,10 @@ void KVStore::setInternal(const std::string& key, const std::vector& values) { std::vector strValues(values.size()); - auto toString = [](const T & value) -> std::string { - std::ostringstream oss; - oss << value; - return oss.str(); - }; std::transform(values.begin(), values.end(), strValues.begin(), - toString); + toString); setInternal(key, strValues); } @@ -166,10 +179,7 @@ void KVStore::setInternal(const std::string& key, const std::vector& values) template T KVStore::getInternal(const std::string& key, T*) { - std::istringstream ss(getInternal(key, static_cast(nullptr))); - T ret; - ss >> ret; - return ret; + return fromString(getInternal(key, static_cast(nullptr))); } template @@ -178,19 +188,46 @@ std::vector KVStore::getInternal(const std::string& key, std::vector*) std::vector strValues = getInternal(key, static_cast*>(nullptr)); std::vector values(strValues.size()); - auto parse = [](const std::string & strValue) -> T { - std::istringstream iss(strValue); - T value; - iss >> value; - return value; - }; std::transform(strValues.begin(), strValues.end(), values.begin(), - parse); + fromString); return values; } + +/** + * Concatenates all parameters into one std::string. + * Uses '.' to connect the terms. + * @param args components of the string + * @tparam delim optional delimiter + * @tparam typename ... Args any type implementing str + * @return string created from he args + */ +template +std::string key(const Arg1& a1, const Args& ... args) +{ + std::string ret = toString(a1); + std::initializer_list strings {toString(args)...}; + for (const std::string& s : strings) { + ret += delim + s; + } + + return ret; +} + +/** + * Function added for key function completeness. + * + * @tparam delim = '.' parameter not used, added for consistency + * @return empty string + */ +template +std::string key() +{ + return std::string(); +} + } // namespace config #endif // COMMON_CONFIG_KVSTORE_HPP -- 2.7.4