/
msemsearray.h
3676 lines (3275 loc) · 180 KB
/
msemsearray.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
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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2015 Noah Lopez
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#ifndef MSEMSEARRAY_H
#define MSEMSEARRAY_H
/*compiler specific defines*/
#ifdef _MSC_VER
#if (1700 > _MSC_VER)
#define MSVC2010_COMPATIBLE 1
#endif /*(1700 > _MSC_VER)*/
#if (1900 > _MSC_VER)
#define MSVC2013_COMPATIBLE 1
#endif /*(1900 > _MSC_VER)*/
#else /*_MSC_VER*/
#if (defined(__GNUC__) || defined(__GNUG__))
#define GPP_COMPATIBLE 1
#if ((5 > __GNUC__) && (!defined(__clang__)))
#define GPP4P8_COMPATIBLE 1
#endif /*((5 > __GNUC__) && (!defined(__clang__)))*/
#endif
#endif /*_MSC_VER*/
//define MSE_MSEARRAY_USE_MSE_PRIMITIVES 1
#ifdef MSE_MSEARRAY_USE_MSE_PRIMITIVES
#include "mseprimitives.h"
#endif // MSE_MSEARRAY_USE_MSE_PRIMITIVES
#include "msescope.h"
#include "mseoptional.h"
#include <array>
#include <assert.h>
#include <memory>
#include <unordered_map>
#include <functional>
#include <tuple>
#include <climits> // ULONG_MAX
#include <stdexcept>
#include <type_traits>
#include <shared_mutex>
#ifdef MSE_SELF_TESTS
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#endif // MSE_SELF_TESTS
#ifdef _MSC_VER
#pragma warning( push )
#pragma warning( disable : 4100 4456 4189 )
#endif /*_MSC_VER*/
#ifdef MSE_CUSTOM_THROW_DEFINITION
#include <iostream>
#define MSE_THROW(x) MSE_CUSTOM_THROW_DEFINITION(x)
#else // MSE_CUSTOM_THROW_DEFINITION
#define MSE_THROW(x) throw(x)
#endif // MSE_CUSTOM_THROW_DEFINITION
namespace mse {
#ifndef _STD
#define _STD std::
#endif /*_STD*/
#ifndef _NOEXCEPT
#define _NOEXCEPT
#endif /*_NOEXCEPT*/
#ifndef _NOEXCEPT_OP
#define _NOEXCEPT_OP(x) noexcept(x)
#endif /*_NOEXCEPT_OP*/
#ifndef _THROW_NCEE
#define _THROW_NCEE(x, y) MSE_THROW(x(y))
#endif /*_THROW_NCEE*/
#ifdef MSE_MSEARRAY_USE_MSE_PRIMITIVES
typedef mse::CSize_t msear_size_t;
typedef mse::CInt msear_int;
typedef bool msear_bool; // no added safety benefit to using mse::CBool in this case
#define msear_as_a_size_t as_a_size_t
#else // MSE_MSEARRAY_USE_MSE_PRIMITIVES
#if SIZE_MAX <= ULONG_MAX
#define MSE_MSEARRAY_BASE_INTEGER_TYPE long int
#else // SIZE_MAX <= ULONG_MAX
#define MSE_MSEARRAY_BASE_INTEGER_TYPE long long int
#endif // SIZE_MAX <= ULONG_MAX
typedef size_t msear_size_t;
typedef MSE_MSEARRAY_BASE_INTEGER_TYPE msear_int;
typedef bool msear_bool;
typedef size_t msear_as_a_size_t;
#endif // MSE_MSEARRAY_USE_MSE_PRIMITIVES
class nii_array_range_error : public std::range_error {
public:
using std::range_error::range_error;
};
class nii_array_null_dereference_error : public std::logic_error {
public:
using std::logic_error::logic_error;
};
class msearray_range_error : public std::range_error { public:
using std::range_error::range_error;
};
class msearray_null_dereference_error : public std::logic_error { public:
using std::logic_error::logic_error;
};
/* msear_pointer behaves similar to native pointers. It's a bit safer in that it initializes to
nullptr by default and checks for attempted dereference of null pointers. */
template<typename _Ty>
class msear_pointer {
public:
msear_pointer() : m_ptr(nullptr) {}
msear_pointer(_Ty* ptr) : m_ptr(ptr) {}
//msear_pointer(const msear_pointer<_Ty>& src) : m_ptr(src.m_ptr) {}
template<class _Ty2, class = typename std::enable_if<
std::is_same<_Ty2, _Ty>::value || ((!std::is_const<_Ty2>::value) && std::is_same<const _Ty2, _Ty>::value)
, void>::type>
msear_pointer(const msear_pointer<_Ty2>& src) : m_ptr(src.m_ptr) {}
_Ty& operator*() const {
#ifndef MSE_DISABLE_MSEAR_POINTER_CHECKS
if (nullptr == m_ptr) { MSE_THROW(msearray_null_dereference_error("attempt to dereference null pointer - mse::msear_pointer")); }
#endif /*MSE_DISABLE_MSEAR_POINTER_CHECKS*/
return (*m_ptr);
}
_Ty* operator->() const {
#ifndef MSE_DISABLE_MSEAR_POINTER_CHECKS
if (nullptr == m_ptr) { MSE_THROW(msearray_null_dereference_error("attempt to dereference null pointer - mse::msear_pointer")); }
#endif /*MSE_DISABLE_MSEAR_POINTER_CHECKS*/
return m_ptr;
}
msear_pointer<_Ty>& operator=(_Ty* ptr) {
m_ptr = ptr;
return (*this);
}
template<class _Ty2, class = typename std::enable_if<
std::is_same<_Ty2, _Ty>::value
|| ((!std::is_const<_Ty2>::value) && std::is_same<const _Ty2, _Ty>::value)
, void>::type>
msear_pointer<_Ty>& operator=(const msear_pointer<_Ty2>& src) {
m_ptr = src.m_ptr;
return (*this);
}
bool operator==(const msear_pointer _Right_cref) const { return (_Right_cref.m_ptr == m_ptr); }
bool operator!=(const msear_pointer _Right_cref) const { return (!((*this) == _Right_cref)); }
//bool operator==(const _Ty* _Right_cref) const { return (_Right_cref == m_ptr); }
//bool operator!=(const _Ty* _Right_cref) const { return (!((*this) == _Right_cref)); }
bool operator!() const { return (!m_ptr); }
operator bool() const { return (m_ptr != nullptr); }
explicit operator _Ty*() const { return m_ptr; }
void not_async_shareable_tag() const {} /* Indication that this type is not eligible to be shared between threads. */
_Ty* m_ptr;
};
class non_thread_safe_mutex {
public:
void lock() { // lock exclusive
if (m_is_locked) {
MSE_THROW(std::system_error(std::make_error_code(std::errc::resource_deadlock_would_occur)));
}
m_is_locked = true;
}
bool try_lock() { // try to lock exclusive
if (m_is_locked) {
return false;
}
else
{
m_is_locked = true;
return true;
}
}
template<class _Rep, class _Period>
bool try_lock_for(const std::chrono::duration<_Rep, _Period>& _Rel_time) { // try to lock for duration
return (try_lock_until(std::chrono::steady_clock::now() + _Rel_time));
}
template<class _Clock, class _Duration>
bool try_lock_until(const std::chrono::time_point<_Clock, _Duration>& _Abs_time) { // try to lock until time point
return try_lock();
}
void unlock() { // unlock exclusive
assert(m_is_locked);
m_is_locked = false;
}
bool m_is_locked = false;
};
class non_thread_safe_shared_mutex {
public:
void lock() { // lock exclusive
if (m_is_exclusive_locked || (1 <= m_shared_lock_count)) {
MSE_THROW(std::system_error(std::make_error_code(std::errc::resource_deadlock_would_occur)));
}
m_is_exclusive_locked = true;
}
bool try_lock() { // try to lock exclusive
bool retval = true;
if (m_is_exclusive_locked || (1 <= m_shared_lock_count)) {
retval = false;
}
else {
m_is_exclusive_locked = true;
}
return retval;
}
template<class _Rep, class _Period>
bool try_lock_for(const std::chrono::duration<_Rep, _Period>& _Rel_time) { // try to lock for duration
return (try_lock_until(std::chrono::steady_clock::now() + _Rel_time));
}
template<class _Clock, class _Duration>
bool try_lock_until(const std::chrono::time_point<_Clock, _Duration>& _Abs_time) { // try to lock until time point
return try_lock();
}
void unlock() { // unlock exclusive
assert(m_is_exclusive_locked && (0 == m_shared_lock_count));
m_is_exclusive_locked = false;
}
void lock_shared() { // lock non-exclusive
if (m_is_exclusive_locked) {
MSE_THROW(std::system_error(std::make_error_code(std::errc::resource_deadlock_would_occur)));
}
m_shared_lock_count += 1;
}
bool try_lock_shared() { // try to lock non-exclusive
bool retval = true;
if (m_is_exclusive_locked) {
retval = false;
}
else {
m_shared_lock_count += 1;
}
return retval;
}
template<class _Rep, class _Period>
bool try_lock_shared_for(const std::chrono::duration<_Rep, _Period>& _Rel_time) { // try to lock non-exclusive for relative time
return (try_lock_shared_until(_Rel_time + std::chrono::steady_clock::now()));
}
template<class _Clock, class _Duration>
bool try_lock_shared_until(const std::chrono::time_point<_Clock, _Duration>& _Abs_time) { // try to lock non-exclusive until absolute time
return try_lock_shared();
}
void unlock_shared() { // unlock non-exclusive
assert((1 <= m_shared_lock_count) && (!m_is_exclusive_locked));
m_shared_lock_count -= 1;
}
bool m_is_exclusive_locked = false;
size_t m_shared_lock_count = 0;
};
class non_thread_safe_recursive_shared_timed_mutex : public non_thread_safe_shared_mutex {
public:
typedef non_thread_safe_shared_mutex base_class;
void lock() {
base_class::lock_shared();
}
bool try_lock() {
return base_class::try_lock_shared();
}
template<class _Rep, class _Period>
bool try_lock_for(const std::chrono::duration<_Rep, _Period>& _Rel_time) {
return (try_lock_until(std::chrono::steady_clock::now() + _Rel_time));
}
template<class _Clock, class _Duration>
bool try_lock_until(const std::chrono::time_point<_Clock, _Duration>& _Abs_time) {
return base_class::try_lock_shared_until(_Abs_time);
}
void unlock() {
base_class::unlock_shared();
}
void nonrecursive_lock() {
base_class::lock();
}
bool try_nonrecursive_lock() {
return base_class::try_lock();
}
template<class _Rep, class _Period>
bool try_nonrecursive_lock_for(const std::chrono::duration<_Rep, _Period>& _Rel_time) {
return (try_nonrecursive_lock_until(std::chrono::steady_clock::now() + _Rel_time));
}
template<class _Clock, class _Duration>
bool try_nonrecursive_lock_until(const std::chrono::time_point<_Clock, _Duration>& _Abs_time) {
return base_class::try_lock_until(_Abs_time);
}
void nonrecursive_unlock() {
base_class::unlock();
}
};
class dummy_recursive_shared_timed_mutex {
public:
void lock() {
}
bool try_lock() {
return true;
}
template<class _Rep, class _Period>
bool try_lock_for(const std::chrono::duration<_Rep, _Period>& _Rel_time) { // try to lock for duration
return (try_lock_until(std::chrono::steady_clock::now() + _Rel_time));
}
template<class _Clock, class _Duration>
bool try_lock_until(const std::chrono::time_point<_Clock, _Duration>& _Abs_time) { // try to lock until time point
return try_lock();
}
void unlock() {
}
void nonrecursive_lock() {
lock();
}
bool try_nonrecursive_lock() { // try to lock nonrecursive
return try_lock();
}
template<class _Rep, class _Period>
bool try_nonrecursive_lock_for(const std::chrono::duration<_Rep, _Period>& _Rel_time) { // try to nonrecursive lock for duration
return (try_nonrecursive_lock_until(std::chrono::steady_clock::now() + _Rel_time));
}
template<class _Clock, class _Duration>
bool try_nonrecursive_lock_until(const std::chrono::time_point<_Clock, _Duration>& _Abs_time) { // try to nonrecursive lock until time point
return try_lock_until(_Abs_time);
}
void nonrecursive_unlock() {
unlock();
}
void lock_shared() { // lock non-exclusive
}
bool try_lock_shared() { // try to lock non-exclusive
return true;
}
template<class _Rep, class _Period>
bool try_lock_shared_for(const std::chrono::duration<_Rep, _Period>& _Rel_time) { // try to lock non-exclusive for relative time
return (try_lock_shared_until(_Rel_time + std::chrono::steady_clock::now()));
}
template<class _Clock, class _Duration>
bool try_lock_shared_until(const std::chrono::time_point<_Clock, _Duration>& _Abs_time) { // try to lock non-exclusive until absolute time
return try_lock_shared();
}
void unlock_shared() { // unlock non-exclusive
}
};
template<typename T>
struct HasNonrecursiveUnlockMethod_msemsearray {
template<typename U, void(U::*)() const> struct SFINAE {};
template<typename U> static char Test(SFINAE<U, &U::nonrecursive_unlock>*);
template<typename U> static int Test(...);
static const bool Has = (sizeof(Test<T>(0)) == sizeof(char));
};
template<typename T>
struct HasUnlockSharedMethod_msemsearray {
template<typename U, void(U::*)() const> struct SFINAE {};
template<typename U> static char Test(SFINAE<U, &U::unlock_shared>*);
template<typename U> static int Test(...);
static const bool Has = (sizeof(Test<T>(0)) == sizeof(char));
};
template<class _Mutex>
class recursive_shared_mutex_wrapped : public _Mutex {
public:
typedef _Mutex base_class;
void nonrecursive_lock() {
nonrecursive_lock_helper(typename std::conditional<HasNonrecursiveUnlockMethod_msemsearray<_Mutex>::Has, std::true_type, std::false_type>::type());
}
bool try_nonrecursive_lock() { // try to lock nonrecursive
return nonrecursive_try_lock_helper(typename std::conditional<HasNonrecursiveUnlockMethod_msemsearray<_Mutex>::Has, std::true_type, std::false_type>::type());
}
void nonrecursive_unlock() {
nonrecursive_unlock_helper(typename std::conditional<HasNonrecursiveUnlockMethod_msemsearray<_Mutex>::Has, std::true_type, std::false_type>::type());
}
void lock_shared() { // lock non-exclusive
lock_shared_helper(typename std::conditional<HasUnlockSharedMethod_msemsearray<_Mutex>::Has, std::true_type, std::false_type>::type());
}
bool try_lock_shared() { // try to lock non-exclusive
return try_lock_shared_helper(typename std::conditional<HasUnlockSharedMethod_msemsearray<_Mutex>::Has, std::true_type, std::false_type>::type());
}
void unlock_shared() { // unlock non-exclusive
unlock_shared_helper(typename std::conditional<HasUnlockSharedMethod_msemsearray<_Mutex>::Has, std::true_type, std::false_type>::type());
}
private:
void nonrecursive_lock_helper(std::true_type) {
base_class::nonrecursive_lock();
}
void nonrecursive_lock_helper(std::false_type) {
base_class::lock();
}
bool nonrecursive_try_lock_helper(std::true_type) {
return base_class::nonrecursive_try_lock();
}
bool nonrecursive_try_lock_helper(std::false_type) {
return base_class::try_lock();
}
void nonrecursive_unlock_helper(std::true_type) {
base_class::nonrecursive_unlock();
}
void nonrecursive_unlock_helper(std::false_type) {
base_class::unlock();
}
void lock_shared_helper(std::true_type) {
base_class::lock_shared();
}
void lock_shared_helper(std::false_type) {
base_class::lock();
}
bool try_lock_shared_helper(std::true_type) {
return base_class::try_lock_shared();
}
bool try_lock_shared_helper(std::false_type) {
return base_class::try_lock();
}
void unlock_shared_helper(std::true_type) {
base_class::unlock_shared();
}
void unlock_shared_helper(std::false_type) {
base_class::unlock();
}
};
template<class _Mutex>
class unique_nonrecursive_lock
{ // a version of std::unique_lock that calls "nonrecursive_lock()" instead of "lock()"
public:
typedef unique_nonrecursive_lock<_Mutex> _Myt;
typedef _Mutex mutex_type;
// CONSTRUCT, ASSIGN, AND DESTROY
unique_nonrecursive_lock() _NOEXCEPT
: _Pmtx(0), _Owns(false)
{ // default construct
}
explicit unique_nonrecursive_lock(_Mutex& _Mtx)
: _Pmtx(&_Mtx), _Owns(false)
{ // construct and lock
_Pmtx->nonrecursive_lock();
_Owns = true;
}
unique_nonrecursive_lock(_Mutex& _Mtx, std::adopt_lock_t)
: _Pmtx(&_Mtx), _Owns(true)
{ // construct and assume already locked
}
unique_nonrecursive_lock(_Mutex& _Mtx, std::defer_lock_t) _NOEXCEPT
: _Pmtx(&_Mtx), _Owns(false)
{ // construct but don't lock
}
unique_nonrecursive_lock(_Mutex& _Mtx, std::try_to_lock_t)
: _Pmtx(&_Mtx), _Owns(_Pmtx->try_nonrecursive_lock())
{ // construct and try to lock
}
template<class _Rep,
class _Period>
unique_nonrecursive_lock(_Mutex& _Mtx,
const std::chrono::duration<_Rep, _Period>& _Rel_time)
: _Pmtx(&_Mtx), _Owns(_Pmtx->try_nonrecursive_lock_for(_Rel_time))
{ // construct and lock with timeout
}
template<class _Clock,
class _Duration>
unique_nonrecursive_lock(_Mutex& _Mtx,
const std::chrono::time_point<_Clock, _Duration>& _Abs_time)
: _Pmtx(&_Mtx), _Owns(_Pmtx->try_nonrecursive_lock_until(_Abs_time))
{ // construct and lock with timeout
}
unique_nonrecursive_lock(unique_nonrecursive_lock&& _Other) _NOEXCEPT
: _Pmtx(_Other._Pmtx), _Owns(_Other._Owns)
{ // destructive copy
_Other._Pmtx = 0;
_Other._Owns = false;
}
unique_nonrecursive_lock& operator=(unique_nonrecursive_lock&& _Other)
{ // destructive copy
if (this != &_Other)
{ // different, move contents
if (_Owns)
_Pmtx->nonrecursive_unlock();
_Pmtx = _Other._Pmtx;
_Owns = _Other._Owns;
_Other._Pmtx = 0;
_Other._Owns = false;
}
return (*this);
}
~unique_nonrecursive_lock() _NOEXCEPT
{ // clean up
if (_Owns)
_Pmtx->nonrecursive_unlock();
}
unique_nonrecursive_lock(const unique_nonrecursive_lock&) = delete;
unique_nonrecursive_lock& operator=(const unique_nonrecursive_lock&) = delete;
// LOCK AND UNLOCK
void lock()
{ // lock the mutex
_Validate();
_Pmtx->nonrecursive_lock();
_Owns = true;
}
bool try_lock()
{ // try to lock the mutex
_Validate();
_Owns = _Pmtx->try_nonrecursive_lock();
return (_Owns);
}
template<class _Rep,
class _Period>
bool try_lock_for(const std::chrono::duration<_Rep, _Period>& _Rel_time)
{ // try to lock mutex for _Rel_time
_Validate();
_Owns = _Pmtx->try_nonrecursive_lock_for(_Rel_time);
return (_Owns);
}
template<class _Clock,
class _Duration>
bool try_lock_until(
const std::chrono::time_point<_Clock, _Duration>& _Abs_time)
{ // try to lock mutex until _Abs_time
_Validate();
_Owns = _Pmtx->try_nonrecursive_lock_until(_Abs_time);
return (_Owns);
}
void unlock()
{ // try to unlock the mutex
if (!_Pmtx || !_Owns)
_THROW_NCEE(std::system_error,
_STD make_error_code(std::errc::operation_not_permitted));
_Pmtx->nonrecursive_unlock();
_Owns = false;
}
// MUTATE
void swap(unique_nonrecursive_lock& _Other) _NOEXCEPT
{ // swap with _Other
_STD swap(_Pmtx, _Other._Pmtx);
_STD swap(_Owns, _Other._Owns);
}
_Mutex *release() _NOEXCEPT
{ // disconnect
_Mutex *_Res = _Pmtx;
_Pmtx = 0;
_Owns = false;
return (_Res);
}
// OBSERVE
bool owns_lock() const _NOEXCEPT
{ // return true if this object owns the lock
return (_Owns);
}
explicit operator bool() const _NOEXCEPT
{ // return true if this object owns the lock
return (_Owns);
}
_Mutex *mutex() const _NOEXCEPT
{ // return pointer to managed mutex
return (_Pmtx);
}
private:
_Mutex *_Pmtx;
bool _Owns;
void _Validate() const
{ // check if the mutex can be locked
if (!_Pmtx)
_THROW_NCEE(std::system_error,
_STD make_error_code(std::errc::operation_not_permitted));
if (_Owns)
_THROW_NCEE(std::system_error,
_STD make_error_code(std::errc::resource_deadlock_would_occur));
}
};
#ifndef _XSTD
#define _XSTD ::std::
#endif /*_XSTD*/
#ifndef _STD
#define _STD std::
#endif /*_STD*/
#ifndef _NOEXCEPT
#define _NOEXCEPT
#endif /*_NOEXCEPT*/
#ifndef _CONST_FUN
#define _CONST_FUN constexpr
#endif /*_CONST_FUN*/
/* Some iterators are prone to having their target container prematurely deallocated out from under them. If you have a safe pointer
to the target container, you can use TSyncWeakFixedIterator<> as a safe iterator that welds a copy of the safe pointer (aka "lease")
to the iterator. */
template <class _TIterator, class _TLeasePointer>
class TSyncWeakFixedIterator : public _TIterator {
public:
typedef _TIterator base_class;
TSyncWeakFixedIterator(const TSyncWeakFixedIterator&) = default;
template<class _TLeasePointer2, class = typename std::enable_if<std::is_convertible<_TLeasePointer2, _TLeasePointer>::value, void>::type>
TSyncWeakFixedIterator(const TSyncWeakFixedIterator<_TIterator, _TLeasePointer2>&src) : base_class(src), m_lease_pointer(src.lease_pointer()) {}
auto operator*() const -> typename std::add_lvalue_reference<decltype(*((*this).m_target_pointer))>::type {
/*const auto &test_cref =*/ *m_lease_pointer; // this should throw if m_lease_pointer is no longer valid
return (*((*this).m_target_pointer));
}
auto operator->() const -> decltype(std::addressof(*((*this).m_target_pointer))) {
/*const auto &test_cref =*/ *m_lease_pointer; // this should throw if m_lease_pointer is no longer valid
return std::addressof(*((*this).m_target_pointer));
}
_TLeasePointer lease_pointer() const { return (*this).m_lease_pointer; }
template <class _TIterator2, class _TLeasePointer2>
static TSyncWeakFixedIterator make(const _TIterator2& src_iterator, const _TLeasePointer2& lease_pointer) {
return TSyncWeakFixedIterator(src_iterator, lease_pointer);
}
void not_async_shareable_tag() const {} /* Indication that this type is not eligible to be shared between threads. */
protected:
TSyncWeakFixedIterator(const _TIterator& src_iterator, const _TLeasePointer& lease_pointer/* often a registered pointer */)
: base_class(src_iterator), m_lease_pointer(lease_pointer) {}
private:
TSyncWeakFixedIterator& operator=(const TSyncWeakFixedIterator& _Right_cref) = delete;
_TLeasePointer m_lease_pointer;
//friend class TSyncWeakFixedConstIterator<_TIterator, _TLeasePointer>;
};
template <class _Ty, class _TLeasePointer>
class TSyncWeakFixedIterator<_Ty*, _TLeasePointer> : public TSaferPtrForLegacy<_Ty> {
public:
typedef TSaferPtrForLegacy<_Ty> _TIterator;
typedef _TIterator base_class;
TSyncWeakFixedIterator(const TSyncWeakFixedIterator&) = default;
template<class _TLeasePointer2, class = typename std::enable_if<std::is_convertible<_TLeasePointer2, _TLeasePointer>::value, void>::type>
TSyncWeakFixedIterator(const TSyncWeakFixedIterator<_TIterator, _TLeasePointer2>&src) : base_class(src), m_lease_pointer(src.lease_pointer()) {}
auto operator*() const -> typename std::add_lvalue_reference<decltype(*((*this).m_target_pointer))>::type {
/*const auto &test_cref =*/ *m_lease_pointer; // this should throw if m_lease_pointer is no longer valid
return (*((*this).m_target_pointer));
}
auto operator->() const -> decltype(std::addressof(*((*this).m_target_pointer))) {
/*const auto &test_cref =*/ *m_lease_pointer; // this should throw if m_lease_pointer is no longer valid
return std::addressof(*((*this).m_target_pointer));
}
_TLeasePointer lease_pointer() const { return (*this).m_lease_pointer; }
template <class _TIterator2, class _TLeasePointer2>
static TSyncWeakFixedIterator make(const _TIterator2& src_iterator, const _TLeasePointer2& lease_pointer) {
return TSyncWeakFixedIterator(src_iterator, lease_pointer);
}
void not_async_shareable_tag() const {} /* Indication that this type is not eligible to be shared between threads. */
protected:
TSyncWeakFixedIterator(const _TIterator& src_iterator, const _TLeasePointer& lease_pointer/* often a registered pointer */)
: base_class(src_iterator), m_lease_pointer(lease_pointer) {}
TSyncWeakFixedIterator(const _Ty* & src_iterator, const _TLeasePointer& lease_pointer/* often a registered pointer */)
: base_class(TSaferPtrForLegacy<_Ty>(src_iterator)), m_lease_pointer(lease_pointer) {}
private:
TSyncWeakFixedIterator& operator=(const TSyncWeakFixedIterator& _Right_cref) = delete;
_TLeasePointer m_lease_pointer;
//friend class TSyncWeakFixedConstIterator<_TIterator, _TLeasePointer>;
};
template <class _TIterator, class _TLeasePointer>
TSyncWeakFixedIterator<_TIterator, _TLeasePointer> make_syncweak_iterator(const _TIterator& src_iterator, const _TLeasePointer& lease_pointer) {
return TSyncWeakFixedIterator<_TIterator, _TLeasePointer>::make(src_iterator, lease_pointer);
}
template<typename T>
struct HasNotAsyncShareableTagMethod_msemsearray
{
template<typename U, void(U::*)() const> struct SFINAE {};
template<typename U> static char Test(SFINAE<U, &U::not_async_shareable_tag>*);
template<typename U> static int Test(...);
static const bool Has = (sizeof(Test<T>(0)) == sizeof(char));
};
template<typename T>
struct HasAsyncShareableTagMethod_msemsearray
{
template<typename U, void(U::*)() const> struct SFINAE {};
template<typename U> static char Test(SFINAE<U, &U::async_shareable_tag>*);
template<typename U> static int Test(...);
static const bool Has = (sizeof(Test<T>(0)) == sizeof(char));
};
template<class _Ty, size_t _Size>
class array_helper_type {
public:
static typename std::array<_Ty, _Size> std_array_initial_value2(_XSTD initializer_list<_Ty> _Ilist) {
/* Template specializations of this function construct mse::msearrays of non-default constructible
elements. This (non-specialized) implementation here should cause a compile error when invoked. */
if (0 < _Ilist.size()) { MSE_THROW(msearray_range_error("sorry, arrays of this size are not supported when the elements are non-default constructible - mse::mstd::array")); }
typename std::array<_Ty, _Size> retval{};
return retval;
}
};
template<class _Ty> class array_helper_type<_Ty, 1> {
public:
static typename std::array<_Ty, 1> std_array_initial_value2(_XSTD initializer_list<_Ty> _Ilist) {
/* This template specialization constructs an mse::msearray of size 1 and supports non-default
constructible elements. */
if (1 != _Ilist.size()) { MSE_THROW(msearray_range_error("the size of the initializer list does not match the size of the array - mse::mstd::array")); }
typename std::array<_Ty, 1> retval{ *(_Ilist.begin()) }; return retval;
}
};
/* Template specializations that construct mse::msearrays of different sizes are located later in the file. */
template<class _StateMutex>
class destructor_lock_guard1 {
public:
explicit destructor_lock_guard1(_StateMutex& _Mtx) : _MyStateMutex(_Mtx) {
try {
_Mtx.lock();
}
catch (...) {
/* It may not be safe to continue if the object is destroyed while the object state is locked (and presumably
in use) by another part of the code. */
std::cerr << "\n\nFatal Error: mse::destructor_lock_guard1() failed \n\n";
std::terminate();
}
}
~destructor_lock_guard1() _NOEXCEPT {
_MyStateMutex.unlock();
}
destructor_lock_guard1(const destructor_lock_guard1&) = delete;
destructor_lock_guard1& operator=(const destructor_lock_guard1&) = delete;
private:
_StateMutex& _MyStateMutex;
};
typedef
#if !defined(NDEBUG) || defined(MSE_ENABLE_REENTRANCY_CHECKS_BY_DEFAULT)
non_thread_safe_mutex
#else // !defined(NDEBUG) || defined(MSE_ENABLE_REENTRANCY_CHECKS_BY_DEFAULT)
dummy_recursive_shared_timed_mutex
#endif // !defined(NDEBUG) || defined(MSE_ENABLE_REENTRANCY_CHECKS_BY_DEFAULT)
default_state_mutex;
namespace us {
template<class _Ty, size_t _Size, class _TStateMutex = default_state_mutex>
class msearray;
}
/* nii_array<> is essentially a memory-safe array that does not expose (unprotected) non-static member functions
like begin() or end() which return (memory) unsafe iterators. It does provide static member function templates
like ss_begin<>(...) and ss_end<>(...) which take a pointer parameter and return a (bounds-checked) iterator that
inherits the safety of the given pointer. nii_array<> also supports "scope" iterators which are safe without any
run-time overhead. nii_array<> is a data type that is eligible to be shared between asynchronous threads. */
template<class _Ty, size_t _Size, class _TStateMutex = default_state_mutex>
class nii_array {
public:
typedef std::array<_Ty, _Size> std_array;
typedef std_array _MA;
typedef nii_array<_Ty, _Size> _Myt;
nii_array() {}
nii_array(_MA&& _X) : m_array(std::forward<decltype(_X)>(_X)) {}
nii_array(const _MA& _X) : m_array(_X) {}
nii_array(_Myt&& _X) : m_array(std::forward<decltype(_X.contained_array())>(_X.contained_array())) {}
nii_array(const _Myt& _X) : m_array(_X.contained_array()) {}
//nii_array(_XSTD initializer_list<typename _MA::base_class::value_type> _Ilist) : m_array(_Ilist) {}
static std::array<_Ty, _Size> std_array_initial_value(std::true_type, _XSTD initializer_list<_Ty> _Ilist) {
/* _Ty is default constructible. */
std::array<_Ty, _Size> retval;
assert(_Size >= _Ilist.size());
auto stop_size = _Size;
if (_Size > _Ilist.size()) {
stop_size = _Ilist.size();
/* just to make sure that all the retval elements are initialized as if by aggregate initialization. */
retval = std::array<_Ty, _Size>{};
}
msear_size_t count = 0;
auto Il_it = _Ilist.begin();
auto target_it = retval.begin();
for (; (count < stop_size); Il_it++, count += 1, target_it++) {
(*target_it) = (*Il_it);
}
return retval;
}
static std::array<_Ty, _Size> std_array_initial_value(std::false_type, _XSTD initializer_list<_Ty> _Ilist) {
/* _Ty is not default constructible. */
return array_helper_type<_Ty, _Size>::std_array_initial_value2(_Ilist);
}
nii_array(_XSTD initializer_list<_Ty> _Ilist) : m_array(std_array_initial_value(std::is_default_constructible<_Ty>(), _Ilist)) {
/* std::array<> is an "aggregate type" (basically a POD struct with no base class, constructors or private
data members (details here: http://en.cppreference.com/w/cpp/language/aggregate_initialization)). As such,
support for construction from initializer list is automatically generated by the compiler. Specifically,
aggregate types support "aggregate initialization". But since mstd::array has a member with an explicitly
defined constructor (or at least I think that's why), it is not an aggregate type and therefore doesn't
qualify to have support for "aggregate initialization" automatically generated by the compiler. It doesn't
seem possible to emulate full aggregate initialization compatibility, so we'll just have to do the best we
can. */
}
~nii_array() {
mse::destructor_lock_guard1<_TStateMutex> lock1(m_mutex1);
/* This is just a no-op function that will cause a compile error when _Ty is not an eligible type. */
valid_if_Ty_is_not_an_xscope_type();
}
operator const _MA() const { return contained_array(); }
operator _MA() { return contained_array(); }
_CONST_FUN typename std_array::const_reference operator[](msear_size_t _P) const {
return (*this).at(msear_as_a_size_t(_P));
}
typename std_array::reference operator[](msear_size_t _P) {
return (*this).at(msear_as_a_size_t(_P));
}
typename std_array::reference front() { // return first element of mutable sequence
if (0 == (*this).size()) { MSE_THROW(nii_array_range_error("front() on empty - typename std_array::reference front() - nii_array")); }
return m_array.front();
}
_CONST_FUN typename std_array::const_reference front() const { // return first element of nonmutable sequence
if (0 == (*this).size()) { MSE_THROW(nii_array_range_error("front() on empty - typename std_array::const_reference front() - nii_array")); }
return m_array.front();
}
typename std_array::reference back() { // return last element of mutable sequence
if (0 == (*this).size()) { MSE_THROW(nii_array_range_error("back() on empty - typename std_array::reference back() - nii_array")); }
return m_array.back();
}
_CONST_FUN typename std_array::const_reference back() const { // return last element of nonmutable sequence
if (0 == (*this).size()) { MSE_THROW(nii_array_range_error("back() on empty - typename std_array::const_reference back() - nii_array")); }
return m_array.back();
}
typedef typename std_array::value_type value_type;
//typedef typename std_array::size_type size_type;
typedef msear_size_t size_type;
//typedef typename std_array::difference_type difference_type;
typedef msear_int difference_type;
typedef typename std_array::pointer pointer;
typedef typename std_array::const_pointer const_pointer;
typedef typename std_array::reference reference;
typedef typename std_array::const_reference const_reference;
typedef typename std_array::iterator iterator;
typedef typename std_array::const_iterator const_iterator;
typedef typename std_array::reverse_iterator reverse_iterator;
typedef typename std_array::const_reverse_iterator const_reverse_iterator;
void assign(const _Ty& _Value)
{ // assign value to all elements
std::lock_guard<_TStateMutex> lock1(m_mutex1);
m_array.assign(_Value);
}
void fill(const _Ty& _Value)
{ // assign value to all elements
std::lock_guard<_TStateMutex> lock1(m_mutex1);
m_array.fill(_Value);
}
void swap(_Myt& _Other) /*_NOEXCEPT_OP(_NOEXCEPT_OP(_Swap_adl(this->m_array[0], _Other.m_array[0])))*/
{ // swap contents with _Other
std::lock_guard<_TStateMutex> lock1(m_mutex1);
m_array.swap(_Other.m_array);
}
void swap(_MA& _Other) /*_NOEXCEPT_OP(_NOEXCEPT_OP(_Swap_adl(this->m_array[0], _Other[0])))*/
{ // swap contents with _Other
std::lock_guard<_TStateMutex> lock1(m_mutex1);
m_array.swap(_Other);
}
_CONST_FUN size_type size() const _NOEXCEPT
{ // return length of sequence
return m_array.size();
}
_CONST_FUN size_type max_size() const _NOEXCEPT
{ // return maximum possible length of sequence
return m_array.max_size();
}
_CONST_FUN bool empty() const _NOEXCEPT
{ // test if sequence is empty
return m_array.empty();
}
reference at(msear_size_t _Pos)
{ // subscript mutable sequence with checking
return m_array.at(msear_as_a_size_t(_Pos));
}
_CONST_FUN const_reference at(msear_size_t _Pos) const
{ // subscript nonmutable sequence with checking
return m_array.at(msear_as_a_size_t(_Pos));
}
value_type *data() _NOEXCEPT
{ // return pointer to mutable data array
return m_array.data();
}
const value_type *data() const _NOEXCEPT
{ // return pointer to nonmutable data array
return m_array.data();
}
nii_array& operator=(const nii_array& _Right_cref) {
std::lock_guard<_TStateMutex> lock1(m_mutex1);
m_array = _Right_cref.m_array;
return (*this);
}
/*
nii_array& operator=(const std_array& _Right_cref) {
std::lock_guard<_TStateMutex> lock1(m_mutex1);
m_array = _Right_cref;
return (*this);
}
*/
class random_access_const_iterator_base : public std::iterator<std::random_access_iterator_tag, value_type, difference_type, const_pointer, const_reference> {};
class random_access_iterator_base : public std::iterator<std::random_access_iterator_tag, value_type, difference_type, pointer, reference> {};
class xscope_ss_const_iterator_type;
class xscope_ss_iterator_type;
/* The reason we specify the default parameter in the definition instead of this forward declaration is that there seems to be a
bug in clang (3.8.0) such that if we don't specify the default parameter in the definition it seems to subsequently behave as if
one were never specified. g++ and msvc don't seem to have the same issue. */
template<typename _TArrayPointer, class/* = typename std::enable_if<(!std::is_base_of<XScopeTagBase, _TArrayPointer>::value), void>::type*/>
class Tss_iterator_type;
/* Tss_const_iterator_type is a bounds checked const_iterator. */
template<typename _TArrayConstPointer, class = typename std::enable_if<(!std::is_base_of<XScopeTagBase, _TArrayConstPointer>::value), void>::type>
class Tss_const_iterator_type : public random_access_const_iterator_base {
public:
typedef typename std::iterator_traits<typename std_array::const_iterator>::iterator_category iterator_category;
typedef typename std::iterator_traits<typename std_array::const_iterator>::value_type value_type;
//typedef typename std::iterator_traits<typename std_array::const_iterator>::difference_type difference_type;
typedef msear_int difference_type;
typedef typename std::iterator_traits<typename std_array::const_iterator>::pointer const_pointer;
typedef typename std::iterator_traits<typename std_array::const_iterator>::reference const_reference;
typedef typename std::iterator_traits<typename std_array::const_iterator>::pointer pointer;
typedef typename std::iterator_traits<typename std_array::const_iterator>::reference reference;
template<class = typename std::enable_if<std::is_default_constructible<_TArrayConstPointer>::value, void>::type>
Tss_const_iterator_type() {}
Tss_const_iterator_type(const _TArrayConstPointer& owner_cptr) : m_owner_cptr(owner_cptr) {}
Tss_const_iterator_type(const Tss_const_iterator_type& src) = default;
template<class _Ty2, class = typename std::enable_if<std::is_convertible<_Ty2, _TArrayConstPointer>::value, void>::type>
Tss_const_iterator_type(const Tss_iterator_type<_Ty2, void>& src) : m_owner_cptr(src.target_container_ptr()), m_index(src.position()) {}
void reset() { set_to_end_marker(); }
bool points_to_an_item() const {
if (m_owner_cptr->size() > m_index) { return true; }
else {