mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-05-06 12:50:02 +02:00
printing of std::map
This commit is contained in:
parent
f22f23849d
commit
d0c3e006fb
@ -17,6 +17,7 @@
|
||||
#include <sstream>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
namespace sls {
|
||||
|
||||
@ -393,6 +394,23 @@ ToString(const T &container, const std::string &unit) {
|
||||
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>
|
||||
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);
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include "sls_detector_defs.h"
|
||||
#include "catch.hpp"
|
||||
#include <array>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
// using namespace sls;
|
||||
@ -194,3 +195,30 @@ TEST_CASE("int64_t from string"){
|
||||
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}");
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user