-
Notifications
You must be signed in to change notification settings - Fork 108
/
gsl-lite.hpp
2416 lines (1917 loc) · 64.8 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: Guideline 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 <exception>
#include <iterator>
#include <limits>
#include <memory>
#include <ostream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#define gsl_lite_VERSION "0.29.0"
// 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_THROWS 0
#endif
#if defined( GSL_UNENFORCED_ON_CONTRACT_VIOLATION )
# define gsl_CONFIG_CONTRACT_LEVEL_OFF 1
#endif
// Configuration:
#ifndef gsl_FEATURE_HAVE_IMPLICIT_MACRO
# define gsl_FEATURE_HAVE_IMPLICIT_MACRO 1
#endif
#ifndef gsl_FEATURE_HAVE_OWNER_MACRO
# define gsl_FEATURE_HAVE_OWNER_MACRO 1
#endif
#ifndef gsl_FEATURE_EXPERIMENTAL_RETURN_GUARD
# define gsl_FEATURE_EXPERIMENTAL_RETURN_GUARD 0
#endif
#ifndef gsl_CONFIG_SPAN_INDEX_TYPE
# define gsl_CONFIG_SPAN_INDEX_TYPE size_t
#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 !defined( gsl_CONFIG_CONTRACT_VIOLATION_THROWS ) && \
!defined( gsl_CONFIG_CONTRACT_VIOLATION_TERMINATES )
# define gsl_CONFIG_CONTRACT_VIOLATION_THROWS_V 0
#elif defined( gsl_CONFIG_CONTRACT_VIOLATION_THROWS ) && \
!defined( gsl_CONFIG_CONTRACT_VIOLATION_TERMINATES )
# define gsl_CONFIG_CONTRACT_VIOLATION_THROWS_V 1
#elif !defined( gsl_CONFIG_CONTRACT_VIOLATION_THROWS ) && \
defined( gsl_CONFIG_CONTRACT_VIOLATION_TERMINATES )
# define gsl_CONFIG_CONTRACT_VIOLATION_THROWS_V 0
#else
# error only one of gsl_CONFIG_CONTRACT_VIOLATION_THROWS and gsl_CONFIG_CONTRACT_VIOLATION_TERMINATES may be defined.
#endif
// Compiler detection (C++20 is speculative):
// Note: MSVC supports C++14 since it supports C++17.
#ifdef _MSVC_LANG
# define gsl_MSVC_LANG _MSVC_LANG
#else
# define gsl_MSVC_LANG 0
#endif
#define gsl_CPP11_OR_GREATER (__cplusplus >= 201103L || gsl_MSVC_LANG >= 201103L )
#define gsl_CPP14_OR_GREATER (__cplusplus >= 201402L || gsl_MSVC_LANG >= 201703L )
#define gsl_CPP17_OR_GREATER (__cplusplus >= 201703L || gsl_MSVC_LANG >= 201703L )
#define gsl_CPP20_OR_GREATER (__cplusplus >= 202000L || gsl_MSVC_LANG >= 202000L )
// half-open range [lo..hi):
#define gsl_BETWEEN( v, lo, hi ) ( lo <= v && v < hi )
#if defined(_MSC_VER) && !defined(__clang__)
# define gsl_COMPILER_MSVC_VERSION (_MSC_VER / 100 - 5 - (_MSC_VER < 1900))
#else
# define gsl_COMPILER_MSVC_VERSION 0
# define gsl_COMPILER_NON_MSVC 1
#endif
// Note: simplistic version computation; works for GCC versions on http://godbolt.org/
#if defined(__GNUC__) && !defined(__clang__)
# define gsl_COMPILER_GNUC_VERSION ( 10 * (10 *__GNUC__ + __GNUC_MINOR__) + __GNUC_PATCHLEVEL__)
#else
# define gsl_COMPILER_GNUC_VERSION 0
#endif
// Compiler non-strict aliasing:
#if defined __clang__ || defined __GNUC__
# define gsl_may_alias __attribute__((__may_alias__))
#else
# define gsl_may_alias
#endif
// Presence of wide character support:
#ifdef __DJGPP__
# define gsl_HAVE_WCHAR 0
#else
# define gsl_HAVE_WCHAR 1
#endif
// Presence of C++11 language features:
#define gsl_CPP11_10 (gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 10)
#define gsl_CPP11_11 (gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 11)
#define gsl_CPP11_12 (gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 12)
#define gsl_CPP11_14 (gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 14)
#define gsl_HAVE_AUTO gsl_CPP11_10
#define gsl_HAVE_NULLPTR gsl_CPP11_10
#define gsl_HAVE_ENUM_CLASS gsl_CPP11_11
#define gsl_HAVE_ALIAS_TEMPLATE gsl_CPP11_12
#define gsl_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG gsl_CPP11_12
#define gsl_HAVE_EXPLICIT_CONVERSION gsl_CPP11_12
#define gsl_HAVE_INITIALIZER_LIST gsl_CPP11_12
#define gsl_HAVE_CONSTEXPR_11 gsl_CPP11_14
#define gsl_HAVE_IS_DEFAULT gsl_CPP11_14
#define gsl_HAVE_IS_DELETE gsl_CPP11_14
#define gsl_HAVE_NOEXCEPT gsl_CPP11_14
#if gsl_CPP11_OR_GREATER
// see above
#endif
// Presence of C++14 language features:
#define gsl_CPP14_00 (gsl_CPP14_OR_GREATER)
#define gsl_CPP14_14 (gsl_CPP14_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 14)
#define gsl_HAVE_CONSTEXPR_14 gsl_CPP14_00
#define gsl_HAVE_DECLTYPE_AUTO gsl_CPP14_14
// Presence of C++17 language features:
#define gsl_CPP17_00 (gsl_CPP17_OR_GREATER)
#define gsl_HAVE_ENUM_CLASS_CONSTRUCTION_FROM_UNDERLYING_TYPE gsl_CPP17_00
// Presence of C++ library features:
#ifdef _HAS_CPP0X
# define gsl_HAS_CPP0X _HAS_CPP0X
#else
# define gsl_HAS_CPP0X 0
#endif
#define gsl_CPP11_LA (gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 11)
#define gsl_HAVE_ARRAY gsl_CPP11_LA
#define gsl_HAVE_TR1_TYPE_TRAITS gsl_CPP11_LA
#define gsl_CPP11_LB (gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 14 || (gsl_COMPILER_MSVC_VERSION >= 9 && gsl_HAS_CPP0X))
#define gsl_HAVE_CONTAINER_DATA_METHOD gsl_CPP11_LB
#define gsl_CPP11_LC (gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 14)
#define gsl_HAVE_SIZED_TYPES gsl_CPP11_LC
#define gsl_CPP11_LD (gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 14 || (gsl_COMPILER_MSVC_VERSION >= 10 && gsl_HAS_CPP0X))
#define gsl_HAVE_MAKE_SHARED gsl_CPP11_LD
#define gsl_HAVE_SHARED_PTR gsl_CPP11_LD
#define gsl_HAVE_UNIQUE_PTR gsl_CPP11_LD
#define gsl_CPP14_LA (gsl_CPP14_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 12)
#define gsl_HAVE_MAKE_UNIQUE gsl_CPP14_LA
#define gsl_HAVE_TYPE_TRAITS (gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 11)
#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
// For the rest, consider VC12, VC14 as C++11 for GSL Lite.
// C++ feature usage:
#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_CONVERSION
# define gsl_explicit explicit
#else
# define gsl_explicit /*explicit*/
#endif
#if gsl_FEATURE_HAVE_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_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 >= 14
# 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
// - C26490: gsl::t.1 : don't use reinterpret_cast
gsl_DISABLE_MSVC_WARNINGS( 26410 26415 26418 26472 26439 26440 26473 26481 26482 26490 )
namespace gsl {
namespace detail {
// C++11 emulation:
#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 detail::remove_volatile<typename detail::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< int v > struct integral_constant { enum { value = v }; };
typedef integral_constant< true > true_type;
typedef integral_constant< false > false_type;
#endif
#if gsl_HAVE_ARRAY
template< class T >
struct is_std_array_oracle : false_type {};
template< class T, std::size_t N >
struct is_std_array_oracle< std::array<T, N> > : true_type {};
template< class T >
struct is_std_array : is_std_array_oracle< typename remove_cv<T>::type > {};
#endif
} // 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, class = typename std::enable_if< std::is_pointer<T>::value >::type >
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_HAVE_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__) );
#else
# define Expects( x ) ::gsl::fail_fast_assert( (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__) );
#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 ) {}
};
// workaround for gcc 5 throw/terminate constexpr bug:
#if gsl_BETWEEN( gsl_COMPILER_GNUC_VERSION, 430, 600 ) && gsl_HAVE_CONSTEXPR_14
# if gsl_CONFIG_CONTRACT_VIOLATION_THROWS_V
gsl_api inline gsl_constexpr14 auto fail_fast_assert( bool cond, char const * const message ) -> void
{
!cond ? throw fail_fast( message ) : 0;
}
# else
gsl_api inline gsl_constexpr14 auto fail_fast_assert( bool cond ) -> void
{
struct F { static gsl_constexpr14 void f(){}; };
!cond ? std::terminate() : F::f();
}
# endif
#else // workaround
# if gsl_CONFIG_CONTRACT_VIOLATION_THROWS_V
gsl_api inline gsl_constexpr14 void fail_fast_assert( bool cond, char const * const message )
{
if ( !cond )
throw fail_fast( message );
}
# else
gsl_api inline gsl_constexpr14 void fail_fast_assert( bool cond ) gsl_noexcept
{
if ( !cond )
std::terminate();
}
# endif
#endif // workaround
//
// GSL.util: utilities
//
#if gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 11
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;
}
#if gsl_CPP17_OR_GREATER
gsl_api int uncaught_exceptions() gsl_noexcept
{
return std::uncaught_exceptions();
}
#else
gsl_api int uncaught_exceptions()
{
return std::uncaught_exception() ? 1 : 0;
}
#endif
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 ) )
{}
gsl_api final_action_return( final_action_return && other ) gsl_noexcept
: final_action<F>( std::move( other ) )
{}
gsl_api ~final_action_return() override
{
if ( this->uncaught_exceptions() )
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;
};
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 ) )
{}
gsl_api final_action_error( final_action_error && other ) gsl_noexcept
: final_action<F>( std::move( other ) )
{}
gsl_api ~final_action_error() override
{
if ( ! this->uncaught_exceptions() )
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;
};
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 >= 11
class final_action
{
public:
typedef void (*Action)();
gsl_api final_action( Action action )
: action_( action )
, invoke_( true )
{}
gsl_api final_action( final_action const & other )
: action_( other.action_ )
, invoke_( other.invoke_ )
{
other.invoke_ = false;
}
gsl_api virtual ~final_action()
{
if ( invoke_ )
action_();
}
protected:
gsl_api void dismiss()
{
invoke_ = false;
}
gsl_api int uncaught_exceptions()
{
return std::uncaught_exception() ? 1 : 0;
}
private:
gsl_api final_action & operator=( final_action const & );
private:
Action action_;
mutable bool invoke_;
};
template< class F >
gsl_api inline final_action finally( F const & f )
{
return final_action(( f ));
}
#if gsl_FEATURE_EXPERIMENTAL_RETURN_GUARD
class final_action_return : public final_action
{
public:
gsl_api explicit final_action_return( Action action )
: final_action( action )
{}
gsl_api ~final_action_return()
{
if ( this->uncaught_exceptions() )
this->dismiss();
}
private:
gsl_api final_action_return & operator=( final_action_return const & );
};
template< class F >
gsl_api inline final_action_return on_return( F const & action )
{
return final_action_return( action );
}
class final_action_error : public final_action
{
public:
gsl_api explicit final_action_error( Action action )
: final_action( action )
{}
gsl_api ~final_action_error()
{
if ( ! this->uncaught_exceptions() )
this->dismiss();
}
private:
gsl_api final_action_error & operator=( final_action_error const & );
};
template< class F >
gsl_api inline final_action_error on_error( F const & action )
{
return final_action_error( action );
}
#endif // gsl_FEATURE_EXPERIMENTAL_RETURN_GUARD
#endif // gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION == 11
#if gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 12
template< class T, class U >
gsl_api inline gsl_constexpr T narrow_cast( U && u ) gsl_noexcept
{
return static_cast<T>( std::forward<U>( u ) );
}
#else
template< class T, class U >
gsl_api inline T narrow_cast( U u ) gsl_noexcept
{
return static_cast<T>( u );
}
#endif // gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 12
struct narrowing_error : public std::exception {};
#if gsl_HAVE_TYPE_TRAITS
namespace details
{
template< class T, class U >
struct is_same_signedness : public std::integral_constant<bool, std::is_signed<T>::value == std::is_signed<U>::value>
{};
}
#endif
template< class T, class U >
gsl_api inline T narrow( U u )
{
T t = narrow_cast<T>( u );
if ( static_cast<U>( t ) != u )
{
#if gsl_CONFIG_CONTRACT_VIOLATION_THROWS_V
throw narrowing_error();
#else
std::terminate();
#endif
}
#if gsl_HAVE_TYPE_TRAITS
# if gsl_COMPILER_MSVC_VERSION
// Suppress MSVC level 4 warning C4127 (conditional expression is constant)
if ( 0, ! details::is_same_signedness<T, U>::value && ( ( t < T() ) != ( u < U() ) ) )
# else
if ( ! details::is_same_signedness<T, U>::value && ( ( t < T() ) != ( u < U() ) ) )
# endif
#else
// Don't assume T() works:
if ( ( t < 0 ) != ( u < 0 ) )
#endif
{
#if gsl_CONFIG_CONTRACT_VIOLATION_THROWS_V
throw narrowing_error();
#else
std::terminate();
#endif
}
return t;
}
//
// at() - Bounds-checked way of accessing static arrays, std::array, std::vector.
//
template< class T, size_t N >
gsl_api inline gsl_constexpr14 T & at( T(&arr)[N], size_t index )
{
Expects( index < N );
return arr[index];
}
#if gsl_HAVE_ARRAY
template< class T, size_t N >
gsl_api inline gsl_constexpr14 T & at( std::array<T, N> & arr, size_t index )
{
Expects( index < N );
return arr[index];
}
#endif
template< class Cont >
gsl_api inline gsl_constexpr14 typename Cont::value_type & at( Cont & cont, size_t index )
{
Expects( index < cont.size() );
return cont[index];
}
#if gsl_HAVE_INITIALIZER_LIST
template< class T >
gsl_api inline const gsl_constexpr14 T & at( std::initializer_list<T> cont, size_t index )
{
Expects( index < cont.size() );
return *( cont.begin() + index );
}
#endif
template< class T >
class span;
template< class T >
gsl_api inline gsl_constexpr14 T & at( span<T> s, size_t index )
{
return s.at( index );
}
//
// GSL.views: views
//
//
// not_null<> - Wrap any indirection and enforce non-null.
//
template< class T >
class not_null
{
public:
gsl_api gsl_constexpr14 not_null( T t ) : ptr_ ( t ){ Expects( ptr_ != gsl_nullptr ); }
gsl_api not_null & operator=( T t ) { ptr_ = t ; Expects( ptr_ != gsl_nullptr ); return *this; }
#if gsl_HAVE_IS_DEFAULT
gsl_api gsl_constexpr not_null( not_null const & other ) = default;
gsl_api gsl_constexpr not_null( not_null && other ) = default;
gsl_api ~not_null() = default;
gsl_api not_null & operator=( not_null const & other ) = default;
gsl_api not_null & operator=( not_null && other ) = default;
#else
gsl_api gsl_constexpr not_null( not_null const & other ) : ptr_ ( other.ptr_ ) {}
gsl_api ~not_null() {};
gsl_api not_null & operator=( not_null const & other ) { ptr_ = other.ptr_; return *this; }
#endif
#if gsl_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG
template< class U, class Dummy = typename std::enable_if<std::is_convertible<U, T>::value, void>::type >
gsl_api gsl_constexpr not_null( not_null<U> const & other )
: ptr_( other.get() )
{}
template< class U, class Dummy = typename std::enable_if<std::is_convertible<U, T>::value, void>::type >
gsl_api not_null & operator=( not_null<U> const & other )
{
ptr_ = other.get();
return *this;
}
#else
template< class U >
gsl_api gsl_constexpr not_null( not_null<U> const & other )
: ptr_( other.get() )
{}
template< class U >
gsl_api gsl_constexpr not_null & operator=( not_null<U> const & other )
{
ptr_ = other.get();
return *this;
}
#endif
gsl_api gsl_constexpr14 T get() const
{
Ensures( ptr_ != gsl_nullptr );
return ptr_;
}
gsl_api gsl_constexpr operator T() const { return get(); }
gsl_api gsl_constexpr T operator->() const { return get(); }
#if gsl_HAVE_DECLTYPE_AUTO
gsl_api gsl_constexpr decltype(auto) operator*() const { return *get(); }
#endif
gsl_is_delete_access:
// prevent compilation when initialized with a nullptr or literal 0:
#if gsl_HAVE_NULLPTR
gsl_api not_null( std::nullptr_t ) gsl_is_delete;
gsl_api not_null & operator=( std::nullptr_t ) gsl_is_delete;
#else
gsl_api not_null( int ) gsl_is_delete;
gsl_api not_null & operator=( int ) gsl_is_delete;
#endif
// unwanted operators...pointers only point to single objects!
gsl_api not_null & operator++() gsl_is_delete;
gsl_api not_null & operator--() gsl_is_delete;
gsl_api not_null operator++( int ) gsl_is_delete;
gsl_api not_null operator--( int ) gsl_is_delete;
gsl_api not_null & operator+ ( size_t ) gsl_is_delete;
gsl_api not_null & operator+=( size_t ) gsl_is_delete;
gsl_api not_null & operator- ( size_t ) gsl_is_delete;
gsl_api not_null & operator-=( size_t ) gsl_is_delete;
gsl_api not_null & operator+=( std::ptrdiff_t ) gsl_is_delete;
gsl_api not_null & operator-=( std::ptrdiff_t ) gsl_is_delete;
gsl_api void operator[]( std::ptrdiff_t ) const gsl_is_delete;
private:
T ptr_;
};
// more not_null unwanted operators
template< class T, class U >