]> sigrok.org Git - pulseview.git/blob - pv/data/segment.hpp
Implement single sample value getters
[pulseview.git] / pv / data / segment.hpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
5  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #ifndef PULSEVIEW_PV_DATA_SEGMENT_HPP
22 #define PULSEVIEW_PV_DATA_SEGMENT_HPP
23
24 #include "pv/util.hpp"
25
26 #include <mutex>
27 #include <thread>
28 #include <deque>
29
30 #include <QObject>
31
32 using std::recursive_mutex;
33 using std::deque;
34
35 namespace SegmentTest {
36 struct SmallSize8Single;
37 struct MediumSize8Single;
38 struct MaxSize8Single;
39 struct MediumSize24Single;
40 struct MediumSize32Single;
41 struct MaxSize32Single;
42 struct MediumSize32Multi;
43 struct MaxSize32Multi;
44 struct MaxSize32MultiAtOnce;
45 struct MaxSize32MultiIterated;
46 }  // namespace SegmentTest
47
48 namespace pv {
49 namespace data {
50
51 typedef struct {
52         uint64_t sample_index, chunk_num, chunk_offs;
53         uint8_t* chunk;
54 } SegmentDataIterator;
55
56 class Segment : public QObject
57 {
58         Q_OBJECT
59
60 private:
61         static const uint64_t MaxChunkSize;
62
63 public:
64         Segment(uint32_t segment_id, uint64_t samplerate, unsigned int unit_size);
65
66         virtual ~Segment();
67
68         uint64_t get_sample_count() const;
69
70         const pv::util::Timestamp& start_time() const;
71
72         double samplerate() const;
73         void set_samplerate(double samplerate);
74
75         unsigned int unit_size() const;
76
77         uint32_t segment_id() const;
78
79         void set_complete();
80         bool is_complete() const;
81
82         void free_unused_memory();
83
84 protected:
85         void append_single_sample(void *data);
86         void append_samples(void *data, uint64_t samples);
87         const uint8_t* get_raw_sample(uint64_t sample_num) const;
88         void get_raw_samples(uint64_t start, uint64_t count, uint8_t *dest) const;
89
90         SegmentDataIterator* begin_sample_iteration(uint64_t start);
91         void continue_sample_iteration(SegmentDataIterator* it, uint64_t increase);
92         void end_sample_iteration(SegmentDataIterator* it);
93         uint8_t* get_iterator_value(SegmentDataIterator* it);
94         uint64_t get_iterator_valid_length(SegmentDataIterator* it);
95
96         uint32_t segment_id_;
97         mutable recursive_mutex mutex_;
98         deque<uint8_t*> data_chunks_;
99         uint8_t* current_chunk_;
100         uint64_t used_samples_, unused_samples_;
101         uint64_t sample_count_;
102         pv::util::Timestamp start_time_;
103         double samplerate_;
104         uint64_t chunk_size_;
105         unsigned int unit_size_;
106         int iterator_count_;
107         bool mem_optimization_requested_;
108         bool is_complete_;
109
110         friend struct SegmentTest::SmallSize8Single;
111         friend struct SegmentTest::MediumSize8Single;
112         friend struct SegmentTest::MaxSize8Single;
113         friend struct SegmentTest::MediumSize24Single;
114         friend struct SegmentTest::MediumSize32Single;
115         friend struct SegmentTest::MaxSize32Single;
116         friend struct SegmentTest::MediumSize32Multi;
117         friend struct SegmentTest::MaxSize32Multi;
118         friend struct SegmentTest::MaxSize32MultiAtOnce;
119         friend struct SegmentTest::MaxSize32MultiIterated;
120 };
121
122 } // namespace data
123 } // namespace pv
124
125 typedef std::shared_ptr<pv::data::Segment> SharedPtrToSegment;
126
127 Q_DECLARE_METATYPE(SharedPtrToSegment);
128
129 #endif // PULSEVIEW_PV_DATA_SEGMENT_HPP