From 46e9b450c0a4151cb4a4c5ac1369cc89ca36dce0 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Thu, 19 Mar 2020 11:09:20 +0100 Subject: [PATCH] assume dec unless 0x --- slsDetectorSoftware/src/CmdProxy.cpp | 24 ++++++------- slsDetectorSoftware/src/CmdProxy.h | 50 +++++++++++++-------------- slsSupportLib/include/ToString.h | 6 ++++ slsSupportLib/tests/test-ToString.cpp | 14 ++++++++ 4 files changed, 57 insertions(+), 37 deletions(-) diff --git a/slsDetectorSoftware/src/CmdProxy.cpp b/slsDetectorSoftware/src/CmdProxy.cpp index d9a607edf..6d05572c9 100644 --- a/slsDetectorSoftware/src/CmdProxy.cpp +++ b/slsDetectorSoftware/src/CmdProxy.cpp @@ -1345,7 +1345,7 @@ std::string CmdProxy::VetoReference(int action) { if (args.size() != 2) { WrongNumberOfParameters(2); } - det->setVetoReference(std::stoi(args[0]), stoiHex(args[1]), {det_id}); + det->setVetoReference(StringTo(args[0]), StringTo(args[1]), {det_id}); os << sls::ToString(args) << '\n'; } else { throw sls::RuntimeError("Unknown action"); @@ -1577,7 +1577,7 @@ std::string CmdProxy::DigitalIODelay(int action) { if (args.size() != 2) { WrongNumberOfParameters(2); } - det->setDigitalIODelay(stoulHex(args[0]), std::stoi(args[1]), {det_id}); + det->setDigitalIODelay(StringTo(args[0]), StringTo(args[1]), {det_id}); os << sls::ToString(args) << '\n'; } else { throw sls::RuntimeError("Unknown action"); @@ -1619,13 +1619,13 @@ std::string CmdProxy::PatternWord(int action) { if (args.size() != 1) { WrongNumberOfParameters(1); } - auto t = det->getPatternWord(stoiHex(args[0]), {det_id}); + auto t = det->getPatternWord(StringTo(args[0]), {det_id}); os << OutStringHex(t) << '\n'; } else if (action == defs::PUT_ACTION) { if (args.size() != 2) { WrongNumberOfParameters(2); } - det->setPatternWord(stoiHex(args[0]), stoulHex(args[1]), {det_id}); + det->setPatternWord(StringTo(args[0]), StringTo(args[1]), {det_id}); os << sls::ToString(args) << '\n'; } else { throw sls::RuntimeError("Unknown action"); @@ -1678,8 +1678,8 @@ std::string CmdProxy::PatternLoopAddresses(int action) { if (args.size() != 2) { WrongNumberOfParameters(2); } - det->setPatternLoopAddresses(level, stoiHex(args[0]), - stoiHex(args[1]), {det_id}); + det->setPatternLoopAddresses(level, StringTo(args[0]), + StringTo(args[1]), {det_id}); os << sls::ToString(args) << '\n'; } else { throw sls::RuntimeError("Unknown action"); @@ -1769,7 +1769,7 @@ std::string CmdProxy::PatternWaitAddress(int action) { if (args.size() != 1) { WrongNumberOfParameters(1); } - det->setPatternWaitAddr(level, stoiHex(args[0]), {det_id}); + det->setPatternWaitAddr(level, StringTo(args[0]), {det_id}); os << args.front() << '\n'; } else { throw sls::RuntimeError("Unknown action"); @@ -1978,13 +1978,13 @@ std::string CmdProxy::Register(int action) { if (args.size() != 1) { WrongNumberOfParameters(1); } - auto t = det->readRegister(stoiHex(args[0]), {det_id}); + auto t = det->readRegister(StringTo(args[0]), {det_id}); os << OutStringHex(t) << '\n'; } else if (action == defs::PUT_ACTION) { if (args.size() != 2) { WrongNumberOfParameters(2); } - det->writeRegister(stoiHex(args[0]), stoiHex(args[1]), {det_id}); + det->writeRegister(StringTo(args[0]), StringTo(args[1]), {det_id}); os << sls::ToString(args) << '\n'; } else { throw sls::RuntimeError("Unknown action"); @@ -2005,7 +2005,7 @@ std::string CmdProxy::AdcRegister(int action) { if (args.size() != 2) { WrongNumberOfParameters(2); } - det->writeAdcRegister(stoiHex(args[0]), stoiHex(args[1]), {det_id}); + det->writeAdcRegister(StringTo(args[0]), StringTo(args[1]), {det_id}); os << sls::ToString(args) << '\n'; } else { throw sls::RuntimeError("Unknown action"); @@ -2041,8 +2041,8 @@ std::string CmdProxy::BitOperations(int action) { if (args.size() != 2) { WrongNumberOfParameters(2); } - uint32_t addr = stoiHex(args[0]); - int bitnr = std::stoi(args[1]); + auto addr = StringTo(args[0]); + auto bitnr = StringTo(args[1]); if (bitnr < 0 || bitnr > 31) { return std::string("Bit number out of range") + std::to_string(bitnr); diff --git a/slsDetectorSoftware/src/CmdProxy.h b/slsDetectorSoftware/src/CmdProxy.h index ca4c50a31..faea5ab30 100644 --- a/slsDetectorSoftware/src/CmdProxy.h +++ b/slsDetectorSoftware/src/CmdProxy.h @@ -448,19 +448,19 @@ class CmdProxy { return ToString(value, unit); } - inline unsigned int stoiHex(const std::string& s) { - unsigned long lresult = stoul(s, nullptr, 16); - unsigned int result = lresult; - if (result != lresult) { - throw std::out_of_range("cannot convert to unsigned int"); - } - return result; - } + // inline unsigned int stoiHex(const std::string& s) { + // unsigned long lresult = stoul(s, nullptr, 16); + // unsigned int result = lresult; + // if (result != lresult) { + // throw std::out_of_range("cannot convert to unsigned int"); + // } + // return result; + // } - inline unsigned long int stoulHex(const std::string& s) { - unsigned long result = stoul(s, nullptr, 16); - return result; - } + // inline unsigned long int stoulHex(const std::string& s) { + // unsigned long result = stoul(s, nullptr, 16); + // return result; + // } using FunctionMap = std::map; using StringMap = std::map; @@ -1325,22 +1325,22 @@ class CmdProxy { /* on chip dacs */ - INTEGER_USER_IND_COMMAND(vchip_comp_fe, getOnChipDAC, setOnChipDAC, stoiHex, defs::VB_COMP_FE, + INTEGER_USER_IND_COMMAND(vchip_comp_fe, getOnChipDAC, setOnChipDAC, StringTo, defs::VB_COMP_FE, "[chip index 0-10, -1 for all][10 bit hex value] \n\t[Gotthard2] On chip Dac for comparator current of analogue front end."); - INTEGER_USER_IND_COMMAND(vchip_opa_1st, getOnChipDAC, setOnChipDAC, stoiHex, defs::VB_OPA_1ST, + INTEGER_USER_IND_COMMAND(vchip_opa_1st, getOnChipDAC, setOnChipDAC, StringTo, defs::VB_OPA_1ST, "[chip index 0-10, -1 for all][10 bit hex value] \n\t[Gotthard2] On chip Dac for opa current for driving the other DACs in chip."); - INTEGER_USER_IND_COMMAND(vchip_opa_fd, getOnChipDAC, setOnChipDAC, stoiHex, defs::VB_OPA_FD, + INTEGER_USER_IND_COMMAND(vchip_opa_fd, getOnChipDAC, setOnChipDAC, StringTo, defs::VB_OPA_FD, "[chip index 0-10, -1 for all][10 bit hex value] \n\t[Gotthard2] On chip Dac current for CDS opa stage."); - INTEGER_USER_IND_COMMAND(vchip_comp_adc, getOnChipDAC, setOnChipDAC, stoiHex, defs::VB_COMP_ADC, + INTEGER_USER_IND_COMMAND(vchip_comp_adc, getOnChipDAC, setOnChipDAC, StringTo, defs::VB_COMP_ADC, "[chip index 0-10, -1 for all][10 bit hex value] \n\t[Gotthard2] On chip Dac for comparator current of ADC."); - INTEGER_USER_IND_COMMAND(vchip_ref_comp_fe, getOnChipDAC, setOnChipDAC, stoiHex, defs::VREF_COMP_FE, + INTEGER_USER_IND_COMMAND(vchip_ref_comp_fe, getOnChipDAC, setOnChipDAC, StringTo, defs::VREF_COMP_FE, "[chip index 0-10, -1 for all][10 bit hex value] \n\t[Gotthard2] On chip Dac for reference voltage of the comparator of analogue front end."); - INTEGER_USER_IND_COMMAND(vchip_cs, getOnChipDAC, setOnChipDAC, stoiHex, defs::VB_CS, + INTEGER_USER_IND_COMMAND(vchip_cs, getOnChipDAC, setOnChipDAC, StringTo, defs::VB_CS, "[chip index 0-10, -1 for all][10 bit hex value] \n\t[Gotthard2] On chip Dac for current injection into preamplifier."); @@ -1620,10 +1620,10 @@ class CmdProxy { INTEGER_IND_COMMAND(v_limit, getVoltage, setVoltage, std::stoi, defs::V_LIMIT, "[n_value]\n\t[Ctb][Moench] Soft limit for power supplies(ctb only) and DACS in mV."); - INTEGER_COMMAND_HEX(adcenable, getADCEnableMask, setADCEnableMask, stoiHex, + INTEGER_COMMAND_HEX(adcenable, getADCEnableMask, setADCEnableMask, StringTo, "[bitmask]\n\t[Ctb][Moench] ADC Enable Mask for 1Gb Mode for each 32 ADC channel."); - INTEGER_COMMAND_HEX(adcenable10g, getTenGigaADCEnableMask, setTenGigaADCEnableMask, stoiHex, + INTEGER_COMMAND_HEX(adcenable10g, getTenGigaADCEnableMask, setTenGigaADCEnableMask, StringTo, "[bitmask]\n\t[Ctb][Moench] ADC Enable Mask for 10Gb mode for each 32 ADC channel. However, if any of consecutive 4 bits are enabled, the complete 4 bits are enabled."); /* CTB Specific */ @@ -1707,16 +1707,16 @@ class CmdProxy { EXECUTE_SET_COMMAND_NOID_1ARG(savepattern, savePattern, "[fname]\n\t[Ctb][Moench][Mythen3] Saves pattern to file (ascii). Also executes pattern."); - INTEGER_COMMAND_HEX(patioctrl, getPatternIOControl, setPatternIOControl, stoulHex, + INTEGER_COMMAND_HEX(patioctrl, getPatternIOControl, setPatternIOControl, StringTo, "[64 bit mask]\n\t[Ctb][Moench][Mythen3] 64 bit mask defining input (0) and output (1) signals."); - INTEGER_COMMAND_HEX(patclkctrl, getPatternClockControl, setPatternClockControl, stoulHex, + INTEGER_COMMAND_HEX(patclkctrl, getPatternClockControl, setPatternClockControl, StringTo, "[64 bit mask]\n\t[Ctb][Moench][Mythen3] 64 bit mask defining output clock enable."); - INTEGER_COMMAND_HEX(patmask, getPatternMask, setPatternMask, stoulHex, + INTEGER_COMMAND_HEX(patmask, getPatternMask, setPatternMask, StringTo, "[64 bit mask]\n\t[Ctb][Moench][Mythen3] 64 bit mask applied to every pattern. Only these bits for each pattern will be masked against."); - INTEGER_COMMAND_HEX(patsetbit, getPatternBitMask, setPatternBitMask, stoulHex, + INTEGER_COMMAND_HEX(patsetbit, getPatternBitMask, setPatternBitMask, StringTo, "[64 bit mask]\n\t[Ctb][Moench][Mythen3] 64 bit values applied to the selected patmask for every pattern."); /* Moench */ @@ -1745,7 +1745,7 @@ class CmdProxy { EXECUTE_SET_COMMAND(bustest, executeBusTest, "\n\t[Jungfrau][Gotthard][Mythen3][Gotthard2][Ctb][Moench] Bus test, ie. keeps writing and reading back different values in R/W register."); - INTEGER_COMMAND_HEX(adcinvert, getADCInvert, setADCInvert, stoiHex, + INTEGER_COMMAND_HEX(adcinvert, getADCInvert, setADCInvert, StringTo, "[bitmask]\n\t[Ctb][Moench][Jungfrau][Moench] ADC Inversion Mask.\n\t[Jungfrau][Moench] Inversions on top of the default mask."); /* Insignificant */ diff --git a/slsSupportLib/include/ToString.h b/slsSupportLib/include/ToString.h index 3ba1ef121..1e1cc922a 100644 --- a/slsSupportLib/include/ToString.h +++ b/slsSupportLib/include/ToString.h @@ -622,6 +622,12 @@ inline uint64_t StringTo(const std::string &s) { return std::stoull(s, nullptr, base); } +template <> +inline int StringTo(const std::string &s) { + int base = s.find("0x") != std::string::npos ? 16 : 10; + return std::stoi(s, nullptr, base); +} + /** For types with a .str() method use this for conversion */ template typename std::enable_if::value, std::string>::type diff --git a/slsSupportLib/tests/test-ToString.cpp b/slsSupportLib/tests/test-ToString.cpp index fd326b742..599be4520 100644 --- a/slsSupportLib/tests/test-ToString.cpp +++ b/slsSupportLib/tests/test-ToString.cpp @@ -165,3 +165,17 @@ TEST_CASE("uint64 from string"){ } + +TEST_CASE("int from string"){ + REQUIRE(StringTo("-1") == -1); + REQUIRE(StringTo("-0x1") == -0x1); + REQUIRE(StringTo("-0x1") == -1); + REQUIRE(StringTo("0") == 0); + REQUIRE(StringTo("5") == 5); + REQUIRE(StringTo("16") == 16); + REQUIRE(StringTo("20") == 20); + REQUIRE(StringTo("0x14") == 20); + REQUIRE(StringTo("0x15") == 21); + REQUIRE(StringTo("0xffffff") == 0xffffff); + +}