Various binary output-related changes
authorSoeren Apel <soeren@apelpie.net>
Sun, 15 Dec 2019 16:20:51 +0000 (17:20 +0100)
committerUwe Hermann <uwe@hermann-uwe.de>
Mon, 16 Dec 2019 23:19:57 +0000 (00:19 +0100)
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
pv/data/decodesignal.hpp
pv/views/decoder_output/QHexView.cpp
pv/views/trace/analogsignal.cpp
pv/views/trace/decodetrace.cpp

index 33861ea55408d482ceb138c318545cdad5723497..00812917013ae361f739ea649dbf9611792bef0e 100644 (file)
@@ -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<mutex> lock(output_mutex_);
 
-       try {
-               const DecodeSegment *segment = &(segments_.at(segment_id));
-               const map<const decode::Row, decode::RowData> *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<const decode::Row, decode::RowData> *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<DecodeBinaryDataChunk>()});
+                               {dec.get(), dec->get_binary_class(i), deque<DecodeBinaryDataChunk>()});
        }
 }
 
index 1fe696ebe329003397a731bc1725598f94a187a2..e591eb010bfb007f357f5c28b5961f1ac822669c 100644 (file)
@@ -21,6 +21,7 @@
 #define PULSEVIEW_PV_DATA_DECODESIGNAL_HPP
 
 #include <atomic>
+#include <deque>
 #include <condition_variable>
 #include <unordered_set>
 #include <vector>
@@ -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<DecodeBinaryDataChunk> chunks;
+       deque<DecodeBinaryDataChunk> chunks;
 };
 
 struct DecodeSegment
index ef57fd4c700a31974734568779baadda279bf1b5..ca2d91b572b7efe3b7ecc2c14b56f6921e4357d8 100644 (file)
@@ -29,6 +29,7 @@
 
 #include <QApplication>
 #include <QClipboard>
+#include <QDebug>
 #include <QFont>
 #include <QKeyEvent>
 #include <QScrollBar>
@@ -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_];
index 6d4b2c67962a24bf4405f42e3c184d6dc60fce35..faa80a11bc94243bd277755770cc5e00c9e0b482 100644 (file)
@@ -1083,11 +1083,10 @@ void AnalogSignal::hover_point_changed(const QPoint &hp)
        if (hp.x() <= 0) {
                value_at_hover_pos_ = std::numeric_limits<float>::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<float>::quiet_NaN();
-               }
        }
 }
 
index 1d8c9b484d25fd560aa5a52b65de532c8fd75285..e14b4ecbf433469467c5b820eabf534aac04e1c7 100644 (file)
@@ -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;