-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMisc.cpp
More file actions
executable file
·977 lines (811 loc) · 28.8 KB
/
Misc.cpp
File metadata and controls
executable file
·977 lines (811 loc) · 28.8 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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
/*************************************************************************************
cpl - cross-platform library - v. 0.1.0.
Copyright (C) 2016 Janus Lynggaard Thorborg (www.jthorborg.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
See \licenses\ for additional details on licenses associated with this program.
**************************************************************************************
file:Misc.cpp
Implementation of Misc.h
*************************************************************************************/
#include "MacroConstants.h"
#include "PlatformMisc.h"
#include "Misc.h"
#include <ctime>
#include "PlatformSpecific.h"
#include "CThread.h"
#include <cstdarg>
#include "Types.h"
#include "stdext.h"
#include <atomic>
#include "CExclusiveFile.h"
#include <typeinfo>
#include <future>
#include "Exceptions.h"
#include "Core.h"
#include <queue>
#include "filesystem.h"
#ifdef __GNUG__
#include <cstdlib>
#include <memory>
#include <cxxabi.h>
#endif
namespace cpl
{
namespace Misc
{
std::mutex idMutex;
std::queue<int> freeIDList;
int instanceCounter;
int addHandlers();
static std::string GetDirectoryPath();
static int __unusedInitialization = addHandlers();
static std::atomic<std::terminate_handler> oldTerminate;
static std::thread::id MainThreadID = std::this_thread::get_id();
#ifdef __GNUG__
std::string DemangleRawName(const string_ref name) {
//http://stackoverflow.com/questions/281818/unmangling-the-result-of-stdtype-infoname
int status = -4; // some arbitrary value to eliminate the compiler warning
// enable c++11 by passing the flag -std=c++11 to g++
std::unique_ptr<char, void(*)(void*)> res {
abi::__cxa_demangle(name.c_str(), NULL, NULL, &status),
std::free
};
return (status == 0) ? res.get() : name.c_str();
}
#else
std::string DemangleRawName(const string_ref name)
{
return name.string();
}
#endif
void __cdecl _purescall(void)
{
auto except = "Pure virtual function called. This is a programming error, usually happening if a freed object is used again, "
"or calling virtual functions inside destructors/constructors.";
LogException(except);
cpl::CrashIfUserDoesntDebug(except);
}
void terminateHook()
{
// see if we terminated because of an exception
if (std::current_exception())
{
try
{
// as there is an exception, retrow it
throw;
}
catch (const std::exception & e)
{
auto what = std::string("Software exception at std::terminate_hook: ") + e.what();
LogException(what);
MsgBox(what, cpl::programInfo.name + ": Software exception", MsgStyle::sOk | MsgIcon::iStop);
}
}
auto oldHandler = oldTerminate.load();
if (oldHandler)
oldHandler();
else
std::abort();
}
int addHandlers()
{
static bool hasBeenAdded = false;
if (!hasBeenAdded)
{
oldTerminate.store(std::set_terminate(terminateHook));
#ifdef CPL_MSVC
hasBeenAdded = true;
_set_purecall_handler(_purescall);
#ifdef _DEBUG
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
#endif
}
return 0;
}
std::pair<int, std::string> ExecCommand(const string_ref cmd)
{
// http://stackoverflow.com/a/478960/1287254
char buffer[1024];
std::string result;
auto PipeOpen = [](const string_ref cmd)
{
#ifdef CPL_WINDOWS
return ::_popen(cmd.c_str(), "r");
#else
return ::popen(cmd.c_str(), "r");
#endif
};
auto PipeClose = [](const auto & file)
{
#ifdef CPL_WINDOWS
return ::_pclose(file);
#else
return ::pclose(file);
#endif
};
auto pipe = PipeOpen(cmd);
if (!pipe)
CPL_RUNTIME_EXCEPTION(format("Error executing commandline: \"%s\"", cmd.c_str()));
while (!std::feof(pipe))
{
if (std::fgets(buffer, sizeof(buffer), pipe) != NULL)
result += buffer;
}
return { PipeClose(pipe), result };
}
std::pair<bool, std::string> ReadFile(const fs::path& path) noexcept
{
char buffer[1024];
std::string result;
std::unique_ptr<FILE, decltype(&std::fclose)> file(std::fopen(path.string().c_str(), "r"), &std::fclose);
if (!file)
return { false, result };
if(std::fseek(file.get(), 0, SEEK_END))
return { false, result };
result.reserve(std::ftell(file.get()));
std::rewind(file.get());
while (!std::feof(file.get()))
{
if (std::fgets(buffer, sizeof(buffer), file.get()) != NULL)
result += buffer;
}
return { true, result };
}
bool WriteFile(const fs::path& path, const string_ref contents) noexcept
{
std::unique_ptr<FILE, decltype(&std::fclose)> file(std::fopen(path.string().c_str(), "w"), &std::fclose);
if (!file)
return false;
return std::fwrite(contents.c_str(), contents.size() + 1, 1, file.get()) == 1;
}
/*********************************************************************************************
Delays the execution for at least msecs. Should have good precision bar context-switches,
may spin.
*********************************************************************************************/
void PreciseDelay(double msecs)
{
#ifdef CPL_JUCE
auto start = juce::Time::getHighResolutionTicks();
double factor = 1.0 / juce::Time::getHighResolutionTicksPerSecond();
msecs /= 1000;
while ((juce::Time::getHighResolutionTicks() - start) * factor < msecs)
;
#else
auto start = std::chrono::high_resolution_clock::now();
while (true)
{
auto now = std::chrono::high_resolution_clock::now();
auto elapsed = now - start;
if (elapsed.count() / 1.0e8 > msecs)
{
break;
}
}
#endif
}
/*********************************************************************************************
Returns the path of our directory.
For macs, this is <pathtobundle>/contents/resources/
for windows, this is the directory of the DLL.
*********************************************************************************************/
const std::string & DirectoryPath()
{
static std::string dirPath = GetDirectoryPath();
return dirPath;
}
const fs::path& DirFSPath()
{
static fs::path path = GetDirectoryPath();
return path;
}
void TextEditFile(const std::string& path)
{
#ifdef CPL_WINDOWS
std::system(("Notepad.exe \"" + path + "\"").c_str());
#elif defined(CPL_MAC)
std::string cmdLine = "open \"" + path + "\"";
std::system((cmdLine).c_str());
#else
std::string cmdLine = "gedit \"" + path + "\"";
std::system((cmdLine).c_str());
#endif
}
std::int32_t AcquireInstanceCounter()
{
std::unique_lock<std::mutex> lockGuard(idMutex);
if (freeIDList.size() > 0)
{
auto top = freeIDList.front();
freeIDList.pop();
return top;
}
return instanceCounter++;
}
int AcquireUniqueInstanceID()
{
std::int32_t pID;
#if defined(CPL_WINDOWS)
pID = static_cast<std::int32_t>(GetProcessId(GetCurrentProcess()));
#elif defined(CPL_MAC) || defined(CPL_UNIXC)
pID = static_cast<std::int32_t>(getpid());
#endif
if (instanceCounter > std::numeric_limits<unsigned char>::max())
MsgBox("Warning: You currently have had more than 256 instances open, this may cause a wrap around in instance-id's", programInfo.name, iInfo | sOk, nullptr, true);
std::int32_t id = ((pID << 8) & 0xFFFFFF00) | AcquireInstanceCounter();
return id;
}
void ReleaseUniqueInstanceID(std::int32_t id)
{
std::unique_lock<std::mutex> lockGuard(idMutex);
freeIDList.push(id);
}
/*********************************************************************************************
Checks whether we are being debugged.
*********************************************************************************************/
bool IsBeingDebugged()
{
#ifdef CPL_WINDOWS
return IsDebuggerPresent() ? true : false;
#elif defined(CPL_MAC)
int junk;
int mib[4];
struct kinfo_proc info;
size_t size;
// Initialize the flags so that, if sysctl fails for some bizarre
// reason, we get a predictable result.
info.kp_proc.p_flag = 0;
// Initialize mib, which tells sysctl the info we want, in this case
// we're looking for information about a specific process ID.
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();
// Call sysctl.
size = sizeof(info);
junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
// We're being debugged if the P_TRACED flag is set.
if (junk == 0)
return ((info.kp_proc.p_flag & P_TRACED) != 0);
else
return false;
#elif defined(CPL_UNIXC)
auto contents = ExecCommand("grep 'TracerPid' /proc/self/status");
if (contents.first == 0 && contents.second.size() > 0)
{
auto pos = contents.second.find(":");
if (pos != std::string::npos)
{
auto number = contents.second.c_str() + pos + 1;
auto tracerPID = std::strtol(number, nullptr, 10);
return tracerPID != 0;
}
}
#else
#error No detection of debugging implementation
#endif
return false;
}
void OutputToDebugger(const string_ref message)
{
CPL_DEBUGOUT(message.data());
}
const char * GetImageBase()
{
#ifdef CPL_WINDOWS
HMODULE hMod;
if (GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (const char*)&GetDirectoryPath, &hMod))
{
return reinterpret_cast<char *>(hMod);
}
#elif defined(CPL_MAC) || defined(CPL_UNIXC)
Dl_info exeInfo;
dladdr((void*)GetImageBase, &exeInfo);
return (const char*)exeInfo.dli_fbase;
#endif
return nullptr;
}
/*********************************************************************************************
returns the amount of characters needed to print pargs to the format list,
EXCLUDING the null character
*********************************************************************************************/
int GetSizeRequiredFormat(const string_ref fmt, va_list pargs)
{
#ifdef CPL_WINDOWS
return _vscprintf(fmt.c_str(), pargs);
#else
int retval;
va_list argcopy;
va_copy(argcopy, pargs);
retval = vsnprintf(nullptr, 0, fmt.c_str(), argcopy);
va_end(argcopy);
return retval;
#endif
}
/*********************************************************************************************
Define a symbol for __rdtsc if it doesn't exist
*********************************************************************************************/
#ifndef CPL_MSVC
__inline__ uint64_t __rdtsc() {
#if defined(__x86_64__) || defined(__i386__)
#ifdef CPL_M_64BIT_
uint64_t a, d;
__asm__ volatile ("rdtsc" : "=a" (a), "=d" (d));
return (d << 32) | a;
#else
uint64_t x;
__asm__ volatile ("rdtsc" : "=A" (x));
return x;
#endif
#elif defined(__aarch64__) || defined(__arm64__)
// ARM64: Use the system counter
uint64_t val;
__asm__ volatile("mrs %0, cntvct_el0" : "=r" (val));
return val;
#else
#error "Implement rdtsc for your platform"
#endif
}
#endif
/*********************************************************************************************
Returns the CPU clock counter
*********************************************************************************************/
std::uint64_t ClockCounter()
{
return __rdtsc();
}
/*********************************************************************************************
Returns a precise point in time. Use TimeDifference() to get the difference in miliseconds
between time points.
*********************************************************************************************/
long long TimeCounter()
{
long long t = 0;
#ifdef _WINDOWS_
::QueryPerformanceCounter((LARGE_INTEGER*)&t);
#elif defined(CPL_MAC) && defined(CPL_JUCE)
t = juce::Time::getHighResolutionTicks();
#elif defined(CPL_MAC)
auto t1 = mach_absolute_time();
*(decltype(t1)*)&t = t1;
#elif defined(__CPP11__)
using namespace std::chrono;
auto tpoint = high_resolution_clock::now().time_since_epoch().count();
(*(decltype(tpoint)*)&t) = tpoint;
#endif
return t;
}
double TimeDifference(long long time)
{
return TimeToMilisecs(TimeCounter() - time);
}
double TimeDifferenceSeconds(long long time)
{
return TimeToSeconds(TimeCounter() - time);
}
double TimeToSeconds(long long time)
{
double ret = 0.0;
#ifdef _WINDOWS_
long long f;
::QueryPerformanceFrequency((LARGE_INTEGER*)&f);
//long long t = TimeCounter();
ret = (time) * (1.0 / f);
#elif defined(CPL_MAC) && defined(CPL_JUCE)
return time / (double)juce::Time::getHighResolutionTicksPerSecond();
#elif defined(CPL_MAC)
auto t1 = *(decltype(mach_absolute_time())*)&time;
struct mach_timebase_info tinfo;
if (mach_timebase_info(&tinfo) == KERN_SUCCESS)
{
double hTime2sFactor = tinfo.numer / (tinfo.denom * 1000.0 * 1000.0 * 1000.0);
ret = (((t1)* hTime2sFactor));
}
#elif defined(__CPP11__)
using namespace std::chrono;
high_resolution_clock::rep t1;
t1 = *(high_resolution_clock::rep *)&time;
seconds elapsed(t1);
ret = elapsed.count();
#endif
return ret;
}
double TimeToMilisecs(long long time)
{
return TimeToSeconds(time) * 1000;
}
/*********************************************************************************************
Get a formatted time string in the format "hh:mm:ss"
*********************************************************************************************/
std::string GetTime()
{
time_t timeObj;
tm * ctime;
time(&timeObj);
#ifdef CPL_MSVC
tm pTime;
gmtime_s(&pTime, &timeObj);
ctime = &pTime;
#else
// consider using a safer alternative here.
ctime = gmtime(&timeObj);
#endif
char buffer[100];
cpl::sprintfs(buffer, "%01d:%01d:%01d", ctime->tm_hour, ctime->tm_min, ctime->tm_sec);
return buffer;
}
std::string GetDate()
{
time_t timeObj;
tm * ctime;
time(&timeObj);
#ifdef CPL_MSVC
tm pTime;
gmtime_s(&pTime, &timeObj);
ctime = &pTime;
#else
// consider using a safer alternative here.
ctime = gmtime(&timeObj);
#endif
char buffer[100];
cpl::sprintfs(buffer, "%d/%d/%d", ctime->tm_mday, ctime->tm_mon + 1, ctime->tm_year + 1900);
return buffer;
}
void _internalAlignedFree(void* obj)
{
#ifdef CPL_WINDOWS
_aligned_free(obj);
#else
if (obj)
{
free(((void**)obj)[-1]);
}
#endif
}
void* alignedBytesMalloc(std::size_t size, std::size_t alignment)
{
void * ptr = nullptr;
#ifdef CPL_WINDOWS
ptr = _aligned_malloc(size, alignment);
#else
// : http://stackoverflow.com/questions/196329/osx-lacks-memalign
void *mem = malloc(size + (alignment - 1) + sizeof(void*));
char *amem = ((char*)mem) + sizeof(void*);
amem += ((alignment - ((uintptr_t)amem & (alignment - 1))) & (alignment - 1));
((void**)amem)[-1] = mem;
ptr = amem;;
#endif
return ptr;
}
void* _internalAlignedRealloc(void* ptr, std::size_t elementSize, std::size_t numObjects, std::size_t alignment)
{
#ifdef CPL_WINDOWS
return _aligned_realloc(ptr, numObjects * elementSize, alignment);
#else
#ifdef CPL_MAC
// all allocations on OS X are aligned to 16-byte boundaries
// NOTE: removed, as alignedFree doesn't account for this
//if(alignment <= 16)
// return reinterpret_cast<Type *>(std::realloc(ptr, numObjects * sizeof(Type)));
#endif
// https://github.com/numpy/numpy/issues/5312
void *p1, **p2, *base;
std::size_t
old_offs,
offs = alignment - 1 + sizeof(void*),
n = elementSize * numObjects;
if (ptr != nullptr)
{
base = *(((void**)ptr) - 1);
if ((p1 = std::realloc(base, n + offs)) == nullptr)
return nullptr;
if (p1 == base)
return ptr;
p2 = (void**)(((std::uintptr_t)(p1) + offs) & ~(alignment - 1));
old_offs = (size_t)((std::uintptr_t)ptr - (std::uintptr_t)base);
std::memmove(p2, (char*)p1 + old_offs, n);
}
else
{
if ((p1 = std::malloc(n + offs)) == nullptr)
return nullptr;
p2 = (void**)(((std::uintptr_t)(p1) + offs) & ~(alignment - 1));
}
*(p2 - 1) = p1;
return p2;
#endif
}
/*********************************************************************************************
'private' function, initializes the global DirectoryPath
*********************************************************************************************/
static std::string GetDirectoryPath()
{
if (programInfo.hasCustomDirectory)
return programInfo.customDirectory();
#ifdef CPL_WINDOWS
// change to MAX_PATH on all supported systems
char path[MAX_PATH + 2];
unsigned long nLen = 0;
unsigned long nSize = sizeof path;
/*
GetModuleFileName() returns the path to the dll, inclusive of the dll name.
We shave this off to get the directory
*/
HMODULE hMod;
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
(char*)&GetDirectoryPath, &hMod);
nLen = GetModuleFileNameA(hMod, path, nSize);
if (!nLen)
return "";
while (nLen-- > 0) {
if (CPL_DIRC_COMP(path[nLen])) {
path[nLen] = '\0';
break;
}
};
path[MAX_PATH + 1] = NULL;
return path;
#elif defined(CPL_MAC) && defined(__APE_LOCATE_USING_COCOA)
/*
GetBundlePath() returns the path to the bundle, inclusive of the bundle name.
Since everything we have is in the subdir /contents/ we append that.
NOTE: This is broken
*/
// change to MAX_PATH on all supported systems
char path[MAX_PATH + 2];
unsigned long nLen = 0;
unsigned long nSize = sizeof path;
nLen = GetBundlePath(path, nSize);
if (!nLen)
return "";
path[nLen] = '\0';
std::string ret = path;
ret += "/Contents";
return ret;
#elif defined(CPL_MAC) && defined(__APE_LOCATE_USING_CF)
// change to MAX_PATH on all supported systems
char path[MAX_PATH + 2];
unsigned long nLen = 0;
unsigned long nSize = sizeof path;
CFBundleRef ref = CFBundleGetBundleWithIdentifier(CFSTR("com.Lightbridge.AudioProgrammingEnvironment"));
if (ref)
{
CFURLRef url = CFBundleCopyBundleURL(ref);
if (url)
{
CFURLGetFileSystemRepresentation(url, true, (unsigned char*)path, nSize);
CFRelease(url);
return path;
}
}
#elif defined(CPL_UNIXC) || defined(CPL_MAC)
Dl_info exeInfo;
dladdr((void*)GetDirectoryPath, &exeInfo);
fs::path fullPath(exeInfo.dli_fname);
auto parent = fullPath.parent_path();
#ifdef CPL_MAC
if(parent.stem().string() == "MacOS" && parent.parent_path().stem() == "Contents")
parent = parent.parent_path() / "Resources";
#endif
return parent.string() + "/";
#endif
return "<Error getting directory of executable>";
}
/*********************************************************************************************
Floating point rounding function.
*********************************************************************************************/
long Round(double number)
{
return (number >= 0.0) ? (long)(number + 0.5) : (long)(number - 0.5);
}
/*********************************************************************************************
Delays execution for a certain amount of time. Sleeping for a time less than (or equal to)
zero yields the thread instead.
*********************************************************************************************/
long Delay(int ms)
{
#ifdef __CPP11__
if (ms <= 0)
std::this_thread::yield();
else
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
#elif defined (CPL_WINDOWS)
::Sleep(ms);
#elif defined(CPL_MAC) || defined(CPL_UNIXC)
usleep(ms * 1000);
#endif
return 0;
}
/*********************************************************************************************
Imprecise, incrementing timer.
At least 1000/60 seconds precision.
*********************************************************************************************/
unsigned int QuickTime()
{
#ifdef CPL_WINDOWS
// TODO: GetTickCount64()
return GetTickCount();
#else
// from sweep/lice
struct timeval tm = {0,};
gettimeofday(&tm, NULL);
// this is not really ideal, but i would really like to keep the counter in a 32-bit int.
// a simple static_cast<unsigned> does NOT work as expected: result is TRUNCATED instead
// of following the standard which says unsigned integers wrap around INT_MAX.
// we do this ourselves, therefore.
return static_cast<unsigned long long>(tm.tv_sec*1000.0 + tm.tv_usec / 1000.0) % UINT32_MAX;
#endif
}
/*********************************************************************************************
'private' function, maps to a MessageBox
*********************************************************************************************/
inline static int _mbx(void * systemData, const string_ref text, const string_ref title, int nStyle = 0)
{
std::promise<int> promise;
auto future = promise.get_future();
// this pattern ensures the message box is called on the main thread, no matter what thread we're in.
auto boxGenerator = [&]()
{
#ifdef CPL_WINDOWS
if (!systemData)
nStyle |= MB_DEFAULT_DESKTOP_ONLY;
auto ret = (int)MessageBoxA(reinterpret_cast<HWND>(systemData), text.c_str(), title.c_str(), nStyle);
#elif defined(CPL_MAC)
auto ret = MacBox(systemData, text.c_str(), title.c_str(), nStyle);
#elif defined(CPL_JUCE)
auto ret = MsgButton::bOk;
auto iconStyle = (nStyle >> 8) & 0xFF;
auto buttonStyle = nStyle & 0xFF;
juce::AlertWindow::AlertIconType iconType;
switch (iconStyle)
{
case iInfo:
iconType = juce::AlertWindow::AlertIconType::InfoIcon;
break;
case iWarning:
case iStop:
iconType = juce::AlertWindow::AlertIconType::WarningIcon;
break;
case iQuestion:
iconType = juce::AlertWindow::AlertIconType::QuestionIcon;
break;
default:
iconType = juce::AlertWindow::AlertIconType::NoIcon;
break;
}
switch (buttonStyle)
{
case MsgStyle::sOk:
{
juce::NativeMessageBox::showMessageBox(iconType, title.c_str(), text.c_str(), nullptr);
ret = MsgButton::bOk;
break;
}
case MsgStyle::sYesNo:
{
bool result = juce::NativeMessageBox::showOkCancelBox(iconType, title.c_str(), text.c_str(), nullptr, nullptr);
ret = result ? MsgButton::bYes : MsgButton::bNo;
break;
}
case sYesNoCancel:
case sConTryCancel:
{
int result = juce::NativeMessageBox::showYesNoCancelBox(iconType, title.c_str(), text.c_str(), nullptr, nullptr);
if (result == 0)
ret = MsgButton::bCancel;
else if (result == 1)
ret = MsgButton::bYes;
else
ret = MsgButton::bNo;
break;
}
}
#elif defined(CPL_UNIXC)
std::string options = "zenity ";
auto iconStyle = (nStyle >> 8) & 0xFF;
switch (iconStyle)
{
default:
case iInfo: options += "--info "; break;
case iStop: options += "--error "; break;
case iWarning: options += "--warning "; break;
case iQuestion: options += "--question "; break;
}
options += "--text=\"" + text.string() + "\" --title=\"" + title.string() + "\"";
auto ret = ExecCommand(options).first ? MsgButton::bNo : MsgButton::bYes;
#endif
promise.set_value(ret);
};
#ifdef CPL_JUCE
if (juce::MessageManager::getInstance()->isThisTheMessageThread())
{
boxGenerator();
}
else if (
// deadlock: we are not the main thread, but we have it locked and we are currently blocking progress
juce::MessageManager::getInstance()->currentThreadHasLockedMessageManager()
// deadlock: OS X always finds a weird way to query the context about something random.
// in this situation, the context is locked, so we also create a deadlock here.
|| juce::OpenGLContext::getCurrentContext())
{
return MsgButton::bError;
}
else
{
cpl::GUIUtils::MainEvent(boxGenerator);
}
#else
if (std::this_thread::get_id() == MainThreadID)
{
boxGenerator();
}
else
{
throw CPLNotImplementedException("Non-main thread message boxes not implemented for non-GUI builds.");
}
#endif
future.wait();
return future.get();
}
/*********************************************************************************************
MsgBoxData - used to transfer information about a messagebox through threads
*********************************************************************************************/
struct MsgBoxData
{
std::string title;
std::string text;
int nStyle;
void * systemWindow;
MsgBoxData(const string_ref title, const string_ref text, int style, void * window = NULL)
: title(title.string()), text(text.string()), nStyle(style), systemWindow(window) {};
};
/*********************************************************************************************
Spawns a messagebox in a threaded context. Max of 10 open boxes.
*********************************************************************************************/
static void * ThreadedMessageBox(void * imp_data)
{
//Global for all instances of this plugin: But we do not want to spam screen with msgboxes
static std::atomic<int> nOpenMsgBoxes {0};
const int nMaxBoxes = 10;
if (nOpenMsgBoxes >= nMaxBoxes)
return reinterpret_cast<void*>(-1);
MsgBoxData * data = reinterpret_cast<MsgBoxData*>(imp_data);
nOpenMsgBoxes++;
int ret = _mbx(data->systemWindow, data->text.c_str(), data->title.c_str(), data->nStyle);
delete data;
nOpenMsgBoxes--;
return reinterpret_cast<void*>(static_cast<std::intptr_t>(ret));
}
/*********************************************************************************************
MsgBox - Spawns a messagebox, optionally blocking.
*********************************************************************************************/
int MsgBox(const string_ref text, const string_ref title, int nStyle, void * parent, const bool bBlocking)
{
if (bBlocking)
return _mbx(parent, text.c_str(), title.c_str(), nStyle);
else
{
// spawn and detach instantly.
/* NOTE:
This is a severely deprecated method on both windows and osx
Implement this as some sort of global stack of messageboxes,
which gets shown when a global, static RenderMsgBoxes() function
is called from a gui loop
*/
CThread msgThread(ThreadedMessageBox);
msgThread.run(new MsgBoxData(title, text, nStyle));
return 0;
}
}
};
};