-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertX.hpp
More file actions
418 lines (339 loc) · 11.6 KB
/
Copy pathConvertX.hpp
File metadata and controls
418 lines (339 loc) · 11.6 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#pragma once
#include <string>
/// @brief Platform-dependent string type used by `ConvertX`.
///
/// Resolves to `std::wstring` when `_UNICODE` is defined; otherwise resolves to
/// `std::string`.
#if _UNICODE
using tstring = std::wstring;
#else
using tstring = std::string;
#endif // _UNICODE
/// @brief Provides unit conversion helpers for supported conversion domains.
///
/// The current interface exposes time and text conversions through the nested
/// `Time` and `Text` helpers. `SmartConvert` selects the appropriate conversion
/// logic based on the supplied unit tags.
class ConvertX
{
public:
/// @brief Initializes a new `ConvertX` instance.
ConvertX();
/// @brief Destroys the `ConvertX` instance.
~ConvertX();
/// @brief Identifies the supported unit variants.
///
/// Values are grouped by conversion domain using sentinel markers so ranges
/// can be validated efficiently.
enum class UnitTag
{
/// @brief Start marker for time-based units.
TIMESTART = 0,
/// @brief Seconds.
Seconds = TIMESTART,
/// @brief Minutes.
Minutes,
/// @brief Hours.
Hours,
/// @brief Days.
Days,
/// @brief Weeks.
Weeks,
/// @brief Months.
///
/// Uses a fixed 30-day approximation.
Months,
/// @brief Years.
///
/// Uses a fixed 365-day approximation.
Years,
/// @brief Milliseconds.
Milliseconds,
/// @brief Microseconds.
Microseconds,
/// @brief End marker for time-based units.
TIMEEND = Microseconds,
/// @brief Start marker for text-based units.
TEXTSTART = TIMESTART + 32,
/// @brief Plain string text.
String = TEXTSTART,
/// @brief Unicode code points represented as text.
CodePoints,
/// @brief Binary text representation.
BinaryString,
/// @brief Octal text representation.
OctalString,
/// @brief Hexadecimal text representation.
HexadecimalString,
/// @brief End marker for text-based units.
TEXTEND = HexadecimalString,
};
/// @brief Identifies the high-level conversion domain.
enum class ConversionType
{
/// @brief Time conversion.
Time = 0,
/// @brief Text conversion.
Text,
/// @brief Color conversion.
Color,
/// @brief Temperature conversion.
Temperature,
/// @brief Data size conversion.
DataSize,
/// @brief Pressure conversion.
Pressure,
/// @brief Angle conversion.
Angle,
/// @brief Length conversion.
Length,
/// @brief Area conversion.
Area,
/// @brief Speed conversion.
Speed,
/// @brief Power conversion.
Power,
/// @brief Energy conversion.
Energy,
/// @brief Volume conversion.
Volume,
/// @brief Weight conversion.
Weight
};
/// @brief Converts a value between two unit tags using the appropriate domain.
///
/// @param value The source value to convert.
/// @param fromType The unit tag describing the source format or unit.
/// @param toType The unit tag describing the target format or unit.
/// @return The converted value as a `tstring`.
tstring SmartConvert(const tstring& value, UnitTag fromType, UnitTag toType);
/// @brief Determines which conversion domain applies to the supplied unit tags.
///
/// @param value The source value to inspect if needed by the implementation.
/// @param fromType The source unit tag.
/// @param toType The destination unit tag.
/// @return The detected `ConversionType`.
ConversionType DetectConversionType(const tstring& value, UnitTag fromType, UnitTag toType);
/// @brief Validates whether the supplied unit tags belong to the same supported domain.
///
/// @param fromType The source unit tag.
/// @param toType The destination unit tag.
/// @return `true` if the conversion is valid; otherwise, `false`.
bool IsValidUnits(UnitTag fromType, UnitTag toType);
/// @brief Time conversion helpers using seconds as the canonical base unit.
struct Time
{
public:
/// @brief Converts a time value between two time units.
///
/// @param value The source numeric value.
/// @param fromType The source time unit.
/// @param toType The destination time unit.
/// @return The converted numeric value.
static double Convert(double value, UnitTag fromType, UnitTag toType);
/// @brief Converts from seconds to a target time unit.
struct FromSeconds
{
public:
/// @brief Converts a seconds value to the specified time unit.
///
/// @param seconds The value in seconds.
/// @param toType The destination time unit.
/// @return The converted numeric value.
static double Convert(double seconds, UnitTag toType);
/// @brief Converts seconds to years.
///
/// Uses a fixed 365-day year.
///
/// @param seconds The value in seconds.
/// @return The value in years.
static double ToYears(double seconds);
/// @brief Converts seconds to months.
///
/// Uses a fixed 30-day month.
///
/// @param seconds The value in seconds.
/// @return The value in months.
static double ToMonths(double seconds);
/// @brief Converts seconds to weeks.
///
/// @param seconds The value in seconds.
/// @return The value in weeks.
static double ToWeeks(double seconds);
/// @brief Converts seconds to days.
///
/// @param seconds The value in seconds.
/// @return The value in days.
static double ToDays(double seconds);
/// @brief Converts seconds to hours.
///
/// @param seconds The value in seconds.
/// @return The value in hours.
static double ToHours(double seconds);
/// @brief Converts seconds to minutes.
///
/// @param seconds The value in seconds.
/// @return The value in minutes.
static double ToMinutes(double seconds);
/// @brief Converts seconds to milliseconds.
///
/// @param seconds The value in seconds.
/// @return The value in milliseconds.
static double ToMilliseconds(double seconds);
/// @brief Converts seconds to microseconds.
///
/// @param seconds The value in seconds.
/// @return The value in microseconds.
static double ToMicroseconds(double seconds);
};
/// @brief Converts supported time units to seconds.
struct ToSeconds
{
/// @brief Converts a value from the specified time unit to seconds.
///
/// @param value The source numeric value.
/// @param fromType The source time unit.
/// @return The value in seconds.
static double Convert(double value, UnitTag fromType);
/// @brief Converts years to seconds.
///
/// Uses a fixed 365-day year.
///
/// @param years The value in years.
/// @return The value in seconds.
static double FromYears(double years);
/// @brief Converts months to seconds.
///
/// Uses a fixed 30-day month.
///
/// @param months The value in months.
/// @return The value in seconds.
static double FromMonths(double months);
/// @brief Converts weeks to seconds.
///
/// @param weeks The value in weeks.
/// @return The value in seconds.
static double FromWeeks(double weeks);
/// @brief Converts days to seconds.
///
/// @param days The value in days.
/// @return The value in seconds.
static double FromDays(double days);
/// @brief Converts hours to seconds.
///
/// @param hours The value in hours.
/// @return The value in seconds.
static double FromHours(double hours);
/// @brief Converts minutes to seconds.
///
/// @param minutes The value in minutes.
/// @return The value in seconds.
static double FromMinutes(double minutes);
/// @brief Converts milliseconds to seconds.
///
/// @param milliseconds The value in milliseconds.
/// @return The value in seconds.
static double FromMilliseconds(double milliseconds);
/// @brief Converts microseconds to seconds.
///
/// @param microseconds The value in microseconds.
/// @return The value in seconds.
static double FromMicroseconds(double microseconds);
};
/// @brief Number of seconds in one microsecond.
constexpr static double SecondsInMicrosecond = 0.000001;
/// @brief Number of seconds in one millisecond.
constexpr static double SecondsInMillisecond = 0.001;
/// @brief Number of seconds in one minute.
constexpr static double SecondsInMinute = 60.0;
/// @brief Number of seconds in one hour.
constexpr static double SecondsInHour = 3600.0;
/// @brief Number of seconds in one day.
constexpr static double SecondsInDay = 86400.0;
/// @brief Number of seconds in one week.
constexpr static double SecondsInWeek = 604800.0;
/// @brief Number of seconds in one month.
///
/// Uses a fixed 30-day month approximation.
constexpr static double SecondsInMonth = 2592000.0;
/// @brief Number of seconds in one year.
///
/// Uses a fixed 365-day year approximation.
constexpr static double SecondsInYear = 31536000.0;
};
/// @brief Text conversion helpers using plain string data as the canonical base form.
struct Text
{
public:
/// @brief Converts text between supported textual representations.
///
/// @param value The source text.
/// @param fromType The source text representation.
/// @param toType The destination text representation.
/// @return The converted text.
static tstring Convert(const tstring& value, UnitTag fromType, UnitTag toType);
/// @brief Converts from plain string text to other text representations.
struct FromString
{
public:
/// @brief Converts a plain string to the specified text representation.
///
/// @param value The source plain string.
/// @param toType The destination text representation.
/// @return The converted text.
static tstring Convert(const tstring& value, UnitTag toType);
/// @brief Converts a plain string to a code point representation.
///
/// @param value The source plain string.
/// @return The code point representation.
static tstring ToCodePoints(const tstring& value);
/// @brief Converts a plain string to a binary representation.
///
/// @param value The source plain string.
/// @return The binary representation.
static tstring ToBinaryString(const tstring& value);
/// @brief Converts a plain string to an octal representation.
///
/// @param value The source plain string.
/// @return The octal representation.
static tstring ToOctalString(const tstring& value);
/// @brief Converts a plain string to a hexadecimal representation.
///
/// @param value The source plain string.
/// @return The hexadecimal representation.
static tstring ToHexadecimalString(const tstring& value);
};
/// @brief Converts supported text representations back to plain string text.
struct ToString
{
public:
/// @brief Converts a text representation to a plain string.
///
/// @param value The source text representation.
/// @param fromType The source text representation type.
/// @return The decoded plain string.
static tstring Convert(const tstring& value, UnitTag fromType);
/// @brief Converts a code point representation to a plain string.
///
/// @param value The code point representation.
/// @return The decoded plain string.
static tstring FromCodePoints(const tstring& value);
/// @brief Converts a binary representation to a plain string.
///
/// @param value The binary representation.
/// @return The decoded plain string.
static tstring FromBinaryString(const tstring& value);
/// @brief Converts an octal representation to a plain string.
///
/// @param value The octal representation.
/// @return The decoded plain string.
static tstring FromOctalString(const tstring& value);
/// @brief Converts a hexadecimal representation to a plain string.
///
/// @param value The hexadecimal representation.
/// @return The decoded plain string.
static tstring FromHexadecimalString(const tstring& value);
};
};
private:
};