printing of std::map

This commit is contained in:
Erik Frojdh 2020-03-26 12:39:56 +01:00
parent f22f23849d
commit d0c3e006fb
2 changed files with 50 additions and 8 deletions

View File

@ -17,6 +17,7 @@
#include <sstream> #include <sstream>
#include <type_traits> #include <type_traits>
#include <vector> #include <vector>
#include <map>
namespace sls { namespace sls {
@ -393,6 +394,23 @@ 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) {
double tval{0}; double tval{0};
@ -610,26 +628,22 @@ template <> inline defs::timingSourceType StringTo(const std::string &s) {
throw sls::RuntimeError("Unknown timing source type " + s); throw sls::RuntimeError("Unknown timing source type " + s);
} }
template <> template <> inline uint32_t StringTo(const std::string &s) {
inline uint32_t StringTo(const std::string &s) {
int base = s.find("0x") != std::string::npos ? 16 : 10; int base = s.find("0x") != std::string::npos ? 16 : 10;
return std::stoul(s, nullptr, base); return std::stoul(s, nullptr, base);
} }
template <> template <> inline uint64_t StringTo(const std::string &s) {
inline uint64_t StringTo(const std::string &s) {
int base = s.find("0x") != std::string::npos ? 16 : 10; int base = s.find("0x") != std::string::npos ? 16 : 10;
return std::stoull(s, nullptr, base); return std::stoull(s, nullptr, base);
} }
template <> template <> inline int StringTo(const std::string &s) {
inline int StringTo(const std::string &s) {
int base = s.find("0x") != std::string::npos ? 16 : 10; int base = s.find("0x") != std::string::npos ? 16 : 10;
return std::stoi(s, nullptr, base); return std::stoi(s, nullptr, base);
} }
template <> template <> inline int64_t StringTo(const std::string &s) {
inline int64_t StringTo(const std::string &s) {
int base = s.find("0x") != std::string::npos ? 16 : 10; int base = s.find("0x") != std::string::npos ? 16 : 10;
return std::stol(s, nullptr, base); return std::stol(s, nullptr, base);
} }

View File

@ -4,6 +4,7 @@
#include "sls_detector_defs.h" #include "sls_detector_defs.h"
#include "catch.hpp" #include "catch.hpp"
#include <array> #include <array>
#include <map>
#include <vector> #include <vector>
// using namespace sls; // using namespace sls;
@ -194,3 +195,30 @@ TEST_CASE("int64_t from string"){
REQUIRE(StringTo<int64_t>("0xffffff") == 0xffffff); REQUIRE(StringTo<int64_t>("0xffffff") == 0xffffff);
} }
TEST_CASE("std::map of strings"){
std::map<std::string, std::string> 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<int, int> 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}");
}