From c8a6db0e46202d49b47158e902e1afb8b1d5fbcf Mon Sep 17 00:00:00 2001 From: Soeren Apel Date: Sun, 15 Dec 2019 17:20:51 +0100 Subject: [PATCH] Various binary output-related changes 1) Remove some try..catch clauses that can easily be handled by if (...) instead so that debugging unwanted exceptions is easier 2) Replace some iterator-based loops by loops using index-based container access 3) Replace the binary chunk container by a deque --- pv/data/decodesignal.cpp | 41 ++++++++++++++-------------- pv/data/decodesignal.hpp | 4 ++- pv/views/decoder_output/QHexView.cpp | 34 +++++++++++++++++------ pv/views/trace/analogsignal.cpp | 5 ++-- pv/views/trace/decodetrace.cpp | 8 ++++-- 5 files changed, 55 insertions(+), 37 deletions(-) diff --git a/pv/data/decodesignal.cpp b/pv/data/decodesignal.cpp index 33861ea5..00812917 100644 --- a/pv/data/decodesignal.cpp +++ b/pv/data/decodesignal.cpp @@ -442,15 +442,13 @@ int64_t DecodeSignal::get_decoded_sample_count(uint32_t segment_id, int64_t result = 0; - try { - const DecodeSegment *segment = &(segments_.at(segment_id)); - if (include_processing) - result = segment->samples_decoded_incl; - else - result = segment->samples_decoded_excl; - } catch (out_of_range&) { - // Do nothing - } + if (segment_id >= segments_.size()) + return result; + + if (include_processing) + result = segments_[segment_id].samples_decoded_incl; + else + result = segments_[segment_id].samples_decoded_excl; return result; } @@ -493,18 +491,16 @@ void DecodeSignal::get_annotation_subset( { lock_guard lock(output_mutex_); - try { - const DecodeSegment *segment = &(segments_.at(segment_id)); - const map *rows = - &(segment->annotation_rows); + if (segment_id >= segments_.size()) + return; - const auto iter = rows->find(row); - if (iter != rows->end()) - (*iter).second.get_annotation_subset(dest, - start_sample, end_sample); - } catch (out_of_range&) { - // Do nothing - } + const DecodeSegment *segment = &(segments_.at(segment_id)); + const map *rows = + &(segment->annotation_rows); + + const auto iter = rows->find(row); + if (iter != rows->end()) + (*iter).second.get_annotation_subset(dest, start_sample, end_sample); } void DecodeSignal::get_annotation_subset( @@ -537,6 +533,9 @@ void DecodeSignal::get_annotation_subset( uint32_t DecodeSignal::get_binary_data_chunk_count(uint32_t segment_id, const Decoder* dec, uint32_t bin_class_id) const { + if (segments_.size() == 0) + return 0; + try { const DecodeSegment *segment = &(segments_.at(segment_id)); @@ -1351,7 +1350,7 @@ void DecodeSignal::create_decode_segment() for (uint32_t i = 0; i < n; i++) segments_.back().binary_classes.push_back( - {dec.get(), dec->get_binary_class(i), vector()}); + {dec.get(), dec->get_binary_class(i), deque()}); } } diff --git a/pv/data/decodesignal.hpp b/pv/data/decodesignal.hpp index 1fe696eb..e591eb01 100644 --- a/pv/data/decodesignal.hpp +++ b/pv/data/decodesignal.hpp @@ -21,6 +21,7 @@ #define PULSEVIEW_PV_DATA_DECODESIGNAL_HPP #include +#include #include #include #include @@ -38,6 +39,7 @@ using std::atomic; using std::condition_variable; +using std::deque; using std::map; using std::mutex; using std::pair; @@ -70,7 +72,7 @@ struct DecodeBinaryClass { const decode::Decoder* decoder; const decode::DecodeBinaryClassInfo* info; - vector chunks; + deque chunks; }; struct DecodeSegment diff --git a/pv/views/decoder_output/QHexView.cpp b/pv/views/decoder_output/QHexView.cpp index ef57fd4c..ca2d91b5 100644 --- a/pv/views/decoder_output/QHexView.cpp +++ b/pv/views/decoder_output/QHexView.cpp @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -90,9 +91,11 @@ void QHexView::setData(const DecodeBinaryClass* data) { data_ = data; - data_size_ = 0; - for (const DecodeBinaryDataChunk& chunk : data_->chunks) - data_size_ += chunk.data.size(); + size_t size = 0; + size_t chunks = data_->chunks.size(); + for (size_t i = 0; i < chunks; i++) + size += data_->chunks[i].data.size(); + data_size_ = size; viewport()->update(); } @@ -124,16 +127,21 @@ void QHexView::initialize_byte_iterator(size_t offset) current_chunk_offset_ = 0; current_offset_ = offset; - for (const DecodeBinaryDataChunk& chunk : data_->chunks) - if (offset >= chunk.data.size()) { + size_t chunks = data_->chunks.size(); + for (size_t i = 0; i < chunks; i++) { + size_t size = data_->chunks[i].data.size(); + + if (offset >= size) { current_chunk_id_++; - offset -= chunk.data.size(); + offset -= size; } else { current_chunk_offset_ = offset; break; } + } - current_chunk_ = data_->chunks[current_chunk_id_]; + if (current_chunk_id_ < data_->chunks.size()) + current_chunk_ = data_->chunks[current_chunk_id_]; } uint8_t QHexView::get_next_byte(bool* is_next_chunk) @@ -141,12 +149,20 @@ uint8_t QHexView::get_next_byte(bool* is_next_chunk) if (is_next_chunk != nullptr) *is_next_chunk = (current_chunk_offset_ == 0); - uint8_t v = current_chunk_.data[current_chunk_offset_]; + uint8_t v = 0; + if (current_chunk_offset_ < current_chunk_.data.size()) + v = current_chunk_.data[current_chunk_offset_]; current_offset_++; current_chunk_offset_++; - if (current_chunk_offset_ == current_chunk_.data.size()) { + if (current_offset_ > data_size_) { + qWarning() << "QHexView::get_next_byte() overran binary data boundary:" << + current_offset_ << "of" << data_size_ << "bytes"; + return 0xEE; + } + + if ((current_chunk_offset_ == current_chunk_.data.size()) && (current_offset_ < data_size_)) { current_chunk_id_++; current_chunk_offset_ = 0; current_chunk_ = data_->chunks[current_chunk_id_]; diff --git a/pv/views/trace/analogsignal.cpp b/pv/views/trace/analogsignal.cpp index 6d4b2c67..faa80a11 100644 --- a/pv/views/trace/analogsignal.cpp +++ b/pv/views/trace/analogsignal.cpp @@ -1083,11 +1083,10 @@ void AnalogSignal::hover_point_changed(const QPoint &hp) if (hp.x() <= 0) { value_at_hover_pos_ = std::numeric_limits::quiet_NaN(); } else { - try { + if ((size_t)hp.x() < value_at_pixel_pos_.size()) value_at_hover_pos_ = value_at_pixel_pos_.at(hp.x()); - } catch (out_of_range&) { + else value_at_hover_pos_ = std::numeric_limits::quiet_NaN(); - } } } diff --git a/pv/views/trace/decodetrace.cpp b/pv/views/trace/decodetrace.cpp index 1d8c9b48..e14b4ecb 100644 --- a/pv/views/trace/decodetrace.cpp +++ b/pv/views/trace/decodetrace.cpp @@ -198,9 +198,11 @@ void DecodeTrace::paint_mid(QPainter &p, ViewItemPaintParams &pp) for (const Row& row : rows) { // Cache the row title widths int row_title_width; - try { - row_title_width = row_title_widths_.at(row); - } catch (out_of_range&) { + auto cached_width = row_title_widths_.find(row); + + if (cached_width != row_title_widths_.end()) + row_title_width = cached_width->second; + else { const int w = p.boundingRect(QRectF(), 0, row.title()).width() + RowTitleMargin; row_title_widths_[row] = w; -- 2.30.2