forked from sttp/cppapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonTypes.h
More file actions
515 lines (434 loc) · 17.5 KB
/
CommonTypes.h
File metadata and controls
515 lines (434 loc) · 17.5 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//******************************************************************************************************
// CommonTypes.h - Gbtc
//
// Copyright © 2019, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
// file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 03/09/2012 - Stephen C. Wills
// Generated original version of source code.
//
//******************************************************************************************************
// ReSharper disable CppClangTidyCppcoreguidelinesMacroUsage
// ReSharper disable CppClangTidyBugproneReservedIdentifier
#pragma once
#include <climits>
#include <cstddef>
#include <map>
#include <unordered_map>
#include <string>
// Reduce boost code analysis warnings
#pragma warning(push)
#pragma warning(disable:4068)
#pragma warning(disable:6001)
#pragma warning(disable:6031)
#pragma warning(disable:6255)
#pragma warning(disable:6258)
#pragma warning(disable:6386)
#pragma warning(disable:6387)
#pragma warning(disable:26439)
#pragma warning(disable:26444)
#pragma warning(disable:26451)
#pragma warning(disable:26495)
#pragma warning(disable:26498)
#pragma warning(disable:26812)
#pragma warning(disable:26819)
#if defined(_MSC_VER)
#pragma clang diagnostic ignored "-Wunknown-pragmas"
#pragma diag_suppress 70
#pragma diag_suppress 1440
#endif
#include <boost/any.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/exception/exception.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/thread.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/thread/locks.hpp>
// Fix boost v1.75 duplicate symbol issue for MSVC
#if defined(_MSC_VER) && BOOST_VERSION == 107500
#include "boost/use_awaitable.hpp"
#endif
#include <boost/asio.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ip/udp.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/device/array.hpp>
#pragma warning(pop)
#if _WIN32 || _WIN64
#if _WIN64
#define _32BIT 0
#define _64BIT 1
#else
#define _32BIT 1
#define _64BIT 0
#endif
#endif
#if __GNUC__ // NOLINT
#if __x86_64__ || __ppc64__
#define _32BIT 0
#define _64BIT 1
#else
#define _32BIT 1
#define _64BIT 0
#endif
#endif
#define BOOST_LEGACY (BOOST_VERSION < 106600)
namespace sttp
{
static_assert(sizeof(float) * CHAR_BIT == 32, "float not defined as 32-bits");
static_assert(sizeof(double) * CHAR_BIT == 64, "double not defined as 64-bits");
// Floating-point types
typedef float float32_t;
typedef double float64_t;
typedef boost::multiprecision::cpp_dec_float_100 decimal_t;
static const float64_t NaN = std::numeric_limits<double>::quiet_NaN();
// DateTime type
typedef boost::posix_time::ptime datetime_t;
struct Int8
{
static constexpr int8_t MaxValue = static_cast<int8_t>(127);
static constexpr int8_t MinValue = static_cast<int8_t>(-128);
};
struct UInt8
{
static constexpr uint8_t MaxValue = static_cast<uint8_t>(255);
static constexpr uint8_t MinValue = static_cast<uint8_t>(0);
};
struct Int16
{
static constexpr int16_t MaxValue = static_cast<int16_t>(32767);
static constexpr int16_t MinValue = static_cast<int16_t>(-32768);
};
struct UInt16
{
static constexpr uint16_t MaxValue = static_cast<uint16_t>(65535);
static constexpr uint16_t MinValue = static_cast<uint16_t>(0);
};
struct Int32
{
static constexpr int32_t MaxValue = static_cast<int32_t>(2147483647);
static constexpr int32_t MinValue = static_cast<int32_t>(-2147483647) - 1;
};
struct UInt32
{
static constexpr uint32_t MaxValue = static_cast<uint32_t>(4294967295U);
static constexpr uint32_t MinValue = static_cast<uint32_t>(0U);
};
struct Int64
{
static constexpr int64_t MaxValue = static_cast<int64_t>(9223372036854775807LL);
static constexpr int64_t MinValue = static_cast<int64_t>(-9223372036854775807LL) - 1LL;
};
struct UInt64
{
static constexpr uint64_t MaxValue = static_cast<uint64_t>(18446744073709551615ULL);
static constexpr uint64_t MinValue = static_cast<uint64_t>(0ULL);
};
struct Decimal
{
// ReSharper disable CppVariableCanBeMadeConstexpr
static const decimal_t MaxValue;
static const decimal_t MinValue;
static const decimal_t DotNetMaxValue;
static const decimal_t DotNetMinValue;
// ReSharper restore CppVariableCanBeMadeConstexpr
};
struct DateTime
{
static const datetime_t MaxValue;
static const datetime_t MinValue;
};
struct Ticks
{
static constexpr int64_t MaxValue = 3155378975999999999LL; // 12/31/1999 11:59:59.999
static constexpr int64_t MinValue = 0LL; // 01/01/0001 00:00:00.000
static constexpr int64_t UnixBaseOffset = 621355968000000000LL; // 01/01/1970 00:00:00.000
static constexpr int64_t PTimeBaseOffset = 441481536000000000LL; // 01/01/1400 00:00:00.000
static constexpr int64_t PerSecond = 10000000LL;
static constexpr int64_t PerMillisecond = Ticks::PerSecond / 1000LL;
static constexpr int64_t PerMicrosecond = Ticks::PerSecond / 1000000LL;
static constexpr int64_t PerMinute = 60LL * Ticks::PerSecond;
static constexpr int64_t PerHour = 60LL * Ticks::PerMinute;
static constexpr int64_t PerDay = 24LL * Ticks::PerHour;
// Flag (64th bit) that marks a Ticks value as a leap second, i.e., second 60 (one beyond normal second 59).
static constexpr int64_t LeapSecondFlag = 1LL << 63; // NOLINT(clang-diagnostic-shift-sign-overflow)
// Flag (63rd bit) that indicates if leap second is positive or negative; 0 for add, 1 for delete.
static constexpr int64_t LeapSecondDirection = 1LL << 62;
static constexpr int64_t ValueMask = ~LeapSecondFlag & ~LeapSecondDirection;
};
inline int32_t ConvertInt32(const size_t value)
{
#if _64BIT
if (value > static_cast<size_t>(Int32::MaxValue))
throw std::runtime_error("Cannot cast size_t value to int32_t: value is out of range");
#endif
return static_cast<int32_t>(value);
}
inline uint32_t ConvertUInt32(const size_t value)
{
#if _64BIT
if (value > static_cast<size_t>(UInt32::MaxValue))
throw std::runtime_error("Cannot cast size_t value to uint32_t: value is out of range");
#endif
return static_cast<uint32_t>(value);
}
template<class T>
using SharedPtr = boost::shared_ptr<T>;
template<class T>
using EnableSharedThisPtr = boost::enable_shared_from_this<T>;
template<class T>
SharedPtr<T> NewSharedPtr()
{
return boost::make_shared<T>();
}
template<class T, class P1>
SharedPtr<T> NewSharedPtr(P1 p1)
{
return boost::make_shared<T>(p1);
}
template<class T, class P1, class P2>
SharedPtr<T> NewSharedPtr(P1 p1, P2 p2)
{
return boost::make_shared<T>(p1, p2);
}
template<class T, class P1, class P2, class P3>
SharedPtr<T> NewSharedPtr(P1 p1, P2 p2, P3 p3)
{
return boost::make_shared<T>(p1, p2, p3);
}
template<class T, class P1, class P2, class P3, class P4>
SharedPtr<T> NewSharedPtr(P1 p1, P2 p2, P3 p3, P4 p4)
{
return boost::make_shared<T>(p1, p2, p3, p4);
}
template<class T, class P1, class P2, class P3, class P4, class P5>
SharedPtr<T> NewSharedPtr(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
{
return boost::make_shared<T>(p1, p2, p3, p4, p5);
}
template<class T, class S>
SharedPtr<T> CastSharedPtr(SharedPtr<S> const& source)
{
return boost::dynamic_pointer_cast<T>(source);
}
typedef boost::any Object;
typedef boost::uuids::uuid Guid;
typedef boost::posix_time::time_duration TimeSpan;
typedef boost::posix_time::milliseconds Milliseconds;
typedef boost::posix_time::microseconds Microseconds;
typedef boost::system::error_code ErrorCode;
typedef boost::system::system_error SystemError;
typedef boost::exception Exception;
typedef boost::thread Thread;
typedef boost::mutex Mutex;
typedef boost::shared_mutex SharedMutex;
typedef boost::condition_variable WaitHandle;
typedef boost::lock_guard<Mutex> ScopeLock;
typedef boost::unique_lock<Mutex> UniqueLock;
typedef boost::unique_lock<SharedMutex> WriterLock;
typedef boost::shared_lock<SharedMutex> ReaderLock;
#if BOOST_LEGACY
typedef boost::asio::io_service IOContext;
typedef boost::asio::io_service::strand Strand;
#else
typedef boost::asio::io_context IOContext;
typedef boost::asio::io_context::strand Strand;
#endif
typedef boost::asio::steady_timer SteadyTimer;
typedef boost::asio::ip::address IPAddress;
typedef boost::asio::ip::tcp::socket TcpSocket;
typedef boost::asio::ip::udp::socket UdpSocket;
typedef boost::asio::ip::tcp::acceptor TcpAcceptor;
typedef boost::asio::ip::tcp::endpoint TcpEndPoint;
typedef boost::asio::ip::tcp::resolver DnsResolver;
typedef boost::iostreams::filtering_streambuf<boost::iostreams::input> StreamBuffer;
typedef boost::iostreams::gzip_decompressor GZipDecompressor;
typedef boost::iostreams::gzip_compressor GZipCompressor;
typedef SharedPtr<Thread> ThreadPtr;
static const ThreadPtr ThreadNullPtr;
#define ThreadSleep(ms) boost::this_thread::sleep(boost::posix_time::milliseconds(ms))
#if BOOST_LEGACY
#define bind_executor(ex, ...) ex.wrap(__VA_ARGS__)
#define post(ex, ...) ex.post(__VA_ARGS__)
#endif
// Empty types
struct Empty
{
static const std::string String;
static const sttp::datetime_t DateTime;
static const sttp::Guid Guid;
static const sttp::Object Object;
static const sttp::IPAddress IPAddress;
};
// std::unordered_map string hasher
struct StringHash : std::unary_function<std::string, size_t>
{
size_t operator()(const std::string& value) const;
};
// std::unordered_map string equality tester
struct StringEqual : std::binary_function<std::string, std::string, bool>
{
bool operator()(const std::string& left, const std::string& right) const;
};
template<class T>
using StringMap = std::unordered_map<std::string, T, StringHash, StringEqual>;
// std::map string comparer
struct StringComparer : std::binary_function<std::string, std::string, bool>
{
bool operator()(const std::string& left, const std::string& right) const;
};
template<class T>
using SortedStringMap = std::map<std::string, T, StringComparer>;
template<class TKey, class TValue>
bool TryGetValue(const std::map<TKey, TValue>& dictionary, const TKey& key, TValue& value, const TValue& defaultValue)
{
auto iterator = dictionary.find(key);
if (iterator != dictionary.end())
{
value = iterator->second;
return true;
}
value = defaultValue;
return false;
}
template<class TKey, class TValue>
bool TryGetValue(const std::unordered_map<TKey, TValue>& dictionary, const TKey& key, TValue& value, const TValue& defaultValue)
{
auto iterator = dictionary.find(key);
if (iterator != dictionary.end())
{
value = iterator->second;
return true;
}
value = defaultValue;
return false;
}
template<class TValue>
bool TryGetValue(const StringMap<TValue>& dictionary, const std::string& key, TValue& value, const TValue& defaultValue)
{
auto iterator = dictionary.find(key);
if (iterator != dictionary.end())
{
value = iterator->second;
return true;
}
value = defaultValue;
return false;
}
inline bool TryGetValue(const StringMap<std::string>& dictionary, const std::string& key, std::string& value)
{
return TryGetValue<std::string>(dictionary, key, value, Empty::String);
}
template<class T>
T Cast(const Object& source)
{
return boost::any_cast<T>(source);
}
struct MemoryStream : boost::iostreams::array_source
{
MemoryStream(const std::vector<uint8_t>& buffer) : boost::iostreams::array_source(reinterpret_cast<const char*>(buffer.data()), buffer.size())
{
}
MemoryStream(const uint8_t* buffer, const uint32_t offset, const uint32_t length) : boost::iostreams::array_source(reinterpret_cast<const char*>(buffer + offset), length)
{
}
};
template<class T, class TElem = char>
void CopyStream(T* source, std::vector<uint8_t>& sink)
{
std::istreambuf_iterator<TElem> it{ source };
std::istreambuf_iterator<TElem> eos{};
for (; it != eos; ++it)
sink.push_back(static_cast<uint8_t>(*it));
}
template<class T, class TElem = char>
void CopyStream(T& source, std::vector<uint8_t>& sink)
{
std::istreambuf_iterator<TElem> it{ source };
std::istreambuf_iterator<TElem> eos{};
for (; it != eos; ++it)
sink.push_back(static_cast<uint8_t>(*it));
}
template<class T>
static uint32_t WriteBytes(std::vector<uint8_t>& buffer, const T& value)
{
static const int32_t length = sizeof(T);
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&value);
for (int32_t i = 0; i < length; i++)
buffer.push_back(bytes[i]);
return length;
}
uint32_t WriteBytes(std::vector<uint8_t>& buffer, const uint8_t* source, uint32_t offset, uint32_t length);
uint32_t WriteBytes(std::vector<uint8_t>& buffer, const std::vector<uint8_t>& source);
uint32_t WriteBytes(std::vector<uint8_t>& buffer, const Guid& value);
Guid NewGuid();
// Handy string functions (boost wrappers)
bool IsEmptyOrWhiteSpace(const std::string& value);
bool IsEqual(const std::string& left, const std::string& right, bool ignoreCase = true);
bool StartsWith(const std::string& value, const std::string& findValue, bool ignoreCase = true);
bool EndsWith(const std::string& value, const std::string& findValue, bool ignoreCase = true);
bool Contains(const std::string& value, const std::string& findValue, bool ignoreCase = true);
int32_t Count(const std::string& value, const std::string& findValue, bool ignoreCase = true);
int32_t Compare(const std::string& leftValue, const std::string& rightValue, bool ignoreCase = true);
int32_t IndexOf(const std::string& value, const std::string& findValue, bool ignoreCase = true);
int32_t IndexOf(const std::string& value, const std::string& findValue, int32_t index, bool ignoreCase = true);
int32_t LastIndexOf(const std::string& value, const std::string& findValue, bool ignoreCase = true);
std::vector<std::string> Split(const std::string& value, const std::string& delimiterValue, bool ignoreCase = true);
std::string Split(const std::string& value, const std::string& delimiterValue, int32_t index, bool ignoreCase = true);
std::string Replace(const std::string& value, const std::string& findValue, const std::string& replaceValue, bool ignoreCase = true);
std::string ToUpper(const std::string& value);
std::string ToLower(const std::string& value);
std::string Trim(const std::string& value);
std::string Trim(const std::string& value, const std::string& trimValues);
std::string TrimRight(const std::string& value);
std::string TrimRight(const std::string& value, const std::string& trimValues);
std::string TrimLeft(const std::string& value);
std::string TrimLeft(const std::string& value, const std::string& trimValues);
std::string PadLeft(const std::string& value, uint32_t count, char padChar);
std::string PadRight(const std::string& value, uint32_t count, char padChar);
#ifndef __STDC_WANT_SECURE_LIB__
#define strcpy_s(dest, size, src) strncpy(dest, src, size)
#endif
// Handy date/time functions (boost wrappers)
enum class TimeInterval
{
Year,
Month,
DayOfYear,
Day,
Week,
WeekDay,
Hour,
Minute,
Second,
Millisecond
};
datetime_t DateAdd(const datetime_t& value, int32_t addValue, TimeInterval interval);
int32_t DateDiff(const datetime_t& startTime, const datetime_t& endTime, TimeInterval interval);
int32_t DatePart(const datetime_t& value, TimeInterval interval);
datetime_t Now();
datetime_t UtcNow();
float32_t TimeSince(const datetime_t& value);
}
// Setup standard hash code for Guid
template<>
struct std::hash<sttp::Guid>
{
size_t operator () (const sttp::Guid& uuid) const noexcept
{
return boost::hash<sttp::Guid>()(uuid);
}
};