-
Notifications
You must be signed in to change notification settings - Fork 3
/
CalculatorLogic.hpp
227 lines (180 loc) · 5.89 KB
/
CalculatorLogic.hpp
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#ifndef CALCULATOR_LOGIC_HPP
#define CALCULATOR_LOGIC_HPP
// Include QT API
#include <QString>
#include <QObject>
/**
* CalculatorLogic - handles calculator logic (div, sub, mult, add, etc).
*
* @version 1.0
* @since 19.07.2019
* @authors Denis Z. (code4un@yandex.ru)
**/
class CalculatorLogic : public QObject
{
// -----------------------------------------------------------
// ===========================================================
// QT MACROS
// ===========================================================
Q_OBJECT
// -----------------------------------------------------------
private:
// -----------------------------------------------------------
// ===========================================================
// CONSTANTS
// ===========================================================
/** NONE **/
static const unsigned char NONE_OPERATION_TYPE = 0;
/** + **/
static const unsigned char SUM_OPERATION_TYPE = 1;
/** - **/
static const unsigned char SUB_OPERATION_TYPE = 2;
/** / **/
static const unsigned char DIV_OPERATION_TYPE = 3;
/** * **/
static const unsigned char MUL_OPERATION_TYPE = 4;
/** sqrt **/
static const unsigned char SQRT_OPERATION_TYPE = 5;
// ===========================================================
// FIELDS
// ===========================================================
/** Stored Value. **/
QString mStoredValue;
/** First argument. **/
QString mLeftArg;
/** Right Argument **/
QString mRightArg;
/** Cached Output-String. **/
QString mOutput;
/** Operation-Type **/
unsigned char mOperationType;
// ===========================================================
// METHODS
// ===========================================================
/**
* Updates Output-string.
*
* @thread_safety - not thread-safe.
* @throws - no exceptions.
**/
void onUpdateOutput( ) noexcept;
/**
* Called to clear (reset) state after calculation.
*
* (?) Unlikely #resetLogic() this method doesn't resets
* stored items & output.
*
* @thread_safety - not thread-safe.
* @throws - no exceptions.
**/
void onCalculationDone( ) noexcept;
// -----------------------------------------------------------
public:
// -----------------------------------------------------------
// ===========================================================
// CONSTRUCTOR
// ===========================================================
/**
* CalculatorLogic constructor.
*
* @param qParent - parent-QObject, default is null.
* @throws - no exceptions.
**/
explicit CalculatorLogic( QObject *const qParent = nullptr ) noexcept;
// ===========================================================
// DESTRUCTOR
// ===========================================================
/**
* CalculatorLogic destructor.
*
* @throws - no exceptions.
**/
virtual ~CalculatorLogic( ) noexcept;
// ===========================================================
// GETTERS & SETTERS
// ===========================================================
/**
* Called when operation (div, sub, sum, etc) called.
*
* @thread_safety - not thread-safe.
* @param pType - operation-type in string-format ("+", "/", etc).
* @returns - output-string to be displayed.
* @throws - no exceptions.
**/
Q_INVOKABLE QString setOperationType( const QString pType ) noexcept;
// ===========================================================
// METHODS
// ===========================================================
/**
* Called when dot (separator) requested.
*
* @thread_safety - not thread-safe.
* @returns - output-string to be displayed.
* @throws - no exceptions.
**/
Q_INVOKABLE QString onDot( ) noexcept;
/**
* Called when QML Keyboard Inuput-Event handled.
*
* @thread_safety - not thread-safe.
* @returns - output-string to be displayed.
* @throws - no exceptions.
**/
Q_INVOKABLE QString onKeyboardInput( const QString pValue ) noexcept;
/**
* Called when number added to argument.
*
* @thread_safety - not thread-safe.
* @param pNumber - number to add.
* @returns - output-string to be displayed.
* @throws - no exceptions.
**/
Q_INVOKABLE QString onNumberInput( const QString pNumber ) noexcept;
/**
* Called to replace Right-Argument with Memory-Item.
*
* @thread_safety - not thread-safe.
* @returns - output-string to be displayed.
* @throws - no exceptions.
**/
Q_INVOKABLE QString onMR( ) noexcept;
/**
* Sets Memory-Item.
*
* @thread_safety - not thread-safe.
* @throws - no exceptions.
**/
Q_INVOKABLE void onMS( ) noexcept;
/**
* Called when last argument-number requested (erase, chop).
*
* @thread_safety - not thread-safe.
* @returns - output-string to be displayed.
* @throws - no exceptions.
**/
Q_INVOKABLE QString onRemoveLastNumber( ) noexcept;
/**
* Calculates result using two arguments & operation-type.
*
* @thread_safety - not thread-safe.
* @return - result in string-type.
* @throws - no exceptions.
**/
Q_INVOKABLE QString doMath( ) noexcept;
/**
* Resets stored values.
*
* @thread_safety - not thread-safe.
* @throws - no exceptions.
**/
Q_INVOKABLE void resetMemory( ) noexcept;
/**
* Reset Calculator Logic State.
*
* @thread_safety - not thread-safe.
* @throws - no exceptions.
**/
Q_INVOKABLE void resetLogic( ) noexcept;
// -----------------------------------------------------------
};
#endif // !CALCULATOR_LOGIC_HPP