mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-04-20 02:40:03 +02:00
ToString of Result<map>
This commit is contained in:
parent
d0c3e006fb
commit
4d8a63eee1
@ -2,6 +2,7 @@
|
||||
#include "TypeTraits.h"
|
||||
#include "catch.hpp"
|
||||
#include <string>
|
||||
#include "ToString.h"
|
||||
|
||||
using sls::Result;
|
||||
|
||||
@ -172,3 +173,26 @@ TEST_CASE("Printing Result<int>"){
|
||||
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}]");
|
||||
}
|
||||
|
@ -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 T, typename Rep = double>
|
||||
typename std::enable_if<is_duration<T>::value, std::string>::type
|
||||
@ -333,6 +335,23 @@ ToStringHex(const T &container) {
|
||||
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
|
||||
* Container<std::string> is excluded
|
||||
@ -378,6 +397,8 @@ ToString(const T &vec) {
|
||||
return os.str();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Container and specified unit, call ToString(value, unit) */
|
||||
template <typename T>
|
||||
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();
|
||||
}
|
||||
|
||||
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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user