-
Notifications
You must be signed in to change notification settings - Fork 1
/
kmatrix.cpp
172 lines (147 loc) · 3.37 KB
/
kmatrix.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "kmatrix.h"
const static KValue EMPTY_K = {0, 0, 0, 0, 0};
KMatrix::KMatrix(QObject *parent) : QObject(parent)
{
this->value = EMPTY_K;
}
bool KMatrix::isEmpty() const
{
QReadLocker locker(&this->rwLock);
return this->value == EMPTY_K;
}
void KMatrix::clear()
{
this->setValue(EMPTY_K);
}
double KMatrix::fx() const
{
QReadLocker locker(&this->rwLock);
return this->value.fx;
}
double KMatrix::fy() const
{
QReadLocker locker(&this->rwLock);
return this->value.fy;
}
double KMatrix::x0() const
{
QReadLocker locker(&this->rwLock);
return this->value.x0;
}
double KMatrix::y0() const
{
QReadLocker locker(&this->rwLock);
return this->value.y0;
}
double KMatrix::s() const
{
QReadLocker locker(&this->rwLock);
return this->value.s;
}
KValue KMatrix::getValue() const
{
QReadLocker locker(&this->rwLock);
return this->value;
}
static inline bool lockAndSet(double &var, double value, QReadWriteLock *rwlock)
{
QWriteLocker lock(rwlock);
if (value != var) {
var = value;
return true;
}
return false;
}
static inline bool lockAndSet(KValue &var, double fx, double fy, double x0, double y0, double s,
QReadWriteLock *rwlock)
{
QWriteLocker lock(rwlock);
bool result = false;
if (fx != var.fx) {
var.fx = fx;
result = true;
}
if (fy != var.fy) {
var.fy = fy;
result = true;
}
if (x0 != var.x0) {
var.x0 = x0;
result = true;
}
if (y0 != var.y0) {
var.y0 = y0;
result = true;
}
if (s != var.s) {
var.s = s;
result = true;
}
return result;
}
static inline bool lockAndSet(double &varX, double &varY, double x, double y,
QReadWriteLock *rwlock)
{
QWriteLocker lock(rwlock);
bool result = false;
if (x != varX) {
varX = x;
result = true;
}
if (y != varY) {
varY = y;
result = true;
}
return result;
}
void KMatrix::setFx(double fx)
{
if (lockAndSet(this->value.fx, fx, &this->rwLock))
emit this->dataChanged();
}
void KMatrix::setFy(double fy)
{
if (lockAndSet(this->value.fy, fy, &this->rwLock))
emit this->dataChanged();
}
void KMatrix::setX0(double x0)
{
if (lockAndSet(this->value.x0, x0, &this->rwLock))
emit this->dataChanged();
}
void KMatrix::setY0(double y0)
{
if (lockAndSet(this->value.y0, y0, &this->rwLock))
emit this->dataChanged();
}
void KMatrix::setS(double s)
{
if (lockAndSet(this->value.s, s, &this->rwLock))
emit this->dataChanged();
}
void KMatrix::setValue(const KValue &value)
{
if (lockAndSet(this->value, value.fx, value.fy, value.x0, value.y0, value.s, &this->rwLock))
emit this->dataChanged();
}
void KMatrix::setValue(double fx, double fy, double x0, double y0, double s)
{
if (lockAndSet(this->value, fx, fy, x0, y0, s, &this->rwLock))
emit this->dataChanged();
}
KMatrix &KMatrix::operator =(const KValue &value)
{
this->setValue(value);
return *this;
}
bool KMatrix::operator ==(const KValue &value) const
{
QReadLocker lock(&this->rwLock);
return this->value == value;
}
bool KMatrix::operator ==(const KMatrix &other) const
{
QReadLocker lock(&this->rwLock);
QReadLocker otherLock(&other.rwLock);
return this->value == other.value;
}