diff --git a/slsDetectorSoftware/tests/test-Result.cpp b/slsDetectorSoftware/tests/test-Result.cpp index 6cc668a16..5fd666d82 100644 --- a/slsDetectorSoftware/tests/test-Result.cpp +++ b/slsDetectorSoftware/tests/test-Result.cpp @@ -2,6 +2,7 @@ #include "TypeTraits.h" #include "catch.hpp" #include +#include "ToString.h" using sls::Result; @@ -172,3 +173,26 @@ TEST_CASE("Printing Result"){ REQUIRE(os.str() == "[1, 2, 3]"); } + +TEST_CASE("String conversions"){ + Result res{1,2,3}; + REQUIRE(ToString(res) == "[1, 2, 3]"); + + Result res2{"one", "two", "three"}; + REQUIRE(ToString(res2) == "[one, two, three]"); + + using Smap = std::map; + Smap m; + m["one"] = "1"; + Result 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 res4{m, m2, m}; + REQUIRE(ToString(res4) == "[{one: 1}, {one: 1, three: 3, two: 2}, {one: 1}]"); +} diff --git a/slsSupportLib/include/ToString.h b/slsSupportLib/include/ToString.h index 4c6a40838..e2ef82421 100644 --- a/slsSupportLib/include/ToString.h +++ b/slsSupportLib/include/ToString.h @@ -248,6 +248,8 @@ inline std::string ToString(const defs::timingSourceType s) { // causes a copy but might be needed in generic code 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 */ template typename std::enable_if::value, std::string>::type @@ -333,6 +335,23 @@ ToStringHex(const T &container) { return os.str(); } +template +std::string ToString(const std::map& 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 * Container is excluded @@ -378,6 +397,8 @@ ToString(const T &vec) { return os.str(); } + + /** Container and specified unit, call ToString(value, unit) */ template typename std::enable_if::value, std::string>::type @@ -394,22 +415,7 @@ ToString(const T &container, const std::string &unit) { return os.str(); } -template -std::string ToString(std::map 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 T StringTo(const std::string &t, const std::string &unit) {