forked from gsl-lite/gsl-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsl-lite.hpp
3019 lines (2383 loc) · 81.9 KB
/
gsl-lite.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
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
//
// gsl-lite is based on GSL: Guidelines Support Library.
// For more information see https://github.com/martinmoene/gsl-lite
//
// Copyright (c) 2015-2018 Martin Moene
// Copyright (c) 2015-2018 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// 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.
#pragma once
#ifndef GSL_GSL_LITE_HPP_INCLUDED
#define GSL_GSL_LITE_HPP_INCLUDED
#include <algorithm>
#include <exception>
#include <iterator>
#include <limits>
#include <memory>
#include <ostream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#define gsl_lite_MAJOR 0
#define gsl_lite_MINOR 34
#define gsl_lite_PATCH 0
#define gsl_lite_VERSION gsl_STRINGIFY(gsl_lite_MAJOR) "." gsl_STRINGIFY(gsl_lite_MINOR) "." gsl_STRINGIFY(gsl_lite_PATCH)
// gsl-lite backward compatibility:
#ifdef gsl_CONFIG_ALLOWS_SPAN_CONTAINER_CTOR
# define gsl_CONFIG_ALLOWS_UNCONSTRAINED_SPAN_CONTAINER_CTOR gsl_CONFIG_ALLOWS_SPAN_CONTAINER_CTOR
# pragma message ("gsl_CONFIG_ALLOWS_SPAN_CONTAINER_CTOR is deprecated since gsl-lite 0.7.0; replace with gsl_CONFIG_ALLOWS_UNCONSTRAINED_SPAN_CONTAINER_CTOR, or consider span(with_container, cont).")
#endif
// M-GSL compatibility:
#if defined( GSL_THROW_ON_CONTRACT_VIOLATION )
# define gsl_CONFIG_CONTRACT_VIOLATION_THROWS 1
#endif
#if defined( GSL_TERMINATE_ON_CONTRACT_VIOLATION )
# define gsl_CONFIG_CONTRACT_VIOLATION_TERMINATES 1
#endif
#if defined( GSL_UNENFORCED_ON_CONTRACT_VIOLATION )
# define gsl_CONFIG_CONTRACT_LEVEL_OFF 1
#endif
// Configuration: Features
#ifndef gsl_FEATURE_WITH_CONTAINER_TO_STD
# define gsl_FEATURE_WITH_CONTAINER_TO_STD 99
#endif
#ifndef gsl_FEATURE_MAKE_SPAN_TO_STD
# define gsl_FEATURE_MAKE_SPAN_TO_STD 99
#endif
#ifndef gsl_FEATURE_BYTE_SPAN_TO_STD
# define gsl_FEATURE_BYTE_SPAN_TO_STD 99
#endif
#ifndef gsl_FEATURE_IMPLICIT_MACRO
# define gsl_FEATURE_IMPLICIT_MACRO 0
#endif
#ifndef gsl_FEATURE_OWNER_MACRO
# define gsl_FEATURE_OWNER_MACRO 1
#endif
#ifndef gsl_FEATURE_EXPERIMENTAL_RETURN_GUARD
# define gsl_FEATURE_EXPERIMENTAL_RETURN_GUARD 0
#endif
// Configuration: Other
#ifndef gsl_CONFIG_DEPRECATE_TO_LEVEL
# define gsl_CONFIG_DEPRECATE_TO_LEVEL 0
#endif
#ifndef gsl_CONFIG_SPAN_INDEX_TYPE
# define gsl_CONFIG_SPAN_INDEX_TYPE size_t
#endif
#ifndef gsl_CONFIG_NOT_NULL_EXPLICIT_CTOR
# define gsl_CONFIG_NOT_NULL_EXPLICIT_CTOR 0
#endif
#ifndef gsl_CONFIG_NOT_NULL_GET_BY_CONST_REF
# define gsl_CONFIG_NOT_NULL_GET_BY_CONST_REF 0
#endif
#ifndef gsl_CONFIG_CONFIRMS_COMPILATION_ERRORS
# define gsl_CONFIG_CONFIRMS_COMPILATION_ERRORS 0
#endif
#ifndef gsl_CONFIG_ALLOWS_NONSTRICT_SPAN_COMPARISON
# define gsl_CONFIG_ALLOWS_NONSTRICT_SPAN_COMPARISON 1
#endif
#ifndef gsl_CONFIG_ALLOWS_UNCONSTRAINED_SPAN_CONTAINER_CTOR
# define gsl_CONFIG_ALLOWS_UNCONSTRAINED_SPAN_CONTAINER_CTOR 0
#endif
#if defined( gsl_CONFIG_CONTRACT_LEVEL_ON )
# define gsl_CONFIG_CONTRACT_LEVEL_MASK 0x11
#elif defined( gsl_CONFIG_CONTRACT_LEVEL_OFF )
# define gsl_CONFIG_CONTRACT_LEVEL_MASK 0x00
#elif defined( gsl_CONFIG_CONTRACT_LEVEL_EXPECTS_ONLY )
# define gsl_CONFIG_CONTRACT_LEVEL_MASK 0x01
#elif defined( gsl_CONFIG_CONTRACT_LEVEL_ENSURES_ONLY )
# define gsl_CONFIG_CONTRACT_LEVEL_MASK 0x10
#else
# define gsl_CONFIG_CONTRACT_LEVEL_MASK 0x11
#endif
#if 2 <= defined( gsl_CONFIG_CONTRACT_VIOLATION_THROWS ) + defined( gsl_CONFIG_CONTRACT_VIOLATION_TERMINATES ) + defined ( gsl_CONFIG_CONTRACT_VIOLATION_CALLS_HANDLER )
# error only one of gsl_CONFIG_CONTRACT_VIOLATION_THROWS, gsl_CONFIG_CONTRACT_VIOLATION_TERMINATES and gsl_CONFIG_CONTRACT_VIOLATION_CALLS_HANDLER may be defined.
#elif defined( gsl_CONFIG_CONTRACT_VIOLATION_THROWS )
# define gsl_CONFIG_CONTRACT_VIOLATION_THROWS_V 1
# define gsl_CONFIG_CONTRACT_VIOLATION_CALLS_HANDLER_V 0
#elif defined( gsl_CONFIG_CONTRACT_VIOLATION_TERMINATES )
# define gsl_CONFIG_CONTRACT_VIOLATION_THROWS_V 0
# define gsl_CONFIG_CONTRACT_VIOLATION_CALLS_HANDLER_V 0
#elif defined( gsl_CONFIG_CONTRACT_VIOLATION_CALLS_HANDLER )
# define gsl_CONFIG_CONTRACT_VIOLATION_THROWS_V 0
# define gsl_CONFIG_CONTRACT_VIOLATION_CALLS_HANDLER_V 1
#else
# define gsl_CONFIG_CONTRACT_VIOLATION_THROWS_V 0
# define gsl_CONFIG_CONTRACT_VIOLATION_CALLS_HANDLER_V 0
#endif
// C++ language version detection (C++20 is speculative):
// Note: VC14.0/1900 (VS2015) lacks too much from C++14.
#ifndef gsl_CPLUSPLUS
# if defined(_MSVC_LANG ) && !defined(__clang__)
# define gsl_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG )
# else
# define gsl_CPLUSPLUS __cplusplus
# endif
#endif
#define gsl_CPP98_OR_GREATER ( gsl_CPLUSPLUS >= 199711L )
#define gsl_CPP11_OR_GREATER ( gsl_CPLUSPLUS >= 201103L )
#define gsl_CPP14_OR_GREATER ( gsl_CPLUSPLUS >= 201402L )
#define gsl_CPP17_OR_GREATER ( gsl_CPLUSPLUS >= 201703L )
#define gsl_CPP20_OR_GREATER ( gsl_CPLUSPLUS >= 202000L )
// C++ language version (represent 98 as 3):
#define gsl_CPLUSPLUS_V ( gsl_CPLUSPLUS / 100 - (gsl_CPLUSPLUS > 200000 ? 2000 : 1994) )
// half-open range [lo..hi):
#define gsl_BETWEEN( v, lo, hi ) ( (lo) <= (v) && (v) < (hi) )
// Compiler versions:
//
// MSVC++ 6.0 _MSC_VER == 1200 (Visual Studio 6.0)
// MSVC++ 7.0 _MSC_VER == 1300 (Visual Studio .NET 2002)
// MSVC++ 7.1 _MSC_VER == 1310 (Visual Studio .NET 2003)
// MSVC++ 8.0 _MSC_VER == 1400 (Visual Studio 2005)
// MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008)
// MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010)
// MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012)
// MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013)
// MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015)
// MSVC++ 14.1 _MSC_VER >= 1910 (Visual Studio 2017)
#if defined(_MSC_VER ) && !defined(__clang__)
# define gsl_COMPILER_MSVC_VER (_MSC_VER )
# define gsl_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * ( 5 + (_MSC_VER < 1900 ) ) )
#else
# define gsl_COMPILER_MSVC_VER 0
# define gsl_COMPILER_MSVC_VERSION 0
#endif
#define gsl_COMPILER_VERSION( major, minor, patch ) ( 10 * ( 10 * (major) + (minor) ) + (patch) )
#if defined(__clang__)
# define gsl_COMPILER_CLANG_VERSION gsl_COMPILER_VERSION( __clang_major__, __clang_minor__, __clang_patchlevel__ )
#else
# define gsl_COMPILER_CLANG_VERSION 0
#endif
#if defined(__GNUC__) && !defined(__clang__)
# define gsl_COMPILER_GNUC_VERSION gsl_COMPILER_VERSION( __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__ )
#else
# define gsl_COMPILER_GNUC_VERSION 0
#endif
// Method enabling (C++98, VC120 (VS2013) cannot use __VA_ARGS__)
#define gsl_REQUIRES_0(VA) \
template< bool B = (VA), typename std::enable_if<B, int>::type = 0 >
#define gsl_REQUIRES_T(VA) \
, typename = typename std::enable_if< (VA), gsl::detail::enabler >::type
#define gsl_REQUIRES_R(R, VA) \
typename std::enable_if<VA, R>::type
#define gsl_REQUIRES_A(VA) \
, typename std::enable_if<VA, void*>::type = nullptr
// Compiler non-strict aliasing:
#if defined(__clang__) || defined(__GNUC__)
# define gsl_may_alias __attribute__((__may_alias__))
#else
# define gsl_may_alias
#endif
// Presence of gsl, language and library features:
#define gsl_IN_STD( v ) ( ((v) == 98 ? 3 : (v)) >= gsl_CPLUSPLUS_V )
#define gsl_DEPRECATE_TO_LEVEL( level ) ( level <= gsl_CONFIG_DEPRECATE_TO_LEVEL )
#define gsl_FEATURE_TO_STD( feature ) ( gsl_IN_STD( gsl_FEATURE( feature##_TO_STD ) ) )
#define gsl_FEATURE( feature ) ( gsl_FEATURE_##feature )
#define gsl_CONFIG( feature ) ( gsl_CONFIG_##feature )
#define gsl_HAVE( feature ) ( gsl_HAVE_##feature )
// Presence of wide character support:
#ifdef __DJGPP__
# define gsl_HAVE_WCHAR 0
#else
# define gsl_HAVE_WCHAR 1
#endif
// Presence of language & library features:
#ifdef _HAS_CPP0X
# define gsl_HAS_CPP0X _HAS_CPP0X
#else
# define gsl_HAS_CPP0X 0
#endif
#define gsl_CPP11_100 (gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VER >= 1600)
#define gsl_CPP11_110 (gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VER >= 1700)
#define gsl_CPP11_120 (gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VER >= 1800)
#define gsl_CPP11_140 (gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VER >= 1900)
#define gsl_CPP14_000 (gsl_CPP14_OR_GREATER)
#define gsl_CPP14_120 (gsl_CPP14_OR_GREATER || gsl_COMPILER_MSVC_VER >= 1800)
#define gsl_CPP14_140 (gsl_CPP14_OR_GREATER || gsl_COMPILER_MSVC_VER >= 1900)
#define gsl_CPP17_000 (gsl_CPP17_OR_GREATER)
#define gsl_CPP17_140 (gsl_CPP17_OR_GREATER || gsl_COMPILER_MSVC_VER >= 1900)
#define gsl_CPP11_140_CPP0X_90 (gsl_CPP11_140 || (gsl_COMPILER_MSVC_VER >= 1500 && gsl_HAS_CPP0X))
#define gsl_CPP11_140_CPP0X_100 (gsl_CPP11_140 || (gsl_COMPILER_MSVC_VER >= 1600 && gsl_HAS_CPP0X))
// Presence of C++11 language features:
#define gsl_HAVE_AUTO gsl_CPP11_100
#define gsl_HAVE_NULLPTR gsl_CPP11_100
#define gsl_HAVE_RVALUE_REFERENCE gsl_CPP11_100
#define gsl_HAVE_ENUM_CLASS gsl_CPP11_110
#define gsl_HAVE_ALIAS_TEMPLATE gsl_CPP11_120
#define gsl_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG gsl_CPP11_120
#define gsl_HAVE_EXPLICIT gsl_CPP11_120
#define gsl_HAVE_INITIALIZER_LIST gsl_CPP11_120
#define gsl_HAVE_CONSTEXPR_11 gsl_CPP11_140
#define gsl_HAVE_IS_DEFAULT gsl_CPP11_140
#define gsl_HAVE_IS_DELETE gsl_CPP11_140
#define gsl_HAVE_NOEXCEPT gsl_CPP11_140
#if gsl_CPP11_OR_GREATER
// see above
#endif
// Presence of C++14 language features:
#define gsl_HAVE_CONSTEXPR_14 gsl_CPP14_000
#define gsl_HAVE_DECLTYPE_AUTO gsl_CPP14_140
// Presence of C++17 language features:
// MSVC: template parameter deduction guides since Visual Studio 2017 v15.7
#define gsl_HAVE_ENUM_CLASS_CONSTRUCTION_FROM_UNDERLYING_TYPE gsl_CPP17_000
#define gsl_HAVE_DEDUCTION_GUIDES (gsl_CPP17_000 && ! gsl_BETWEEN( gsl_COMPILER_MSVC_VERSION, 1, 999 ) )
// Presence of C++ library features:
#define gsl_HAVE_ADDRESSOF gsl_CPP17_000
#define gsl_HAVE_ARRAY gsl_CPP11_110
#define gsl_HAVE_TYPE_TRAITS gsl_CPP11_110
#define gsl_HAVE_TR1_TYPE_TRAITS gsl_CPP11_110
#define gsl_HAVE_CONTAINER_DATA_METHOD gsl_CPP11_140_CPP0X_90
#define gsl_HAVE_STD_DATA gsl_CPP17_000
#define gsl_HAVE_SIZED_TYPES gsl_CPP11_140
#define gsl_HAVE_MAKE_SHARED gsl_CPP11_140_CPP0X_100
#define gsl_HAVE_SHARED_PTR gsl_CPP11_140_CPP0X_100
#define gsl_HAVE_UNIQUE_PTR gsl_CPP11_140_CPP0X_100
#define gsl_HAVE_MAKE_UNIQUE gsl_CPP14_120
#define gsl_HAVE_UNCAUGHT_EXCEPTIONS gsl_CPP17_140
#define gsl_HAVE_ADD_CONST gsl_HAVE_TYPE_TRAITS
#define gsl_HAVE_INTEGRAL_CONSTANT gsl_HAVE_TYPE_TRAITS
#define gsl_HAVE_REMOVE_CONST gsl_HAVE_TYPE_TRAITS
#define gsl_HAVE_REMOVE_REFERENCE gsl_HAVE_TYPE_TRAITS
#define gsl_HAVE_TR1_ADD_CONST gsl_HAVE_TR1_TYPE_TRAITS
#define gsl_HAVE_TR1_INTEGRAL_CONSTANT gsl_HAVE_TR1_TYPE_TRAITS
#define gsl_HAVE_TR1_REMOVE_CONST gsl_HAVE_TR1_TYPE_TRAITS
#define gsl_HAVE_TR1_REMOVE_REFERENCE gsl_HAVE_TR1_TYPE_TRAITS
// C++ feature usage:
#if gsl_HAVE( ADDRESSOF )
# define gsl_ADDRESSOF(x) std::addressof(x)
#else
# define gsl_ADDRESSOF(x) (&x)
#endif
#if gsl_HAVE( CONSTEXPR_11 )
# define gsl_constexpr constexpr
#else
# define gsl_constexpr /*constexpr*/
#endif
#if gsl_HAVE( CONSTEXPR_14 )
# define gsl_constexpr14 constexpr
#else
# define gsl_constexpr14 /*constexpr*/
#endif
#if gsl_HAVE( EXPLICIT )
# define gsl_explicit explicit
#else
# define gsl_explicit /*explicit*/
#endif
#if gsl_FEATURE( IMPLICIT_MACRO )
# define implicit /*implicit*/
#endif
#if gsl_HAVE( IS_DELETE )
# define gsl_is_delete = delete
#else
# define gsl_is_delete
#endif
#if gsl_HAVE( IS_DELETE )
# define gsl_is_delete_access public
#else
# define gsl_is_delete_access private
#endif
#if !gsl_HAVE( NOEXCEPT ) || gsl_CONFIG( CONTRACT_VIOLATION_THROWS_V )
# define gsl_noexcept /*noexcept*/
#else
# define gsl_noexcept noexcept
#endif
#if gsl_HAVE( NULLPTR )
# define gsl_nullptr nullptr
#else
# define gsl_nullptr NULL
#endif
#define gsl_DIMENSION_OF( a ) ( sizeof(a) / sizeof(0[a]) )
// Other features:
#define gsl_HAVE_CONSTRAINED_SPAN_CONTAINER_CTOR \
( gsl_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG && gsl_HAVE_CONTAINER_DATA_METHOD )
// Note: !defined(__NVCC__) doesn't work with nvcc here:
#define gsl_HAVE_UNCONSTRAINED_SPAN_CONTAINER_CTOR \
( gsl_CONFIG_ALLOWS_UNCONSTRAINED_SPAN_CONTAINER_CTOR && (__NVCC__== 0) )
// GSL API (e.g. for CUDA platform):
#ifndef gsl_api
# ifdef __CUDACC__
# define gsl_api __host__ __device__
# else
# define gsl_api /*gsl_api*/
# endif
#endif
// Additional includes:
#if gsl_HAVE( ARRAY )
# include <array>
#endif
#if gsl_HAVE( INITIALIZER_LIST )
# include <initializer_list>
#endif
#if gsl_HAVE( TYPE_TRAITS )
# include <type_traits>
#elif gsl_HAVE( TR1_TYPE_TRAITS )
# include <tr1/type_traits>
#endif
#if gsl_HAVE( SIZED_TYPES )
# include <cstdint>
#endif
// MSVC warning suppression macros:
#if gsl_COMPILER_MSVC_VERSION >= 140
# define gsl_SUPPRESS_MSGSL_WARNING(expr) [[gsl::suppress(expr)]]
# define gsl_SUPPRESS_MSVC_WARNING(code, descr) __pragma(warning(suppress: code) )
# define gsl_DISABLE_MSVC_WARNINGS(codes) __pragma(warning(push)) __pragma(warning(disable: codes))
# define gsl_RESTORE_MSVC_WARNINGS() __pragma(warning(pop ))
#else
# define gsl_SUPPRESS_MSGSL_WARNING(expr)
# define gsl_SUPPRESS_MSVC_WARNING(code, descr)
# define gsl_DISABLE_MSVC_WARNINGS(codes)
# define gsl_RESTORE_MSVC_WARNINGS()
#endif
// Suppress the following MSVC GSL warnings:
// - C26410: gsl::r.32: the parameter 'ptr' is a reference to const unique pointer, use const T* or const T& instead
// - C26415: gsl::r.30: smart pointer parameter 'ptr' is used only to access contained pointer. Use T* or T& instead
// - C26418: gsl::r.36: shared pointer parameter 'ptr' is not copied or moved. Use T* or T& instead
// - C26472: gsl::t.1 : don't use a static_cast for arithmetic conversions;
// use brace initialization, gsl::narrow_cast or gsl::narow
// - C26439: gsl::f.6 : special function 'function' can be declared 'noexcept'
// - C26440: gsl::f.6 : function 'function' can be declared 'noexcept'
// - C26473: gsl::t.1 : don't cast between pointer types where the source type and the target type are the same
// - C26481: gsl::b.1 : don't use pointer arithmetic. Use span instead
// - C26482: gsl::b.2 : only index into arrays using constant expressions
// - C26446: gdl::b.4 : prefer to use gsl::at() instead of unchecked subscript operator
// - C26490: gsl::t.1 : don't use reinterpret_cast
// - C26487: gsl::l.4 : don't return a pointer '(<some number>'s result)' that may be invalid
gsl_DISABLE_MSVC_WARNINGS( 26410 26415 26418 26472 26439 26440 26473 26481 26482 26446 26490 26487 )
namespace gsl {
// forward declare span<>:
template< class T >
class span;
// C++11 emulation:
namespace std11 {
#if gsl_HAVE( ADD_CONST )
using std::add_const;
#elif gsl_HAVE( TR1_ADD_CONST )
using std::tr1::add_const;
#else
template< class T > struct add_const { typedef const T type; };
#endif // gsl_HAVE( ADD_CONST )
#if gsl_HAVE( REMOVE_CONST )
using std::remove_cv;
using std::remove_const;
using std::remove_volatile;
#elif gsl_HAVE( TR1_REMOVE_CONST )
using std::tr1::remove_cv;
using std::tr1::remove_const;
using std::tr1::remove_volatile;
#else
template< class T > struct remove_const { typedef T type; };
template< class T > struct remove_const<T const> { typedef T type; };
template< class T > struct remove_volatile { typedef T type; };
template< class T > struct remove_volatile<T volatile> { typedef T type; };
template< class T >
struct remove_cv
{
typedef typename remove_volatile<typename remove_const<T>::type>::type type;
};
#endif // gsl_HAVE( REMOVE_CONST )
#if gsl_HAVE( INTEGRAL_CONSTANT )
using std::integral_constant;
using std::true_type;
using std::false_type;
#elif gsl_HAVE( TR1_INTEGRAL_CONSTANT )
using std::tr1::integral_constant;
using std::tr1::true_type;
using std::tr1::false_type;
#else
template< class T, T v > struct integral_constant { enum { value = v }; };
typedef integral_constant< bool, true > true_type;
typedef integral_constant< bool, false > false_type;
#endif
} // namespace std11
// C++17 emulation:
namespace std17 {
template< bool v > struct bool_constant : std11::integral_constant<bool, v>{};
#if gsl_CPP11_120
template< class...>
using void_t = void;
#endif
#if gsl_HAVE( STD_DATA )
using std::data;
using std::size;
#elif gsl_HAVE( CONSTRAINED_SPAN_CONTAINER_CTOR )
template< class T, size_t N >
inline gsl_constexpr auto size( const T(&)[N] ) gsl_noexcept -> size_t
{
return N;
}
template< class C >
inline gsl_constexpr auto size( C const & cont ) -> decltype( cont.size() )
{
return cont.size();
}
template< class T, size_t N >
inline gsl_constexpr auto data( T(&arr)[N] ) gsl_noexcept -> T*
{
return &arr[0];
}
template< class C >
inline gsl_constexpr auto data( C & cont ) -> decltype( cont.data() )
{
return cont.data();
}
template< class C >
inline gsl_constexpr auto data( C const & cont ) -> decltype( cont.data() )
{
return cont.data();
}
template< class E >
inline gsl_constexpr auto data( std::initializer_list<E> il ) gsl_noexcept -> E const *
{
return il.begin();
}
#endif // span_HAVE( DATA )
} // namespace std17
namespace detail {
/// for nsel_REQUIRES_T
/*enum*/ class enabler{};
#if gsl_HAVE( TYPE_TRAITS )
template< class Q >
struct is_span_oracle : std::false_type{};
template< class T>
struct is_span_oracle< span<T> > : std::true_type{};
template< class Q >
struct is_span : is_span_oracle< typename std::remove_cv<Q>::type >{};
template< class Q >
struct is_std_array_oracle : std::false_type{};
#if gsl_HAVE( ARRAY )
template< class T, std::size_t Extent >
struct is_std_array_oracle< std::array<T, Extent> > : std::true_type{};
#endif
template< class Q >
struct is_std_array : is_std_array_oracle< typename std::remove_cv<Q>::type >{};
template< class Q >
struct is_array : std::false_type{};
template< class T >
struct is_array<T[]> : std::true_type{};
template< class T, std::size_t N >
struct is_array<T[N]> : std::true_type{};
#if gsl_CPP11_140 && ! gsl_BETWEEN( gsl_COMPILER_GNUC_VERSION, 1, 500 )
template< class, class = void >
struct has_size_and_data : std::false_type{};
template< class C >
struct has_size_and_data
<
C, std17::void_t<
decltype( std17::size(std::declval<C>()) ),
decltype( std17::data(std::declval<C>()) ) >
> : std::true_type{};
template< class, class, class = void >
struct is_compatible_element : std::false_type {};
template< class C, class E >
struct is_compatible_element
<
C, E, std17::void_t<
decltype( std17::data(std::declval<C>()) ) >
> : std::is_convertible< typename std::remove_pointer<decltype( std17::data( std::declval<C&>() ) )>::type(*)[], E(*)[] >{};
template< class C >
struct is_container : std17::bool_constant
<
! is_span< C >::value
&& ! is_array< C >::value
&& ! is_std_array< C >::value
&& has_size_and_data< C >::value
>{};
template< class C, class E >
struct is_compatible_container : std17::bool_constant
<
is_container<C>::value
&& is_compatible_element<C,E>::value
>{};
#else // gsl_CPP11_140
template<
class C, class E
gsl_REQUIRES_T((
! is_span< C >::value
&& ! is_array< C >::value
&& ! is_std_array< C >::value
&& ( std::is_convertible< typename std::remove_pointer<decltype( std17::data( std::declval<C&>() ) )>::type(*)[], E(*)[] >::value)
// && has_size_and_data< C >::value
))
, class = decltype( std17::size(std::declval<C>()) )
, class = decltype( std17::data(std::declval<C>()) )
>
struct is_compatible_container : std::true_type{};
#endif // gsl_CPP11_140
#endif // gsl_HAVE( TYPE_TRAITS )
} // namespace detail
//
// GSL.util: utilities
//
// index type for all container indexes/subscripts/sizes
typedef gsl_CONFIG_SPAN_INDEX_TYPE index; // p0122r3 uses std::ptrdiff_t
//
// GSL.owner: ownership pointers
//
#if gsl_HAVE( SHARED_PTR )
using std::unique_ptr;
using std::shared_ptr;
using std::make_shared;
# if gsl_HAVE( MAKE_UNIQUE )
using std::make_unique;
# endif
#endif
#if gsl_HAVE( ALIAS_TEMPLATE )
# if gsl_HAVE( TYPE_TRAITS )
template< class T
gsl_REQUIRES_T( std::is_pointer<T>::value )
>
using owner = T;
# else
template< class T > using owner = T;
# endif
#else
template< class T > struct owner { typedef T type; };
#endif
#define gsl_HAVE_OWNER_TEMPLATE gsl_HAVE_ALIAS_TEMPLATE
#if gsl_FEATURE( OWNER_MACRO )
# if gsl_HAVE( OWNER_TEMPLATE )
# define Owner(t) ::gsl::owner<t>
# else
# define Owner(t) ::gsl::owner<t>::type
# endif
#endif
//
// GSL.assert: assertions
//
#define gsl_ELIDE_CONTRACT_EXPECTS ( 0 == ( gsl_CONFIG_CONTRACT_LEVEL_MASK & 0x01 ) )
#define gsl_ELIDE_CONTRACT_ENSURES ( 0 == ( gsl_CONFIG_CONTRACT_LEVEL_MASK & 0x10 ) )
#if gsl_ELIDE_CONTRACT_EXPECTS
# define Expects( x ) /* Expects elided */
#elif gsl_CONFIG( CONTRACT_VIOLATION_THROWS_V )
# define Expects( x ) ::gsl::fail_fast_assert( (x), "GSL: Precondition failure at " __FILE__ ":" gsl_STRINGIFY(__LINE__) )
#elif gsl_CONFIG( CONTRACT_VIOLATION_CALLS_HANDLER_V )
# define Expects( x ) ::gsl::fail_fast_assert( (x), #x, "GSL: Precondition failure", __FILE__, __LINE__ )
#else
# define Expects( x ) ::gsl::fail_fast_assert( (x) )
#endif
#if gsl_ELIDE_CONTRACT_EXPECTS
# define gsl_EXPECTS_UNUSED_PARAM( x ) /* Make param unnamed if Expects elided */
#else
# define gsl_EXPECTS_UNUSED_PARAM( x ) x
#endif
#if gsl_ELIDE_CONTRACT_ENSURES
# define Ensures( x ) /* Ensures elided */
#elif gsl_CONFIG( CONTRACT_VIOLATION_THROWS_V )
# define Ensures( x ) ::gsl::fail_fast_assert( (x), "GSL: Postcondition failure at " __FILE__ ":" gsl_STRINGIFY(__LINE__) )
#elif gsl_CONFIG( CONTRACT_VIOLATION_CALLS_HANDLER_V )
# define Ensures( x ) ::gsl::fail_fast_assert( (x), #x, "GSL: Postcondition failure", __FILE__, __LINE__ )
#else
# define Ensures( x ) ::gsl::fail_fast_assert( (x) )
#endif
#define gsl_STRINGIFY( x ) gsl_STRINGIFY_( x )
#define gsl_STRINGIFY_( x ) #x
struct fail_fast : public std::logic_error
{
gsl_api explicit fail_fast( char const * const message )
: std::logic_error( message ) {}
};
# if gsl_CONFIG( CONTRACT_VIOLATION_THROWS_V )
gsl_api inline void fail_fast_assert( bool cond, char const * const message )
{
if ( !cond )
throw fail_fast( message );
}
# elif gsl_CONFIG( CONTRACT_VIOLATION_CALLS_HANDLER_V )
// Should be defined by user
gsl_api void fail_fast_assert_handler( char const * const expression, char const * const message, char const * const file, int line );
gsl_api inline void fail_fast_assert( bool cond, char const * const expression, char const * const message, char const * const file, int line )
{
if ( !cond )
fail_fast_assert_handler( expression, message, file, line );
}
# else
gsl_api inline void fail_fast_assert( bool cond ) gsl_noexcept
{
#ifdef __CUDA_ARCH__
assert(cond);
#else // __CUDA_ARCH__
if ( !cond )
std::terminate();
#endif // __CUDA_ARCH__
}
# endif
//
// GSL.util: utilities
//
#if gsl_FEATURE( EXPERIMENTAL_RETURN_GUARD )
// Add uncaught_exceptions for pre-2017 MSVC, GCC and Clang
// Return unsigned char to save stack space, uncaught_exceptions can only increase by 1 in a scope
namespace detail {
inline unsigned char to_uchar( unsigned x ) gsl_noexcept
{
return static_cast<unsigned char>( x );
}
} // namespace detail
namespace std11 {
#if gsl_HAVE( UNCAUGHT_EXCEPTIONS )
inline unsigned char uncaught_exceptions() gsl_noexcept
{
return detail::to_uchar( std::uncaught_exceptions() );
}
#elif gsl_COMPILER_MSVC_VERSION
extern "C" char * __cdecl _getptd();
inline unsigned char uncaught_exceptions() gsl_noexcept
{
return detail::to_uchar( *reinterpret_cast<unsigned*>(_getptd() + (sizeof(void*) == 8 ? 0x100 : 0x90) ) );
}
#elif gsl_COMPILER_CLANG_VERSION || gsl_COMPILER_GNUC_VERSION
extern "C" char * __cxa_get_globals();
inline unsigned char uncaught_exceptions() gsl_noexcept
{
return detail::to_uchar( *reinterpret_cast<unsigned*>(__cxa_get_globals() + sizeof(void*) ) );
}
#endif
} // namespace std11
#endif
#if gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 110
template< class F >
class final_action
{
public:
gsl_api explicit final_action( F action ) gsl_noexcept
: action_( std::move( action ) )
, invoke_( true )
{}
gsl_api final_action( final_action && other ) gsl_noexcept
: action_( std::move( other.action_ ) )
, invoke_( other.invoke_ )
{
other.invoke_ = false;
}
gsl_api virtual ~final_action() gsl_noexcept
{
if ( invoke_ )
action_();
}
gsl_is_delete_access:
gsl_api final_action( final_action const & ) gsl_is_delete;
gsl_api final_action & operator=( final_action const & ) gsl_is_delete;
gsl_api final_action & operator=( final_action && ) gsl_is_delete;
protected:
gsl_api void dismiss() gsl_noexcept
{
invoke_ = false;
}
private:
F action_;
bool invoke_;
};
template< class F >
gsl_api inline final_action<F> finally( F const & action ) gsl_noexcept
{
return final_action<F>( action );
}
template< class F >
gsl_api inline final_action<F> finally( F && action ) gsl_noexcept
{
return final_action<F>( std::forward<F>( action ) );
}
#if gsl_FEATURE( EXPERIMENTAL_RETURN_GUARD )
template< class F >
class final_action_return : public final_action<F>
{
public:
gsl_api explicit final_action_return( F && action ) gsl_noexcept
: final_action<F>( std::move( action ) )
, exception_count( std11::uncaught_exceptions() )
{}
gsl_api final_action_return( final_action_return && other ) gsl_noexcept
: final_action<F>( std::move( other ) )
, exception_count( std11::uncaught_exceptions() )
{}
gsl_api ~final_action_return() override
{
if ( std11::uncaught_exceptions() != exception_count )
this->dismiss();
}
gsl_is_delete_access:
gsl_api final_action_return( final_action_return const & ) gsl_is_delete;
gsl_api final_action_return & operator=( final_action_return const & ) gsl_is_delete;
private:
unsigned char exception_count;
};
template< class F >
gsl_api inline final_action_return<F> on_return( F const & action ) gsl_noexcept
{
return final_action_return<F>( action );
}
template< class F >
gsl_api inline final_action_return<F> on_return( F && action ) gsl_noexcept
{
return final_action_return<F>( std::forward<F>( action ) );
}
template< class F >
class final_action_error : public final_action<F>
{
public:
gsl_api explicit final_action_error( F && action ) gsl_noexcept
: final_action<F>( std::move( action ) )
, exception_count( std11::uncaught_exceptions() )
{}
gsl_api final_action_error( final_action_error && other ) gsl_noexcept
: final_action<F>( std::move( other ) )
, exception_count( std11::uncaught_exceptions() )
{}
gsl_api ~final_action_error() override
{
if ( std11::uncaught_exceptions() == exception_count )
this->dismiss();
}
gsl_is_delete_access:
gsl_api final_action_error( final_action_error const & ) gsl_is_delete;
gsl_api final_action_error & operator=( final_action_error const & ) gsl_is_delete;
private:
unsigned char exception_count;
};
template< class F >
gsl_api inline final_action_error<F> on_error( F const & action ) gsl_noexcept
{
return final_action_error<F>( action );
}
template< class F >
gsl_api inline final_action_error<F> on_error( F && action ) gsl_noexcept
{
return final_action_error<F>( std::forward<F>( action ) );
}
#endif // gsl_FEATURE( EXPERIMENTAL_RETURN_GUARD )
#else // gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 110
class final_action
{
public:
typedef void (*Action)();
gsl_api final_action( Action action )
: action_( action )
, invoke_( true )
{}