-
Notifications
You must be signed in to change notification settings - Fork 1
/
signaldata.cpp
136 lines (108 loc) · 2.76 KB
/
signaldata.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "signaldata.h"
#include <qvector.h>
#include <qmutex.h>
#include <qreadwritelock.h>
class SignalData::PrivateData
{
public:
PrivateData():
boundingRect( 1.0, 1.0, -2.0, -2.0 ) // invalid作废的
{
//保留的值量
values.reserve( 1000 );
}
//内联函数,添加点
inline void append( const QPointF &sample )
{
values.append( sample );
// adjust the bounding rectangle调整、适应边界正方形
if ( boundingRect.width() < 0 || boundingRect.height() < 0 )
{
boundingRect.setRect( sample.x(), sample.y(), 0.0, 0.0 );
}
else
{
boundingRect.setRight( sample.x() );
if ( sample.y() > boundingRect.bottom() )
boundingRect.setBottom( sample.y() );
if ( sample.y() < boundingRect.top() )
boundingRect.setTop( sample.y() );
}
}
QReadWriteLock lock;
QVector<QPointF> values;
//边界区域
QRectF boundingRect;
QMutex mutex; // protecting pendingValues
//悬而未决的值
QVector<QPointF> pendingValues;
};
SignalData::SignalData()
{
d_data = new PrivateData();
}
SignalData::~SignalData()
{
delete d_data;
}
int SignalData::size() const
{
return d_data->values.size();
}
QPointF SignalData::value( int index ) const
{
return d_data->values[index];
}
QRectF SignalData::boundingRect() const
{
return d_data->boundingRect;
}
void SignalData::lock()
{
d_data->lock.lockForRead();
}
void SignalData::unlock()
{
d_data->lock.unlock();
}
void SignalData::append( const QPointF &sample )
{
d_data->mutex.lock();
d_data->pendingValues += sample;
const bool isLocked = d_data->lock.tryLockForWrite();
if ( isLocked )
{
const int numValues = d_data->pendingValues.size();
const QPointF *pendingValues = d_data->pendingValues.data();
for ( int i = 0; i < numValues; i++ )
d_data->append( pendingValues[i] );
d_data->pendingValues.clear();
d_data->lock.unlock();
}
d_data->mutex.unlock();
}
void SignalData::clearStaleValues( double limit )
{
d_data->lock.lockForWrite();
d_data->boundingRect = QRectF( 1.0, 1.0, -2.0, -2.0 ); // invalid
const QVector<QPointF> values = d_data->values;
d_data->values.clear();
d_data->values.reserve( values.size() );
int index;
for ( index = values.size() - 1; index >= 0; index-- )
{
if ( values[index].x() < limit )
break;
}
if ( index > 0 )
d_data->append( values[index++] );
while ( index < values.size() - 1 )
d_data->append( values[index++] );
d_data->lock.unlock();
}
//引用接口
SignalData &SignalData::instance()
{
static SignalData valueVector;
return valueVector;
}