forked from mail-ru-im/im-desktop
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.h
375 lines (305 loc) · 9.63 KB
/
common.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
#pragma once
#if !defined(InOut)
#define InOut
#endif
#if !defined(Out)
#define Out
#endif
#if !defined(UNUSED_ARG)
#define UNUSED_ARG(arg) ((void)arg)
#endif
// ----------------------------------------------
// fix for msvc++/clang compartibility
#if defined(_DEBUG) && !defined(DEBUG)
#define DEBUG
#endif
#if defined(DEBUG) && !defined(_DEBUG)
#define _DEBUG
#endif
// ----------------------------------------------
#if !defined(__FUNCTION__)
#define __FUNCTION__ ""
#endif
#define __DISABLE(x) {}
#define __STRA(x) _STRA(x)
#define _STRA(x) #x
#define __STRW(x) _STRW(x)
#define _STRW(x) L#x
#define __LINEA__ __STRA(__LINE__)
#define __LINEW__ __STRW(__LINE__)
#define __FUNCLINEA__ __FUNCTION__ "(#" __LINEA__ ")"
#define __FUNCLINEW__ __FUNCTIONW__ L"(#" __LINEW__ L")"
#define __FILELINEA__ __FILE__ "(" __LINEA__ ")"
#define __FILELINEW__ __FILEW__ L"(" __LINEW__ L")"
#define __TODOA__ __FILELINEA__ ": "
#define __TODOW__ __FILELINEW__ L": "
#ifdef __APPLE__
# undef __TODOA__
# define __TODOA__ ""
# undef __TODOW__
# define __TODOW__ ""
# undef __FILELINEA__
# define __FILELINEA__ ""
# undef __FILELINEW__
# define __FILELINEW__ ""
# undef __FUNCLINEA__
# define __FUNCLINEA__ ""
# undef __FUNCLINEW__
# define __FUNCLINEW__ ""
#endif
#ifndef _countof
#define _countof(array) (sizeof(array) / sizeof(array[0]))
#endif
namespace logutils
{
constexpr const char* yn(const bool v) noexcept { return (v ? "yes" : "no"); }
constexpr const char* tf(const bool v) noexcept { return (v ? "true" : "false"); }
}
namespace build
{
constexpr bool is_debug() noexcept
{
#if defined(_DEBUG) || defined(DEBUG)
return true;
#else
return false;
#endif
}
constexpr bool is_release() noexcept
{
return !is_debug();
}
constexpr bool is_testing() noexcept
{
#if defined(IM_AUTO_TESTING)
return true;
#else
return false;
#endif
}
constexpr bool is_store() noexcept
{
#if defined(BUILD_FOR_STORE)
return true;
#else
return false;
#endif
}
}
namespace environment
{
inline namespace impl
{
constexpr std::string_view get() noexcept
{
#if defined(APP_ENVIRONMENT)
return std::string_view(APP_ENVIRONMENT);
#else
return std::string_view();
#endif
}
}
constexpr bool is_alpha() noexcept
{
return get() == "ALPHA";
}
constexpr bool is_beta() noexcept
{
return get() == "BETA";
}
constexpr bool is_release() noexcept
{
return get() == "RELEASE";
}
constexpr bool is_develop() noexcept
{
return get().empty() || get() == "DEVELOP";
}
}
namespace platform
{
constexpr bool is_windows() noexcept
{
#if defined(_WIN32)
return true;
#else
return false;
#endif
}
constexpr bool is_apple() noexcept
{
#if defined(__APPLE__)
return true;
#else
return false;
#endif
}
constexpr bool is_linux() noexcept
{
#if defined(__linux__)
return true;
#else
return false;
#endif
}
constexpr bool is_x86_64() noexcept
{
#ifdef __x86_64__
return true;
#else
return false;
#endif
}
}
namespace common::utils
{
inline std::vector<std::string_view>
splitSV(std::string_view strv, std::string_view delims)
{
std::vector<std::string_view> output;
size_t first = 0;
while (first < strv.size())
{
const auto second = strv.find_first_of(delims, first);
if (first != second)
output.emplace_back(strv.substr(first, second - first));
if (second == std::string_view::npos)
break;
first = second + 1;
}
return output;
}
inline std::vector<std::string_view>
splitSV(std::string_view strv, char delim)
{
return splitSV(strv, std::string_view(&delim, 1));
}
}
namespace core
{
constexpr unsigned long BYTE = 1;
constexpr unsigned long KILOBYTE = 1024 * BYTE;
constexpr unsigned long MEGABYTE = 1024 * KILOBYTE;
constexpr unsigned long GIGABYTE = 1024 * MEGABYTE;
const std::string KILOBYTE_STR = "kb";
const std::string MEGABYTE_STR = "mb";
const std::string GIGABYTE_STR = "gb";
const std::string MILLISECONDS_STR = "ms";
namespace stats
{
using event_prop_key_type = std::string;
using event_prop_val_type = std::string;
using event_prop_kv_type = std::pair<event_prop_key_type, event_prop_val_type>;
using event_props_type = std::vector<event_prop_kv_type>;
constexpr int32_t msg_pending_delay_s = 5;
inline std::string round_value(const int32_t _value, const int32_t _step, const int32_t _max_value)
{
assert(_step);
if (!_step)
return std::string();
if (_value > _max_value)
return (std::to_string(_max_value) + '+');
const auto base = _value / _step;
const auto over = (_value % _step) ? 1 : 0;
return std::to_string((base + over) * _step);
}
inline time_t round_to_hours(time_t _value)
{
return (_value / 3600) * 3600;
}
inline std::string round_interval(const long long _min_val, const long long _value,
const long long _step, const long long _max_value)
{
assert(_value >= _min_val && _value <= _max_value && _step);
if ((_value < _min_val) || (_value > _max_value) || !_step)
return std::string();
auto steps = (_value - _min_val) / _step;
auto start = _min_val + steps * _step;
auto end = (start + _step > _max_value) ? _max_value : start + _step;
return std::to_string(start) + '-' + std::to_string(end);
}
inline std::string memory_size_interval(size_t _bytes)
{
std::string interval;
if (_bytes <= 100 * MEGABYTE)
interval = round_interval(0, _bytes / MEGABYTE, 100, 100) + MEGABYTE_STR;
else if (_bytes <= 500 * MEGABYTE)
interval = round_interval(100, _bytes/MEGABYTE, 50, 500) + MEGABYTE_STR;
else if (_bytes <= 1 * GIGABYTE)
interval = round_interval(500, _bytes/MEGABYTE, 100, 1024) + MEGABYTE_STR;
else
interval = "more1" + GIGABYTE_STR;
return interval;
}
inline std::string duration_interval(long long _ms)
{
std::string interval;
if (_ms <= 250)
interval = core::stats::round_interval(0, _ms, 50, 250);
else if (_ms <= 2000)
interval = core::stats::round_interval(250, _ms, 250, 2000);
else if (_ms <= 5000)
interval = core::stats::round_interval(2000, _ms, 1000, 5000);
else
interval = "more5000";
interval += MILLISECONDS_STR;
return interval;
}
inline std::string disk_space_interval(long long _bytes)
{
std::string interval;
if (_bytes > 1500 * MEGABYTE)
interval = "1500mb +";
else if (_bytes < 100 * MEGABYTE)
interval = "< 100mb";
else
interval = round_interval(100, _bytes / MEGABYTE, 100, 1500) + MEGABYTE_STR;
return interval;
}
inline std::string traffic_size_interval(size_t _bytes)
{
std::string interval;
if (_bytes <= 100 * KILOBYTE)
interval = round_interval(0, _bytes / KILOBYTE, 100, 100) + KILOBYTE_STR;
else if (_bytes <= 500 * KILOBYTE)
interval = round_interval(100, _bytes / KILOBYTE, 400, 500) + KILOBYTE_STR;
else if (_bytes <= MEGABYTE)
interval = round_interval(500, _bytes / KILOBYTE, 500, 1024) + KILOBYTE_STR;
else if (_bytes <= 5 * MEGABYTE)
interval = round_interval(1, _bytes / MEGABYTE, 1, 5) + MEGABYTE_STR;
else if (_bytes <= 20 * MEGABYTE)
interval = round_interval(5, _bytes / MEGABYTE, 5, 20) + MEGABYTE_STR;
else if (_bytes <= 50 * MEGABYTE)
interval = round_interval(20, _bytes / MEGABYTE, 30, 50) + MEGABYTE_STR;
else if (_bytes <= 500 * MEGABYTE)
interval = round_interval(50, _bytes / MEGABYTE, 50, 500) + MEGABYTE_STR;
else
interval = "more500" + MEGABYTE_STR;
return interval;
}
}
}
namespace ffmpeg
{
constexpr bool is_enable_streaming() noexcept
{
return false;//platform::is_windows();
}
}
namespace core
{
namespace dump
{
constexpr bool is_crash_handle_enabled() noexcept
{
// XXX : don't change it
// use /settings/dump_type.txt: 0 for not handle crashes
// 1 for make mini dump
// 2 for make full dump
if constexpr (platform::is_apple())
return environment::is_alpha() || environment::is_beta() || environment::is_release();
else
return !build::is_debug();
}
}
}