]> sigrok.org Git - pulseview.git/blob - pv/data/decode/rowdata.cpp
Rework decoder infrastructure
[pulseview.git] / pv / data / decode / rowdata.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <pv/data/decode/decoder.hpp>
21 #include <pv/data/decode/row.hpp>
22 #include <pv/data/decode/rowdata.hpp>
23
24 using std::vector;
25
26 namespace pv {
27 namespace data {
28 namespace decode {
29
30 RowData::RowData(Row* row) :
31         row_(row)
32 {
33         assert(row);
34 }
35
36 uint64_t RowData::get_max_sample() const
37 {
38         if (annotations_.empty())
39                 return 0;
40         return annotations_.back().end_sample();
41 }
42
43 uint64_t RowData::get_annotation_count() const
44 {
45         return annotations_.size();
46 }
47
48 void RowData::get_annotation_subset(
49         vector<pv::data::decode::Annotation> &dest,
50         uint64_t start_sample, uint64_t end_sample) const
51 {
52         // Determine whether we must apply per-class filtering or not
53         bool all_ann_classes_enabled = true;
54         bool all_ann_classes_disabled = true;
55
56         uint32_t max_ann_class_id = 0;
57         for (AnnotationClass* c : row_->ann_classes()) {
58                 if (!c->visible)
59                         all_ann_classes_enabled = false;
60                 else
61                         all_ann_classes_disabled = false;
62                 if (c->id > max_ann_class_id)
63                         max_ann_class_id = c->id;
64         }
65
66         if (all_ann_classes_enabled) {
67                 // No filtering, send everyting out as-is
68                 for (const auto& annotation : annotations_)
69                         if ((annotation.end_sample() > start_sample) &&
70                                 (annotation.start_sample() <= end_sample))
71                                 dest.push_back(annotation);
72         } else {
73                 if (!all_ann_classes_disabled) {
74                         // Filter out invisible annotation classes
75                         vector<size_t> class_visible;
76                         class_visible.resize(max_ann_class_id + 1, 0);
77                         for (AnnotationClass* c : row_->ann_classes())
78                                 if (c->visible)
79                                         class_visible[c->id] = 1;
80
81                         for (const auto& annotation : annotations_)
82                                 if ((annotation.end_sample() > start_sample) &&
83                                         (annotation.start_sample() <= end_sample) &&
84                                         (class_visible[annotation.ann_class()]))
85                                         dest.push_back(annotation);
86                 }
87         }
88 }
89
90 void RowData::emplace_annotation(srd_proto_data *pdata)
91 {
92         annotations_.emplace_back(pdata, row_);
93 }
94
95 }  // namespace decode
96 }  // namespace data
97 }  // namespace pv