ToString of Result<map>

This commit is contained in:
Erik Frojdh 2020-03-26 14:10:06 +01:00
parent d0c3e006fb
commit 4d8a63eee1
2 changed files with 46 additions and 16 deletions

View File

@ -2,6 +2,7 @@
#include "TypeTraits.h" #include "TypeTraits.h"
#include "catch.hpp" #include "catch.hpp"
#include <string> #include <string>
#include "ToString.h"
using sls::Result; using sls::Result;
@ -172,3 +173,26 @@ TEST_CASE("Printing Result<int>"){
REQUIRE(os.str() == "[1, 2, 3]"); REQUIRE(os.str() == "[1, 2, 3]");
} }
TEST_CASE("String conversions"){
Result<int> res{1,2,3};
REQUIRE(ToString(res) == "[1, 2, 3]");
Result<std::string> res2{"one", "two", "three"};
REQUIRE(ToString(res2) == "[one, two, three]");
using Smap = std::map<std::string, std::string>;
Smap m;
m["one"] = "1";
Result<Smap> res3{m, m, m};
REQUIRE(res3.size()== 3);
REQUIRE(ToString(res3) == "[{one: 1}, {one: 1}, {one: 1}]");
Smap m2;
m2["one"] = "1";
m2["two"] = "2";
m2["three"] = "3";
Result<Smap> res4{m, m2, m};
REQUIRE(ToString(res4) == "[{one: 1}, {one: 1, three: 3, two: 2}, {one: 1}]");
}

View File

@ -248,6 +248,8 @@ inline std::string ToString(const defs::timingSourceType s) {
// causes a copy but might be needed in generic code // causes a copy but might be needed in generic code
inline std::string ToString(const std::string &s) { return s; } inline std::string ToString(const std::string &s) { return s; }
// inline const std::string& ToString(const std::string &s) { return s; }
/** Convert std::chrono::duration with specified output unit */ /** Convert std::chrono::duration with specified output unit */
template <typename T, typename Rep = double> template <typename T, typename Rep = double>
typename std::enable_if<is_duration<T>::value, std::string>::type typename std::enable_if<is_duration<T>::value, std::string>::type
@ -333,6 +335,23 @@ ToStringHex(const T &container) {
return os.str(); return os.str();
} }
template <typename KeyType, typename ValueType>
std::string ToString(const std::map<KeyType, ValueType>& m) {
std::ostringstream os;
os << '{';
if (!m.empty()) {
auto it = m.cbegin();
os << ToString(it->first) << ": " << ToString(it->second);
it++;
while (it != m.cend()) {
os << ", "<< ToString(it->first) << ": " << ToString(it->second);
it++;
}
}
os << '}';
return os.str();
}
/** /**
* For a container loop over all elements and call ToString on the element * For a container loop over all elements and call ToString on the element
* Container<std::string> is excluded * Container<std::string> is excluded
@ -378,6 +397,8 @@ ToString(const T &vec) {
return os.str(); return os.str();
} }
/** Container and specified unit, call ToString(value, unit) */ /** Container and specified unit, call ToString(value, unit) */
template <typename T> template <typename T>
typename std::enable_if<is_container<T>::value, std::string>::type typename std::enable_if<is_container<T>::value, std::string>::type
@ -394,22 +415,7 @@ ToString(const T &container, const std::string &unit) {
return os.str(); return os.str();
} }
template <typename KeyType, typename ValueType>
std::string ToString(std::map<KeyType, ValueType> m) {
std::ostringstream os;
os << '{';
if (!m.empty()) {
auto it = m.cbegin();
os << ToString(it->first) << ": " << ToString(it->second);
it++;
while (it != m.cend()) {
os << ", "<< ToString(it->first) << ": " << ToString(it->second);
it++;
}
}
os << '}';
return os.str();
}
template <typename T> template <typename T>
T StringTo(const std::string &t, const std::string &unit) { T StringTo(const std::string &t, const std::string &unit) {