-
Notifications
You must be signed in to change notification settings - Fork 0
/
CEntity_CDouble.h
385 lines (342 loc) · 10.4 KB
/
CEntity_CDouble.h
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#ifndef __CENTITY_CDOUBLE_H__
#define __CENTITY_CDOUBLE_H__
/*
* File: CEntity_CDouble.h
* Brief: CEntity_CDouble class header
* Details: File contain CEntity_CDouble::CEntity definition.
* Author: Martin Bezecny
*/
#include <iostream>
#include <sstream>
#include <string>
#include <stdexcept>
#include <typeinfo>
#include <utility> // Due to: std::declval<CEntityBase>
#include "ClassInfo.h"
#include "CEntityBase.h"
#include "check.h"
/*
* Namespace for encapsulating of CDouble variant of CEntity class
* Details: For selecting this variant of CEntity class uncomment using namespace section in the CEntity.h
*/
namespace CEntity_CDouble
{
/*
* Method: CDouble class.
*Details: Definition of CDouble class.
*/
class CDouble {
double iVal; ///< Encapsulated double value.
public:
/*
* Method: Implicit c'tor
* Details: Value attribute is set to 0.
*/
CDouble() :iVal(0) {}
/*
* Method: Conversion c'tor
* Details: Value is set.
* Parameters: aVal New encapsulated double Value
*/
CDouble(double aVal) :iVal(aVal) {}
/*
* Method: Copy c'tor
* Details: Create new instance by copying only \p iVal parameter.
* Parameters: aVal Original instance for copying
*/
CDouble(const CDouble& aVal) :iVal(aVal.iVal) {}
/*
* Method: Assigment operator
* Return: CDouble instance iVal
*/
CDouble& operator=(const CDouble& aVal)
{
iVal = aVal.iVal;
return *this;
}
/*
* Method: Inverse operator
* Return: CDouble instance with inversed iVal value
*/
CDouble operator-() const
{
if (iVal == 0) return CDouble();
return CDouble(-iVal);
}
/*
* Method: Comparing by Value operator
* Return: Return bool result of comparation iVal values
*/
bool operator==(const CDouble& aValue) const
{
return iVal == aValue.iVal;
}
/*
* Method: Threeway comparison by Value operator
* Return: Return std::partial_ordering result of comparation
*/
std::partial_ordering operator<=>(const CDouble& aValue) const
{
return iVal <=> aValue.iVal;
}
/*
* Method: Output to the stream operator. (\em serialization)
* Parameters: aOStream Output stream
* Parameters: aValue Serialized instantions of CDouble
* Return: Return std::ostream with serialized CDouble
*/
friend std::ostream& operator<<(std::ostream& aOStream, const CDouble& aValue)
{
aOStream << aValue.iVal;
return aOStream;
}
/*
* Method: Input from the stream operator. (\em deserialization)
* Parameters: aIStream Input stream
* Return: Return rest of std::istream
*/
friend std::istream& operator>>(std::istream& aIStream, CDouble& aValue)
{
aIStream >> aValue.iVal;
return aIStream;
}
};
/*
* Method: CEntity class ( CDouble variant)
* Details: Definition of CEntity class ( CDouble variant). There are defined all common methods and attributes.
*/
class CEntity : public CEntityBase
{
ClassInfo<CEntity> iInstanceInfo; ///< Instance of the class info for usage statistics
CDouble iValue; ///< Encapsulated CDouble value
public:
// c'tors
/*
* Method: Implicit c'tor
* Details: Value attribute is set to 0.
*/
CEntity() : CEntityBase(), iValue(0)
{
}
/*
* Method: Conversion c'tor
* Details: Value is set. Pointer attributes are initialised by given values or implicitly to the this value (CEntityBase definition).
* Parameters: aValue New encapsulated CDouble Value
* Parameters: aNext Connection with next node (when needed)
* Parameters: aExtraInt Special value used for some container types
*/
explicit CEntity(const CDouble aValue, CEntity* aNext = nullptr, int aExtraInt = 0) :
CEntityBase(aNext, aExtraInt), iValue(aValue)
{
}
/*
* Method: Copy c'tor
* Details: Create new instance by copying only \p iValue parameter. Pointer attributes are initialised to the this value (CEntityBase definition).
* Parameters: aValue Original instance for copying
*/
CEntity(const CEntity& aValue) : CEntityBase(), iValue(aValue.Value())
{
}
/*
* Method: Conversion c'tor from plain C string (striped string)
* Details: Create new instance from Value in the string. Pointer attributes are initialised by given values or implicitly to the this value (CEntityBase definition).
* Parameters: aStr Plain C string with positive/negative decimal number or with 0.
* Parameters: aNext Connection with next node (when needed)
* Parameters: aExtraInt Special value used for some container types
*/
explicit CEntity(const char* aStr, CEntity* aNext = nullptr, int aExtraInt = 0) :
CEntityBase(aNext, aExtraInt), iValue(false)
{
std::istringstream iss(aStr, std::istringstream::in);
iss >> iValue;
}
/*
* Method: Conversion c'tor from std::string
* Details: Create new instance from Value in the string. Pointer attributes are initialised by given values or implicitly to the this value (CEntityBase definition).
* Parameters: aString std::string with positive/negative decimal number or with 0.
* Parameters: aNext Connection with next node (when needed)
* Parameters: aExtraInt Special value used for some container types
*/
explicit CEntity(const std::string& aString, CEntity* aNext = nullptr, int aExtraInt = 0) :
CEntity(aString.c_str(), aNext, aExtraInt)
{
}
// d'tor
/*
* Method: Virtual d'tor
*/
virtual ~CEntity() noexcept(noexcept(std::declval<CEntityBase>().~CEntityBase())) // noexcept(KCEntityDtorNoException)
{
iValue = 0;
}
// InstanceInfo getters
/*
* Method: ID getter
* Return: Unique instance ID
*/
size_t ID() const
{
return iInstanceInfo.ID();
}
// setter/getter
/*
* Method: Value setter
* Parameters: aValue New Value
*/
void SetValue(const CDouble& aValue)
{
iValue = aValue;
}
/*
* Method: Value getter
* Return: Actual CDouble \p Value
*/
CDouble Value() const
{
return iValue;
}
// operators
/*
* Method: Complement operator
* Return: CEntity instance with complemented attribute Value.
*/
CEntity operator-() const
{
return CEntity(-iValue);
}
/*
* Method: Assigment operator
* Return: CEntity instance with copied attribute Value.
*/
CEntity& operator=(const CEntity& aValue)
{
SetValue(aValue.Value());
return *this;
}
/*
* Method: Comparing by Value operator
* Return: Return bool result of comparation
*/
bool operator==(const CEntity& aValue) const
{
return iValue == aValue.Value();
}
#ifdef CENTITY_THREE_WAY_COMPARISON
/*
* Method: Threeway comparison by Value operator
* Return: Return std::partial_ordering result of comparation
*/
std::partial_ordering operator<=>(const CEntity& aValue) const
{
return iValue <=> aValue.Value();
}
#else
/*
* Method: Comparing by Value operator
* Return: Return CDouble result of comparation
*/
CDouble operator!=(const CEntity& aValue) const
{
return iValue != aValue.Value();
}
/*
* Method: Comparing by Value operator
* Return: Return CDouble result of comparation
*/
CDouble operator<(const CEntity& aValue) const
{
return iValue < aValue.Value();
}
/*
* Method: Comparing by Value operator
* Return: Return CDouble result of comparation
*/
CDouble operator>(const CEntity& aValue) const
{
return iValue > aValue.Value();
}
#endif /* CENTITY_THREE_WAY_COMPARISON */
/** Method: Output to the stream operator. (\em serialization)
* Parameters: aOStream Output stream
* Parameters: aValue Serialized instantion of CEntity
* Return: Return std::ostream with serialized Value
*/
friend std::ostream& operator<<(std::ostream& aOStream, const CEntity& aValue)
{
std::ios_base::fmtflags fmtflags = aOStream.setf(std::ios_base::boolalpha); // Save the state of the CDoublealpha flag & set it
aOStream << ' ' << aValue.Value();
aOStream.flags(fmtflags); // restore previous flags
return aOStream;
}
/** Method: Input from the stream operator. (\em deserialization)
* Parameters: aIStream Input stream
* \param[out] aValue Place for deserialized instantion of CEntity
* Return: Return rest of std::istream
*/
friend std::istream& operator>>(std::istream& aIStream, CEntity& aValue)
{
std::ios_base::fmtflags fmtflags = aIStream.setf(std::ios_base::boolalpha); // Save the state of the CDoublealpha flag & set it
aIStream >> aValue.iValue;
aIStream.flags(fmtflags); // restore previous flags
return aIStream;
}
#ifdef CENTITY_TEST_API
// constants for testing CEntity functionality
/** Method: First test value
* Return: Return CDouble value ( 0)
* \note Useful for automated testing of CEntity functionality
*/
static CDouble TestValue0()
{
return CDouble(0);
}
/** Method: First test string value
* Return: Return string with CDouble value ( 0)
* \note Useful for automated testing of CEntity functionality
*/
static std::string TestStringValue0()
{
return std::string("0");
}
/** Method: Second test value
* Return: Return CDouble value ( 1)
* \note Useful for automated testing of CEntity functionality
*/
static CDouble TestValue1()
{
return CDouble(1);
}
/** Method: Second test string value
* Return: Return string with CDouble value ( 1)
* \note Useful for automated testing of CEntity functionality
*/
static std::string TestStringValue1()
{
return std::string("1");
}
/** Method: Random test value
* Return: Return random CDouble value
* \note Useful for automated testing of CEntity functionality
*/
static CDouble TestValueRandom()
{
return CDouble(std::rand()%100);
}
/** Method: Random test string value
* Return: Return string with random CDouble value
* \note Useful for automated testing of CEntity functionality
*/
static std::string TestStringValueRandom()
{
return std::to_string(std::rand() % 100);
}
static std::string TestStringSet0() {
return "[0],[0.3],[0.4],[0.5],[0.6],[0.3]";
}
static std::string TestStringSet1() {
return "[1],[7.4],[9.6],[13.4],[15.6],[7.4]";
}
#endif /* CENTITY_TEST_API */
}; /* class CEntity */
} /* namespace CEntity_CDouble */
#endif /* __CENTITY_CDOUBLE_H__ */