string conversion for uint

This commit is contained in:
Erik Frojdh
2020-03-19 10:30:05 +01:00
parent 4813b5567a
commit bf1b1a2d9e
2 changed files with 104 additions and 88 deletions

View File

@ -18,7 +18,6 @@
#include <type_traits>
#include <vector>
namespace sls {
using defs = slsDetectorDefs;
@ -179,7 +178,7 @@ inline std::string ToString(const defs::externalSignalFlag s) {
}
}
inline std::string ToString(const defs::readoutMode s){
inline std::string ToString(const defs::readoutMode s) {
switch (s) {
case defs::ANALOG_ONLY:
return std::string("analog");
@ -233,7 +232,6 @@ inline std::string ToString(const defs::burstMode s) {
}
}
inline std::string ToString(const defs::timingSourceType s) {
switch (s) {
case defs::TIMING_INTERNAL:
@ -247,9 +245,7 @@ inline std::string ToString(const defs::timingSourceType s) {
// in case we already have a string
// causes a copy but might be needed in generic code
inline std::string ToString(const std::string& s) {
return s;
}
inline std::string ToString(const std::string &s) { return s; }
/** Convert std::chrono::duration with specified output unit */
template <typename T, typename Rep = double>
@ -422,14 +418,13 @@ T StringTo(const std::string &t, const std::string &unit) {
}
}
template <typename T> T StringTo(const std::string& t) {
template <typename T> T StringTo(const std::string &t) {
std::string tmp{t};
auto unit = RemoveUnit(tmp);
return StringTo<T>(tmp, unit);
}
template <>
inline defs::detectorType StringTo(const std::string& s) {
template <> inline defs::detectorType StringTo(const std::string &s) {
if (s == "Eiger")
return defs::EIGER;
if (s == "Gotthard")
@ -447,8 +442,7 @@ inline defs::detectorType StringTo(const std::string& s) {
throw sls::RuntimeError("Unknown detector type " + s);
}
template <>
inline defs::detectorSettings StringTo(const std::string& s) {
template <> inline defs::detectorSettings StringTo(const std::string &s) {
if (s == "standard")
return defs::STANDARD;
if (s == "fast")
@ -494,8 +488,7 @@ inline defs::detectorSettings StringTo(const std::string& s) {
throw sls::RuntimeError("Unknown setting " + s);
}
template <>
inline defs::speedLevel StringTo(const std::string& s) {
template <> inline defs::speedLevel StringTo(const std::string &s) {
if (s == "full_speed")
return defs::FULL_SPEED;
if (s == "half_speed")
@ -505,8 +498,7 @@ inline defs::speedLevel StringTo(const std::string& s) {
throw sls::RuntimeError("Unknown speed " + s);
}
template <>
inline defs::timingMode StringTo(const std::string& s) {
template <> inline defs::timingMode StringTo(const std::string &s) {
if (s == "auto")
return defs::AUTO_TIMING;
if (s == "trigger")
@ -518,8 +510,7 @@ inline defs::timingMode StringTo(const std::string& s) {
throw sls::RuntimeError("Unknown timing mode " + s);
}
template <>
inline defs::frameDiscardPolicy StringTo(const std::string& s) {
template <> inline defs::frameDiscardPolicy StringTo(const std::string &s) {
if (s == "nodiscard")
return defs::NO_DISCARD;
if (s == "discardempty")
@ -529,8 +520,7 @@ inline defs::frameDiscardPolicy StringTo(const std::string& s) {
throw sls::RuntimeError("Unknown frame discard policy " + s);
}
template <>
inline defs::fileFormat StringTo(const std::string& s) {
template <> inline defs::fileFormat StringTo(const std::string &s) {
if (s == "hdf5")
return defs::HDF5;
if (s == "binary")
@ -538,8 +528,7 @@ inline defs::fileFormat StringTo(const std::string& s) {
throw sls::RuntimeError("Unknown file format " + s);
}
template <>
inline defs::externalSignalFlag StringTo(const std::string& s) {
template <> inline defs::externalSignalFlag StringTo(const std::string &s) {
if (s == "trigger_in_rising_edge")
return defs::TRIGGER_IN_RISING_EDGE;
if (s == "trigger_in_falling_edge")
@ -547,8 +536,7 @@ inline defs::externalSignalFlag StringTo(const std::string& s) {
throw sls::RuntimeError("Unknown external signal flag " + s);
}
template <>
inline defs::readoutMode StringTo(const std::string& s) {
template <> inline defs::readoutMode StringTo(const std::string &s) {
if (s == "analog")
return defs::ANALOG_ONLY;
if (s == "digital")
@ -558,8 +546,7 @@ inline defs::readoutMode StringTo(const std::string& s) {
throw sls::RuntimeError("Unknown readout mode " + s);
}
template <>
inline defs::frameModeType StringTo(const std::string& s) {
template <> inline defs::frameModeType StringTo(const std::string &s) {
if (s == "pedestal")
return defs::PEDESTAL;
if (s == "newpedestal")
@ -571,8 +558,7 @@ inline defs::frameModeType StringTo(const std::string& s) {
throw sls::RuntimeError("Unknown frame mode " + s);
}
template <>
inline defs::detectorModeType StringTo(const std::string& s) {
template <> inline defs::detectorModeType StringTo(const std::string &s) {
if (s == "counting")
return defs::COUNTING;
if (s == "interpolating")
@ -582,8 +568,7 @@ inline defs::detectorModeType StringTo(const std::string& s) {
throw sls::RuntimeError("Unknown detector mode " + s);
}
template <>
inline defs::dacIndex StringTo(const std::string& s) {
template <> inline defs::dacIndex StringTo(const std::string &s) {
if (s == "vcmp_ll")
return defs::VCMP_LL;
if (s == "vcmp_lr")
@ -607,8 +592,7 @@ inline defs::dacIndex StringTo(const std::string& s) {
throw sls::RuntimeError("Unknown dac Index " + s);
}
template <>
inline defs::burstMode StringTo(const std::string& s) {
template <> inline defs::burstMode StringTo(const std::string &s) {
if (s == "off")
return defs::BURST_OFF;
if (s == "internal")
@ -618,8 +602,7 @@ inline defs::burstMode StringTo(const std::string& s) {
throw sls::RuntimeError("Unknown burst mode " + s);
}
template <>
inline defs::timingSourceType StringTo(const std::string& s) {
template <> inline defs::timingSourceType StringTo(const std::string &s) {
if (s == "internal")
return defs::TIMING_INTERNAL;
if (s == "external")
@ -627,6 +610,17 @@ 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) {
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) {
int base = s.find("0x") != std::string::npos ? 16 : 10;
return std::stoull(s, nullptr, base);
}
/** For types with a .str() method use this for conversion */
template <typename T>
@ -635,6 +629,4 @@ ToString(const T &obj) {
return obj.str();
}
} // namespace sls

View File

@ -141,3 +141,27 @@ TEST_CASE("vec"){
REQUIRE(ToString(vec) == "[error, idle]");
}
TEST_CASE("uint32 from string"){
REQUIRE(StringTo<uint32_t>("0") == 0);
REQUIRE(StringTo<uint32_t>("5") == 5u);
REQUIRE(StringTo<uint32_t>("16") == 16u);
REQUIRE(StringTo<uint32_t>("20") == 20u);
REQUIRE(StringTo<uint32_t>("0x14") == 20u);
REQUIRE(StringTo<uint32_t>("0x15") == 21u);
REQUIRE(StringTo<uint32_t>("0x15") == 0x15);
REQUIRE(StringTo<uint32_t>("0xffffff") == 0xffffff);
}
TEST_CASE("uint64 from string"){
REQUIRE(StringTo<uint64_t>("0") == 0);
REQUIRE(StringTo<uint64_t>("5") == 5u);
REQUIRE(StringTo<uint64_t>("16") == 16u);
REQUIRE(StringTo<uint64_t>("20") == 20u);
REQUIRE(StringTo<uint64_t>("0x14") == 20u);
REQUIRE(StringTo<uint64_t>("0x15") == 21u);
REQUIRE(StringTo<uint64_t>("0xffffff") == 0xffffff);
}