-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.hpp
420 lines (355 loc) · 10.7 KB
/
utils.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
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
/*
Copyright (c) 2024 James Baker
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
The official repository for this library is at https://github.com/VA7ODR/EasyAppBase
*/
#pragma once
#include "data.hpp"
#include <boost/core/demangle.hpp>
#include <boost/stacktrace/stacktrace.hpp>
#include <functional>
#include <string>
#include <string>
#include <chrono>
#include <format>
#include"imgui.h"
#include "shared_recursive_mutex.hpp"
using namespace std::chrono_literals;
inline auto Now()
{
return std::chrono::utc_clock::now();
}
inline auto SystemNow()
{
return std::chrono::system_clock::now();
}
inline auto SteadyNow()
{
return std::chrono::steady_clock::now();
}
inline std::string PrettyDateTime(auto time, const std::string_view formatIn = "{:%Y-%m-%d %H:%M:%S}")
{
try {
auto ret = std::vformat(formatIn, std::make_format_args(time));
return ret;
} catch (std::format_error & e) {
return "Invalid time: " + std::string(e.what());
}
}
inline std::string PrettyDate(auto time)
{
return sPrettyDateTime(time, "{:%Y-%m-%d}");
}
inline std::string PrettyTime(auto time)
{
return sPrettyDateTime(time, "{:%H:%M:%S}");
}
inline std::string PrettyDuration(auto duration = 0s, const std::string_view format = "{:%H:%M:%S}")
{
return std::vformat(format, std::make_format_args(duration));
}
inline std::string PrettyDuration(auto start, auto end, const std::string_view format = "{:%H:%M:%S}")
{
return PrettyDuration(end - start, format);
}
inline std::chrono::utc_clock::time_point UTCTime(auto time)
{
return std::chrono::clock_cast<std::chrono::utc_clock>(time);
}
inline std::chrono::system_clock::time_point SystemTime(auto time = std::chrono::utc_clock::now())
{
return std::chrono::clock_cast<std::chrono::system_clock>(time);
}
inline std::chrono::steady_clock::time_point SteadyTime(auto time = std::chrono::utc_clock::now())
{
return std::chrono::clock_cast<std::chrono::steady_clock>(time);
}
inline void Asserter(bool bCondition, const std::string& sCondition, const std::string & sFile, const std::string & sFunction, int iLine) {
if (!bCondition) {
throw std::runtime_error(sCondition + " failed in " + sFile + " " + sFunction + " " + std::to_string(iLine));
}
}
template <class jsonType = json::value>
void ShowJson(const std::string & sTitle, jsonType & jData, bool & bShow)
{
static ImGuiTableFlags flags = ImGuiTableFlags_BordersV | ImGuiTableFlags_BordersOuterH | ImGuiTableFlags_Resizable | ImGuiTableFlags_RowBg | ImGuiTableFlags_NoBordersInBody;
static ImGuiTreeNodeFlags tree_node_flags = ImGuiTreeNodeFlags_SpanAllColumns;
ImGui::Text("Right-click field name or value to copy it to the clipboard.");
if (ImGui::BeginTable(sTitle.c_str(), 3, flags)) {
ImGui::TableSetupColumn("Index", ImGuiTableColumnFlags_NoHide | ImGuiTableColumnFlags_WidthStretch);
ImGui::TableSetupColumn("Value", ImGuiTableColumnFlags_NoHide | ImGuiTableColumnFlags_WidthStretch);
ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_NoHide | ImGuiTableColumnFlags_WidthStretch);
ImGui::TableHeadersRow();
std::function<void(const std::string &, jsonType &)> recursiveLambda = [&recursiveLambda](const std::string & sName, jsonType & jValue) -> void
{
switch (jValue.isA()) {
case json::JSON_VOID:
return;
case json::JSON_NULL:
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::TreeNodeEx(sName.c_str(), tree_node_flags | ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_Bullet | ImGuiTreeNodeFlags_NoTreePushOnOpen);
if (ImGui::IsItemClicked(1)) {
ImGui::SetClipboardText(sName.c_str());
}
ImGui::TableNextColumn();
ImGui::Text("NULL");
if (ImGui::IsItemClicked(1)) {
ImGui::SetClipboardText("NULL");
}
ImGui::TableNextColumn();
ImGui::Text("null");
break;
case json::JSON_BOOLEAN:
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::TreeNodeEx(sName.c_str(), tree_node_flags | ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_Bullet | ImGuiTreeNodeFlags_NoTreePushOnOpen);
if (ImGui::IsItemClicked(1)) {
ImGui::SetClipboardText(sName.c_str());
}
ImGui::TableNextColumn();
ImGui::Text(jValue.boolean() ? "true" : "false");
if (ImGui::IsItemClicked(1)) {
ImGui::SetClipboardText(jValue.boolean() ? "true" : "false");
}
ImGui::TableNextColumn();
ImGui::Text("bool");
break;
case json::JSON_NUMBER:
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::TreeNodeEx(sName.c_str(), tree_node_flags | ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_Bullet | ImGuiTreeNodeFlags_NoTreePushOnOpen);
if (ImGui::IsItemClicked(1)) {
ImGui::SetClipboardText(sName.c_str());
}
ImGui::TableNextColumn();
ImGui::Text("%s", jValue.c_str());
if (ImGui::IsItemClicked(1)) {
ImGui::SetClipboardText(jValue.c_str());
}
ImGui::TableNextColumn();
ImGui::Text("number");
break;
case json::JSON_STRING:
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::TreeNodeEx(sName.c_str(), tree_node_flags | ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_Bullet | ImGuiTreeNodeFlags_NoTreePushOnOpen);
if (ImGui::IsItemClicked(1)) {
ImGui::SetClipboardText(sName.c_str());
}
ImGui::TableNextColumn();
ImGui::Text("%s", jValue.c_str());
if (ImGui::IsItemClicked(1)) {
ImGui::SetClipboardText(jValue.c_str());
}
ImGui::TableNextColumn();
ImGui::Text("string");
break;
case json::JSON_OBJECT:
{
ImGui::TableNextRow();
ImGui::TableNextColumn();
bool open = ImGui::TreeNodeEx(sName.c_str(), tree_node_flags | ImGuiTreeNodeFlags_DefaultOpen);
if (ImGui::IsItemClicked(1)) {
ImGui::SetClipboardText(sName.c_str());
}
ImGui::TableNextColumn();
ImGui::TextDisabled("--");
ImGui::TableNextColumn();
ImGui::Text("object");
if (open) {
for (auto & sub : jValue) {
recursiveLambda(sub.key(), sub);
}
ImGui::TreePop();
}
break;
}
case json::JSON_ARRAY:
{
ImGui::TableNextRow();
ImGui::TableNextColumn();
bool open = ImGui::TreeNodeEx(sName.c_str(), tree_node_flags | ImGuiTreeNodeFlags_DefaultOpen);
if (ImGui::IsItemClicked(1)) {
ImGui::SetClipboardText(sName.c_str());
}
ImGui::TableNextColumn();
ImGui::TextDisabled("--");
ImGui::TableNextColumn();
ImGui::Text("array");
if (open) {
size_t iIndex = 0;
for (auto & sub : jValue) {
recursiveLambda(std::to_string(iIndex++), sub);
}
ImGui::TreePop();
}
break;
}
}
};
recursiveLambda(sTitle.c_str(), jData);
ImGui::EndTable();
}
}
void ShowJsonWindow(const std::string & sTitle, json::value & jData, bool & bShow);
std::string PrettyHex(const std::vector<unsigned char> &in);
template <class jsonType = json::value>
jsonType ImVec4ToJSONArray(const ImVec4 & in)
{
jsonType ret;
ret[0] = in.x;
ret[1] = in.y;
ret[2] = in.z;
ret[3] = in.w;
return ret;
}
template <class jsonType = json::value>
ImVec4 JSONArrayToImVec4(jsonType & jData)
{
ImVec4 ret {0, 0, 0, 0};
if (jData.IsArray()) {
ret.x = jData[0]._float();
ret.y = jData[1]._float();
ret.z = jData[2]._float();
ret.w = jData[3]._float();
}
return ret;
}
template <typename T, class jsonType = json::value>
class jsonTypedRef
{
public:
jsonTypedRef(jsonType & jValIn)
requires std::is_same_v<T, bool>
: jVal(jValIn)
{
m_val = jVal.boolean();
}
jsonTypedRef(jsonType & jValIn)
requires std::is_floating_point_v<T>
: jVal(jValIn)
{
m_val = jVal._double();
}
jsonTypedRef(jsonType & jValIn)
requires std::is_integral_v<T> && std::is_signed_v<T> && (!std::is_same_v<T, bool>)
: jVal(jValIn)
{
m_val = jVal._int64();
}
jsonTypedRef(jsonType & jValIn)
requires std::is_integral_v<T> && std::is_unsigned_v<T> && (!std::is_same_v<T, bool>)
: jVal(jValIn)
{
m_val = jVal._uint64();
}
jsonTypedRef(jsonType & jValIn)
requires std::is_same_v<T, char *> || std::is_same_v<T, std::string> || std::is_same_v<T, sdstring>
: jVal(jValIn)
{
m_val = jVal.string().data();
}
~jsonTypedRef() { jVal = m_val; }
operator T *()
requires(!std::is_pointer_v<T>)
{
return &m_val;
}
operator T()
requires(std::is_pointer_v<T>)
{
return m_val;
}
private:
T m_val;
jsonType & jVal;
};
template <typename T, class jsonType = json::value>
class jsonTypedRefTSSh : public jsonTypedRef<T, jsonType>
{
public:
jsonTypedRefTSSh(jsonType & jValIn, SharedRecursiveMutex & mtxIn) :
jsonTypedRef<T, jsonType>(jValIn),
lock(mtxIn)
{
}
private:
RecursiveSharedLock lock;
};
template <typename T, class jsonType = json::value>
class jsonTypedRefTSEx : public jsonTypedRef<T, jsonType>
{
public:
jsonTypedRefTSEx(jsonType & jValIn, SharedRecursiveMutex & mtxIn) :
jsonTypedRef<T, jsonType>(jValIn),
lock(mtxIn)
{
}
private:
RecursiveExclusiveLock lock;
};
template <class T = json::value>
class refTSSh
{
public:
refTSSh(T & in, SharedRecursiveMutex & mtxIn) :
lock(mtxIn),
ref(in)
{
}
operator T *()
requires(!std::is_pointer_v<T>)
{
return &ref;
}
operator T()
requires(std::is_pointer_v<T>)
{
return ref;
}
private:
RecursiveSharedLock lock;
T & ref;
};
template <class T = json::value>
class refTSEx
{
public:
refTSEx(T & in, SharedRecursiveMutex & mtxIn) :
lock(mtxIn),
ref(in)
{
}
operator T *()
requires(!std::is_pointer_v<T>)
{
return &ref;
}
operator T()
requires(std::is_pointer_v<T>)
{
return ref;
}
private:
RecursiveExclusiveLock lock;
T & ref;
};
#define ASSERT(bCondition) Asserter(bCondition, #bCondition, SOURCE_FILE, __FUNCTION__, __LINE__)
#define SOURCE_FILE std::string(__FILE__).substr(strlen(SOURCE_DIR) + 1)
std::string GetAppDataFolder();
std::string PrettyHex(const std::vector<unsigned char> &in);