FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cppfront/include/cpp2util.h at main · iopenstack/cppfront · GitHub
iopenstack
/
cppfront
Public
forked from
hsutter/cppfront
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cppfront
/
include
/
cpp2util.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
2977 lines (2599 loc) · 98.5 KB
Breadcrumbs
cppfront
/
include
/
cpp2util.h
Copy path
File metadata and controls
2977 lines (2599 loc) · 98.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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 2022-2024 Herb Sutter
//
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//
Part of the Cppfront Project, under the Apache License v2.0 with LLVM Exceptions.
//
See https://github.com/hsutter/cppfront/blob/main/LICENSE for license information.
//
===========================================================================
//
Cpp2 utilities:
//
Language support implementations
//
#include'd by generated Cpp1 code
//
There are two kinds of entities in this file.
//
//
1) Entities in namespace cpp2:: itself, and documented at /cppfront/docs
//
//
These are intended for programs to use directly, to the extent
//
described in the documentation. Using any parts not described in the
//
documentation is not supported.
//
//
2) Entities in namespace cpp2::impl::, and macros
//
//
These should not be used by the program. They form the language
//
support library intended to be called only from generated code.
//
//
For example, if a Cpp2 function leaves a local variable
//
uninitialized, cppfront will generate uses of impl::deferred_init<>
//
under the covers and guarantee it is constructed exactly once, so
//
the implementation here doesn't need to check for double construction
//
because it can't happen; using the name impl::deferred_init directly
//
from program code is not supported.
//
//
3) Entities in other subnamespaces, such as cpp2::string_util
//
//
These are typically metafunction "runtime-library" functions,
//
implementation details called by metafunction-generated code.
//
For example, @regex generates code that uses string_util:: functions.
//
//
===========================================================================
#
ifndef
CPP2_CPP2UTIL_H
#
define
CPP2_CPP2UTIL_H
//
If this implementation doesn't support source_location yet, disable it
#
include
<
version
>
#
undef
CPP2_USE_SOURCE_LOCATION
#
if
defined(__cpp_lib_source_location)
#
define
CPP2_USE_SOURCE_LOCATION
Yes
#
endif
//
If the user requested making the entire C++ standard library available
//
via module import (incl. via -pure-cpp2) or header include, do that
#
if
defined(CPP2_IMPORT_STD) || defined(CPP2_INCLUDE_STD)
//
If C++23 'import std;' was requested but isn't available, fall back
//
to the 'include std' path
#
if
defined(CPP2_IMPORT_STD) && defined(__cpp_lib_modules)
import
std.compat;
//
If 'include std' was requested, include all standard headers.
//
This list tracks the current draft standard, so as of this
//
writing includes draft C++26 headers like <debugging>.
//
Use a feature test #ifdef for each header that isn't supported
//
by all of { VS 2022, g++-10, clang++-12 }
#
else
#
ifdef
_MSC_VER
#
include
"
intrin.h
"
#
endif
#
include
<
algorithm
>
#
include
<
any
>
#
include
<
array
>
#
include
<
atomic
>
#
ifdef
__cpp_lib_barrier
#
include
<
barrier
>
#
endif
#
include
<
bit
>
#
include
<
bitset
>
#
include
<
cassert
>
#
include
<
cctype
>
#
include
<
cerrno
>
#
include
<
cfenv
>
#
include
<
cfloat
>
#
include
<
charconv
>
#
include
<
chrono
>
#
include
<
cinttypes
>
#
include
<
climits
>
#
include
<
clocale
>
#
include
<
cmath
>
#
include
<
codecvt
>
#
include
<
compare
>
#
include
<
complex
>
#
include
<
concepts
>
#
include
<
condition_variable
>
#
ifdef
__cpp_lib_coroutine
#
include
<
coroutine
>
#
endif
#
include
<
csetjmp
>
#
include
<
csignal
>
#
include
<
cstdarg
>
#
include
<
cstddef
>
#
include
<
cstdint
>
#
include
<
cstdio
>
#
include
<
cstdlib
>
#
include
<
cstring
>
#
include
<
ctime
>
#
if
__has_include(<cuchar>)
#
include
<
cuchar
>
#
endif
#
include
<
cwchar
>
#
include
<
cwctype
>
#
ifdef
__cpp_lib_debugging
#
include
<
debugging
>
#
endif
#
include
<
deque
>
#
ifndef
CPP2_NO_EXCEPTIONS
#
include
<
exception
>
#
endif
//
libstdc++ currently has a dependency on linking TBB if <execution> is
//
included, and TBB seems to be not automatically installed and linkable
//
on some GCC installations, so let's not pull in that little-used header
//
in our -pure-cpp2 "import std;" simulation mode... if you need this,
//
use mixed mode (not -pure-cpp2) and #include all the headers you need
//
including this one
//
//
#include <execution>
#
ifdef
__cpp_lib_expected
#
include
<
expected
>
#
endif
#
include
<
filesystem
>
#
if
defined(__cpp_lib_format) || (defined(_MSC_VER) && _MSC_VER >= 1929)
#
include
<
format
>
#
endif
#
ifdef
__cpp_lib_flat_map
#
include
<
flat_map
>
#
endif
#
ifdef
__cpp_lib_flat_set
#
include
<
flat_set
>
#
endif
#
include
<
forward_list
>
#
include
<
fstream
>
#
include
<
functional
>
#
include
<
future
>
#
ifdef
__cpp_lib_generator
#
include
<
generator
>
#
endif
#
ifdef
__cpp_lib_hazard_pointer
#
include
<
hazard_pointer
>
#
endif
#
include
<
initializer_list
>
#
ifdef
__cpp_lib_inplace_vector
#
include
<
inplace_vector
>
#
endif
#
include
<
iomanip
>
#
include
<
ios
>
#
include
<
iosfwd
>
#
include
<
iostream
>
#
include
<
iso646.h
>
#
include
<
istream
>
#
include
<
iterator
>
#
ifdef
__cpp_lib_latch
#
include
<
latch
>
#
endif
#
include
<
limits
>
#
ifdef
__cpp_lib_linalg
#
include
<
linalg
>
#
endif
#
include
<
list
>
#
include
<
locale
>
#
include
<
map
>
#
ifdef
__cpp_lib_mdspan
#
include
<
mdspan
>
#
endif
#
include
<
memory
>
#
ifdef
__cpp_lib_memory_resource
#
include
<
memory_resource
>
#
endif
#
include
<
mutex
>
#
include
<
new
>
#
include
<
numbers
>
#
include
<
numeric
>
#
include
<
optional
>
#
include
<
ostream
>
#
ifdef
__cpp_lib_print
#
include
<
print
>
#
endif
#
include
<
queue
>
#
include
<
random
>
#
include
<
ranges
>
#
include
<
ratio
>
#
ifdef
__cpp_lib_rcu
#
include
<
rcu
>
#
endif
#
include
<
regex
>
#
include
<
scoped_allocator
>
#
ifdef
__cpp_lib_semaphore
#
include
<
semaphore
>
#
endif
#
include
<
set
>
#
include
<
shared_mutex
>
#
ifdef
__cpp_lib_source_location
#
include
<
source_location
>
#
endif
#
include
<
span
>
#
ifdef
__cpp_lib_spanstream
#
include
<
spanstream
>
#
endif
#
include
<
sstream
>
#
include
<
stack
>
#
ifdef
__cpp_lib_stacktrace
#
include
<
stacktrace
>
#
endif
#
ifdef
__cpp_lib_stdatomic_h
#
include
<
stdatomic.h
>
#
endif
#
include
<
stdexcept
>
#
if
__has_include(<stdfloat>)
#
if
!defined(_MSC_VER) || _HAS_CXX23
#
include
<
stdfloat
>
#
endif
#
endif
#
ifdef
__cpp_lib_jthread
#
include
<
stop_token
>
#
endif
#
include
<
streambuf
>
#
include
<
string
>
#
include
<
string_view
>
#
ifdef
__cpp_lib_syncbuf
#
include
<
syncstream
>
#
endif
#
include
<
system_error
>
#
ifdef
__cpp_lib_text_encoding
#
include
<
text_encoding
>
#
endif
#
include
<
thread
>
#
include
<
tuple
>
#
include
<
type_traits
>
#
include
<
typeindex
>
#
ifndef
CPP2_NO_RTTI
#
include
<
typeinfo
>
#
endif
#
include
<
unordered_map
>
#
include
<
unordered_set
>
#
include
<
utility
>
#
include
<
valarray
>
#
include
<
variant
>
#
include
<
vector
>
#
endif
//
Otherwise, just #include the facilities used in this header
#
else
#
ifdef
_MSC_VER
#
include
"
intrin.h
"
#
endif
#
include
<
algorithm
>
#
include
<
any
>
#
include
<
compare
>
#
include
<
concepts
>
#
include
<
cstddef
>
#
include
<
cstdint
>
#
include
<
cstdio
>
#
ifndef
CPP2_NO_EXCEPTIONS
#
include
<
exception
>
#
endif
#
ifdef
__cpp_lib_expected
#
include
<
expected
>
#
endif
#
if
defined(__cpp_lib_format) || (defined(_MSC_VER) && _MSC_VER >= 1929)
#
include
<
format
>
#
endif
#
include
<
functional
>
#
include
<
iostream
>
#
include
<
sstream
>
#
include
<
iterator
>
#
include
<
limits
>
#
include
<
map
>
#
include
<
memory
>
#
include
<
numeric
>
#
include
<
new
>
#
include
<
random
>
#
include
<
optional
>
#
if
defined(CPP2_USE_SOURCE_LOCATION)
#
include
<
source_location
>
#
endif
#
include
<
ranges
>
#
include
<
set
>
#
include
<
span
>
#
include
<
sstream
>
#
include
<
string
>
#
include
<
string_view
>
#
include
<
system_error
>
#
include
<
tuple
>
#
include
<
type_traits
>
#
ifndef
CPP2_NO_RTTI
#
include
<
typeinfo
>
#
endif
#
include
<
utility
>
#
include
<
variant
>
#
include
<
vector
>
#
endif
//
cpp2util.h uses signed integer types for indices and container sizes
//
so disable clang signed-to-unsigned conversion warnings in this header.
#
ifdef
__clang__
#
pragma
clang diagnostic push
#
pragma
clang diagnostic ignored "-Wsign-conversion"
#
endif
//
-----------------------------------------------------------------------
//
//
Macros
//
//
-----------------------------------------------------------------------
//
#
define
CPP2_TYPEOF
(
x
) std::
remove_cvref_t
<
decltype
(x)>
#
if
__cplusplus >= 202302L && \
( \
(
defined
(__clang_major__) && __clang_major__ >=
15
) \
|| (
defined
(__GNUC__) && __GNUC__ >=
12
) \
)
#
define
CPP2_COPY
(
x
)
auto
(x)
#
else
#
define
CPP2_COPY
(
x
)
CPP2_TYPEOF
(x)(x)
#
endif
#
define
CPP2_FORWARD
(
x
) std::forward<
decltype
(x)>(x)
#
define
CPP2_PACK_EMPTY
(
x
) (
sizeof
...(x) ==
0
)
#
define
CPP2_CONTINUE_BREAK
(
NAME
)
goto
CONTINUE_
##
NAME
;
CONTINUE_
##
NAME
:
continue
;
goto
BREAK_
##
NAME
;
BREAK_
##
NAME
:
break
;
//
these redundant goto's to avoid 'unused label' warnings
//
Compiler version identification.
//
//
This can use useful with 'if constexpr' to disable code known not to
//
work on some otherwise-supported compilers (without macros), for example:
//
//
// Disable tests on lower-level compilers that have blocking bugs
//
[]<auto V = gcc_clang_msvc_min_versions(1400, 1600, 1920)> () { if constexpr (V) {
//
// ... tests that would fail due to older compilers' bugs ...
//
}}();
//
//
Note: Test Clang first because it pretends to be other compilers.
//
#
if
defined(__clang_major__)
constexpr
auto
gcc_ver =
0
;
constexpr
auto
clang_ver = __clang_major__ *
100
+ __clang_minor__;
constexpr
auto
msvc_ver =
0
;
#
elif
defined(_MSC_VER)
constexpr
auto
gcc_ver =
0
;
constexpr
auto
clang_ver =
0
;
constexpr
auto
msvc_ver = _MSC_VER;
#
elif
defined(__GNUC__)
constexpr
auto
gcc_ver = __GNUC__ *
100
+ __GNUC_MINOR__;
constexpr
auto
clang_ver =
0
;
constexpr
auto
msvc_ver =
0
;
#
endif
constexpr
auto
gcc_clang_msvc_min_versions
(
auto
gcc,
auto
clang,
auto
msvc
)
{
return
gcc_ver >= gcc || clang_ver >= clang || msvc_ver >= msvc;
}
#
if
defined(_MSC_VER) && !defined(__clang_major__)
//
MSVC can't handle 'inline constexpr' variables yet in all cases
#
define
CPP2_CONSTEXPR
const
#
else
#
define
CPP2_CONSTEXPR
constexpr
#
endif
//
Workaround <https://github.com/llvm/llvm-project/issues/70556>.
#
define
CPP2_FORCE_INLINE_LAMBDA_CLANG
/*
empty
*/
#
if
defined(_MSC_VER) && !defined(__clang_major__)
#
define
CPP2_FORCE_INLINE
__forceinline
#
define
CPP2_FORCE_INLINE_LAMBDA
[[msvc::forceinline]]
#
define
CPP2_LAMBDA_NO_DISCARD
#
else
#
define
CPP2_FORCE_INLINE
__attribute__
((always_inline))
#
if
defined
(__clang__)
#define
CPP2_FORCE_INLINE_LAMBDA
/*
empty
*/
#
undef
CPP2_FORCE_INLINE_LAMBDA_CLANG
#
define
CPP2_FORCE_INLINE_LAMBDA_CLANG
__attribute__
((always_inline))
#
else
#
define
CPP2_FORCE_INLINE_LAMBDA
__attribute__
((always_inline))
#endif
#
if
defined(__clang_major__)
//
Also check __cplusplus, only to satisfy Clang -pedantic-errors
#
if
__cplusplus >= 202302L && (__clang_major__ > 13 || (__clang_major__ == 13 && __clang_minor__ >= 2))
#
define
CPP2_LAMBDA_NO_DISCARD
[[nodiscard]]
#
else
#
define
CPP2_LAMBDA_NO_DISCARD
#
endif
#
elif
defined(__GNUC__)
#
if
__GNUC__ >= 9
#
define
CPP2_LAMBDA_NO_DISCARD
[[nodiscard]]
#
else
#
define
CPP2_LAMBDA_NO_DISCARD
#
endif
#
if
((__GNUC__ * 100) + __GNUC_MINOR__) < 1003
//
GCC 10.2 doesn't support this feature (10.3 is fine)
#
undef
CPP2_FORCE_INLINE_LAMBDA
#
define
CPP2_FORCE_INLINE_LAMBDA
#
endif
#
else
#
define
CPP2_LAMBDA_NO_DISCARD
#
endif
#
endif
namespace
cpp2
{
//
-----------------------------------------------------------------------
//
//
Convenience names for fundamental types
//
//
Note: De jure, some of these are optional per the C and C++ standards
//
De facto, all of these are supported in all implementations I know of
//
//
-----------------------------------------------------------------------
//
//
Encouraged by default: Fixed-precision names
using
i8
= std::
int8_t
;
using
i16
= std::
int16_t
;
using
i32
= std::
int32_t
;
using
i64
= std::
int64_t
;
using
u8
= std::
uint8_t
;
using
u16
= std::
uint16_t
;
using
u32
= std::
uint32_t
;
using
u64
= std::
uint64_t
;
//
Discouraged: Variable precision names
//
short
using
ushort =
unsigned
short
;
//
int
using
uint =
unsigned
int
;
//
long
using
ulong =
unsigned
long
;
using
longlong =
long
long
;
using
ulonglong =
unsigned
long
long
;
using
longdouble =
long
double
;
//
Strongly discouraged, for compatibility/interop only
using
_schar =
signed
char
;
//
normally use i8 instead
using
_uchar =
unsigned
char
;
//
normally use u8 instead
//
-----------------------------------------------------------------------
//
//
String utilities
//
namespace
string_util
{
//
Break a string_view into a vector of views of simple qidentifier
//
substrings separated by other characters
inline
auto
split_string_list
(std::string_view str)
-> std::vector<std::string_view>
{
std::vector<std::string_view> ret;
auto
is_id_char = [](
char
c) {
return
std::isalnum
(c) || c ==
'
_
'
;
};
auto
pos =
decltype
(
std::ssize
(str)){
0
};
while
( pos <
std::ssize
(str) ) {
//
Skip non-alnum
while
(pos <
std::ssize
(str) && !
is_id_char
(str[pos])) {
++pos;
}
auto
start = pos;
//
Find the end of the current component
while
(pos <
std::ssize
(str) &&
is_id_char
(str[pos])) {
++pos;
}
//
Add nonempty substring to the vector
if
(start < pos) {
ret.
emplace_back
(str.
substr
(start, pos - start));
}
}
return
ret;
}
//
From https://stackoverflow.com/questions/216823/how-to-trim-a-stdstring
//
Trim from start (in place)
inline
void
ltrim
(std::string &s) {
s.
erase
(
s.
begin
(),
std::find_if
(s.
begin
(), s.
end
(), [](
unsigned
char
ch) {
return
!
std::isspace
(ch); })
);
}
//
Trim from end (in place)
inline
void
rtrim
(std::string &s) {
s.
erase
(
std::find_if
(s.
rbegin
(), s.
rend
(), [](
unsigned
char
ch) {
return
!
std::isspace
(ch); }).
base
(),
s.
end
()
);
}
//
Trim from both ends (in place)
inline
void
trim
(std::string &s) {
rtrim
(s);
ltrim
(s);
}
//
Trim from both ends (copying)
inline
std::string
trim_copy
(std::string_view s) {
std::string
t
(s);
trim
(t);
return
t;
}
//
From https://oleksandrkvl.github.io/2021/04/02/cpp-20-overview.html#nttp
template
<
typename
CharT, std::
size_t
N>
struct
fixed_string
{
constexpr
fixed_string
(
const
CharT
(&s)[N+1]) {
std::copy_n
(s, N +
1
, c_str);
}
constexpr
const
CharT*
data
()
const
{
return
c_str;
}
constexpr
std::
size_t
size
()
const
{
return
N;
}
constexpr
auto
str
()
const
{
return
std::basic_string<CharT>(c_str);
}
CharT c_str[N+
1
];
};
template
<
typename
CharT, std::
size_t
N>
fixed_string
(
const
CharT
(&)[N])->fixed_string<CharT, N-1>;
//
Other string utility functions.
constexpr
bool
is_escaped
(std::string_view s) {
return
s.
starts_with
(
"
\"
"
)
&& s.
ends_with
(
"
\"
"
)
;
}
inline
bool
string_to_int
(std::string
const
& s,
int
& v,
int
base =
10
) {
try
{
v =
stoi
(s,
nullptr
, base);
return
true
;
}
catch
(std::invalid_argument
const
&)
{
return
false
;
}
catch
(std::out_of_range
const
&)
{
return
false
;
}
}
template
<
int
Base =
10
>
inline
std::string
int_to_string
(
int
i) {
if
constexpr
(
8
== Base) {
std::ostringstream oss;
oss << std::oct << i;
return
oss.
str
();
}
else
if
constexpr
(
10
== Base) {
return
std::to_string
(i);
}
else
if
constexpr
(
16
== Base) {
std::ostringstream oss;
oss << std::hex << i;
return
oss.
str
();
}
else
{
[] <
bool
flag =
false
>() {
static_assert
(flag,
"
Unsupported int_to_string Base
"
);
}();
}
}
inline
char
safe_toupper
(
char
ch) {
return
static_cast
<
char
>(
std::toupper
(
static_cast
<
unsigned
char
>(ch)));
}
inline
char
safe_tolower
(
char
ch) {
return
static_cast
<
char
>(
std::tolower
(
static_cast
<
unsigned
char
>(ch)));
}
inline
std::string
replace_all
(
std::string str,
const
std::string& from,
const
std::string& to
)
{
size_t
start_pos =
0
;
while
((start_pos = str.
find
(from, start_pos)) != std::string::npos) {
str.
replace
(start_pos, from.
length
(), to);
start_pos += to.
length
();
//
safe also when 'to' is a substring of 'from'
}
return
str;
}
template
<
typename
List>
inline
std::string
join
(List
const
& list) {
std::string r =
"
"
;
std::string sep =
"
"
;
for
(
auto
const
& cur : list) {
r += sep + cur;
sep =
"
,
"
;
}
return
r;
}
}
//
namespace string_util
//
-----------------------------------------------------------------------
//
//
Conveniences for expressing Cpp1 references (rarely useful)
//
//
Note: Only needed in rare cases to take full control of matching an
//
odd Cpp1 signature exactly. Most cases don't need this... for
//
example, a Cpp1 virtual function signature declaration like
//
//
virtual void myfunc(int& val) const
//
//
can already be directly overriden by a Cpp2 declaration of
//
//
myfunc: (override this, inout val: int)
//
// identical to this in Cpp1 syntax:
//
// void myfunc(int& val) const override
//
//
without any need to say cpp1_ref on the int parameter.
//
//
-----------------------------------------------------------------------
//
template
<
typename
T>
using
cpp1_ref = std::
add_lvalue_reference_t
<T>;
template
<
typename
T>
using
cpp1_rvalue_ref = std::
add_rvalue_reference_t
<T>;
//
-----------------------------------------------------------------------
//
//
Helper for concepts
//
//
-----------------------------------------------------------------------
//
template
<
typename
Ret,
typename
Arg>
auto
argument_of_helper
(Ret(*) (Arg)) -> Arg;
template
<
typename
Ret,
typename
F,
typename
Arg>
auto
argument_of_helper
(Ret(F::*) (Arg)) -> Arg;
template
<
typename
Ret,
typename
F,
typename
Arg>
auto
argument_of_helper
(Ret(F::*) (Arg)&) -> Arg;
template
<
typename
Ret,
typename
F,
typename
Arg>
auto
argument_of_helper
(Ret(F::*) (Arg)&&) -> Arg;
template
<
typename
Ret,
typename
F,
typename
Arg>
auto
argument_of_helper
(Ret(F::*) (Arg)
const
) -> Arg;
template
<
typename
Ret,
typename
F,
typename
Arg>
auto
argument_of_helper
(Ret(F::*) (Arg)
const
&) -> Arg;
template
<
typename
Ret,
typename
F,
typename
Arg>
auto
argument_of_helper
(Ret(F::*) (Arg)
const
&&) -> Arg;
template
<
typename
F>
auto
argument_of_helper
(F
const
&) -> CPP2_TYPEOF(argument_of_helper(&
F::operator
()));
template
<
typename
T>
using
argument_of_t
=
CPP2_TYPEOF
(argument_of_helper(std::declval<T>()));
template
<
typename
F>
auto
argument_of_helper_op_is
(F
const
&) -> CPP2_TYPEOF(argument_of_helper(&F::op_is));
template
<
typename
T>
using
argument_of_op_is_t
=
CPP2_TYPEOF
(argument_of_helper_op_is(std::declval<T>()));
template
<
typename
T>
using
pointee_t
= std::
iter_value_t
<T>;
template
<
template
<
typename
...>
class
C
,
typename
... Ts>
constexpr
auto
specialization_of_template_helper
(C< Ts...>
const
& ) -> std::true_type {
return
{};
}
template
<
template
<
typename
,
auto
...>
class
C
,
typename
T,
auto
... Ns>
requires
(
sizeof
...(Ns) > 0)
constexpr
auto specialization_of_template_helper(C< T, Ns... >
const
& ) -> std::true_type {
return
{};
}
//
-----------------------------------------------------------------------
//
//
Concepts
//
//
-----------------------------------------------------------------------
//
template
<
typename
X,
template
<
typename
...>
class
C
>
concept
specialization_of_template =
requires
(X x) {
{ specialization_of_template_helper<C>(std::forward<X>(x)) } -> std::same_as<std::true_type>;
};
template
<
typename
X,
template
<
typename
,
auto
...>
class
C
>
concept
specialization_of_template_type_and_nttp =
requires
(X x) {
{ specialization_of_template_helper<C>(std::forward<X>(x)) } -> std::same_as<std::true_type>;
};
template
<
typename
X>
concept
boolean_testable = std::convertible_to<X,
bool
> &&
requires
(X&& x) {
{ !std::forward<X>(x) } -> std::convertible_to<
bool
>;
};
template
<
typename
X>
concept
dereferencable =
requires
(X x) { *x; };
template
<
typename
X>
concept
default_constructible = std::is_default_constructible_v<std::
remove_cvref_t
<X>>;
template
<
typename
X>
concept
bounded_array = std::is_bounded_array_v<std::
remove_cvref_t
<X>>;
template
<
typename
X>
concept
pointer_like = dereferencable<X> && default_constructible<X> && std::equality_comparable<X>
&& !bounded_array<X>;
template
<
typename
From,
typename
To >
concept
brace_initializable_to =
requires
(From x) { To{x}; };
template
<
typename
X,
typename
C >
concept
same_type_as = std::same_as<std::
remove_cvref_t
<X>, std::
remove_cvref_t
<C>>;
template
<
typename
X>
concept
defined
=
requires
{ std::declval<X>(); };
template
<
typename
X>
concept
has_defined_argument =
requires
{
std::declval<
argument_of_t
<X>>();
};
template
<
typename
X,
typename
F>
concept
covertible_to_argument_of = same_type_as<X,
argument_of_t
<F>>
|| (pointer_like<
argument_of_t
<F>> && brace_initializable_to<X,
pointee_t
<
argument_of_t
<F>>>)
|| (!pointer_like<
argument_of_t
<F>> && brace_initializable_to<X,
argument_of_t
<F>>)
;
template
<
typename
F,
typename
X>
concept
valid_predicate = (std::predicate<F, X> && !has_defined_argument<F>)
|| (std::predicate<F, X> && has_defined_argument<F> && covertible_to_argument_of<X, F>);
template
<
typename
X,
typename
O,
auto
mem_fun_ptr>
concept
predicate_member_fun =
requires
(X x, O o) {
{ (o.*mem_fun_ptr)(x) } -> std::convertible_to<
bool
>;
};
template
<
typename
F,
typename
X>
concept
valid_custom_is_operator = predicate_member_fun<X, F, &F::op_is>
&& (
!
defined
<
argument_of_op_is_t
<F>>
|| brace_initializable_to<X,
argument_of_op_is_t
<F>>
);
template
<
typename
T,
typename
U>
concept
has_common_type =
requires
(T t, U u) {
typename
std::
common_type_t
<T, U>;
};
//
-----------------------------------------------------------------------
//
//
General helpers
//
//
-----------------------------------------------------------------------
//
template
<
typename
T>
constexpr
auto
move
(T&& t) -> decltype(
auto
) {
return
std::move
(t);
}
constexpr
auto
max
(
auto
... values) {
return
std::max
( { values... } );
}
template
<
class
T
,
class
... Ts>
constexpr
auto
is_any = std::disjunction_v<std::is_same<T, Ts>...>;
template
<std::
size_t
Len, std::
size_t
Align>
struct
aligned_storage
{
alignas
(Align)
unsigned
char
data[Len];
};
template
<
typename
T>
requires
requires
{ *std::declval<T&>(); }
using
deref_t
=
decltype
(*std::declval<T&>());
//
Guaranteed to be a total order, unlike built-in operator== for T*
template
<
typename
T>
inline
auto
pointer_eq
(T
const
* a, T
const
* b) {
return
std::compare_three_way{}(a, b) == std::strong_ordering::equal;
}
//
PRs welcome to improve this, for suggestions and background see
//
https://www.boost.org/doc/libs/1_86_0/libs/container_hash/doc/html/hash.html#notes_hash_combine
inline
auto
hash_combine
(
size_t
& seed,
size_t
v) -> void
{
seed ^= v +
0x9e3779b9
+ (seed <<
6
) + (seed >>
2
);
}
//
-----------------------------------------------------------------------
//
//
A type_find_if for iterating over types in parameter packs
//
//
Note: This implementation works around limitations in gcc <12.1,
//
Clang <13, and MSVC <19.29. Otherwise we could avoid type_it and use
//
a lambda with an explicit parameter type list like this:
//
//
template <typename... Ts, typename F>
//
constexpr auto type_find_if(F&& fun)
//
{
//
std::size_t found = std::variant_npos;
//
[&]<std::size_t... Is>(std::index_sequence<Is...>){
//
if constexpr ((requires { {CPP2_FORWARD(fun).template operator()<Is, Ts>()} -> std::convertible_to<bool>;} && ...)) {
//
(((CPP2_FORWARD(fun).template operator()<Is, Ts>()) && (found = Is, true)) || ...);
//
}
//
}(std::index_sequence_for<Ts...>());
//
return found;
//
}
//
//
Note: The internal if constexpr could have else with static_assert.
//
Unfortunately there doesn't seem to be a way to make it work on MSVC.
//
//
-----------------------------------------------------------------------
//
template
<std::
size_t
Index,
typename
T>
struct
type_it
{
using
type = T;
inline
static
const
std::
size_t
index = Index;
};
template
<
typename
... Ts,
typename
F>
constexpr
auto
type_find_if
(F&& fun)
{
std::
size_t
found = std::variant_npos;
[&]<std::
size_t
... Is>(std::index_sequence<Is...>){
if
constexpr
((
requires
{ {
CPP2_FORWARD
(fun)(type_it<Is, Ts>{})} -> boolean_testable;} && ...)) {
((
CPP2_FORWARD
(fun)(type_it<Is, Ts>{}) && (found = Is,
true
)) || ...);
}
}(std::index_sequence_for<Ts...>());
return
found;
}
template
<
typename
F,
template
<
typename
...>
class
C
,
typename
... Ts>
constexpr
auto
type_find_if
(C<Ts...>
const
&, F&& fun)
{
return
type_find_if<Ts...>(
CPP2_FORWARD
(fun));
}
template
<
typename
T,
typename
... Ts>
constexpr
auto
variant_contains_type
(std::variant<Ts...>)
{
if
constexpr
(is_any<T, Ts...>) {
return
std::true_type{};
}
else
{
return
std::false_type{};
}
}
template
<
typename
C,
typename
X>
using
constness_like_t
=
std::
conditional_t
<
std::is_const_v<
std::
remove_pointer_t
<
std::
remove_reference_t
<X>
>
>,
std::
add_const_t
<C>,
std::
remove_const_t
<C>
>;
template
<
class
T
,
class
U
>
[[nodiscard]]
constexpr
auto
forward_like
(U&& x)
noexcept
-> decltype(
auto
)
{
constexpr
bool
is_adding_const = std::is_const_v<std::
remove_reference_t
<T>>;
if
constexpr
(std::is_lvalue_reference_v<T&&>)
{
if
constexpr
(is_adding_const)
return
std::as_const
(x);
else
return
static_cast
<U&>(x);
}
else
{
if
constexpr
(is_adding_const)
return
std::move
(
std::as_const
(x));
else
return
std::move
(x);
}
}
//
-----------------------------------------------------------------------
//
//
contract_group
//
//
-----------------------------------------------------------------------
//
#
ifdef
CPP2_USE_SOURCE_LOCATION
#
define
CPP2_SOURCE_LOCATION_PARAM
,
[[maybe_unused]]
std::source_location where
#
define
CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT
,
[[maybe_unused]]
std::source_location where = std::source_location::current()
#
define
CPP2_SOURCE_LOCATION_PARAM_SOLO
[[maybe_unused]]
std::source_location where
#
define
CPP2_SOURCE_LOCATION_ARG
, where
#
define
CPP2_SOURCE_LOCATION_VALUE
(cpp2::to_string(where.file_name()) +
"
(
"
+ cpp2::to_string(where.line()) +
"
)
"
+ where.function_name())
#
else
#
define
CPP2_SOURCE_LOCATION_PARAM
#
define
CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT
#
define
CPP2_SOURCE_LOCATION_PARAM_SOLO
#
define
CPP2_SOURCE_LOCATION_ARG
#
define
CPP2_SOURCE_LOCATION_VALUE
std::string
(
"
"
)
#endif
//
For C++23: make this std::string_view and drop the macro
//
Before C++23 std::string_view was not guaranteed to be trivially copyable,
//
and so in<T> will pass it by const& and really it should be by value
#
define
CPP2_MESSAGE_PARAM
char
const
*
#
define
CPP2_CONTRACT_MSG
cpp2::message_to_cstr_adapter
inline
auto
message_to_cstr_adapter
(
CPP2_MESSAGE_PARAM
msg ) -> CPP2_MESSAGE_PARAM {
return
msg ? msg :
"
"
; }
inline
auto
message_to_cstr_adapter
( std::string
const
& msg ) -> CPP2_MESSAGE_PARAM {
return
msg.
c_str
(); }
class
contract_group
{
public:
using
handler =
void
(*)(
CPP2_MESSAGE_PARAM
msg
CPP2_SOURCE_LOCATION_PARAM
);
constexpr
contract_group
(handler h = {}) : reporter{h} { }
constexpr
auto
set_handler
(handler h = {}) { reporter = h; }
constexpr
auto
is_active
()
const
-> bool {
return
reporter != handler{}; }
constexpr
auto
enforce
(
bool
b,
CPP2_MESSAGE_PARAM
msg =
"
"
CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT
)
-> void {
if
(!b)
report_violation
(msg
CPP2_SOURCE_LOCATION_ARG
); }
constexpr
auto
report_violation
(
CPP2_MESSAGE_PARAM
msg =
"
"
CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT
)
-> void {
if
(reporter)
reporter
(msg
CPP2_SOURCE_LOCATION_ARG
); }
private:
handler reporter;
};
[[noreturn]]
inline
auto
report_and_terminate
(std::string_view group,
CPP2_MESSAGE_PARAM
msg =
"
"
CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT
)
noexcept
-> void {
std::cerr
#
ifdef
CPP2_USE_SOURCE_LOCATION
<< where.
file_name
() <<
"
(
"
<< where.
line
() <<
"
)
"
<< where.
function_name
() <<
"
:
"
#
endif
<< group <<
"
violation
"
;
if
(msg && msg[
0
] !=
'
\0
'
) {
std::cerr <<
"
:
"
<< msg;
}
std::cerr <<
"
\n
"
;
std::terminate
();
}
auto
inline
cpp2_default = contract_group(
[](
CPP2_MESSAGE_PARAM
msg
CPP2_SOURCE_LOCATION_PARAM
)
noexcept
{
report_and_terminate
(
"
Contract
"
, msg
CPP2_SOURCE_LOCATION_ARG
);
}
);
auto
inline
bounds_safety = contract_group(
[](
CPP2_MESSAGE_PARAM
msg
CPP2_SOURCE_LOCATION_PARAM
)
noexcept
{
report_and_terminate
(
"
Bounds safety
"
, msg
CPP2_SOURCE_LOCATION_ARG
);
}
);
auto
inline
null_safety = contract_group(
[](
CPP2_MESSAGE_PARAM
msg
CPP2_SOURCE_LOCATION_PARAM
)
noexcept
{
report_and_terminate
(
"
Null safety
"
, msg
CPP2_SOURCE_LOCATION_ARG
);
}
);
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL