From 0fb641ab291131ca96bd59bdfaed0b021ee1e176 Mon Sep 17 00:00:00 2001 From: Peter Newman Date: Fri, 12 Dec 2014 03:14:20 +0000 Subject: [PATCH 01/13] Fix some more issues --- common/utils/StringUtils.cpp | 1 + examples/ola-dmxconsole.cpp | 1 + examples/ola-dmxmonitor.cpp | 1 + examples/ola-rdm.cpp | 5 ++- plugins/pathport/PathportPort.h | 4 +- tools/rdmpro/rdm-sniffer.cpp | 65 +++++++++++++++++++-------------- 6 files changed, 45 insertions(+), 32 deletions(-) diff --git a/common/utils/StringUtils.cpp b/common/utils/StringUtils.cpp index 59cb6b8a7a..174709e380 100644 --- a/common/utils/StringUtils.cpp +++ b/common/utils/StringUtils.cpp @@ -397,6 +397,7 @@ void CapitalizeLabel(string *s) { for (string::iterator iter = s->begin(); iter != s->end(); ++iter) { switch (*iter) { case '-': + // fall through, also convert to space case '_': *iter = ' '; case ' ': diff --git a/examples/ola-dmxconsole.cpp b/examples/ola-dmxconsole.cpp index 3c17a39ad2..038ef693ed 100644 --- a/examples/ola-dmxconsole.cpp +++ b/examples/ola-dmxconsole.cpp @@ -445,6 +445,7 @@ void changepalette(int p) { switch (p) { default: palette_number = 0; + // fall through, use 0 as default palette case 0: init_pair(CHANNEL, COLOR_BLACK, COLOR_CYAN); init_pair(ZERO, COLOR_BLACK, COLOR_WHITE); diff --git a/examples/ola-dmxmonitor.cpp b/examples/ola-dmxmonitor.cpp index b51d654689..874ccefb07 100644 --- a/examples/ola-dmxmonitor.cpp +++ b/examples/ola-dmxmonitor.cpp @@ -559,6 +559,7 @@ void DmxMonitor::ChangePalette(int p) { switch (p) { default: m_palette_number = 0; + // fall through, use 0 as default palette case 0: init_pair(CHANNEL, COLOR_BLACK, COLOR_CYAN); init_pair(ZERO, COLOR_BLACK, COLOR_WHITE); diff --git a/examples/ola-rdm.cpp b/examples/ola-rdm.cpp index 8dc8ed25e7..b4fe33aa5f 100644 --- a/examples/ola-rdm.cpp +++ b/examples/ola-rdm.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -308,8 +309,8 @@ void RDMController::HandleResponse( cout << "Request NACKed: " << ola::rdm::NackReasonToString(response_status.NackReason()) << endl; } else { - cout << "Unknown RDM response type " << std::hex << - static_cast(response_status.response_type) << endl; + cout << "Unknown RDM response type " + << ola::IntToHexString(response_status.response_type) << endl; } PrintRemainingMessages(response_status.message_count); m_ola_client.GetSelectServer()->Terminate(); diff --git a/plugins/pathport/PathportPort.h b/plugins/pathport/PathportPort.h index b5ab9b6084..b48952b56f 100644 --- a/plugins/pathport/PathportPort.h +++ b/plugins/pathport/PathportPort.h @@ -79,9 +79,9 @@ class PathportOutputPort: public BasicOutputPort { return m_helper.Description(GetUniverse()); } bool WriteDMX(const DmxBuffer &buffer, uint8_t priority); - bool PreSetUniverse(Universe *old_universe, Universe *new_universe) { + bool PreSetUniverse(OLA_UNUSED Universe *old_universe, + Universe *new_universe) { return m_helper.PreSetUniverse(new_universe); - (void) old_universe; } private: diff --git a/tools/rdmpro/rdm-sniffer.cpp b/tools/rdmpro/rdm-sniffer.cpp index 08b437cefd..2780442e10 100644 --- a/tools/rdmpro/rdm-sniffer.cpp +++ b/tools/rdmpro/rdm-sniffer.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -52,6 +53,7 @@ using std::cout; using std::endl; using std::string; using std::vector; +using ola::IntToHexString; using ola::io::SelectServerInterface; using ola::plugin::usbpro::DispatchingUsbProWidget; using ola::messaging::Descriptor; @@ -173,8 +175,9 @@ RDMSniffer::RDMSniffer(const RDMSnifferOptions &options) m_options(options), m_pid_helper(options.pid_location, 4), m_command_printer(&cout, &m_pid_helper) { - if (!m_pid_helper.Init()) + if (!m_pid_helper.Init()) { OLA_WARN << "Failed to init PidStore"; + } } @@ -235,9 +238,9 @@ void RDMSniffer::ProcessTuple(uint8_t control_byte, uint8_t data_byte) { m_frame.AddByte(data_byte); break; default: - OLA_WARN << "Unknown transition from state " << m_state << - ", with data 0x" << std::hex << static_cast(control_byte) << - " 0x" << static_cast(data_byte); + OLA_WARN << "Unknown transition from state " << m_state + << ", with data " << IntToHexString(control_byte) << " " + << IntToHexString(data_byte); } } else { // control byte @@ -247,9 +250,9 @@ void RDMSniffer::ProcessTuple(uint8_t control_byte, uint8_t data_byte) { m_state = MAB; break; default: - OLA_WARN << "Unknown transition from state " << m_state << - ", with data 0x" << std::hex << static_cast(control_byte) << - " 0x" << static_cast(data_byte); + OLA_WARN << "Unknown transition from state " << m_state + << ", with data " << IntToHexString(control_byte) << " " + << IntToHexString(data_byte); } } else if (data_byte == 1) { switch (m_state) { @@ -261,9 +264,9 @@ void RDMSniffer::ProcessTuple(uint8_t control_byte, uint8_t data_byte) { m_state = BREAK; break; default: - OLA_WARN << "Unknown transition from state " << m_state << - ", with data 0x" << std::hex << static_cast(control_byte) << - " 0x" << static_cast(data_byte); + OLA_WARN << "Unknown transition from state " << m_state + << ", with data " << IntToHexString(control_byte) << " " + << IntToHexString(data_byte); } } else if (data_byte == 2) { switch (m_state) { @@ -276,9 +279,9 @@ void RDMSniffer::ProcessTuple(uint8_t control_byte, uint8_t data_byte) { ProcessFrame(); } } else { - OLA_WARN << "Unknown transition from state " << m_state << - ", with data 0x" << std::hex << static_cast(control_byte) << - " 0x" << static_cast(data_byte); + OLA_WARN << "Unknown transition from state " << m_state + << ", with data " << IntToHexString(control_byte) << " " + << IntToHexString(data_byte); } } } @@ -290,15 +293,17 @@ void RDMSniffer::ProcessTuple(uint8_t control_byte, uint8_t data_byte) { void RDMSniffer::ProcessFrame() { switch (m_frame[0]) { case ola::DMX512_START_CODE: - if (m_options.display_dmx_frames) + if (m_options.display_dmx_frames) { DisplayDmxFrame(); + } break; case RDMCommand::START_CODE: DisplayRDMFrame(); break; default: - if (m_options.display_non_rdm_asc_frames) + if (m_options.display_non_rdm_asc_frames) { DisplayAlternateFrame(); + } } } @@ -310,8 +315,9 @@ void RDMSniffer::DisplayDmxFrame() { unsigned int dmx_slot_count = m_frame.Size() - 1; MaybePrintTimestamp(); cout << "DMX " << std::dec; - if (m_options.dmx_slot_limit < dmx_slot_count) + if (m_options.dmx_slot_limit < dmx_slot_count) { cout << m_options.dmx_slot_limit << "/"; + } cout << dmx_slot_count << ":" << std::hex; unsigned int slots_to_display = std::min( dmx_slot_count, @@ -327,8 +333,8 @@ void RDMSniffer::DisplayDmxFrame() { void RDMSniffer::DisplayAlternateFrame() { unsigned int slot_count = m_frame.Size() - 1; MaybePrintTimestamp(); - cout << "SC 0x" << std::hex << std::setw(2) << static_cast(m_frame[0]) - << " " << std::dec << slot_count << ":" << std::hex; + cout << "SC " << IntToHexString(m_frame[0]) + << " " << slot_count << ":" << std::hex; unsigned int slots_to_display = std::min( slot_count, static_cast(m_options.dmx_slot_limit)); @@ -346,11 +352,13 @@ void RDMSniffer::DisplayRDMFrame() { RDMCommand::Inflate(reinterpret_cast(&m_frame[1]), slot_count)); if (command.get()) { - if (!m_options.summarize_rdm_frames) + if (!m_options.summarize_rdm_frames) { cout << "---------------------------------------" << endl; + } - if (!m_options.summarize_rdm_frames && m_options.timestamp) + if (!m_options.summarize_rdm_frames && m_options.timestamp) { cout << endl; + } MaybePrintTimestamp(); @@ -367,8 +375,9 @@ void RDMSniffer::DisplayRDMFrame() { * Dump out the raw data if we couldn't parse it correctly. */ void RDMSniffer::DisplayRawData(unsigned int start, unsigned int end) { - for (unsigned int i = start; i <= end; i++) + for (unsigned int i = start; i <= end; i++) { cout << std::hex << std::setw(2) << static_cast(m_frame[i]) << " "; + } cout << endl; } @@ -377,8 +386,9 @@ void RDMSniffer::DisplayRawData(unsigned int start, unsigned int end) { * Print the timestamp if timestamps are enabled */ void RDMSniffer::MaybePrintTimestamp() { - if (!m_options.timestamp) + if (!m_options.timestamp) { return; + } ola::TimeStamp now; ola::Clock clock; @@ -443,8 +453,7 @@ int main(int argc, char *argv[]) { "Sniff traffic from a ENTTEC RDM Pro device."); if (!FLAGS_savefile.str().empty() && !FLAGS_readfile.str().empty()) { - ola::DisplayUsage(); - exit(ola::EXIT_USAGE); + ola::DisplayUsageAndExit(); } RDMSniffer::RDMSnifferOptions sniffer_options; @@ -463,7 +472,7 @@ int main(int argc, char *argv[]) { std::ios::out | std::ios::binary); if (!file.is_open()) { cerr << "Could not open file for writing: " << sniffer_options.write_file - << endl; + << endl; exit(ola::EXIT_UNAVAILABLE); } } @@ -475,16 +484,16 @@ int main(int argc, char *argv[]) { } if (argc != 2) { - ola::DisplayUsage(); - exit(ola::EXIT_USAGE); + ola::DisplayUsageAndExit(); } const string device = argv[1]; ola::io::ConnectedDescriptor *descriptor = ola::plugin::usbpro::BaseUsbProWidget::OpenDevice(device); - if (!descriptor) + if (!descriptor) { exit(ola::EXIT_UNAVAILABLE); + } ola::io::SelectServer ss; descriptor->SetOnClose(ola::NewSingleCallback(&Stop, &ss)); From c18242f07c81ce1dd1c6d9a4bc6c78f40c3291fe Mon Sep 17 00:00:00 2001 From: Peter Newman Date: Mon, 29 Dec 2014 14:28:00 +0000 Subject: [PATCH 02/13] Basic optimised ToHex function from Simon's idea --- common/utils/DmxBuffer.cpp | 2 +- common/utils/StringUtilsTest.cpp | 33 ++++++++++++++++++++++++++++++++ include/ola/StringUtils.h | 31 ++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/common/utils/DmxBuffer.cpp b/common/utils/DmxBuffer.cpp index a664e2f8c2..96fbac4339 100644 --- a/common/utils/DmxBuffer.cpp +++ b/common/utils/DmxBuffer.cpp @@ -238,7 +238,7 @@ void DmxBuffer::SetChannel(unsigned int channel, uint8_t data) { } if (channel > m_length) { - OLA_WARN << "attempting to set channel " << channel << " when length is " + OLA_WARN << "Attempting to set channel " << channel << " when length is " << m_length; return; } diff --git a/common/utils/StringUtilsTest.cpp b/common/utils/StringUtilsTest.cpp index c3926df61c..237acd7e54 100644 --- a/common/utils/StringUtilsTest.cpp +++ b/common/utils/StringUtilsTest.cpp @@ -52,6 +52,8 @@ using ola::StripPrefix; using ola::StripSuffix; using ola::ToLower; using ola::ToUpper; +using ola::ToHex; +using std::ostringstream; using std::string; using std::vector; @@ -313,6 +315,7 @@ void StringUtilsTest::testIntToString() { * test the IntToHexString function. */ void StringUtilsTest::testIntToHexString() { + // Using the old IntToHexString OLA_ASSERT_EQ(string("0x00"), IntToHexString((uint8_t)0)); OLA_ASSERT_EQ(string("0x01"), IntToHexString((uint8_t)1)); @@ -323,6 +326,36 @@ void StringUtilsTest::testIntToHexString() { unsigned int i = 0x42; OLA_ASSERT_EQ(string("0x00000042"), IntToHexString(i)); + + // Using the inline string concatenation + ostringstream str; + str << ToHex((uint8_t)0); + OLA_ASSERT_EQ(string("0x00"), str.str()); + str.str(""); + + str << ToHex((uint8_t)1); + OLA_ASSERT_EQ(string("0x01"), str.str()); + str.str(""); + + str << ToHex((uint8_t)0x42); + OLA_ASSERT_EQ(string("0x42"), str.str()); + str.str(""); + + str << ToHex((uint16_t)0x0001); + OLA_ASSERT_EQ(string("0x0001"), str.str()); + str.str(""); + + str << ToHex((uint16_t)0xABCD); + OLA_ASSERT_EQ(string("0xabcd"), str.str()); + str.str(""); + + str << ToHex((uint32_t)0xDEADBEEF); + OLA_ASSERT_EQ(string("0xdeadbeef"), str.str()); + str.str(""); + + str << ToHex(i); + OLA_ASSERT_EQ(string("0x00000042"), str.str()); + str.str(""); } diff --git a/include/ola/StringUtils.h b/include/ola/StringUtils.h index d158362e4f..55dc3f3d65 100644 --- a/include/ola/StringUtils.h +++ b/include/ola/StringUtils.h @@ -27,7 +27,10 @@ #define INCLUDE_OLA_STRINGUTILS_H_ #include +#include +#include #include +#include #include #include #include @@ -108,6 +111,34 @@ std::string IntToString(int i); */ std::string IntToString(unsigned int i); +template +struct _ToHex { + unsigned int width; + T value; +}; + +template +_ToHex GenericToHex(T v, unsigned int width) { + _ToHex x; + x.width = width; + x.value = v; + return x; +} + +template +_ToHex ToHex(T v) { + // TODO(Peter): This may break if we get a type that doesn't have digits + return GenericToHex(v, (std::numeric_limits::digits / HEX_BIT_WIDTH)); +} + +template +std::ostream& operator<<(std::ostream &out, const _ToHex &i) { + // In C++, you only get the 0x on non-zero values, so we have to explicitly + // add it for all values + return out << "0x" << std::setw(i.width) << std::hex << std::setfill('0') + << static_cast(i.value) << std::dec; +} + /** * Convert an unsigned int to a hex string. * @param i the unsigned int to convert From fb99821b015c32dabb24b347be674dbc678ed808 Mon Sep 17 00:00:00 2001 From: Peter Newman Date: Mon, 29 Dec 2014 14:43:20 +0000 Subject: [PATCH 03/13] Tidy up the type, add prefix and document it --- common/utils/StringUtilsTest.cpp | 9 ++++++++ include/ola/StringUtils.h | 35 +++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/common/utils/StringUtilsTest.cpp b/common/utils/StringUtilsTest.cpp index 237acd7e54..d2d61e2272 100644 --- a/common/utils/StringUtilsTest.cpp +++ b/common/utils/StringUtilsTest.cpp @@ -356,6 +356,15 @@ void StringUtilsTest::testIntToHexString() { str << ToHex(i); OLA_ASSERT_EQ(string("0x00000042"), str.str()); str.str(""); + + // Without prefix + str << ToHex((uint8_t)0x42, false); + OLA_ASSERT_EQ(string("42"), str.str()); + str.str(""); + + str << ToHex((uint16_t)0xABCD, false); + OLA_ASSERT_EQ(string("abcd"), str.str()); + str.str(""); } diff --git a/include/ola/StringUtils.h b/include/ola/StringUtils.h index 55dc3f3d65..fb2f03edbc 100644 --- a/include/ola/StringUtils.h +++ b/include/ola/StringUtils.h @@ -111,31 +111,56 @@ std::string IntToString(int i); */ std::string IntToString(unsigned int i); +/** + * Internal type used by ToHex + */ template struct _ToHex { unsigned int width; T value; + bool prefix; }; +/** + * Constructor for the _ToHex type + */ template -_ToHex GenericToHex(T v, unsigned int width) { +_ToHex GenericToHex(T v, unsigned int width, bool prefix) { _ToHex x; x.width = width; x.value = v; + x.prefix = prefix return x; } +/** + * Convert a value to a hex string. + * + * Automatic constructor for _ToHex that deals with widths + * @tparam v the value to convert + * @param prefix show the 0x prefix + * @return A _ToHex struct representing the value, output it to an ostream to + * use it. + * @note We only currently support unsigned ints due to a lack of requirement + * for anything else and issues with negative handling and hex in C++ + */ template -_ToHex ToHex(T v) { +_ToHex ToHex(T v, bool prefix = true) { // TODO(Peter): This may break if we get a type that doesn't have digits - return GenericToHex(v, (std::numeric_limits::digits / HEX_BIT_WIDTH)); + return GenericToHex(v, (std::numeric_limits::digits / HEX_BIT_WIDTH), prefix); } +/** + * Output the _ToHex type to an ostream + */ template std::ostream& operator<<(std::ostream &out, const _ToHex &i) { // In C++, you only get the 0x on non-zero values, so we have to explicitly - // add it for all values - return out << "0x" << std::setw(i.width) << std::hex << std::setfill('0') + // add it for all values if we want it + if (prefix) { + out << "0x"; + } + return out << std::setw(i.width) << std::hex << std::setfill('0') << static_cast(i.value) << std::dec; } From 90d57b8acbdff2423eb848929a12fb5ecb73bded Mon Sep 17 00:00:00 2001 From: Peter Newman Date: Mon, 29 Dec 2014 14:49:24 +0000 Subject: [PATCH 04/13] Switch stuff to use ToHex, resolving Simon's comment --- examples/ola-rdm.cpp | 2 +- tools/rdmpro/rdm-sniffer.cpp | 25 ++++++++++++------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/examples/ola-rdm.cpp b/examples/ola-rdm.cpp index b4fe33aa5f..1cee120537 100644 --- a/examples/ola-rdm.cpp +++ b/examples/ola-rdm.cpp @@ -310,7 +310,7 @@ void RDMController::HandleResponse( ola::rdm::NackReasonToString(response_status.NackReason()) << endl; } else { cout << "Unknown RDM response type " - << ola::IntToHexString(response_status.response_type) << endl; + << ola::ToHex(response_status.response_type) << endl; } PrintRemainingMessages(response_status.message_count); m_ola_client.GetSelectServer()->Terminate(); diff --git a/tools/rdmpro/rdm-sniffer.cpp b/tools/rdmpro/rdm-sniffer.cpp index 2780442e10..a4625577ea 100644 --- a/tools/rdmpro/rdm-sniffer.cpp +++ b/tools/rdmpro/rdm-sniffer.cpp @@ -53,7 +53,7 @@ using std::cout; using std::endl; using std::string; using std::vector; -using ola::IntToHexString; +using ola::ToHex; using ola::io::SelectServerInterface; using ola::plugin::usbpro::DispatchingUsbProWidget; using ola::messaging::Descriptor; @@ -239,8 +239,8 @@ void RDMSniffer::ProcessTuple(uint8_t control_byte, uint8_t data_byte) { break; default: OLA_WARN << "Unknown transition from state " << m_state - << ", with data " << IntToHexString(control_byte) << " " - << IntToHexString(data_byte); + << ", with data " << ToHex(control_byte) << " " + << ToHex(data_byte); } } else { // control byte @@ -251,8 +251,8 @@ void RDMSniffer::ProcessTuple(uint8_t control_byte, uint8_t data_byte) { break; default: OLA_WARN << "Unknown transition from state " << m_state - << ", with data " << IntToHexString(control_byte) << " " - << IntToHexString(data_byte); + << ", with data " << ToHex(control_byte) << " " + << ToHex(data_byte); } } else if (data_byte == 1) { switch (m_state) { @@ -265,8 +265,8 @@ void RDMSniffer::ProcessTuple(uint8_t control_byte, uint8_t data_byte) { break; default: OLA_WARN << "Unknown transition from state " << m_state - << ", with data " << IntToHexString(control_byte) << " " - << IntToHexString(data_byte); + << ", with data " << ToHex(control_byte) << " " + << ToHex(data_byte); } } else if (data_byte == 2) { switch (m_state) { @@ -280,8 +280,8 @@ void RDMSniffer::ProcessTuple(uint8_t control_byte, uint8_t data_byte) { } } else { OLA_WARN << "Unknown transition from state " << m_state - << ", with data " << IntToHexString(control_byte) << " " - << IntToHexString(data_byte); + << ", with data " << ToHex(control_byte) << " " + << ToHex(data_byte); } } } @@ -318,7 +318,7 @@ void RDMSniffer::DisplayDmxFrame() { if (m_options.dmx_slot_limit < dmx_slot_count) { cout << m_options.dmx_slot_limit << "/"; } - cout << dmx_slot_count << ":" << std::hex; + cout << dmx_slot_count << ":"; unsigned int slots_to_display = std::min( dmx_slot_count, static_cast(m_options.dmx_slot_limit)); @@ -333,8 +333,7 @@ void RDMSniffer::DisplayDmxFrame() { void RDMSniffer::DisplayAlternateFrame() { unsigned int slot_count = m_frame.Size() - 1; MaybePrintTimestamp(); - cout << "SC " << IntToHexString(m_frame[0]) - << " " << slot_count << ":" << std::hex; + cout << "SC " << ToHex(m_frame[0]) << " " << slot_count << ":"; unsigned int slots_to_display = std::min( slot_count, static_cast(m_options.dmx_slot_limit)); @@ -376,7 +375,7 @@ void RDMSniffer::DisplayRDMFrame() { */ void RDMSniffer::DisplayRawData(unsigned int start, unsigned int end) { for (unsigned int i = start; i <= end; i++) { - cout << std::hex << std::setw(2) << static_cast(m_frame[i]) << " "; + cout << ToHex(m_frame[i], false) << " "; } cout << endl; } From 1492c0fe58dc48bfa3d21967799133d49bb115ba Mon Sep 17 00:00:00 2001 From: Peter Newman Date: Mon, 29 Dec 2014 14:59:00 +0000 Subject: [PATCH 05/13] Fix the build --- include/ola/StringUtils.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/ola/StringUtils.h b/include/ola/StringUtils.h index fb2f03edbc..1c73a4a0a7 100644 --- a/include/ola/StringUtils.h +++ b/include/ola/StringUtils.h @@ -129,7 +129,7 @@ _ToHex GenericToHex(T v, unsigned int width, bool prefix) { _ToHex x; x.width = width; x.value = v; - x.prefix = prefix + x.prefix = prefix; return x; } @@ -157,7 +157,7 @@ template std::ostream& operator<<(std::ostream &out, const _ToHex &i) { // In C++, you only get the 0x on non-zero values, so we have to explicitly // add it for all values if we want it - if (prefix) { + if (i.prefix) { out << "0x"; } return out << std::setw(i.width) << std::hex << std::setfill('0') From 598f556d2e0b2f53e56bcd95cdea4b02b742016c Mon Sep 17 00:00:00 2001 From: Peter Newman Date: Mon, 29 Dec 2014 16:05:09 +0000 Subject: [PATCH 06/13] Fix the lint and Doxygen issues --- include/ola/StringUtils.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/ola/StringUtils.h b/include/ola/StringUtils.h index 1c73a4a0a7..8da8412be8 100644 --- a/include/ola/StringUtils.h +++ b/include/ola/StringUtils.h @@ -137,7 +137,8 @@ _ToHex GenericToHex(T v, unsigned int width, bool prefix) { * Convert a value to a hex string. * * Automatic constructor for _ToHex that deals with widths - * @tparam v the value to convert + * @tparam T the type of value to convert + * @param v the value to convert * @param prefix show the 0x prefix * @return A _ToHex struct representing the value, output it to an ostream to * use it. @@ -147,7 +148,9 @@ _ToHex GenericToHex(T v, unsigned int width, bool prefix) { template _ToHex ToHex(T v, bool prefix = true) { // TODO(Peter): This may break if we get a type that doesn't have digits - return GenericToHex(v, (std::numeric_limits::digits / HEX_BIT_WIDTH), prefix); + return GenericToHex(v, + (std::numeric_limits::digits / HEX_BIT_WIDTH), + prefix); } /** From a7c97f5f40651cf163ddfc16de7aa823e4478299 Mon Sep 17 00:00:00 2001 From: Peter Newman Date: Mon, 29 Dec 2014 16:10:43 +0000 Subject: [PATCH 07/13] Switch the remaining code to use the new ToHex function --- common/rdm/RDMHelper.cpp | 6 +++--- plugins/usbdmx/AsyncPluginImpl.cpp | 4 ++-- plugins/usbdmx/ScanlimeFadecandy.cpp | 4 ++-- plugins/usbdmx/SyncPluginImpl.cpp | 4 ++-- plugins/usbpro/ArduinoRGBDevice.cpp | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/common/rdm/RDMHelper.cpp b/common/rdm/RDMHelper.cpp index 3679b467c2..f945a28656 100644 --- a/common/rdm/RDMHelper.cpp +++ b/common/rdm/RDMHelper.cpp @@ -994,15 +994,15 @@ string StatusMessageIdToString(uint16_t message_id, // Data Value shall be a signed integer." but I'm sure it's what was // intended. The same thing is technically true with the slots too. str << "Proxy Drop: PID " - << IntToHexString(reinterpret_cast(data1)) << " at TN " + << ToHex(reinterpret_cast(data1)) << " at TN " << data2; break; case STS_ASC_RXOK: - str << "DMX ASC " << IntToHexString(reinterpret_cast(data1)) + str << "DMX ASC " << ToHex(reinterpret_cast(data1)) << " received OK"; break; case STS_ASC_DROPPED: - str << "DMX ASC " << IntToHexString(reinterpret_cast(data1)) + str << "DMX ASC " << ToHex(reinterpret_cast(data1)) << " now dropped"; break; case STS_DMXNSCNONE: diff --git a/plugins/usbdmx/AsyncPluginImpl.cpp b/plugins/usbdmx/AsyncPluginImpl.cpp index 376440e77c..4c3133e6ba 100644 --- a/plugins/usbdmx/AsyncPluginImpl.cpp +++ b/plugins/usbdmx/AsyncPluginImpl.cpp @@ -267,8 +267,8 @@ bool AsyncPluginImpl::USBDeviceAdded(libusb_device *usb_device) { libusb_get_device_descriptor(usb_device, &descriptor); OLA_DEBUG << "USB device added, checking for widget support, vendor " - << IntToHexString(descriptor.idVendor) << ", product " - << IntToHexString(descriptor.idProduct); + << ToHex(descriptor.idVendor) << ", product " + << ToHex(descriptor.idProduct); WidgetFactories::iterator iter = m_widget_factories.begin(); for (; iter != m_widget_factories.end(); ++iter) { diff --git a/plugins/usbdmx/ScanlimeFadecandy.cpp b/plugins/usbdmx/ScanlimeFadecandy.cpp index dd54e53358..80990d4a90 100644 --- a/plugins/usbdmx/ScanlimeFadecandy.cpp +++ b/plugins/usbdmx/ScanlimeFadecandy.cpp @@ -132,9 +132,9 @@ bool InitializeWidget(LibUsbAdaptor *adaptor, unsigned int entry = (channel * 257) + value; unsigned int packet_entry = entry % 31; OLA_DEBUG << "Working on channel " << channel << " value " << value - << " (" << IntToHexString(value) << ") with entry " << entry + << " (" << ToHex(value) << ") with entry " << entry << ", packet entry " << packet_entry << " with val " - << IntToHexString(lut[channel][value]); + << ToHex(lut[channel][value]); ola::utils::SplitUInt16(lut[channel][value], &packet.data[packet_entry + 1], &packet.data[packet_entry]); diff --git a/plugins/usbdmx/SyncPluginImpl.cpp b/plugins/usbdmx/SyncPluginImpl.cpp index 1714740218..b27f1d4e2b 100644 --- a/plugins/usbdmx/SyncPluginImpl.cpp +++ b/plugins/usbdmx/SyncPluginImpl.cpp @@ -165,8 +165,8 @@ bool SyncPluginImpl::CheckDevice(libusb_device *usb_device) { libusb_get_device_descriptor(usb_device, &device_descriptor); OLA_DEBUG << "USB device found, checking for widget support, vendor " - << IntToHexString(device_descriptor.idVendor) << ", product " - << IntToHexString(device_descriptor.idProduct); + << ToHex(device_descriptor.idVendor) << ", product " + << ToHex(device_descriptor.idProduct); pair bus_dev_id(libusb_get_bus_number(usb_device), libusb_get_device_address(usb_device)); diff --git a/plugins/usbpro/ArduinoRGBDevice.cpp b/plugins/usbpro/ArduinoRGBDevice.cpp index 8fc20b5f26..ae95724f83 100644 --- a/plugins/usbpro/ArduinoRGBDevice.cpp +++ b/plugins/usbpro/ArduinoRGBDevice.cpp @@ -71,7 +71,7 @@ ArduinoRGBOutputPort::ArduinoRGBOutputPort(ArduinoRGBDevice *parent, m_bucket(initial_count, rate, rate, *wake_time), m_wake_time(wake_time) { std::ostringstream str; - str << "Serial #: " << ola::IntToHexString(serial); + str << "Serial #: " << ola::ToHex(serial); m_description = str.str(); } } // namespace usbpro From 2450bcb1b7441a07a15fd39ef8f4fbd75aa8fb2b Mon Sep 17 00:00:00 2001 From: Peter Newman Date: Tue, 30 Dec 2014 13:16:54 +0000 Subject: [PATCH 08/13] Address a comment --- include/ola/StringUtils.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/ola/StringUtils.h b/include/ola/StringUtils.h index 8da8412be8..03800a09e0 100644 --- a/include/ola/StringUtils.h +++ b/include/ola/StringUtils.h @@ -147,7 +147,6 @@ _ToHex GenericToHex(T v, unsigned int width, bool prefix) { */ template _ToHex ToHex(T v, bool prefix = true) { - // TODO(Peter): This may break if we get a type that doesn't have digits return GenericToHex(v, (std::numeric_limits::digits / HEX_BIT_WIDTH), prefix); From cc4e8c6f154496e76809758753957cda045adec3 Mon Sep 17 00:00:00 2001 From: Peter Newman Date: Tue, 30 Dec 2014 16:12:06 +0000 Subject: [PATCH 09/13] Fix the comments --- include/ola/StringUtils.h | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/include/ola/StringUtils.h b/include/ola/StringUtils.h index 03800a09e0..5369c50eb6 100644 --- a/include/ola/StringUtils.h +++ b/include/ola/StringUtils.h @@ -116,22 +116,21 @@ std::string IntToString(unsigned int i); */ template struct _ToHex { + public: + _ToHex(T v, unsigned int width, bool prefix) + : width(width), + value(v), + prefix(prefix) { + } + unsigned int width; T value; bool prefix; }; -/** - * Constructor for the _ToHex type - */ -template -_ToHex GenericToHex(T v, unsigned int width, bool prefix) { - _ToHex x; - x.width = width; - x.value = v; - x.prefix = prefix; - return x; -} +inline uint32_t _HexCast(uint8_t v) { return v; } +inline uint16_t _HexCast(uint16_t v) { return v; } +inline uint32_t _HexCast(uint32_t v) { return v; } /** * Convert a value to a hex string. @@ -143,13 +142,13 @@ _ToHex GenericToHex(T v, unsigned int width, bool prefix) { * @return A _ToHex struct representing the value, output it to an ostream to * use it. * @note We only currently support unsigned ints due to a lack of requirement - * for anything else and issues with negative handling and hex in C++ + * for anything else */ template _ToHex ToHex(T v, bool prefix = true) { - return GenericToHex(v, - (std::numeric_limits::digits / HEX_BIT_WIDTH), - prefix); + return _ToHex(v, + (std::numeric_limits::digits / HEX_BIT_WIDTH), + prefix); } /** @@ -163,7 +162,7 @@ std::ostream& operator<<(std::ostream &out, const _ToHex &i) { out << "0x"; } return out << std::setw(i.width) << std::hex << std::setfill('0') - << static_cast(i.value) << std::dec; + << _HexCast(i.value) << std::dec; } /** From c2c0646f44d94b7a519f973c9eaf2663ad588787 Mon Sep 17 00:00:00 2001 From: Peter Newman Date: Tue, 30 Dec 2014 16:22:54 +0000 Subject: [PATCH 10/13] Switch IntToHexString to use the new ToHex function --- common/utils/StringUtils.cpp | 5 ++--- include/ola/StringUtils.h | 23 +++++++++++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/common/utils/StringUtils.cpp b/common/utils/StringUtils.cpp index 174709e380..d3701824ff 100644 --- a/common/utils/StringUtils.cpp +++ b/common/utils/StringUtils.cpp @@ -122,10 +122,9 @@ string IntToString(unsigned int i) { } string IntToHexString(unsigned int i, unsigned int width) { + _ToHex v = _ToHex(i, width, true); ostringstream str; - // In C++, you only get the 0x on non-zero values, so we have to explicitly - // add it for all values - str << "0x" << std::setw(width) << std::hex << std::setfill('0') << i; + str << v; return str.str(); } diff --git a/include/ola/StringUtils.h b/include/ola/StringUtils.h index 5369c50eb6..c113b6416a 100644 --- a/include/ola/StringUtils.h +++ b/include/ola/StringUtils.h @@ -172,6 +172,8 @@ std::ostream& operator<<(std::ostream &out, const _ToHex &i) { * @return The hex string representation of the unsigned int * @note We don't currently support signed ints due to a lack of requirement * for it and issues with negative handling and hex in C++ + * @deprecated ola::ToHex() instead, unless you really want a string rather + * than use in an ostream (30 Dec 2014) */ std::string IntToHexString(unsigned int i, unsigned int width); @@ -179,30 +181,39 @@ std::string IntToHexString(unsigned int i, unsigned int width); * Convert a uint8_t to a hex string. * @param i the number to convert * @return The string representation of the number + * @deprecated ola::ToHex() instead, unless you really want a string rather + * than use in an ostream (30 Dec 2014) */ inline std::string IntToHexString(uint8_t i) { - return IntToHexString(i, (std::numeric_limits::digits / - HEX_BIT_WIDTH)); + ostringstream str; + str << ToHex(i); + return str.str(); } /** * Convert a uint16_t to a hex string. * @param i the number to convert * @return The string representation of the number + * @deprecated ola::ToHex() instead, unless you really want a string rather + * than use in an ostream (30 Dec 2014) */ inline std::string IntToHexString(uint16_t i) { - return IntToHexString(i, (std::numeric_limits::digits / - HEX_BIT_WIDTH)); + ostringstream str; + str << ToHex(i); + return str.str(); } /** * Convert a uint32_t to a hex string. * @param i the number to convert * @return The string representation of the number + * @deprecated ola::ToHex() instead, unless you really want a string rather + * than use in an ostream (30 Dec 2014) */ inline std::string IntToHexString(uint32_t i) { - return IntToHexString(i, (std::numeric_limits::digits / - HEX_BIT_WIDTH)); + ostringstream str; + str << ToHex(i); + return str.str(); } /** From 3829a74be4079dcb1aaa110d5ad8a138e811a010 Mon Sep 17 00:00:00 2001 From: Peter Newman Date: Tue, 30 Dec 2014 16:24:56 +0000 Subject: [PATCH 11/13] Fix the build --- include/ola/StringUtils.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/ola/StringUtils.h b/include/ola/StringUtils.h index c113b6416a..c1798b0260 100644 --- a/include/ola/StringUtils.h +++ b/include/ola/StringUtils.h @@ -185,7 +185,7 @@ std::string IntToHexString(unsigned int i, unsigned int width); * than use in an ostream (30 Dec 2014) */ inline std::string IntToHexString(uint8_t i) { - ostringstream str; + std::ostringstream str; str << ToHex(i); return str.str(); } @@ -198,7 +198,7 @@ inline std::string IntToHexString(uint8_t i) { * than use in an ostream (30 Dec 2014) */ inline std::string IntToHexString(uint16_t i) { - ostringstream str; + std::ostringstream str; str << ToHex(i); return str.str(); } @@ -211,7 +211,7 @@ inline std::string IntToHexString(uint16_t i) { * than use in an ostream (30 Dec 2014) */ inline std::string IntToHexString(uint32_t i) { - ostringstream str; + std::ostringstream str; str << ToHex(i); return str.str(); } From 41194b2a39598cf445aa0dc2a593d61b3c029716 Mon Sep 17 00:00:00 2001 From: Peter Newman Date: Tue, 30 Dec 2014 17:00:39 +0000 Subject: [PATCH 12/13] Fix the clang issues --- include/ola/StringUtils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/ola/StringUtils.h b/include/ola/StringUtils.h index c1798b0260..3c073d844d 100644 --- a/include/ola/StringUtils.h +++ b/include/ola/StringUtils.h @@ -123,7 +123,7 @@ struct _ToHex { prefix(prefix) { } - unsigned int width; + int width; // setw takes an int T value; bool prefix; }; From 795263881f0812722f11020ea4cdd92cc2f42120 Mon Sep 17 00:00:00 2001 From: Peter Newman Date: Tue, 30 Dec 2014 17:12:36 +0000 Subject: [PATCH 13/13] Try again to fix the clang issues --- common/utils/StringUtils.cpp | 4 +++- include/ola/StringUtils.h | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/common/utils/StringUtils.cpp b/common/utils/StringUtils.cpp index d3701824ff..632e4334ef 100644 --- a/common/utils/StringUtils.cpp +++ b/common/utils/StringUtils.cpp @@ -122,7 +122,9 @@ string IntToString(unsigned int i) { } string IntToHexString(unsigned int i, unsigned int width) { - _ToHex v = _ToHex(i, width, true); + _ToHex v = _ToHex(i, + static_cast(width), + true); ostringstream str; str << v; return str.str(); diff --git a/include/ola/StringUtils.h b/include/ola/StringUtils.h index 3c073d844d..aec748256b 100644 --- a/include/ola/StringUtils.h +++ b/include/ola/StringUtils.h @@ -117,7 +117,7 @@ std::string IntToString(unsigned int i); template struct _ToHex { public: - _ToHex(T v, unsigned int width, bool prefix) + _ToHex(T v, int width, bool prefix) : width(width), value(v), prefix(prefix) {