From d0c3e006fb71536595748190c002a214098305b0 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Thu, 26 Mar 2020 12:39:56 +0100 Subject: [PATCH] printing of std::map --- slsSupportLib/include/ToString.h | 30 ++++++++++++++++++++------- slsSupportLib/tests/test-ToString.cpp | 28 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/slsSupportLib/include/ToString.h b/slsSupportLib/include/ToString.h index 64e442e6d..4c6a40838 100644 --- a/slsSupportLib/include/ToString.h +++ b/slsSupportLib/include/ToString.h @@ -17,6 +17,7 @@ #include #include #include +#include namespace sls { @@ -393,6 +394,23 @@ 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) { double tval{0}; @@ -610,26 +628,22 @@ template <> inline defs::timingSourceType StringTo(const std::string &s) { throw sls::RuntimeError("Unknown timing source type " + s); } -template <> -inline uint32_t StringTo(const std::string &s) { +template <> inline uint32_t StringTo(const std::string &s) { int base = s.find("0x") != std::string::npos ? 16 : 10; return std::stoul(s, nullptr, base); } -template <> -inline uint64_t StringTo(const std::string &s) { +template <> inline uint64_t StringTo(const std::string &s) { int base = s.find("0x") != std::string::npos ? 16 : 10; return std::stoull(s, nullptr, base); } -template <> -inline int StringTo(const std::string &s) { +template <> inline int StringTo(const std::string &s) { int base = s.find("0x") != std::string::npos ? 16 : 10; return std::stoi(s, nullptr, base); } -template <> -inline int64_t StringTo(const std::string &s) { +template <> inline int64_t StringTo(const std::string &s) { int base = s.find("0x") != std::string::npos ? 16 : 10; return std::stol(s, nullptr, base); } diff --git a/slsSupportLib/tests/test-ToString.cpp b/slsSupportLib/tests/test-ToString.cpp index b469b2b2f..3034d0005 100644 --- a/slsSupportLib/tests/test-ToString.cpp +++ b/slsSupportLib/tests/test-ToString.cpp @@ -4,6 +4,7 @@ #include "sls_detector_defs.h" #include "catch.hpp" #include +#include #include // using namespace sls; @@ -194,3 +195,30 @@ TEST_CASE("int64_t from string"){ REQUIRE(StringTo("0xffffff") == 0xffffff); } + + +TEST_CASE("std::map of strings"){ + std::map m; + m["key"] = "value"; + auto s = ToString(m); + REQUIRE(s == "{key: value}"); + + m["chrusi"] = "musi"; + REQUIRE(ToString(m) == "{chrusi: musi, key: value}"); + + m["test"] = "tree"; + REQUIRE(ToString(m) == "{chrusi: musi, key: value, test: tree}"); + +} + +TEST_CASE("std::map of ints"){ + + std::map m; + m[5] = 10; + REQUIRE(ToString(m) == "{5: 10}"); + m[500] = 50; + REQUIRE(ToString(m) == "{5: 10, 500: 50}"); + m[372] = 999; + REQUIRE(ToString(m) == "{5: 10, 372: 999, 500: 50}"); + +} \ No newline at end of file