From 856437411824c95cc2bcc1725357594d4ff9d5e0 Mon Sep 17 00:00:00 2001 From: Miguel Young de la Sota Date: Mon, 22 Jul 2024 14:28:47 -0400 Subject: [PATCH] update clang-format config --- .clang-format | 20 +- best/base/fwd.h | 2 +- best/base/hint.h | 4 +- best/base/internal/ord.h | 72 +++--- best/base/niche.h | 4 +- best/base/ord.h | 22 +- best/base/ord_test.cc | 4 +- best/base/port.h | 2 +- best/base/tags.h | 8 +- best/cli/app.cc | 10 +- best/cli/app.h | 72 +++--- best/cli/cli.h | 14 +- best/cli/cli_test.cc | 46 ++-- best/cli/parser.cc | 269 ++++++++++------------ best/cli/parser.h | 75 +++--- best/cli/toy_flags.h | 215 +++++++++--------- best/container/box.h | 131 +++++------ best/container/choice.h | 127 +++++------ best/container/choice_test.cc | 36 +-- best/container/internal/choice.h | 125 +++++----- best/container/internal/pun.h | 25 +- best/container/internal/row.h | 55 +++-- best/container/object.h | 9 +- best/container/option.h | 162 +++++++------ best/container/option_test.cc | 2 +- best/container/pun.h | 4 +- best/container/result.h | 242 ++++++++++---------- best/container/result_test.cc | 6 +- best/container/row.h | 146 ++++++------ best/container/row_test.cc | 18 +- best/container/vec.h | 141 +++++------- best/func/call.h | 10 +- best/func/fnref.h | 4 +- best/func/internal/call.h | 26 +-- best/func/internal/fnref.h | 54 ++--- best/func/tap.h | 2 +- best/iter/bounds.cc | 14 +- best/iter/bounds.h | 55 ++--- best/iter/bounds_test.cc | 4 +- best/iter/iter.h | 34 ++- best/iter/iter_test.cc | 12 +- best/log/internal/crash.cc | 6 +- best/log/internal/crash.h | 6 +- best/log/location.h | 33 ++- best/log/wtf.h | 4 +- best/math/bit.h | 10 +- best/math/conv.h | 18 +- best/math/int.h | 27 +-- best/math/int_test.cc | 2 +- best/math/internal/common_int.h | 24 +- best/math/overflow.h | 40 ++-- best/memory/allocator.cc | 36 ++- best/memory/allocator.h | 66 +++--- best/memory/internal/bytes.h | 98 ++++---- best/memory/internal/layout.h | 16 +- best/memory/layout.h | 12 +- best/memory/ptr.h | 54 ++--- best/memory/span.h | 276 +++++++++++------------ best/memory/span_sort.h | 14 +- best/memory/span_test.cc | 6 +- best/meta/empty.h | 10 +- best/meta/init.h | 58 ++--- best/meta/init_test.cc | 8 +- best/meta/internal/init.h | 60 ++--- best/meta/internal/names.h | 78 +++---- best/meta/internal/ops.h | 14 +- best/meta/internal/reflect.h | 109 +++++---- best/meta/internal/tlist.h | 59 +++-- best/meta/names.h | 12 +- best/meta/ops.h | 4 +- best/meta/reflect.h | 30 +-- best/meta/reflect_test.cc | 26 +-- best/meta/taxonomy.h | 20 +- best/meta/tlist.h | 40 ++-- best/meta/tlist_test.cc | 10 +- best/meta/traits.h | 4 +- best/test/fodder.cc | 12 +- best/test/fodder.h | 12 +- best/test/test.cc | 27 ++- best/test/test.h | 30 +-- best/text/ascii.h | 36 +-- best/text/encoding.h | 48 ++-- best/text/format.cc | 58 ++--- best/text/format.h | 30 +-- best/text/format_test.cc | 6 +- best/text/internal/format_impls.h | 60 +++-- best/text/internal/format_parser.h | 85 ++++--- best/text/internal/format_parser_test.cc | 239 ++++++++++---------- best/text/internal/utf.h | 48 ++-- best/text/rune.h | 118 ++++------ best/text/str.h | 196 ++++++++-------- best/text/str_test.cc | 8 +- best/text/strbuf.h | 22 +- best/text/utf16.h | 35 ++- best/text/utf32.h | 16 +- best/text/utf8.h | 54 ++--- best/text/utf_test.cc | 25 +- 97 files changed, 2216 insertions(+), 2522 deletions(-) diff --git a/.clang-format b/.clang-format index f033b0a..044feb4 100644 --- a/.clang-format +++ b/.clang-format @@ -1,2 +1,20 @@ BasedOnStyle: Google -IndentWidth: 2 \ No newline at end of file + +ColumnLimit: 80 +IndentWidth: 2 +ConstructorInitializerIndentWidth: 2 +ContinuationIndentWidth: 2 + +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: true +InsertBraces: true + +IndentRequiresClause: false +PointerAlignment: Left +QualifierAlignment: Custom +QualifierOrder: [inline, static, constexpr, friend, type] +RequiresClausePosition: WithPreceding + +SpaceBeforeParens: Custom +SpaceBeforeParensOptions: + AfterRequiresInClause: true \ No newline at end of file diff --git a/best/base/fwd.h b/best/base/fwd.h index 352b13d..4a8db7f 100644 --- a/best/base/fwd.h +++ b/best/base/fwd.h @@ -119,7 +119,7 @@ class vptr; // because it's not defined yet. However, if it's a dependent type, we can delay // it to the second phase of two-phase lookup. template > = - best::option>{}> + best::option>{}> class span; // best/meta/empty.h diff --git a/best/base/hint.h b/best/base/hint.h index f21bc38..b97076b 100644 --- a/best/base/hint.h +++ b/best/base/hint.h @@ -103,9 +103,7 @@ BEST_INLINE_ALWAYS constexpr void assume(bool truth) { /// /// Hides a value from the compiler's optimizer. [[nodiscard]] BEST_INLINE_SYNTHETIC constexpr auto&& black_box(auto&& value) { - if (!std::is_constant_evaluated()) { - asm volatile("" ::"m,r"(value)); - } + if (!std::is_constant_evaluated()) { asm volatile("" ::"m,r"(value)); } return decltype(value)(value); } } // namespace best diff --git a/best/base/internal/ord.h b/best/base/internal/ord.h index 4f0fa17..ee9f685 100644 --- a/best/base/internal/ord.h +++ b/best/base/internal/ord.h @@ -60,7 +60,7 @@ template T common_ord(T); template auto common_ord(T, T, auto... rest) // - -> decltype(common_ord(best::lie, rest...)); + -> decltype(common_ord(best::lie, rest...)); sord common_ord(eq); sord common_ord(le); @@ -86,77 +86,77 @@ pord common_ord(uo, gt); pord common_ord(uo, uo); auto common_ord(sord, eq, auto... rest) // - -> decltype(common_ord(sord::equivalent, rest...)); + -> decltype(common_ord(sord::equivalent, rest...)); auto common_ord(sord, le, auto... rest) // - -> decltype(common_ord(sord::equivalent, rest...)); + -> decltype(common_ord(sord::equivalent, rest...)); auto common_ord(sord, gt, auto... rest) // - -> decltype(common_ord(sord::equivalent, rest...)); + -> decltype(common_ord(sord::equivalent, rest...)); auto common_ord(eq, sord, auto... rest) // - -> decltype(common_ord(sord::equivalent, rest...)); + -> decltype(common_ord(sord::equivalent, rest...)); auto common_ord(le, sord, auto... rest) // - -> decltype(common_ord(sord::equivalent, rest...)); + -> decltype(common_ord(sord::equivalent, rest...)); auto common_ord(gt, sord, auto... rest) // - -> decltype(common_ord(sord::equivalent, rest...)); + -> decltype(common_ord(sord::equivalent, rest...)); auto common_ord(word, eq, auto... rest) // - -> decltype(common_ord(word::equivalent, rest...)); + -> decltype(common_ord(word::equivalent, rest...)); auto common_ord(word, le, auto... rest) // - -> decltype(common_ord(word::equivalent, rest...)); + -> decltype(common_ord(word::equivalent, rest...)); auto common_ord(word, gt, auto... rest) // - -> decltype(common_ord(word::equivalent, rest...)); + -> decltype(common_ord(word::equivalent, rest...)); auto common_ord(eq, word, auto... rest) // - -> decltype(common_ord(word::equivalent, rest...)); + -> decltype(common_ord(word::equivalent, rest...)); auto common_ord(le, word, auto... rest) // - -> decltype(common_ord(word::equivalent, rest...)); + -> decltype(common_ord(word::equivalent, rest...)); auto common_ord(gt, word, auto... rest) // - -> decltype(common_ord(word::equivalent, rest...)); + -> decltype(common_ord(word::equivalent, rest...)); auto common_ord(pord, eq, auto... rest) // - -> decltype(common_ord(pord::equivalent, rest...)); + -> decltype(common_ord(pord::equivalent, rest...)); auto common_ord(pord, le, auto... rest) // - -> decltype(common_ord(pord::equivalent, rest...)); + -> decltype(common_ord(pord::equivalent, rest...)); auto common_ord(pord, gt, auto... rest) // - -> decltype(common_ord(pord::equivalent, rest...)); + -> decltype(common_ord(pord::equivalent, rest...)); auto common_ord(eq, pord, auto... rest) // - -> decltype(common_ord(pord::equivalent, rest...)); + -> decltype(common_ord(pord::equivalent, rest...)); auto common_ord(le, pord, auto... rest) // - -> decltype(common_ord(pord::equivalent, rest...)); + -> decltype(common_ord(pord::equivalent, rest...)); auto common_ord(gt, pord, auto... rest) // - -> decltype(common_ord(pord::equivalent, rest...)); + -> decltype(common_ord(pord::equivalent, rest...)); auto common_ord(uo, sord, auto... rest) // - -> decltype(common_ord(pord::equivalent, rest...)); + -> decltype(common_ord(pord::equivalent, rest...)); auto common_ord(uo, word, auto... rest) // - -> decltype(common_ord(pord::equivalent, rest...)); + -> decltype(common_ord(pord::equivalent, rest...)); auto common_ord(uo, pord, auto... rest) // - -> decltype(common_ord(pord::equivalent, rest...)); + -> decltype(common_ord(pord::equivalent, rest...)); auto common_ord(sord, uo, auto... rest) // - -> decltype(common_ord(pord::equivalent, rest...)); + -> decltype(common_ord(pord::equivalent, rest...)); auto common_ord(word, uo, auto... rest) // - -> decltype(common_ord(pord::equivalent, rest...)); + -> decltype(common_ord(pord::equivalent, rest...)); auto common_ord(pord, uo, auto... rest) // - -> decltype(common_ord(pord::equivalent, rest...)); + -> decltype(common_ord(pord::equivalent, rest...)); auto common_ord(sord, sord, auto... rest) // - -> decltype(common_ord(sord::equivalent, rest...)); + -> decltype(common_ord(sord::equivalent, rest...)); auto common_ord(sord, word, auto... rest) // - -> decltype(common_ord(word::equivalent, rest...)); + -> decltype(common_ord(word::equivalent, rest...)); auto common_ord(sord, pord, auto... rest) // - -> decltype(common_ord(pord::equivalent, rest...)); + -> decltype(common_ord(pord::equivalent, rest...)); auto common_ord(word, sord, auto... rest) // - -> decltype(common_ord(word::equivalent, rest...)); + -> decltype(common_ord(word::equivalent, rest...)); auto common_ord(word, word, auto... rest) // - -> decltype(common_ord(word::equivalent, rest...)); + -> decltype(common_ord(word::equivalent, rest...)); auto common_ord(word, pord, auto... rest) // - -> decltype(common_ord(pord::equivalent, rest...)); + -> decltype(common_ord(pord::equivalent, rest...)); auto common_ord(pord, sord, auto... rest) // - -> decltype(common_ord(pord::equivalent, rest...)); + -> decltype(common_ord(pord::equivalent, rest...)); auto common_ord(pord, word, auto... rest) // - -> decltype(common_ord(pord::equivalent, rest...)); + -> decltype(common_ord(pord::equivalent, rest...)); auto common_ord(pord, pord, auto... rest) // - -> decltype(common_ord(pord::equivalent, rest...)); + -> decltype(common_ord(pord::equivalent, rest...)); auto common_impl(best::rank<1>, auto... args) -> decltype(common_ord(args...)); void common_impl(best::rank<0>, auto...); @@ -171,9 +171,9 @@ class chain final { constexpr friend auto operator->*(auto lhs, chain&& rhs) { using output = - common; + common; - if (!(lhs == 0)) return output(lhs); + if (!(lhs == 0)) { return output(lhs); } return output(best::call(BEST_FWD(rhs).cb_)); } diff --git a/best/base/niche.h b/best/base/niche.h index 72151e3..d5500a9 100644 --- a/best/base/niche.h +++ b/best/base/niche.h @@ -50,8 +50,8 @@ struct niche final {}; /// Whether T is a type with a niche. template concept has_niche = - best::is_ref || (best::is_object && best::constructible && - best::equatable); + best::is_ref || (best::is_object && best::constructible && + best::equatable); } // namespace best #endif // BEST_BASE_NICHE_H_ diff --git a/best/base/ord.h b/best/base/ord.h index d26b1a4..89455a8 100644 --- a/best/base/ord.h +++ b/best/base/ord.h @@ -83,12 +83,12 @@ concept testable = best::converts_to && requires(T cond) { /// types to be void, and does not require either to be self-comparable. template concept equatable = - (best::is_void && best::is_void) || requires(const T& a, const U& b) { - { a == b } -> best::testable; - { b == a } -> best::testable; - { a != b } -> best::testable; - { b != a } -> best::testable; - }; + (best::is_void && best::is_void) || requires(const T& a, const U& b) { + { a == b } -> best::testable; + { b == a } -> best::testable; + { a != b } -> best::testable; + { b != a } -> best::testable; + }; /// # `best::comparable` /// @@ -98,11 +98,11 @@ concept equatable = /// types to be void, and does not require either to be self-comparable. template concept comparable = - (best::is_void && best::is_void) || requires(const T& a, const U& b) { - requires best::equatable; - { a <=> b } -> best::converts_to b)>; - { b <=> a } -> best::converts_to a)>; - }; + (best::is_void && best::is_void) || requires(const T& a, const U& b) { + requires best::equatable; + { a <=> b } -> best::converts_to b)>; + { b <=> a } -> best::converts_to a)>; + }; /// # `best::order_type` /// diff --git a/best/base/ord_test.cc b/best/base/ord_test.cc index ecff15b..955c87a 100644 --- a/best/base/ord_test.cc +++ b/best/base/ord_test.cc @@ -39,8 +39,8 @@ best::test Eq = [](auto& t) { best::test Chain = [](auto& t) { t.expect_eq( - best::ord::equal->*best::or_cmp([] { return best::ord::greater; }), - best::ord::greater); + best::ord::equal->*best::or_cmp([] { return best::ord::greater; }), + best::ord::greater); t.expect_eq(best::ord::less->*best::or_cmp([] { return best::ord::greater; }), best::ord::less); }; diff --git a/best/base/port.h b/best/base/port.h index 47175aa..4a26c94 100644 --- a/best/base/port.h +++ b/best/base/port.h @@ -112,7 +112,7 @@ inline constexpr bool is_debug() { /// Like `BEST_ENABLE_IF()`, but merely requires `expr_` to be constexpr. #define BEST_ENABLE_IF_CONSTEXPR(expr_) \ __attribute__(( \ - enable_if(__builtin_constant_p(expr_), "expected a constexpr value"))) + enable_if(__builtin_constant_p(expr_), "expected a constexpr value"))) #else #define BEST_HAS_ENABLE_IF 0 diff --git a/best/base/tags.h b/best/base/tags.h index 70b4520..02c58ee 100644 --- a/best/base/tags.h +++ b/best/base/tags.h @@ -69,10 +69,10 @@ struct ftadle final {}; namespace tags_internal_do_not_use { struct ctad_guard; -#define BEST_CTAD_GUARD_(type_, Tag_) \ - static_assert( \ - ::std::is_same_v, \ - "you may not instantiate " type_ " manually; please use CTAD instead") +#define BEST_CTAD_GUARD_(type_, Tag_) \ + static_assert( \ + ::std::is_same_v, \ + "you may not instantiate " type_ " manually; please use CTAD instead") } // namespace tags_internal_do_not_use } // namespace best diff --git a/best/cli/app.cc b/best/cli/app.cc index e769692..8fbafd6 100644 --- a/best/cli/app.cc +++ b/best/cli/app.cc @@ -29,19 +29,19 @@ void app::install() { auto prev = global_main.exchange(this); if (prev != nullptr) { best::wtf( - "detected two distinct `best::app`s in the same binary, at {:?} and " - "{:?}", - prev->loc_, loc_); + "detected two distinct `best::app`s in the same binary, at {:?} and " + "{:?}", + prev->loc_, loc_); } } best::pretext app::exe() { - if (real_argv.is_empty()) return ""; + if (real_argv.is_empty()) { return ""; } return real_argv[0]; } best::span> app::argv() { - if (real_argv.is_empty()) return {}; + if (real_argv.is_empty()) { return {}; } return real_argv[{.start = 1}]; } diff --git a/best/cli/app.h b/best/cli/app.h index 8efbed9..7a9081f 100644 --- a/best/cli/app.h +++ b/best/cli/app.h @@ -105,7 +105,7 @@ class app final { using mark_format_header_as_used = best::formatter; app(auto main, best::location loc, int (*shim)(uintptr_t)) - : main_(reinterpret_cast(main)), shim_(shim), loc_(loc) { + : main_(reinterpret_cast(main)), shim_(shim), loc_(loc) { install(); } @@ -124,57 +124,57 @@ class app final { namespace best { inline app::app(void (*main)(), best::location loc) - : app(main, loc, [](uintptr_t vp) { - auto main = reinterpret_cast(vp); - main(); - return 0; - }) {} + : app(main, loc, [](uintptr_t vp) { + auto main = reinterpret_cast(vp); + main(); + return 0; + }) {} inline app::app(int (*main)(), best::location loc) - : app(main, loc, [](uintptr_t vp) { - auto main = reinterpret_cast(vp); - return main(); - }) {} + : app(main, loc, [](uintptr_t vp) { + auto main = reinterpret_cast(vp); + return main(); + }) {} template app::app(best::result (*main)(), best::location loc) - : app(main, loc, [](uintptr_t vp) { - auto main = reinterpret_cast (*)()>(vp); - (void)*main(); - return 0; - }) {} + : app(main, loc, [](uintptr_t vp) { + auto main = reinterpret_cast (*)()>(vp); + (void)*main(); + return 0; + }) {} template app::app(void (*main)(Args&), best::location loc) - : app(main, loc, [](uintptr_t vp) { - auto args = best::parse_flags(exe(), argv()); - if (!args) args.err()->print_and_exit(); + : app(main, loc, [](uintptr_t vp) { + auto args = best::parse_flags(exe(), argv()); + if (!args) { args.err()->print_and_exit(); } - auto main = reinterpret_cast(vp); - main(*args); - return 0; - }) {} + auto main = reinterpret_cast(vp); + main(*args); + return 0; + }) {} template app::app(int (*main)(Args&), best::location loc) - : app(main, loc, [](uintptr_t vp) { - auto args = best::parse_flags(exe(), argv()); - if (!args) args.err()->print_and_exit(); + : app(main, loc, [](uintptr_t vp) { + auto args = best::parse_flags(exe(), argv()); + if (!args) { args.err()->print_and_exit(); } - auto main = reinterpret_cast(vp); - return main(*args); - }) {} + auto main = reinterpret_cast(vp); + return main(*args); + }) {} template app::app(best::result (*main)(Args&), best::location loc) - : app(main, loc, [](uintptr_t vp) { - auto args = best::parse_flags(exe(), argv()); - if (!args) args.err()->print_and_exit(); - - auto main = reinterpret_cast (*)(Args&)>(vp); - (void)*main(*args); - return 0; - }) {} + : app(main, loc, [](uintptr_t vp) { + auto args = best::parse_flags(exe(), argv()); + if (!args) { args.err()->print_and_exit(); } + + auto main = reinterpret_cast (*)(Args&)>(vp); + (void)*main(*args); + return 0; + }) {} } // namespace best #endif // BEST_CLI_APP_H_ diff --git a/best/cli/cli.h b/best/cli/cli.h index 278a3e8..8b628d6 100644 --- a/best/cli/cli.h +++ b/best/cli/cli.h @@ -274,7 +274,7 @@ class cli final { /// /// Constructs a new error. explicit error(best::strbuf message, bool is_fatal) - : message_(BEST_MOVE(message)), is_fatal_(is_fatal) {} + : message_(BEST_MOVE(message)), is_fatal_(is_fatal) {} /// # `error::message()` /// @@ -295,8 +295,8 @@ class cli final { friend void BestFmt(auto& fmt, const error& e) { fmt.record("cli::error") - .field("message", e.message()) - .field("is_fatal", e.is_fatal()); + .field("message", e.message()) + .field("is_fatal", e.is_fatal()); } private: @@ -355,16 +355,16 @@ class cli final { /// Executes a parse operation. best::result parse( - void* flags, best::pretext exe, - best::span> argv) const; + void* flags, best::pretext exe, + best::span> argv) const; template friend const cli& cli_for(); template friend best::result parse_flags( - best::pretext exe, - best::span> argv); + best::pretext exe, + best::span> argv); struct impl; std::unique_ptr impl_; diff --git a/best/cli/cli_test.cc b/best/cli/cli_test.cc index 36ad803..d35c6c2 100644 --- a/best/cli/cli_test.cc +++ b/best/cli/cli_test.cc @@ -98,18 +98,18 @@ best::test TopLevelFlags = [](auto& t) { expect_ok(t, {"--name", "solomon", "--addr=cambridge"}, { - .name = "solomon", - .addr = "cambridge", + .name = "solomon", + .addr = "cambridge", }); expect_ok(t, {"--my-name", "solomon", "--my-address=cambridge"}, { - .name = "solomon", - .addr = "cambridge", + .name = "solomon", + .addr = "cambridge", }); expect_ok(t, {"--my-name", "🧶🐈‍⬛", "--my-address==="}, { - .name = "🧶🐈‍⬛", - .addr = "==", + .name = "🧶🐈‍⬛", + .addr = "==", }); expect_ok(t, {"-a"}, {.flag1 = true}); @@ -141,19 +141,17 @@ best::test TopLevelFlags = [](auto& t) { expect_ok(t, {"--flag2=0x0"}, {.flag2 = false}); expect_ok(t, {"--flag2=0b00"}, {.flag2 = false}); - expect_ok(t, - {"-x", "5", "--why", "6", "-z=7", - "--a-flag-with-a-freakishly-long-name=8"}, - { - .flattened = {.eks = 5, - .why = 6, - .zed = 7, - .a_flag_with_a_freakishly_long_name = 8}, - }); + expect_ok( + t, + {"-x", "5", "--why", "6", "-z=7", "--a-flag-with-a-freakishly-long-name=8"}, + { + .flattened = + {.eks = 5, .why = 6, .zed = 7, .a_flag_with_a_freakishly_long_name = 8}, + }); expect_fail( - t, {"--flattened.eks"}, - "cli_test: fatal: unknown flag \"--flattened.eks\"\ncli_test: you can " - "use `--` if you meant to pass this as a positional argument"); + t, {"--flattened.eks"}, + "cli_test: fatal: unknown flag \"--flattened.eks\"\ncli_test: you can " + "use `--` if you meant to pass this as a positional argument"); }; best::test Group = [](auto& t) { @@ -192,7 +190,7 @@ best::test Sub = [](auto& t) { best::test Help = [](auto& t) { best::str usage = - R"help(Usage: cli_test -Xabcdfhxyz [OPTIONS] [sub|sub2|sub3] [ARG1] [ARG2]... + R"help(Usage: cli_test -Xabcdfhxyz [OPTIONS] [sub|sub2|sub3] [ARG1] [ARG2]... this is a test binary for playing with all of best::cli's features @@ -231,7 +229,7 @@ Website: (c) 2024 mcyoung, licensed Apache-2.0 )help"; best::str hidden = - R"help(Usage: cli_test -Xabcdfhxyz [OPTIONS] [sub|sub2|sub3] [ARG1] [ARG2]... + R"help(Usage: cli_test -Xabcdfhxyz [OPTIONS] [sub|sub2|sub3] [ARG1] [ARG2]... this is a test binary for playing with all of best::cli's features @@ -283,7 +281,7 @@ Website: )help"; best::str group = - R"help(Usage: cli_test -X [SUBOPTION] -Xabcdfhxyz [OPTIONS] [sub|sub2|sub3] [ARG1] [ARG2]... + R"help(Usage: cli_test -X [SUBOPTION] -Xabcdfhxyz [OPTIONS] [sub|sub2|sub3] [ARG1] [ARG2]... extra options behind the -X flag # Flags @@ -302,7 +300,7 @@ Website: )help"; best::str group_hidden = - R"help(Usage: cli_test -X [SUBOPTION] -Xabcdfhxyz [OPTIONS] [sub|sub2|sub3] [ARG1] [ARG2]... + R"help(Usage: cli_test -X [SUBOPTION] -Xabcdfhxyz [OPTIONS] [sub|sub2|sub3] [ARG1] [ARG2]... extra options behind the -X flag # Flags @@ -322,7 +320,7 @@ Website: )help"; best::str sub = - R"help(Usage: cli_test sub -hs [OPTIONS] [ARG1] + R"help(Usage: cli_test sub -hs [OPTIONS] [ARG1] longer help for the subcommand with multiple lines @@ -338,7 +336,7 @@ Website: )help"; best::str sub_hidden = - R"help(Usage: cli_test sub -hs [OPTIONS] [ARG1] + R"help(Usage: cli_test sub -hs [OPTIONS] [ARG1] longer help for the subcommand with multiple lines diff --git a/best/cli/parser.cc b/best/cli/parser.cc index 93abaf0..257eb2a 100644 --- a/best/cli/parser.cc +++ b/best/cli/parser.cc @@ -95,21 +95,21 @@ struct cli::impl { best::option find_flag(best::pretext tok) const { return sorted_flags.as_span() - .bisect(tok, &entry::key) - .ok() - .map([&](size_t idx) -> auto&& { return sorted_flags[idx]; }); + .bisect(tok, &entry::key) + .ok() + .map([&](size_t idx) -> auto&& { return sorted_flags[idx]; }); } best::option find_sub(best::pretext tok) const { return sorted_subs.as_span() - .bisect(tok, &entry::key) - .ok() - .map([&](size_t idx) -> auto&& { return sorted_subs[idx]; }); + .bisect(tok, &entry::key) + .ok() + .map([&](size_t idx) -> auto&& { return sorted_subs[idx]; }); } }; cli::cli(best::option app) - : impl_(new impl{.app = app.value_or()}) {} + : impl_(new impl{.app = app.value_or()}) {} cli::cli(cli&&) = default; cli& cli::operator=(cli&&) = default; @@ -144,7 +144,7 @@ void normalize(best::strbuf& name, const auto& about) { // Normalize all underscores to dashes. This does not violate UTF-8-ness. for (size_t i = 0; i < name.size(); ++i) { char& c = name.data()[i]; - if (c == '_') c = '-'; + if (c == '_') { c = '-'; } } } } // namespace @@ -158,7 +158,7 @@ void cli::add(about about, const subcommand& tag, cli& child) { child.impl_->parent = &*impl_; child.impl_->parent_sub = &tag; child.impl_->parent_sub_name = - tag.name.is_empty() ? best::strbuf(about.field) : best::strbuf(tag.name); + tag.name.is_empty() ? best::strbuf(about.field) : best::strbuf(tag.name); impl_->subs.push(std::move(about), &tag, &child); } @@ -184,7 +184,7 @@ void cli::init() { auto has_letter = f.tag->letter != '\0'; for (auto [name_idx, pair] : f.about.names.iter().enumerate()) { auto& [name, vis] = pair; - if (vis == Delete) continue; + if (vis == Delete) { continue; } normalize(name, f.about); if (name == "help" || name == "help-hidden" || name == "version") { @@ -193,18 +193,18 @@ void cli::init() { } impl_->sorted_flags.push(impl::entry{ - .key = name, - .idx = idx, - .is_letter = name_idx == 0 && has_letter, - .is_alias = name_idx > size_t(has_letter), - .vis = vis, + .key = name, + .idx = idx, + .is_letter = name_idx == 0 && has_letter, + .is_alias = name_idx > size_t(has_letter), + .vis = vis, }); } } for (auto [idx, s] : impl_->subs.iter().enumerate()) { for (auto& [name, vis] : s.about.names) { - if (vis == Delete) continue; + if (vis == Delete) { continue; } normalize(name, s.about); impl_->sorted_subs.push(impl::entry{.key = name, .idx = idx, .vis = vis}); @@ -231,24 +231,16 @@ void cli::init() { // Update the visibilities of the copied entries. auto update = [&](about& about) { - for (auto& [x, vis] : about.names) { - vis = merge(vis, g->tag->vis); - } + for (auto& [x, vis] : about.names) { vis = merge(vis, g->tag->vis); } }; - for (auto& f : impl_->flags[{.start = flag_offset}]) { - update(f.about); - } - for (auto& s : impl_->subs[{.start = sub_offset}]) { - update(s.about); - } - for (auto& g : impl_->groups[{.start = group_offset}]) { - update(g.about); - } + for (auto& f : impl_->flags[{.start = flag_offset}]) { update(f.about); } + for (auto& s : impl_->subs[{.start = sub_offset}]) { update(s.about); } + for (auto& g : impl_->groups[{.start = group_offset}]) { update(g.about); } auto has_letter = g->tag->letter != '\0'; for (auto [name_idx, pair] : g->about.names.iter().enumerate()) { auto& [name, vis] = pair; - if (vis == Delete) continue; + if (vis == Delete) { continue; } bool is_flatten = !has_letter && name.is_empty(); bool is_letter = !is_flatten && name_idx == 0 && has_letter; @@ -256,16 +248,16 @@ void cli::init() { normalize(name, g->about); if (name == "help" || name == "help-hidden" || name == "version") { best::wtf( - "field {}::{}'s name ({:?}) is reserved and may not be used", - g->about.strukt->path(), g->about.field, name); + "field {}::{}'s name ({:?}) is reserved and may not be used", + g->about.strukt->path(), g->about.field, name); } impl_->sorted_flags.push(impl::entry{ - .key = name, - .idx = idx, - .is_group = true, - .is_letter = is_letter, - .is_alias = name_idx > size_t(has_letter), + .key = name, + .idx = idx, + .is_group = true, + .is_letter = is_letter, + .is_alias = name_idx > size_t(has_letter), }); // Letter names for groups are parsed in a different way that does not @@ -275,7 +267,7 @@ void cli::init() { auto copy_vis = merge(vis, is_flatten ? Public : Hidden); for (auto entry : g->child->impl_->sorted_flags) { - if (is_flatten && entry.magic != entry.Ordinary) continue; + if (is_flatten && entry.magic != entry.Ordinary) { continue; } if (entry.magic == entry.Ordinary) { entry.idx += entry.is_group ? group_offset : flag_offset; @@ -313,7 +305,7 @@ void cli::init() { // Pull out all of the required flags. for (const auto& f : impl_->flags) { - if (f.get_count() != Required) continue; + if (f.get_count() != Required) { continue; } auto name = f.tag->letter != '\0' ? f.about.names[1].first() : f.about.names[0].first(); @@ -322,23 +314,23 @@ void cli::init() { // Add magic flags. impl_->sorted_flags.push(impl::entry{ - .key = {"help"}, - .idx = -1, - .vis = Public, - .magic = impl::entry::Help, + .key = {"help"}, + .idx = -1, + .vis = Public, + .magic = impl::entry::Help, }); impl_->sorted_flags.push(impl::entry{ - .key = {"h"}, - .idx = -1, - .is_letter = true, - .vis = Public, - .magic = impl::entry::Help, + .key = {"h"}, + .idx = -1, + .is_letter = true, + .vis = Public, + .magic = impl::entry::Help, }); impl_->sorted_flags.push(impl::entry{ - .key = {"help-hidden"}, - .idx = -1, - .vis = Hidden, - .magic = impl::entry::HelpHidden, + .key = {"help-hidden"}, + .idx = -1, + .vis = Hidden, + .magic = impl::entry::HelpHidden, }); // Now, sort the flags so we can bisect through them later. @@ -370,15 +362,15 @@ void cli::init() { } best::result cli::parse( - void* args, best::pretext exe, - best::span> argv) const { + void* args, best::pretext exe, + best::span> argv) const { context ctx = { - .exe = exe.split('/').last().value_or(), - .root = this, - .sub = this, - .cur = this, - .next_positional = 0, - .args = args, + .exe = exe.split('/').last().value_or(), + .root = this, + .sub = this, + .cur = this, + .next_positional = 0, + .args = args, }; bool done_with_flags = false; @@ -416,7 +408,7 @@ best::result cli::parse( } auto push_group = [&](size_t idx, bool update_arg = - true) -> best::result { + true) -> best::result { // We need to parse a flag from the next argument, of the form e.g. // -C opt-level=3. @@ -424,17 +416,17 @@ best::result cli::parse( // This type of flag cannot use a = argument. if (arg) { return best::err( - best::format("{0}: fatal: unexpected argument after {1}", - ctx.exe, token), - /*is_fatal=*/true); + best::format("{0}: fatal: unexpected argument after {1}", ctx.exe, + token), + /*is_fatal=*/true); } auto next_arg = iter.next(); if (!next_arg) { return best::err( - best::format("{0}: fatal: expected sub-flag after {1}", ctx.exe, - token), - /*is_fatal=*/true); + best::format("{0}: fatal: expected sub-flag after {1}", ctx.exe, + token), + /*is_fatal=*/true); } // Update flag and arg. @@ -451,10 +443,9 @@ best::result cli::parse( }; auto interpret_magic = - [&](const impl::entry& e) -> best::result { + [&](const impl::entry& e) -> best::result { switch (e.magic) { - case impl::entry::Ordinary: - return best::ok(); + case impl::entry::Ordinary: return best::ok(); case impl::entry::Help: return best::err(ctx.cur->usage(ctx.exe, false), /*is_fatal=*/false); @@ -474,28 +465,28 @@ best::result cli::parse( auto tok = best::format("-{}", r); auto e = ctx.cur->impl_->find_flag(tok[{.start = 1}]); - if (!e || !e->is_letter) break; + if (!e || !e->is_letter) { break; } if (e->is_group) { // This is a nesting of the form -Copt-level. Unlike the case below, // we do not need to update flag/arg if there remain runes to be // consumed. bool update = runes->rest().is_empty(); BEST_GUARD(push_group(e->idx, update)); - if (!update) flag = runes->rest(); + if (!update) { flag = runes->rest(); } continue; } BEST_GUARD(interpret_magic(*e)); const auto& f = ctx.cur->impl_->flags[e->idx]; - if (f.query->wants_arg) break; + if (f.query->wants_arg) { break; } ctx.token = tok; auto [it, inserted] = seen.insert(f.tag); if (!inserted && f.get_count() != Repeated) { return best::err( - best::format("{0}: fatal: flag {1} appeared more than once", - ctx.exe, tok), - /*is_fatal=*/true); + best::format("{0}: fatal: flag {1} appeared more than once", + ctx.exe, tok), + /*is_fatal=*/true); } // Need to be careful here: we might have a !want_arg flag that @@ -511,7 +502,7 @@ best::result cli::parse( flag = runes->rest(); } - if (flag.is_empty()) goto again; + if (flag.is_empty()) { goto again; } } ctx.token = *next; @@ -533,28 +524,28 @@ best::result cli::parse( const auto& f = ctx.cur->impl_->flags[e->idx]; best::str dash = "-"; - if (!is_letter) dash = "--"; + if (!is_letter) { dash = "--"; } auto tok = best::format("{}{}", dash, flag); ctx.token = tok; auto [it, inserted] = seen.insert(f.tag); if (!inserted && f.get_count() != Repeated) { best::str dash = "-"; - if (!is_letter) dash = "--"; + if (!is_letter) { dash = "--"; } return best::err( - best::format("{0}: fatal: flag {1} appeared more than once", - ctx.exe, tok), - /*is_fatal=*/true); + best::format("{0}: fatal: flag {1} appeared more than once", + ctx.exe, tok), + /*is_fatal=*/true); } if (!arg && f.query->wants_arg) { arg = iter.next(); if (!arg) { return best::err( - best::format("{0}: fatal: expected argument after {1}", - ctx.exe, token), - /*is_fatal=*/true); + best::format("{0}: fatal: expected argument after {1}", ctx.exe, + token), + /*is_fatal=*/true); } } @@ -564,11 +555,11 @@ best::result cli::parse( // Looks like a bad flag. Users need to pass `--`. return best::err( - best::format("{0}: fatal: unknown flag {1:?}\n" - "{0}: you can use `--` if you meant to pass " - "this as a positional argument", - ctx.exe, token), - /*is_fatal=*/true); + best::format("{0}: fatal: unknown flag {1:?}\n" + "{0}: you can use `--` if you meant to pass " + "this as a positional argument", + ctx.exe, token), + /*is_fatal=*/true); } } @@ -581,9 +572,7 @@ best::result cli::parse( // If not, this is definitely a positional. if (auto p = ctx.cur->impl_->args.at(ctx.next_positional)) { BEST_GUARD(p->parse(ctx, *next)); - if (p->get_count() != Repeated) { - ++ctx.next_positional; - } + if (p->get_count() != Repeated) { ++ctx.next_positional; } goto again; } @@ -597,8 +586,8 @@ best::result cli::parse( for (const auto& [req, name] : impl_->required) { if (!seen.contains(req)) { return best::err( - best::format("{0}: fatal: missing flag --{1}", ctx.exe, name), - /*is_fatal=*/true); + best::format("{0}: fatal: missing flag --{1}", ctx.exe, name), + /*is_fatal=*/true); } } @@ -616,7 +605,7 @@ size_t width_of(best::str s) { best::strbuf cli::usage(best::pretext exe, bool hidden) const { best::strbuf out; auto indent = [&](size_t n) { - for (size_t i = 0; i < n; ++i) out.push(" "); + for (size_t i = 0; i < n; ++i) { out.push(" "); } }; auto indent_dots = [&](size_t n) { @@ -648,38 +637,30 @@ best::strbuf cli::usage(best::pretext exe, bool hidden) const { best::format(out, "Usage: {}", exe); if (!parents.is_empty()) { parents->reverse(); - for (auto sub : parents) { - best::format(out, " {}", sub); - } + for (auto sub : parents) { best::format(out, " {}", sub); } } // We also need to dig our way out of any groups we're in. auto* cmd = impl_.get(); - while (cmd->parent_group) cmd = cmd->parent; + while (cmd->parent_group) { cmd = cmd->parent; } // Append all of the letters, but only if we're inside of a subcommand, not a // group. - if (impl_->parent_group != nullptr) { - out.push(" [SUBOPTION]"); - } + if (impl_->parent_group != nullptr) { out.push(" [SUBOPTION]"); } bool needs_dash = true; for (const auto& e : cmd->sorted_flags) { - if (!e.is_letter || e.is_copy || !visible(e.vis, hidden)) continue; - if (std::exchange(needs_dash, false)) { - out.push(" -"); - } + if (!e.is_letter || e.is_copy || !visible(e.vis, hidden)) { continue; } + if (std::exchange(needs_dash, false)) { out.push(" -"); } out.push(e.key); } - if (!cmd->sorted_flags.is_empty()) { - out.push(" [OPTIONS]"); - } + if (!cmd->sorted_flags.is_empty()) { out.push(" [OPTIONS]"); } // Append all of the subcommands. bool first = true; for (const auto& s : cmd->sorted_subs) { - if (s.is_alias) continue; + if (s.is_alias) { continue; } if (std::exchange(first, false)) { out.push(" ["); } else { @@ -687,35 +668,21 @@ best::strbuf cli::usage(best::pretext exe, bool hidden) const { } out.push(s.key); } - if (!first) { - out.push("]"); - } + if (!first) { out.push("]"); } // Append all of the arguments. for (auto [idx, p] : cmd->args.iter().enumerate()) { if (best::str name = p.tag->name; !name.is_empty()) { switch (p.get_count()) { - case Optional: - best::format(out, " [{}]", name); - break; - case Required: - best::format(out, " <{}>", name); - break; - case Repeated: - best::format(out, " [{}]...", name); - break; + case Optional: best::format(out, " [{}]", name); break; + case Required: best::format(out, " <{}>", name); break; + case Repeated: best::format(out, " [{}]...", name); break; } } else { switch (p.get_count()) { - case Optional: - best::format(out, " [ARG{}]", idx + 1); - break; - case Required: - best::format(out, " ", idx + 1); - break; - case Repeated: - best::format(out, " [ARG{}]...", idx + 1); - break; + case Optional: best::format(out, " [ARG{}]", idx + 1); break; + case Required: best::format(out, " ", idx + 1); break; + case Repeated: best::format(out, " [ARG{}]...", idx + 1); break; } } } @@ -735,9 +702,7 @@ best::strbuf cli::usage(best::pretext exe, bool hidden) const { } else { out.push(app.about); } - if (start < out.size()) { - out.push("\n\n"); - } + if (start < out.size()) { out.push("\n\n"); } // This is how wide the column for a flag to be in-line with its // help is. @@ -746,11 +711,9 @@ best::strbuf cli::usage(best::pretext exe, bool hidden) const { // Next, print all of the subcommands. first = true; for (const auto& e : impl_->sorted_subs) { - if (!visible(e.vis, hidden) || e.is_alias) continue; + if (!visible(e.vis, hidden) || e.is_alias) { continue; } - if (std::exchange(first, false)) { - out.push("# Subcommands\n"); - } + if (std::exchange(first, false)) { out.push("# Subcommands\n"); } indent(6); out.push(e.key); @@ -764,12 +727,12 @@ best::strbuf cli::usage(best::pretext exe, bool hidden) const { bool first = true; for (auto line : impl_->subs[e.idx].tag->help.split("\n")) { - if (!std::exchange(first, false)) indent(Width + 2); + if (!std::exchange(first, false)) { indent(Width + 2); } out.push(line); out.push("\n"); } } - if (!first) out.push('\n'); + if (!first) { out.push('\n'); } out.push("# Flags\n"); @@ -792,12 +755,12 @@ best::strbuf cli::usage(best::pretext exe, bool hidden) const { if (f.query->wants_arg) { arg = f.tag->arg; - if (arg.is_empty()) arg = "ARG"; + if (arg.is_empty()) { arg = "ARG"; } } } - if (!visible(e.vis, hidden)) return; - if (help.is_empty()) help = ""; + if (!visible(e.vis, hidden)) { return; } + if (help.is_empty()) { help = ""; } size_t start = out.size(); out.push(" "); @@ -809,7 +772,7 @@ best::strbuf cli::usage(best::pretext exe, bool hidden) const { } best::str prefix = e.key[{ - .end = e.key.size() - e.key->split('.').last()->size(), + .end = e.key.size() - e.key->split('.').last()->size(), }]; // Chop off everything past the last `.` to make the prefix. @@ -817,7 +780,7 @@ best::strbuf cli::usage(best::pretext exe, bool hidden) const { auto helps = help.split("\n"); bool first = true; for (auto [name, vis] : names) { - if (!visible(vis, hidden)) continue; + if (!visible(vis, hidden)) { continue; } bool is_first = std::exchange(first, false); if (!is_first) { @@ -836,7 +799,7 @@ best::strbuf cli::usage(best::pretext exe, bool hidden) const { out.push(arg); } - if (needs_comma) out.push(','); + if (needs_comma) { out.push(','); } auto help = helps.next(); @@ -879,10 +842,10 @@ best::strbuf cli::usage(best::pretext exe, bool hidden) const { bool has_letter = f.tag->letter != '\0'; // Undocumented flags are implicitly hidden. - if (f.tag->help == "" && !hidden) continue; + if (f.tag->help == "" && !hidden) { continue; } // This makes us prefer to alphabetize by letter when possible. - if (has_letter && !e.is_letter) continue; + if (has_letter && !e.is_letter) { continue; } print_flag(e); } @@ -901,9 +864,9 @@ best::strbuf cli::usage(best::pretext exe, bool hidden) const { } // This makes us prefer to alphabetize by name. - if ((has_letter || !e.is_group) && e.is_letter) continue; + if ((has_letter || !e.is_group) && e.is_letter) { continue; } - if (std::exchange(first, false)) out.push("\n"); + if (std::exchange(first, false)) { out.push("\n"); } print_flag(e); } @@ -928,15 +891,13 @@ best::strbuf cli::usage(best::pretext exe, bool hidden) const { best::format(out, "Version: {} v{}\n", app.name.is_empty() ? exe : app.name, app.version); } - if (!app.url.is_empty()) best::format(out, "Website: <{}>\n", app.url); + if (!app.url.is_empty()) { best::format(out, "Website: <{}>\n", app.url); } } if (!app.authors.is_empty()) { out.push("\n"); out.push("(c) "); - if (app.copyright_year) { - best::format(out, "{} ", *app.copyright_year); - } + if (app.copyright_year) { best::format(out, "{} ", *app.copyright_year); } out.push(app.authors); if (!app.license.is_empty()) { best::format(out, ", licensed {}\n", app.license); diff --git a/best/cli/parser.h b/best/cli/parser.h index 6196c23..9f17d95 100644 --- a/best/cli/parser.h +++ b/best/cli/parser.h @@ -53,8 +53,8 @@ const cli& cli_for() { /// is passed as a separate argument. template best::result parse_flags( - best::pretext exe, - best::span> argv) { + best::pretext exe, + best::span> argv) { Flags flags; BEST_GUARD(best::cli_for().parse(&flags, exe, argv)); return flags; @@ -74,15 +74,13 @@ cli cli::build() { "found two or more best::cli_apps on flags struct"); const cli::app* app = nullptr; - if constexpr (!tags.is_empty()) { - app = &tags.first(); - } + if constexpr (!tags.is_empty()) { app = &tags.first(); } best::cli cli(app); // Then, type-erase all the entries. best::reflect.each( - [&](auto field) { cli.type_erase_field(field); }); + [&](auto field) { cli.type_erase_field(field); }); // Having that, draw the rest of the frickin' owl. cli.init(); @@ -113,18 +111,18 @@ void cli::type_erase_field(auto f) { static constexpr auto poses = field.tags(best::types); static_assert( - (flags.size() + subs.size() + groups.size() + poses.size()) <= 1, - "cannot have more than one of best::cli_flag, best::cli_subcommand, or " - "best::cli_group attached to a field"); + (flags.size() + subs.size() + groups.size() + poses.size()) <= 1, + "cannot have more than one of best::cli_flag, best::cli_subcommand, or " + "best::cli_group attached to a field"); static constexpr auto Downcast = [](void* args) -> auto& { return downcast(args)->*Field{}; }; cli::about about{ - .strukt = &best::type_names::of, - .type = &best::type_names::of, - .field = field.name(), + .strukt = &best::type_names::of, + .type = &best::type_names::of, + .field = field.name(), }; if constexpr (!flags.is_empty()) { @@ -134,13 +132,13 @@ void cli::type_erase_field(auto f) { } best::str name = flag.name; - if (name.is_empty()) name = about.field; + if (name.is_empty()) { name = about.field; } about.names.push(best::strbuf(name), flag.vis); } else if constexpr (!subs.is_empty()) { constexpr const cli::subcommand& sub = subs.first(); best::str name = sub.name; - if (name.is_empty()) name = about.field; + if (name.is_empty()) { name = about.field; } about.names.push(best::strbuf(name), sub.vis); } else if constexpr (!groups.is_empty()) { constexpr const cli::group& group = groups.first(); @@ -164,17 +162,16 @@ void cli::type_erase_field(auto f) { constexpr const best::cli::flag& flag = flags.first(); add( - std::move(about), flag, argv_query::of, - +[](context& ctx, - best::pretext argv) -> best::result { - auto result = BestFromArgv(argv, Downcast(ctx.args)); - if (result) return best::ok(); - - return best::err( - best::format("{}: fatal: could not parse argument for {}: {}", - ctx.exe, ctx.token, *result.err()), - /*is_fatal=*/true); - }); + std::move(about), flag, argv_query::of, + +[](context& ctx, best::pretext argv) -> best::result { + auto result = BestFromArgv(argv, Downcast(ctx.args)); + if (result) { return best::ok(); } + + return best::err( + best::format("{}: fatal: could not parse argument for {}: {}", + ctx.exe, ctx.token, *result.err()), + /*is_fatal=*/true); + }); } else if constexpr (!subs.is_empty()) { constexpr const best::cli::subcommand& sub = subs.first(); add(std::move(about), sub, global_for); @@ -186,22 +183,18 @@ void cli::type_erase_field(auto f) { "positional type must implement BestFromArgv()"); const best::cli::positional* pos = &DefaultTag; - if constexpr (!poses.is_empty()) { - pos = &poses.first(); - } + if constexpr (!poses.is_empty()) { pos = &poses.first(); } add( - std::move(about), *pos, argv_query::of, - +[](context& ctx, - best::pretext argv) -> best::result { - auto result = BestFromArgv(argv, Downcast(ctx.args)); - if (result) return best::ok(); - - return best::err( - best::format("{}: fatal: could not parse argument: {}", ctx.exe, - ctx.token, *result.err()), - /*is_fatal=*/true); - }); + std::move(about), *pos, argv_query::of, + +[](context& ctx, best::pretext argv) -> best::result { + auto result = BestFromArgv(argv, Downcast(ctx.args)); + if (result) { return best::ok(); } + + return best::err(best::format("{}: fatal: could not parse argument: {}", + ctx.exe, ctx.token, *result.err()), + /*is_fatal=*/true); + }); } } // FromArgv implementations for common types. @@ -277,7 +270,7 @@ result BestFromArgv(auto raw, best::textbuf& arg) template auto BestFromArgv(auto raw, best::option& arg) - -> decltype(BestFromArgv(raw, arg.emplace())) { + -> decltype(BestFromArgv(raw, arg.emplace())) { return BestFromArgv(raw, arg.emplace()); } template @@ -287,7 +280,7 @@ constexpr auto BestFromArgvQuery(auto& query, best::option*) { template auto BestFromArgv(auto raw, best::vec& arg) - -> decltype(BestFromArgv(raw, arg.push())) { + -> decltype(BestFromArgv(raw, arg.push())) { return BestFromArgv(raw, arg.push()); } template diff --git a/best/cli/toy_flags.h b/best/cli/toy_flags.h index 3e0e116..c5c3547 100644 --- a/best/cli/toy_flags.h +++ b/best/cli/toy_flags.h @@ -29,13 +29,13 @@ struct Subcommand { int sub_flag = 0; best::str arg; - friend constexpr auto BestReflect(auto& m, Subcommand*) { + constexpr friend auto BestReflect(auto& m, Subcommand*) { using ::best::cli; return m.infer().with(&Subcommand::sub_flag, cli::flag{ - .letter = 's', - .arg = "INT", - .help = "a subcommand argument", + .letter = 's', + .arg = "INT", + .help = "a subcommand argument", }); } @@ -47,32 +47,32 @@ struct Subgroup { int a_flag_with_a_freakishly_long_name = 0; - friend constexpr auto BestReflect(auto& m, Subgroup*) { + constexpr friend auto BestReflect(auto& m, Subgroup*) { using ::best::cli; return m.infer() - .with(&Subgroup::eks, - cli::flag{ - .letter = 'x', - .arg = "INT", - .help = "a group integer", - }) - .with(&Subgroup::why, - cli::flag{ - .letter = 'y', - .arg = "INT", - .help = "another group integer", - }) - .with(&Subgroup::zed, - cli::flag{ - .letter = 'z', - .arg = "INT", - .help = "a third group integer", - }) - .with(&Subgroup::a_flag_with_a_freakishly_long_name, - cli::flag{ - .arg = "INT", - .help = "like, freakishly long man", - }); + .with(&Subgroup::eks, + cli::flag{ + .letter = 'x', + .arg = "INT", + .help = "a group integer", + }) + .with(&Subgroup::why, + cli::flag{ + .letter = 'y', + .arg = "INT", + .help = "another group integer", + }) + .with(&Subgroup::zed, + cli::flag{ + .letter = 'z', + .arg = "INT", + .help = "a third group integer", + }) + .with(&Subgroup::a_flag_with_a_freakishly_long_name, + cli::flag{ + .arg = "INT", + .help = "like, freakishly long man", + }); } bool operator==(const Subgroup&) const = default; @@ -97,95 +97,92 @@ struct Toy { best::str arg; best::vec args; - friend constexpr auto BestReflect(auto& m, Toy*) { + constexpr friend auto BestReflect(auto& m, Toy*) { using ::best::cli; return m.infer() - .with(cli::app{ - .name = "toy", - .authors = "mcyoung", - .about = "this is a test binary for playing with all of\n" - "best::cli's features", - .version = "1.0.0", - .url = "https://mcyoung.xyz", - .copyright_year = 2024, - .license = "Apache-2.0", - }) - .with(&Toy::foo, - cli::flag{ - .letter = 'f', - .arg = "INT", - .count = cli::Repeated, - .help = "an integer", - }) - .with(&Toy::bar, - cli::flag{ - .arg = "INT", - .help = "repeated integer", - }) - .with(&Toy::baz, - cli::flag{ - .help = "another integer", - }) - - .with(&Toy::name, - cli::flag{ - .vis = cli::Hidden, - .help = "your name", - }, - cli::alias{"my-name"}) - .with(&Toy::addr, - cli::flag{ - .vis = cli::Hidden, - .help = "your address", - }, - cli::alias{"my-address"}) - - .with(&Toy::flag1, - cli::flag{ - .letter = 'a', - .help = "this is a flag\nnewline", - }) - .with(&Toy::flag2, - cli::flag{ - .letter = 'b', - .count = cli::Repeated, - .help = "this is a flag\nnewline", - }) - .with(&Toy::flag3, - cli::flag{ - .letter = 'c', - .help = "this is a flag\nnewline", - }, - cli::alias{"flag3-alias"}, - cli::alias{"flag3-alias2", cli::Hidden}) - .with(&Toy::flag4, - cli::flag{ - .letter = 'd', - .help = "this is a flag\nnewline", - }) - .with(&Toy::undocumented, cli::flag{}) - - .with( - &Toy::sub, + .with(cli::app{ + .name = "toy", + .authors = "mcyoung", + .about = "this is a test binary for playing with all of\n" + "best::cli's features", + .version = "1.0.0", + .url = "https://mcyoung.xyz", + .copyright_year = 2024, + .license = "Apache-2.0", + }) + .with(&Toy::foo, + cli::flag{ + .letter = 'f', + .arg = "INT", + .count = cli::Repeated, + .help = "an integer", + }) + .with(&Toy::bar, + cli::flag{ + .arg = "INT", + .help = "repeated integer", + }) + .with(&Toy::baz, + cli::flag{ + .help = "another integer", + }) + + .with(&Toy::name, + cli::flag{ + .vis = cli::Hidden, + .help = "your name", + }, + cli::alias{"my-name"}) + .with(&Toy::addr, + cli::flag{ + .vis = cli::Hidden, + .help = "your address", + }, + cli::alias{"my-address"}) + + .with(&Toy::flag1, + cli::flag{ + .letter = 'a', + .help = "this is a flag\nnewline", + }) + .with(&Toy::flag2, + cli::flag{ + .letter = 'b', + .count = cli::Repeated, + .help = "this is a flag\nnewline", + }) + .with(&Toy::flag3, + cli::flag{ + .letter = 'c', + .help = "this is a flag\nnewline", + }, + cli::alias{"flag3-alias"}, cli::alias{"flag3-alias2", cli::Hidden}) + .with(&Toy::flag4, + cli::flag{ + .letter = 'd', + .help = "this is a flag\nnewline", + }) + .with(&Toy::undocumented, cli::flag{}) + + .with(&Toy::sub, cli::subcommand{ - .help = "a subcommand", - .about = "longer help for the subcommand\nwith multiple lines", + .help = "a subcommand", + .about = "longer help for the subcommand\nwith multiple lines", }) - .with( - &Toy::sub2, + .with(&Toy::sub2, cli::subcommand{ - .help = "identical in all ways to `sub`\nexcept for this help", - .about = "longer help for the subcommand\nwith multiple lines", + .help = "identical in all ways to `sub`\nexcept for this help", + .about = "longer help for the subcommand\nwith multiple lines", }, cli::alias{"sub3"}) - .with(&Toy::group, - cli::group{ - .name = "subgroup", - .letter = 'X', - .help = "extra options behind the -X flag", - }) - .with(&Toy::flattened, cli::group{}); + .with(&Toy::group, + cli::group{ + .name = "subgroup", + .letter = 'X', + .help = "extra options behind the -X flag", + }) + .with(&Toy::flattened, cli::group{}); } bool operator==(const Toy&) const = default; diff --git a/best/container/box.h b/best/container/box.h index c1cdd12..eff87d6 100644 --- a/best/container/box.h +++ b/best/container/box.h @@ -68,14 +68,10 @@ class BEST_RELOCATABLE box final { /// # `box::box(box)` /// /// Trivially relocatable. Copies perform memory allocations. - box(const box& that) - requires best::copyable && best::copyable; - box& operator=(const box& that) - requires best::copyable; - box(box&& that) - requires best::moveable; - box& operator=(box&& that) - requires best::moveable; + box(const box& that) requires best::copyable && best::copyable; + box& operator=(const box& that) requires best::copyable; + box(box&& that) requires best::moveable; + box& operator=(box&& that) requires best::moveable; /// # `box::box(value)` /// @@ -83,19 +79,19 @@ class BEST_RELOCATABLE box final { explicit box(const T& from) : box(best::in_place, from) {} explicit box(T&& from) : box(best::in_place, BEST_MOVE(from)) {} explicit box(alloc alloc, const T& from) - : box(BEST_FWD(alloc), best::in_place, from) {} + : box(BEST_FWD(alloc), best::in_place, from) {} explicit box(alloc alloc, T&& from) - : box(BEST_FWD(alloc), best::in_place, BEST_MOVE(from)) {} + : box(BEST_FWD(alloc), best::in_place, BEST_MOVE(from)) {} /// # `box::box(...)` /// /// Constructs a box by calling a constructor in-place. explicit box(best::in_place_t, auto&&... args) - : box(alloc{}, best::in_place, BEST_FWD(args)...) {} + : box(alloc{}, best::in_place, BEST_FWD(args)...) {} explicit box(alloc alloc, best::in_place_t, auto&&... args) - : alloc_(best::in_place, BEST_FWD(alloc)) { - ptr_ = best::ptr(allocator().alloc(best::layout::of())) - .cast(best::types); + : alloc_(best::in_place, BEST_FWD(alloc)) { + ptr_ = + best::ptr(allocator().alloc(best::layout::of())).cast(best::types); ptr_.construct(BEST_FWD(args)...); } @@ -105,13 +101,13 @@ class BEST_RELOCATABLE box final { /// the given allocated, with the layout of `T`. explicit box(unsafe u, best::ptr ptr) : box(u, alloc{}, ptr) {} explicit box(unsafe u, alloc alloc, best::ptr ptr) - : ptr_(ptr), alloc_(best::in_place, BEST_FWD(alloc)) {} + : ptr_(ptr), alloc_(best::in_place, BEST_FWD(alloc)) {} /// # `box::~box()` /// /// Boxes automatically destroy their contents. ~box() { - if (ptr_ == best::ptr::dangling()) return; + if (ptr_ == best::ptr::dangling()) { return; } ptr_.destroy(); allocator().dealloc(ptr_.raw(), best::layout::of()); @@ -162,7 +158,7 @@ class BEST_RELOCATABLE box final { { fmt.format(*box); } - friend constexpr void BestFmtQuery(auto& query, box*) { + constexpr friend void BestFmtQuery(auto& query, box*) { query = query.template of; } @@ -231,14 +227,10 @@ class BEST_RELOCATABLE box final { /// # `box::box(box)` /// /// Trivially relocatable. Copies perform memory allocations. - box(const box& that) - requires best::copyable && best::copyable; - box& operator=(const box& that) - requires best::copyable; - box(box&& that) - requires best::moveable; - box& operator=(box&& that) - requires best::moveable; + box(const box& that) requires best::copyable && best::copyable; + box& operator=(const box& that) requires best::copyable; + box(box&& that) requires best::moveable; + box& operator=(box&& that) requires best::moveable; /// # `box::box(...)` /// @@ -247,10 +239,10 @@ class BEST_RELOCATABLE box final { explicit box(best::span args) : box(alloc{}, args) {} template explicit box(alloc alloc, best::span args) - : size_(args.size()), alloc_(best::in_place, BEST_FWD(alloc)) { - if (size_ == 0) return; + : size_(args.size()), alloc_(best::in_place, BEST_FWD(alloc)) { + if (size_ == 0) { return; } ptr_ = best::ptr(allocator().alloc(best::layout::array(args.size()))) - .cast(best::types); + .cast(best::types); as_span().emplace_from(args); } @@ -259,15 +251,15 @@ class BEST_RELOCATABLE box final { /// Constructs a box by taking ownership of a span. explicit box(unsafe, best::span args) : box(alloc{}, args) {} explicit box(unsafe, alloc alloc, best::span args) - : ptr_(args.data()), - size_(args.size()), - alloc_(best::in_place, BEST_FWD(alloc)) {} + : ptr_(args.data()), + size_(args.size()), + alloc_(best::in_place, BEST_FWD(alloc)) {} /// # `box::~box()` /// /// Boxes automatically destroy their contents. ~box() { - if (data() == best::ptr::dangling()) return; + if (data() == best::ptr::dangling()) { return; } as_span().destroy(); alloc_->dealloc(data(), best::layout::array(size())); @@ -405,20 +397,18 @@ class BEST_RELOCATABLE vbox final { /// if `T` is copyable but the complete type is not. vbox(const vbox& that) = delete; vbox& operator=(const vbox& that) = delete; - vbox(vbox&& that) - requires best::moveable; - vbox& operator=(vbox&& that) - requires best::moveable; + vbox(vbox&& that) requires best::moveable; + vbox& operator=(vbox&& that) requires best::moveable; /// # `vbox::vbox(box)`, `vbox::vbox(vbox)` /// /// Wraps a box, virtual or otherwise. template U> explicit vbox(box that) - : ptr_(BEST_MOVE(that).leak()), alloc_(that.alloc_) {} + : ptr_(BEST_MOVE(that).leak()), alloc_(that.alloc_) {} template U> explicit vbox(vbox that) - : ptr_(BEST_MOVE(that).leak()), alloc_(that.alloc_) {} + : ptr_(BEST_MOVE(that).leak()), alloc_(that.alloc_) {} /// # `vbox::vbox(unsafe, ptr)` /// @@ -426,13 +416,13 @@ class BEST_RELOCATABLE vbox final { /// the given allocator. explicit vbox(unsafe u, best::vptr ptr) : vbox(u, alloc{}, ptr) {} explicit vbox(unsafe u, alloc alloc, best::vptr ptr) - : ptr_(ptr), alloc_(best::in_place, BEST_FWD(alloc)) {} + : ptr_(ptr), alloc_(best::in_place, BEST_FWD(alloc)) {} /// # `vbox::~vbox()` /// /// Boxes automatically destroy their contents. ~vbox() { - if (ptr_.thin() == best::ptr::dangling()) return; + if (ptr_.thin() == best::ptr::dangling()) { return; } ptr_.destroy(); allocator().dealloc(ptr_.raw(), ptr_.layout()); @@ -482,15 +472,14 @@ class BEST_RELOCATABLE vbox final { /// /// Makes a copy of the contents of this `box`, if the complete type is /// copyable. - best::option copy() const - requires best::copyable + best::option copy() const requires best::copyable { - if (!ptr_.is_copyable()) return best::none; + if (!ptr_.is_copyable()) { return best::none; } best::vptr ptr( - unsafe("the vtable is correct because we're making a copy"), - best::ptr(allocator().alloc(ptr_.layout())).cast(best::types), - ptr_.vtable()); + unsafe("the vtable is correct because we're making a copy"), + best::ptr(allocator().alloc(ptr_.layout())).cast(best::types), + ptr_.vtable()); ptr_.copy_to(ptr.raw()); return vbox(alloc_, ptr); @@ -513,26 +502,23 @@ namespace best { template box::box(const box& that) requires best::copyable && best::copyable - : box(that.allocator(), best::in_place, *that) {} + : box(that.allocator(), best::in_place, *that) {} template -box& box::operator=(const box& that) - requires best::copyable +box& box::operator=(const box& that) requires best::copyable { **this = *that; return *this; } template -box::box(box&& that) - requires best::moveable - : ptr_(std::exchange(that.ptr_, best::ptr::dangling())), - alloc_(BEST_MOVE(that.alloc_)) {} +box::box(box&& that) requires best::moveable + : ptr_(std::exchange(that.ptr_, best::ptr::dangling())), + alloc_(BEST_MOVE(that.alloc_)) {} template -box& box::operator=(box&& that) - requires best::moveable +box& box::operator=(box&& that) requires best::moveable { - if (best::equal(this, &that)) return *this; + if (best::equal(this, &that)) { return *this; } this->~box(); new (this) box(BEST_MOVE(that)); return *this; @@ -541,10 +527,9 @@ box& box::operator=(box&& that) template box::box(const box& that) requires best::copyable && best::copyable - : box(that.allocator(), that.as_span()) {} + : box(that.allocator(), that.as_span()) {} template -box& box::operator=(const box& that) - requires best::copyable +box& box::operator=(const box& that) requires best::copyable { if (size() == that.size()) { as_span().copy_from(that.as_span()); @@ -555,38 +540,34 @@ box& box::operator=(const box& that) alloc_->dealloc(data(), best::layout::array(size())); } ptr_ = best::ptr(alloc_->alloc(best::layout::array(that.size()))) - .cast(best::types); + .cast(best::types); size_ = that.size(); as_span().emplace_from(that.as_span()); } return *this; } template -box::box(box&& that) - requires best::moveable - : ptr_(std::exchange(that.ptr_, best::ptr::dangling())), - size_(that.size()), - alloc_(BEST_MOVE(that.alloc_)) {} +box::box(box&& that) requires best::moveable + : ptr_(std::exchange(that.ptr_, best::ptr::dangling())), + size_(that.size()), + alloc_(BEST_MOVE(that.alloc_)) {} template -box& box::operator=(box&& that) - requires best::moveable +box& box::operator=(box&& that) requires best::moveable { - if (best::equal(this, &that)) return *this; + if (best::equal(this, &that)) { return *this; } this->~box(); new (this) box(BEST_MOVE(that)); return *this; } template -vbox::vbox(vbox&& that) - requires best::moveable - : ptr_(std::exchange(that.ptr_, best::ptr::dangling())), - alloc_(BEST_MOVE(that.alloc_)) {} +vbox::vbox(vbox&& that) requires best::moveable + : ptr_(std::exchange(that.ptr_, best::ptr::dangling())), + alloc_(BEST_MOVE(that.alloc_)) {} template -vbox& vbox::operator=(vbox&& that) - requires best::moveable +vbox& vbox::operator=(vbox&& that) requires best::moveable { - if (best::equal(this, &that)) return *this; + if (best::equal(this, &that)) { return *this; } this->~vbox(); new (this) vbox(BEST_MOVE(that)); return *this; diff --git a/best/container/choice.h b/best/container/choice.h index 589390b..db67126 100644 --- a/best/container/choice.h +++ b/best/container/choice.h @@ -144,16 +144,14 @@ class choice final { /// /// This will never select a `void` alternative. template - constexpr choice(Arg&& arg) - requires( - // XXX: This is a load-bearing pre-check. We do not want to call - // convert_from, which executes the whole init_from machinery, if we - // would choose another constructor: namely, a copy/move constructor. - // This is a common case, and avoiding it improves compile times by at - // least a second(). - !best::same> && - convert_from.has_value()) - : choice(best::index<*convert_from>, BEST_FWD(arg)) {} + constexpr choice(Arg&& arg) requires ( + // XXX: This is a load-bearing pre-check. We do not want to call + // convert_from, which executes the whole init_from machinery, if we + // would choose another constructor: namely, a copy/move constructor. + // This is a common case, and avoiding it improves compile times by at + // least a second(). + !best::same> && convert_from.has_value()) + : choice(best::index<*convert_from>, BEST_FWD(arg)) {} /// # `choice::choice(uninit)` /// @@ -168,14 +166,14 @@ class choice final { /// Constructs the `n`th alternative from the given arguments, in-place. template constexpr explicit(sizeof...(Args) == 0) - choice(best::index_t tag, Args&&... args) - requires best::constructible, Args&&...> - : BEST_CHOICE_IMPL_(tag, BEST_FWD(args)...) {} + choice(best::index_t tag, Args&&... args) + requires best::constructible, Args&&...> + : BEST_CHOICE_IMPL_(tag, BEST_FWD(args)...) {} template constexpr choice(best::index_t tag, std::initializer_list il, Args&&... args) requires best::constructible, decltype(il), Args&&...> - : BEST_CHOICE_IMPL_(tag, il, BEST_FWD(args)...) {} + : BEST_CHOICE_IMPL_(tag, il, BEST_FWD(args)...) {} /// # `choice::which()`. /// @@ -231,13 +229,13 @@ class choice final { template constexpr cptr as_ptr(best::index_t i = {}) const { return which() == n ? impl().template ptr( - unsafe("which() is checked right before this")) + unsafe("which() is checked right before this")) : nullptr; } template constexpr ptr as_ptr(best::index_t i = {}) { return which() == n ? impl().template ptr( - unsafe("which() is checked right before this")) + unsafe("which() is checked right before this")) : nullptr; } @@ -286,11 +284,10 @@ class choice final { /// For example, if we call `best::choice::permute<2, 0, 1>()`, we'll /// get a `best::choice`. template < - auto... p, // - auto inverse = choice_internal::InvertedPermutation> + auto... p, // + auto inverse = choice_internal::InvertedPermutation> constexpr best::choice...> permute( - best::vlist permutation = {}) const& - requires(inverse.has_value()) + best::vlist permutation = {}) const& requires (inverse.has_value()) { return index_match([&](auto tag, auto&&... value) { return best::choice...>(best::index<(*inverse)[tag.value]>, @@ -298,15 +295,15 @@ class choice final { }); } template < - auto... p, // - auto inverse = choice_internal::InvertedPermutation> + auto... p, // + auto inverse = choice_internal::InvertedPermutation> constexpr best::choice...> permute(best::vlist permutation = {}) #define BEST_CHOICE_CLANG_FORMAT_HACK_ && - // This macro is to work around what - // appears to be a weird clang-format bug. - BEST_CHOICE_CLANG_FORMAT_HACK_ + // This macro is to work around what + // appears to be a weird clang-format bug. + BEST_CHOICE_CLANG_FORMAT_HACK_ #undef BEST_CHOICE_CLANG_FORMAT_HACK_ - requires(inverse.has_value()) + requires (inverse.has_value()) { return index_match([&](auto tag, auto&&... value) { return best::choice...>(best::index<(*inverse)[tag.value]>, @@ -324,7 +321,7 @@ class choice final { } template - friend constexpr void BestFmtQuery(Q& query, choice*) { + constexpr friend void BestFmtQuery(Q& query, choice*) { query.supports_width = (query.template of.supports_width || ...); query.supports_prec = (query.template of.supports_prec || ...); query.uses_method = [](auto r) { @@ -335,26 +332,26 @@ class choice final { // Comparisons. template BEST_INLINE_ALWAYS constexpr bool operator==(const choice& that) const - requires(best::equatable && ...) + requires (best::equatable && ...) { return which() == that.which() && // index_match( - [&](auto) { return true; }, // void case. - [&](auto tag, const auto& value) { return value == that[tag]; }); + [&](auto) { return true; }, // void case. + [&](auto tag, const auto& value) { return value == that[tag]; }); } template BEST_INLINE_ALWAYS constexpr best::common_ord...> operator<=>(const choice& that) const - requires(best::comparable && ...) + requires (best::comparable && ...) { return (which() <=> that.which())->*best::or_cmp([&] { using Output = best::common_ord...>; return index_match( - [&](auto) -> Output { return best::Equal; }, // void case. - [&](auto tag, const auto& value) -> Output { - return value <=> that[tag]; - }); + [&](auto) -> Output { return best::Equal; }, // void case. + [&](auto tag, const auto& value) -> Output { + return value <=> that[tag]; + }); }); } @@ -362,9 +359,9 @@ class choice final { constexpr void check_ok(size_t n, best::location loc = best::here) const { if (best::unlikely(n != which())) { crash_internal::crash( - {"attempted access of incorrect variant of best::choice; %zu != %zu", - loc}, - n, which()); + {"attempted access of incorrect variant of best::choice; %zu != %zu", + loc}, + n, which()); } } @@ -390,39 +387,39 @@ namespace best { template template constexpr choice::cref choice::operator[]( - best::index_t idx) const& { + best::index_t idx) const& { check_ok(idx.value); return impl().deref(unsafe{"check_ok() called before this"}, idx); } template template constexpr choice::ref choice::operator[]( - best::index_t idx) & { + best::index_t idx) & { check_ok(idx.value); return impl().deref(unsafe{"check_ok() called before this"}, idx); } template template constexpr choice::crref choice::operator[]( - best::index_t idx) const&& { + best::index_t idx) const&& { check_ok(idx.value); return static_cast>( - impl().deref(unsafe{"check_ok() called before this"}, idx)); + impl().deref(unsafe{"check_ok() called before this"}, idx)); } template template constexpr choice::rref choice::operator[]( - best::index_t idx) && { + best::index_t idx) && { check_ok(idx.value); return static_cast>( - impl().deref(unsafe{"check_ok() called before this"}, idx)); + impl().deref(unsafe{"check_ok() called before this"}, idx)); } template template constexpr best::option::template cref> choice::at(best::index_t i) const& { - if (which() != n) return {}; + if (which() != n) { return {}; } return best::call_devoid([&]() -> decltype(auto) { return impl().deref(unsafe{"checked which() before this"}, i); }); @@ -430,8 +427,8 @@ choice::at(best::index_t i) const& { template template constexpr best::option::template ref> choice::at( - best::index_t i) & { - if (which() != n) return {}; + best::index_t i) & { + if (which() != n) { return {}; } return best::call_devoid([&]() -> decltype(auto) { return impl().deref(unsafe{"checked which() before this"}, i); }); @@ -440,53 +437,53 @@ template template constexpr best::option::template crref> choice::at(best::index_t i) const&& { - if (which() != n) return {}; + if (which() != n) { return {}; } return best::call_devoid([&]() -> decltype(auto) { - return best::option(best::bind, - static_cast>(impl().deref( - unsafe{"checked which() before this"}, i))); + return best::option( + best::bind, static_cast>( + impl().deref(unsafe{"checked which() before this"}, i))); }); } template template constexpr best::option::template rref> choice::at(best::index_t i) && { - if (which() != n) return {}; + if (which() != n) { return {}; } return best::call_devoid([&]() -> decltype(auto) { - return best::option(best::bind, - static_cast>(impl().deref( - unsafe{"checked which() before this"}, i))); + return best::option( + best::bind, static_cast>( + impl().deref(unsafe{"checked which() before this"}, i))); }); } template template constexpr best::option< - const best::object::template type>&> + const best::object::template type>&> choice::object(best::index_t i) const& { - if (which() != n) return {}; + if (which() != n) { return {}; } return impl().object(unsafe{"checked which() before this"}, i); } template template constexpr best::option::template type>&> choice::object(best::index_t i) & { - if (which() != n) return {}; + if (which() != n) { return {}; } return impl().object(unsafe{"checked which() before this"}, i); } template template constexpr best::option< - const best::object::template type>&&> + const best::object::template type>&&> choice::object(best::index_t i) const&& { - if (which() != n) return {}; + if (which() != n) { return {}; } return BEST_MOVE(impl().object(unsafe{"checked which() before this"}, i)); } template template constexpr best::option::template type>&&> choice::object(best::index_t i) && { - if (which() != n) return {}; + if (which() != n) { return {}; } BEST_MOVE(impl().object(unsafe{"checked which() before this"}, i)); } @@ -526,12 +523,12 @@ constexpr decltype(auto) choice::match(auto... cases) & { template constexpr decltype(auto) choice::match(auto... cases) const&& { return BEST_MOVE(*this).impl().match( - choice_internal::Overloaded(BEST_FWD(cases)...)); + choice_internal::Overloaded(BEST_FWD(cases)...)); } template constexpr decltype(auto) choice::match(auto... cases) && { return BEST_MOVE(*this).impl().match( - choice_internal::Overloaded(BEST_FWD(cases)...)); + choice_internal::Overloaded(BEST_FWD(cases)...)); } template @@ -545,12 +542,12 @@ constexpr decltype(auto) choice::index_match(auto... cases) & { template constexpr decltype(auto) choice::index_match(auto... cases) const&& { return BEST_MOVE(*this).impl().index_match( - choice_internal::Overloaded(BEST_FWD(cases)...)); + choice_internal::Overloaded(BEST_FWD(cases)...)); } template constexpr decltype(auto) choice::index_match(auto... cases) && { return BEST_MOVE(*this).impl().index_match( - choice_internal::Overloaded(BEST_FWD(cases)...)); + choice_internal::Overloaded(BEST_FWD(cases)...)); } } // namespace best diff --git a/best/container/choice_test.cc b/best/container/choice_test.cc index 6632f83..942bf77 100644 --- a/best/container/choice_test.cc +++ b/best/container/choice_test.cc @@ -122,25 +122,25 @@ best::test Match = [](auto& t) { t.expect_eq(x0.match([](auto x) { return (int)x; }), 42); t.expect_eq(x1.match([](auto x) { return (int)x; }), 43); - t.expect_eq(x0.match( // - [](int x) { return x * 2; }, // - [](float f) { return (int)f; }), + t.expect_eq(x0.match( // + [](int x) { return x * 2; }, // + [](float f) { return (int)f; }), 84); - t.expect_eq(x1.match( // - [](int x) { return x * 2; }, // - [](float f) { return (int)f; }), + t.expect_eq(x1.match( // + [](int x) { return x * 2; }, // + [](float f) { return (int)f; }), 43); best::choice x2(best::index<0>, 42); best::choice x3(best::index<1>, 45); - t.expect_eq(x2.index_match( // - [](best::index_t<0>, int x) { return x * 2; }, // - [](auto, int x) { return x; }), + t.expect_eq(x2.index_match( // + [](best::index_t<0>, int x) { return x * 2; }, // + [](auto, int x) { return x; }), 84); - t.expect_eq(x3.index_match( // - [](best::index_t<0>, int x) { return x * 2; }, // - [](auto, int x) { return x; }), + t.expect_eq(x3.index_match( // + [](best::index_t<0>, int x) { return x * 2; }, // + [](auto, int x) { return x; }), 45); }; @@ -152,12 +152,12 @@ best::test Permute = [](auto& t) { best::test Comparisons = [](auto& t) { std::array, 6> alts = {{ - {best::index<0>, 42}, - {best::index<0>, 45}, - {best::index<1>, 1.5}, - {best::index<1>, 1.7}, - {best::index<2>, false}, - {best::index<2>, true}, + {best::index<0>, 42}, + {best::index<0>, 45}, + {best::index<1>, 1.5}, + {best::index<1>, 1.7}, + {best::index<2>, false}, + {best::index<2>, true}, }}; for (size_t i = 0; i < 6; ++i) { diff --git a/best/container/internal/choice.h b/best/container/internal/choice.h index 5bc89fc..a4a1488 100644 --- a/best/container/internal/choice.h +++ b/best/container/internal/choice.h @@ -50,7 +50,7 @@ class tagged { template constexpr explicit tagged(best::index_t, Args&&... args) requires best::constructible, Args&&...> - : union_(best::index, BEST_FWD(args)...), tag_(n) {} + : union_(best::index, BEST_FWD(args)...), tag_(n) {} constexpr size_t tag() const { return tag_; } @@ -93,19 +93,19 @@ class niched { template constexpr explicit niched(best::index_t, Args&&... args) requires best::constructible - : niched_(ctor_tag(), best::niche{}) {} + : niched_(ctor_tag(), best::niche{}) {} template constexpr explicit niched(best::index_t, Args&&... args) requires best::constructible - : niched_(ctor_tag(), BEST_FWD(args)...) {} + : niched_(ctor_tag(), BEST_FWD(args)...) {} constexpr size_t tag() const { return swap ^ !get(unsafe("we're checking for the niche, so we need " "to pull out the non-empty side"), best::index) - .as_ptr() - .is_niche(); + .as_ptr() + .is_niche(); } template @@ -142,13 +142,13 @@ tagged which_storage(best::tlist, best::rank<0>); template niched<1, B, A> which_storage(best::tlist, best::rank<1>) - requires(has_niche && best::is_empty && - best::constructible); + requires (has_niche && best::is_empty && + best::constructible); template niched<0, A, B> which_storage(best::tlist, best::rank<2>) - requires(has_niche && best::is_empty && - best::constructible); + requires (has_niche && best::is_empty && + best::constructible); template using storage = decltype(which_storage(types, best::rank<2>{})); @@ -169,44 +169,36 @@ class impl : public storage { constexpr impl() = default; - constexpr impl(const impl&) - requires(info.trivial_copy()) + constexpr impl(const impl&) requires (info.trivial_copy()) = default; - constexpr impl(const impl& that) - requires(!info.trivial_copy()) + constexpr impl(const impl& that) requires (!info.trivial_copy()) { that.index_match([&](auto tag, auto&... value) { std::construct_at(this, tag, value...); }); } - constexpr impl& operator=(const impl&) - requires(info.trivial_copy()) + constexpr impl& operator=(const impl&) requires (info.trivial_copy()) = default; - constexpr impl& operator=(const impl& that) - requires(!info.trivial_copy()) + constexpr impl& operator=(const impl& that) requires (!info.trivial_copy()) { that.index_match( - [&](auto tag, auto&... value) { emplace(value...); }); + [&](auto tag, auto&... value) { emplace(value...); }); return *this; } - constexpr impl(impl&&) - requires(info.trivial_move()) + constexpr impl(impl&&) requires (info.trivial_move()) = default; - constexpr impl(impl&& that) - requires(!info.trivial_move()) + constexpr impl(impl&& that) requires (!info.trivial_move()) { that.index_match([&](auto tag, auto&... value) { std::construct_at(this, tag, BEST_MOVE(value)...); }); } - constexpr impl& operator=(impl&&) - requires(info.trivial_move()) + constexpr impl& operator=(impl&&) requires (info.trivial_move()) = default; - constexpr impl& operator=(impl&& that) - requires(!info.trivial_move()) + constexpr impl& operator=(impl&& that) requires (!info.trivial_move()) { that.index_match([&](auto tag, auto&... value) { emplace(BEST_MOVE(value)...); @@ -214,11 +206,9 @@ class impl : public storage { return *this; } - constexpr ~impl() - requires(info.trivial_dtor()) + constexpr ~impl() requires (info.trivial_dtor()) = default; - constexpr ~impl() - requires(!info.trivial_dtor()) + constexpr ~impl() requires (!info.trivial_dtor()) { match([](auto&... value) { (std::destroy_at(best::addr(value)), ...); }); } @@ -229,8 +219,8 @@ class impl : public storage { if constexpr (best::is_object> && best::assignable, Args&&...>) { get(unsafe{"checked tag() before this"}, best::index) - .as_ptr() - .assign(BEST_FWD(args)...); + .as_ptr() + .assign(BEST_FWD(args)...); return; } } @@ -240,33 +230,33 @@ class impl : public storage { template BEST_INLINE_SYNTHETIC constexpr best::as_ref> deref( - unsafe u, best::index_t i = {}) const { + unsafe u, best::index_t i = {}) const { return *get(u, i); } template BEST_INLINE_SYNTHETIC constexpr best::as_ref> deref( - unsafe u, best::index_t i = {}) { + unsafe u, best::index_t i = {}) { return *get(u, i); } template BEST_INLINE_SYNTHETIC constexpr best::as_ptr> ptr( - unsafe u, best::index_t i = {}) const { + unsafe u, best::index_t i = {}) const { return get(u, i).operator->(); } template BEST_INLINE_SYNTHETIC constexpr best::as_ptr> ptr( - unsafe u, best::index_t i = {}) { + unsafe u, best::index_t i = {}) { return get(u, i).operator->(); } template BEST_INLINE_SYNTHETIC constexpr const best::object>& object( - unsafe u, best::index_t i = {}) const { + unsafe u, best::index_t i = {}) const { return get(u, i); } template BEST_INLINE_SYNTHETIC constexpr best::object>& object( - unsafe u, best::index_t i = {}) { + unsafe u, best::index_t i = {}) { return get(u, i); } @@ -289,7 +279,7 @@ class impl : public storage { template BEST_INLINE_SYNTHETIC constexpr decltype(auto) index_match( - F&& callback) const& { + F&& callback) const& { return jump_table(make_index_match_arm(*this, BEST_FWD(callback))); } template @@ -298,7 +288,7 @@ class impl : public storage { } template BEST_INLINE_SYNTHETIC constexpr decltype(auto) index_match( - F&& callback) const&& { + F&& callback) const&& { return jump_table(make_index_match_arm(*this, BEST_FWD(callback))); } template @@ -308,46 +298,43 @@ class impl : public storage { private: template - constexpr static auto make_jump_table(std::index_sequence) { + static constexpr auto make_jump_table(std::index_sequence) { // TODO(mcyoung): It'd be nice to use common_type here... using Output = best::call_result>; return std::array{ - {+[](F&& callback) -> Output { - return best::call(BEST_FWD(callback), best::index); - }...}}; + {+[](F&& callback) -> Output { + return best::call(BEST_FWD(callback), best::index); + }...}}; } template static constexpr auto JumpTable = - make_jump_table(std::make_index_sequence{}); + make_jump_table(std::make_index_sequence{}); constexpr decltype(auto) jump_table(auto&& arms) const - requires(sizeof...(Ts) > 2) + requires (sizeof...(Ts) > 2) { return JumpTable[tag()](BEST_FWD(arms)); } constexpr decltype(auto) jump_table(auto&& arms) const - requires(sizeof...(Ts) == 2) + requires (sizeof...(Ts) == 2) { switch (tag()) { - case 0: - return arms(best::index<0>); - case 1: - return arms(best::index<1>); - default: - best::unreachable(); + case 0: return arms(best::index<0>); + case 1: return arms(best::index<1>); + default: best::unreachable(); } } constexpr decltype(auto) jump_table(auto&& arms) const - requires(sizeof...(Ts) == 1) + requires (sizeof...(Ts) == 1) { return arms(best::index<0>); } - constexpr static auto make_match_arm(auto&& self, auto&& cb) { + static constexpr auto make_match_arm(auto&& self, auto&& cb) { return [&](best::index_t tag) -> decltype(auto) { using Type = best::refcopy, decltype(self)&&>; @@ -365,14 +352,14 @@ class impl : public storage { } } else { unsafe u( - "this function is only called after" - "checking tag() via the jump table."); + "this function is only called after" + "checking tag() via the jump table."); return best::call(BEST_FWD(cb), static_cast(*self.get(u, tag))); } }; } - constexpr static auto make_index_match_arm(auto&& self, auto&& cb) { + static constexpr auto make_index_match_arm(auto&& self, auto&& cb) { return [&](best::index_t tag) -> decltype(auto) { using Type = best::refcopy, decltype(self)&&>; @@ -390,8 +377,8 @@ class impl : public storage { } } else { unsafe u( - "this function is only called after" - "checking tag() via the jump table."); + "this function is only called after" + "checking tag() via the jump table."); return best::call(BEST_FWD(cb), tag, static_cast(*self.get(u, tag))); } @@ -408,26 +395,18 @@ Overloaded(Fs&&...) -> Overloaded; template inline constexpr auto InvertedPermutation = - []() -> container_internal::option> { + []() -> container_internal::option> { std::array inverse; - for (auto& elem : inverse) { - elem = ~size_t{0}; - } + for (auto& elem : inverse) { elem = ~size_t{0}; } std::array perm{p...}; for (size_t i = 0; i < perm.size(); ++i) { - if (perm[i] >= n) { - return {}; - } + if (perm[i] >= n) { return {}; } - if (inverse[perm[i]] != ~size_t{0}) { - return {}; - } + if (inverse[perm[i]] != ~size_t{0}) { return {}; } inverse[perm[i]] = i; } for (auto& elem : inverse) { - if (elem == ~size_t{0}) { - return {}; - } + if (elem == ~size_t{0}) { return {}; } } return inverse; }(); diff --git a/best/container/internal/pun.h b/best/container/internal/pun.h index bd8cecc..e081760 100644 --- a/best/container/internal/pun.h +++ b/best/container/internal/pun.h @@ -39,13 +39,12 @@ struct info_t { }; template inline constexpr info_t info = { - ((std::is_void_v || - std::is_trivially_default_constructible_v)&&...), - ((std::is_void_v || std::is_trivially_copyable_v)&&...), - ((std::is_void_v || (std::is_trivially_move_constructible_v && - std::is_trivially_move_assignable_v)) && - ...), - ((std::is_void_v || std::is_trivially_destructible_v)&&...), + ((std::is_void_v || std::is_trivially_default_constructible_v)&&...), + ((std::is_void_v || std::is_trivially_copyable_v)&&...), + ((std::is_void_v || (std::is_trivially_move_constructible_v && + std::is_trivially_move_assignable_v)) && + ...), + ((std::is_void_v || std::is_trivially_destructible_v)&&...), }; // A raw union. @@ -81,7 +80,7 @@ union BEST_RELOCATABLE impl { BEST_IGNORE_GCC_DIAGNOSTIC("-Wc++11-narrowing") template constexpr explicit impl(best::index_t<0>, Args&&... args) - : h_(best::in_place, BEST_FWD(args)...) {} + : h_(best::in_place, BEST_FWD(args)...) {} BEST_POP_GCC_DIAGNOSTIC() constexpr const auto& get(best::index_t<0>) const { return h_; } @@ -102,10 +101,10 @@ union BEST_RELOCATABLE impl { BEST_IGNORE_GCC_DIAGNOSTIC("-Wc++11-narrowing") template constexpr explicit impl(best::index_t<0>, Args&&... args) - : h_(best::in_place, BEST_FWD(args)...) {} + : h_(best::in_place, BEST_FWD(args)...) {} template constexpr explicit impl(best::index_t<1>, Args&&... args) - : t_(best::in_place, BEST_FWD(args)...) {} + : t_(best::in_place, BEST_FWD(args)...) {} BEST_POP_GCC_DIAGNOSTIC() constexpr const auto& get(best::index_t<0>) const { return h_; } @@ -129,15 +128,15 @@ union BEST_RELOCATABLE impl { BEST_IGNORE_GCC_DIAGNOSTIC("-Wc++11-narrowing") template constexpr explicit impl(best::index_t<0>, Args&&... args) - : h_(best::in_place, BEST_FWD(args)...) {} + : h_(best::in_place, BEST_FWD(args)...) {} template constexpr explicit impl(best::index_t<1>, Args&&... args) - : h2_(best::in_place, BEST_FWD(args)...) {} + : h2_(best::in_place, BEST_FWD(args)...) {} BEST_POP_GCC_DIAGNOSTIC() template constexpr explicit impl(best::index_t, Args&&... args) - : t_(best::index, BEST_FWD(args)...) {} + : t_(best::index, BEST_FWD(args)...) {} constexpr const auto& get(best::index_t<0>) const { return h_; } constexpr auto& get(best::index_t<0>) { return h_; } diff --git a/best/container/internal/row.h b/best/container/internal/row.h index 7b9d579..19295ed 100644 --- a/best/container/internal/row.h +++ b/best/container/internal/row.h @@ -48,7 +48,7 @@ template , size_t count = lut[0]> inline constexpr auto do_lookup() { return best::indices.apply( - [] { return best::vals; }); + [] { return best::vals; }); } template @@ -90,7 +90,7 @@ struct impl { }; template - requires(sizeof...(i) > 2) +requires (sizeof...(i) > 2) struct impl, Elems...> : elem... { template constexpr const auto& get_impl(best::index_t) const { @@ -126,20 +126,20 @@ template constexpr auto make_slicer(std::index_sequence, std::index_sequence, std::index_sequence) { return []... prefix, splat... infix, splat... suffix>( - prefix&&..., infix&&... args, suffix&&...) { + prefix&&..., infix&&... args, suffix&&...) { return Out{BEST_FWD(args)...}; }; } template - requires(count.has_value()) +requires (count.has_value()) constexpr auto slice(auto&& row) { using Out = decltype(row.types.template at().template apply()); return BEST_FWD(row).apply([](auto&&... args) { return make_slicer( - std::make_index_sequence{}, std::make_index_sequence<*count>{}, - std::make_index_sequence{})( - BEST_FWD(args)...); + std::make_index_sequence{}, std::make_index_sequence<*count>{}, + std::make_index_sequence{})( + BEST_FWD(args)...); }); } @@ -150,23 +150,22 @@ constexpr auto make_splicer(std::index_sequence, std::index_sequence, std::index_sequence, auto&&... args) { return [&]... prefix, splat... infix, splat... suffix>( - prefix&&... pre, infix&&..., suffix&&... suf) { + prefix&&... pre, infix&&..., suffix&&... suf) { return Out{BEST_FWD(pre)..., BEST_FWD(args)..., BEST_FWD(suf)...}; }; } template - requires(count.has_value()) +requires (count.has_value()) constexpr auto splice(auto&& row, auto those_types, auto&& those) { using Out = decltype(row.types.template splice(those_types) - .template apply()); + .template apply()); return BEST_FWD(row).apply([&](auto&&... args) { return BEST_FWD(those).apply([&](auto&&... insert) { return make_splicer( - std::make_index_sequence{}, - std::make_index_sequence<*count>{}, - std::make_index_sequence{}, - BEST_FWD(insert)...)(BEST_FWD(args)...); + std::make_index_sequence{}, std::make_index_sequence<*count>{}, + std::make_index_sequence{}, + BEST_FWD(insert)...)(BEST_FWD(args)...); }); }); } @@ -174,29 +173,29 @@ constexpr auto splice(auto&& row, auto those_types, auto&& those) { // See tlist_internal::gather_impl() and tlist_internal::scatter_impl(). template constexpr auto gather(auto&& row) - requires((i < best::as_auto::size()) && ...) + requires ((i < best::as_auto::size()) && ...) { using Out = - decltype(row.types.template gather().template apply()); + decltype(row.types.template gather().template apply()); return Out{BEST_FWD(row)[best::index]...}; } template constexpr auto scatter(auto&& row, auto those_types, auto&& those) - requires((i < best::as_auto::size()) && ...) && - (sizeof...(i) <= best::as_auto::size()) && - (sizeof...(i) <= best::as_auto::size()) && - (best::as_auto::size() <= - best::as_auto::size()) + requires ((i < best::as_auto::size()) && ...) && + (sizeof...(i) <= best::as_auto::size()) && + (sizeof...(i) <= best::as_auto::size()) && + (best::as_auto::size() <= + best::as_auto::size()) { using Out = - decltype(row.types - .template scatter( - those_types.template at()) - .template apply()); + decltype(row.types + .template scatter( + those_types.template at()) + .template apply()); return row.indices.apply([&]() { constexpr auto lut = [&] { std::array::size()> lut{ - (J::value - J::value)...}; + (J::value - J::value)...}; size_t n = 1; ((i < lut.size() ? lut[i] = n++ : 0), ...); return lut; @@ -218,8 +217,8 @@ constexpr auto join(auto&&... those) { return row // ::template type...>{ - BEST_MOVE(rowrow)[best::index] // - .get(best::index)...}; + BEST_MOVE(rowrow)[best::index] // + .get(best::index)...}; }(best::indices<(0 + ... + best::as_auto::size())>); } } // namespace best::row_internal diff --git a/best/container/object.h b/best/container/object.h index 7129a30..eb4e06b 100644 --- a/best/container/object.h +++ b/best/container/object.h @@ -95,17 +95,16 @@ class object final { constexpr explicit object(best::in_place_t, Args&&... args) requires best::constructible && (best::is_object && !std::is_array_v) - : BEST_OBJECT_VALUE_(BEST_FWD(args)...) {} + : BEST_OBJECT_VALUE_(BEST_FWD(args)...) {} constexpr explicit object(best::in_place_t, best::niche) requires best::is_ref - : BEST_OBJECT_VALUE_(nullptr) {} + : BEST_OBJECT_VALUE_(nullptr) {} /// # `object::operator=()` /// /// Assigns to the wrapped value via `ptr::assign()`'s semantics. template - constexpr object& operator=(Arg&& arg) - requires best::assignable + constexpr object& operator=(Arg&& arg) requires best::assignable { as_ptr().assign(BEST_FWD(arg)); return *this; @@ -193,7 +192,7 @@ class object final { fmt.format(*obj); } } - friend constexpr void BestFmtQuery(auto& query, object*) { + constexpr friend void BestFmtQuery(auto& query, object*) { query = query.template of; } diff --git a/best/container/option.h b/best/container/option.h index 2c7f1e9..bbeaa3f 100644 --- a/best/container/option.h +++ b/best/container/option.h @@ -60,15 +60,15 @@ inline constexpr struct none_t { template concept is_option = requires { requires !std::is_class_v::type> || - !std::is_abstract_v::type>; + !std::is_abstract_v::type>; requires best::same, best::option::type>> || - requires { - requires best::is_object::type>; - requires best::same, - best::container_internal::option< - typename best::as_auto::type>>; - }; + requires { + requires best::is_object::type>; + requires best::same, + best::container_internal::option< + typename best::as_auto::type>>; + }; }; /// # `best::option_type` @@ -123,14 +123,14 @@ class option final { private: template static constexpr bool cannot_init_from = - (!best::constructible && !best::constructible) || - best::is_void; + (!best::constructible && !best::constructible) || + best::is_void; template static constexpr bool not_forbidden_conversion = - (!best::same>)&& // - (!best::same>)&& // - (!best::same>); + (!best::same>)&& // + (!best::same>)&& // + (!best::same>); public: /// Helper type aliases. @@ -177,17 +177,15 @@ class option final { constexpr explicit(!best::convertible) option(U&& arg) requires not_forbidden_conversion && best::constructible && (!best::copyable || !best::is_object) - : option(best::in_place, BEST_FWD(arg)) {} - constexpr option(best::devoid arg) - requires best::moveable - : option(best::in_place, BEST_MOVE(arg)) {} + : option(best::in_place, BEST_FWD(arg)) {} + constexpr option(best::devoid arg) requires best::moveable + : option(best::in_place, BEST_MOVE(arg)) {} template constexpr explicit(!best::convertible) option(best::bind_t, U&& arg) requires not_forbidden_conversion && best::constructible - : option(best::in_place, BEST_FWD(arg)) {} - constexpr option(best::bind_t, best::devoid arg) - requires best::moveable - : option(best::in_place, BEST_MOVE(arg)) {} + : option(best::in_place, BEST_FWD(arg)) {} + constexpr option(best::bind_t, best::devoid arg) requires best::moveable + : option(best::in_place, BEST_MOVE(arg)) {} // This constructor exists because otherwise, converting e.g. int to long // makes the compiler think there's a reference to temporary being bound that @@ -196,7 +194,7 @@ class option final { constexpr explicit(!best::convertible) option(U arg) requires not_forbidden_conversion && best::constructible && best::copyable - : option(best::in_place, BEST_FWD(arg)) {} + : option(best::in_place, BEST_FWD(arg)) {} /// # `option::option(option&)` /// @@ -212,11 +210,12 @@ class option final { /// from `U`. template constexpr explicit( - !best::convertible, U&&>>) - option(U&& that) - requires best::constructible, U&&>> && - cannot_init_from - : option() { + !best::convertible, U&&>>) + option(U&& that) + requires best::constructible, U&&>> && + cannot_init_from + : option() { *this = BEST_FWD(that); } template @@ -240,14 +239,12 @@ class option final { /// /// Constructs a best::option out of a T*. nullptr is mapped to /// an empty optional. - constexpr option(ptr that) - requires best::is_ref - : option() { + constexpr option(ptr that) requires best::is_ref + : option() { *this = that; } - constexpr option& operator=(ptr that) - requires best::is_ref + constexpr option& operator=(ptr that) requires best::is_ref { if (that != nullptr) { emplace(*that); @@ -270,13 +267,13 @@ class option final { template constexpr explicit option(best::in_place_t, Args&&... args) requires best::constructible - : BEST_OPTION_IMPL_(best::index<1>, BEST_FWD(args)...) {} + : BEST_OPTION_IMPL_(best::index<1>, BEST_FWD(args)...) {} template constexpr explicit option(best::in_place_t, std::initializer_list il, Args&&... args) requires best::constructible, Args&&...> - : BEST_OPTION_IMPL_(best::index<1>, il, BEST_FWD(args)...) {} + : BEST_OPTION_IMPL_(best::index<1>, il, BEST_FWD(args)...) {} /// # `option::is_empty()` /// @@ -358,7 +355,7 @@ class option final { /// with the given arguments and returns that. template constexpr best::dependent value_or( - Args&&... args) const& { + Args&&... args) const& { return has_value() ? value() : value_type(BEST_FWD(args)...); } template @@ -372,12 +369,12 @@ class option final { /// Extracts the value of this option by copy/move, or constructs a default /// with the given callback. constexpr auto value_or(best::callable auto&& or_else) - const& -> best::dependent { + const& -> best::dependent { return has_value() ? value() : best::call(BEST_FWD(or_else)); } constexpr auto value_or( - best::callable auto&& - or_else) && -> best::dependent { + best::callable auto&& + or_else) && -> best::dependent { return has_value() ? BEST_MOVE(*this).value() : best::call(BEST_FWD(or_else)); } @@ -438,7 +435,7 @@ class option final { /// Returns `best::none` if `this->is_empty()`, else returns `that`. /// Arguments are eagerly evaluated. constexpr option operator&(auto&& that) const { - if (is_empty()) return best::none; + if (is_empty()) { return best::none; } return BEST_FWD(that); } @@ -447,11 +444,11 @@ class option final { /// Returns this option if `this->has_value()`, else returns `that`. /// Arguments are eagerly evaluated. constexpr option operator|(auto&& that) const& { - if (has_value()) return *this; + if (has_value()) { return *this; } return BEST_FWD(that); } constexpr option operator|(auto&& that) && { - if (has_value()) return BEST_MOVE(*this); + if (has_value()) { return BEST_MOVE(*this); } return BEST_FWD(that); } @@ -460,13 +457,13 @@ class option final { /// Returns whichever one of `this` and `that` is nonempty, or `best::none` /// if both are or are not empty. constexpr option operator^(auto&& that) const& { - if (has_value() == that.has_value()) return best::none; - if (has_value()) return *this; + if (has_value() == that.has_value()) { return best::none; } + if (has_value()) { return *this; } return BEST_FWD(that); } constexpr option operator^(auto&& that) && { - if (has_value() == that.has_value()) return best::none; - if (has_value()) return BEST_MOVE(*this); + if (has_value() == that.has_value()) { return best::none; } + if (has_value()) { return BEST_MOVE(*this); } return BEST_FWD(that); } @@ -510,7 +507,7 @@ class option final { constexpr ref or_emplace(Args&&... args) requires best::constructible { - if (has_value()) return **this; + if (has_value()) { return **this; } return emplace(BEST_FWD(args)...); } @@ -665,7 +662,7 @@ class option final { // clang-format on friend void BestFmt(auto& fmt, const option& opt) - requires(best::is_void) || requires { fmt.format(*opt); } + requires (best::is_void) || requires { fmt.format(*opt); } { if (!opt.has_value()) { fmt.write("none"); @@ -676,7 +673,7 @@ class option final { } } - friend constexpr void BestFmtQuery(auto& query, option*) { + constexpr friend void BestFmtQuery(auto& query, option*) { query = query.template of; query.requires_debug = true; } @@ -688,21 +685,21 @@ class option final { public: constexpr option(const container_internal::option& opt) requires best::is_object - : option() { - if (opt) emplace(*opt); + : option() { + if (opt) { emplace(*opt); } } constexpr option(container_internal::option&& opt) requires best::is_object - : option() { - if (opt) emplace(BEST_MOVE(*opt)); + : option() { + if (opt) { emplace(BEST_MOVE(*opt)); } } - constexpr operator container_internal::option() const& - requires best::is_object + constexpr operator container_internal::option() + const& requires best::is_object { return is_empty() ? container_internal::option() : value(); } - constexpr operator container_internal::option() && - requires best::is_object + constexpr + operator container_internal::option() && requires best::is_object { return is_empty() ? container_internal::option() : BEST_MOVE(*this).value(); @@ -718,8 +715,7 @@ class option final { return has_value() && value(unsafe("just checked has_value()")) == u; } template U> - constexpr bool operator==(const U* u) const - requires best::is_ref + constexpr bool operator==(const U* u) const requires best::is_ref { return (!has_value() && u == nullptr) || (has_value() && value(unsafe("just checked has_value()")) == *u); @@ -728,7 +724,7 @@ class option final { template U> constexpr best::order_type operator<=>( - const best::option& that) const { + const best::option& that) const { return impl() <=> that.impl(); } template U> @@ -753,7 +749,7 @@ class option final { template constexpr explicit option(best::tlist>, Choice&& choice) requires best::same, best::as_auto> - : BEST_OPTION_IMPL_(BEST_FWD(choice)) {} + : BEST_OPTION_IMPL_(BEST_FWD(choice)) {} constexpr void check_ok(best::location loc = best::here) const { if (best::unlikely(is_empty())) { @@ -877,37 +873,37 @@ template constexpr auto option::map(auto&& f) const& { using U = best::call_result; return impl().index_match( - [&](best::index_t<0>) -> option { return best::none; }, - [&](best::index_t<1>, auto&&... args) -> option { - return best::call(BEST_FWD(f), BEST_FWD(args)...); - }); + [&](best::index_t<0>) -> option { return best::none; }, + [&](best::index_t<1>, auto&&... args) -> option { + return best::call(BEST_FWD(f), BEST_FWD(args)...); + }); } template constexpr auto option::map(auto&& f) & { using U = best::call_result; return impl().index_match( - [&](best::index_t<0>) -> option { return best::none; }, - [&](best::index_t<1>, auto&&... args) -> option { - return best::call(BEST_FWD(f), BEST_FWD(args)...); - }); + [&](best::index_t<0>) -> option { return best::none; }, + [&](best::index_t<1>, auto&&... args) -> option { + return best::call(BEST_FWD(f), BEST_FWD(args)...); + }); } template constexpr auto option::map(auto&& f) const&& { using U = best::call_result; return BEST_MOVE(*this).impl().index_match( - [&](best::index_t<0>) -> option { return best::none; }, - [&](best::index_t<1>, auto&&... args) -> option { - return best::call(BEST_FWD(f), BEST_FWD(args)...); - }); + [&](best::index_t<0>) -> option { return best::none; }, + [&](best::index_t<1>, auto&&... args) -> option { + return best::call(BEST_FWD(f), BEST_FWD(args)...); + }); } template constexpr auto option::map(auto&& f) && { using U = best::call_result; return BEST_MOVE(*this).impl().index_match( - [&](best::index_t<0>) -> option { return best::none; }, - [&](best::index_t<1>, auto&&... args) -> option { - return best::call(BEST_FWD(f), BEST_FWD(args)...); - }); + [&](best::index_t<0>) -> option { return best::none; }, + [&](best::index_t<1>, auto&&... args) -> option { + return best::call(BEST_FWD(f), BEST_FWD(args)...); + }); } template @@ -982,19 +978,19 @@ template constexpr auto option::then(auto&& f) const&& { using U = best::unref>; return BEST_MOVE(*this).impl().index_match( - [&](best::index_t<0>) -> U { return best::none; }, - [&](best::index_t<1>, auto&&... args) -> U { - return best::call(BEST_FWD(f), BEST_FWD(args)...); - }); + [&](best::index_t<0>) -> U { return best::none; }, + [&](best::index_t<1>, auto&&... args) -> U { + return best::call(BEST_FWD(f), BEST_FWD(args)...); + }); } template constexpr auto option::then(auto&& f) && { using U = best::unref>; return BEST_MOVE(*this).impl().index_match( - [&](best::index_t<0>) -> U { return best::none; }, - [&](best::index_t<1>, auto&&... args) -> U { - return best::call(BEST_FWD(f), BEST_FWD(args)...); - }); + [&](best::index_t<0>) -> U { return best::none; }, + [&](best::index_t<1>, auto&&... args) -> U { + return best::call(BEST_FWD(f), BEST_FWD(args)...); + }); } constexpr auto BestGuardResidual(auto&&, is_option auto&&) { return none; } diff --git a/best/container/option_test.cc b/best/container/option_test.cc index 79b4ba8..c16f1c7 100644 --- a/best/container/option_test.cc +++ b/best/container/option_test.cc @@ -305,7 +305,7 @@ best::test AndThen = [](auto& t) { best::option x2 = 42; auto f = [](int x) -> best::option { - if (x == 0) return best::none; + if (x == 0) { return best::none; } return 1.0 / x; }; diff --git a/best/container/pun.h b/best/container/pun.h index 2ef4f65..9740f78 100644 --- a/best/container/pun.h +++ b/best/container/pun.h @@ -113,12 +113,12 @@ class pun final { template constexpr explicit pun(best::index_t, Args&&... args) requires best::constructible, Args&&...> - : BEST_PUN_IMPL_(best::index, BEST_FWD(args)...) {} + : BEST_PUN_IMPL_(best::index, BEST_FWD(args)...) {} template constexpr explicit pun(best::index_t, niche nh) requires best::has_niche> - : BEST_PUN_IMPL_(best::index, nh) {} + : BEST_PUN_IMPL_(best::index, nh) {} /// # `pun::get()` /// diff --git a/best/container/result.h b/best/container/result.h index d449291..51d2d7a 100644 --- a/best/container/result.h +++ b/best/container/result.h @@ -52,7 +52,7 @@ struct ok { fmt.write("ok"); fmt.format(ok.args); } - friend constexpr void BestFmtQuery(auto& query, ok*) { + constexpr friend void BestFmtQuery(auto& query, ok*) { query = query.template of>; } }; @@ -79,7 +79,7 @@ struct err { fmt.write("err"); fmt.format(err.args); } - friend constexpr void BestFmtQuery(auto& query, err*) { + constexpr friend void BestFmtQuery(auto& query, err*) { query = query.template of>; } }; @@ -91,9 +91,9 @@ err(Args&&...) -> err; /// Whether `T` is some `best::result`. template concept is_result = - best::same, - best::result::ok_type, - typename best::as_auto::err_type>>; + best::same, + best::result::ok_type, + typename best::as_auto::err_type>>; /// # `best::ok_type` /// @@ -115,15 +115,15 @@ struct fmt; template class [[nodiscard( - "best::results may contain an error and must be explicitly " - "handled!")]] result final { + "best::results may contain an error and must be explicitly " + "handled!")]] result final { private: template static constexpr bool cannot_init_from = - ((!best::constructible && !best::constructible) || - best::is_void)&&((!best::constructible && - !best::constructible) || - best::is_void); + ((!best::constructible && !best::constructible) || + best::is_void)&&((!best::constructible && + !best::constructible) || + best::is_void); public: /// Helper type aliases. @@ -165,7 +165,7 @@ class [[nodiscard( constexpr result(auto&&... args) requires best::constructible, decltype(args)...> && (sizeof...(args) > 0) - : BEST_RESULT_IMPL_(BEST_FWD(args)...) {} + : BEST_RESULT_IMPL_(BEST_FWD(args)...) {} /// # `result::result(ok(...))` /// @@ -173,7 +173,7 @@ class [[nodiscard( template constexpr result(best::ok args) requires best::constructible - : BEST_RESULT_IMPL_(best::index<0>, std::move(args).args) {} + : BEST_RESULT_IMPL_(best::index<0>, std::move(args).args) {} /// # `result::result(err(...))` /// @@ -181,7 +181,7 @@ class [[nodiscard( template constexpr result(best::err args) requires best::constructible - : BEST_RESULT_IMPL_(best::index<1>, std::move(args).args) {} + : BEST_RESULT_IMPL_(best::index<1>, std::move(args).args) {} /// # `result::result(result&)` /// @@ -197,12 +197,12 @@ class [[nodiscard( /// from `U`. template constexpr explicit( - !best::convertible, R&&>> || - !best::convertible, R&&>>) - result(R&& that) - requires best::constructible, R&&>> && - best::constructible, R&&>> && - cannot_init_from; + !best::convertible, R&&>> || + !best::convertible, R&&>>) + result(R&& that) + requires best::constructible, R&&>> && + best::constructible, R&&>> && + cannot_init_from; template constexpr result& operator=(R&& that) requires best::constructible, R&&>> && @@ -264,38 +264,34 @@ class [[nodiscard( /// `best::result`'s ok contents can be accessed with the smart pointer /// operators. These internally simply defer to `*ok()`. template - constexpr ok_cref operator*() const& - requires requires { F::check_ok(this); } + constexpr ok_cref operator*() const& requires requires { F::check_ok(this); } { return F::check_ok(this), impl()[best::index<0>]; } template - constexpr ok_ref operator*() & - requires requires { F::check_ok(this); } + constexpr ok_ref operator*() & requires requires { F::check_ok(this); } { return F::check_ok(this), impl()[best::index<0>]; } template - constexpr ok_crref operator*() const&& - requires requires { F::check_ok(this); } + constexpr ok_crref operator*() const&& requires requires { + F::check_ok(this); + } { return F::check_ok(this), BEST_MOVE(*this).impl()[best::index<0>]; } template - constexpr ok_rref operator*() && - requires requires { F::check_ok(this); } + constexpr ok_rref operator*() && requires requires { F::check_ok(this); } { return F::check_ok(this), BEST_MOVE(*this).impl()[best::index<0>]; } template - constexpr ok_cptr operator->() const - requires requires { F::check_ok(this); } + constexpr ok_cptr operator->() const requires requires { F::check_ok(this); } { return F::check_ok(this), *ok(), impl().as_ptr(index<0>); } template - constexpr ok_ptr operator->() - requires requires { F::check_ok(this); } + constexpr ok_ptr operator->() requires requires { F::check_ok(this); } { return F::check_ok(this), *ok(), impl().as_ptr(index<0>); } @@ -303,18 +299,18 @@ class [[nodiscard( // Comparisons. template U, best::equatable F> BEST_INLINE_SYNTHETIC constexpr bool operator==( - const best::result& that) const { + const best::result& that) const { return impl() == that.impl(); } /// NOTE: these implementations are written to minimize gratuitous /// instantiations of option. BEST_INLINE_SYNTHETIC constexpr bool operator==( - const best::equatable auto& u) const { + const best::equatable auto& u) const { return is_ok() && impl()[best::index<0>] == u; } BEST_INLINE_SYNTHETIC constexpr bool operator==( - const best::equatable auto& u) const { + const best::equatable auto& u) const { return is_err() && impl()[best::index<1>] == u; } template U> @@ -334,41 +330,41 @@ class [[nodiscard( template U, best::comparable F> BEST_INLINE_SYNTHETIC constexpr auto operator<=>( - const best::result& that) const { + const best::result& that) const { return impl() <=> that.impl(); } BEST_INLINE_SYNTHETIC constexpr auto operator<=>( - const best::comparable auto& u) const { - if (auto v = ok()) return v <=> u; + const best::comparable auto& u) const { + if (auto v = ok()) { return v <=> u; } return best::ord::less; } BEST_INLINE_SYNTHETIC constexpr auto operator<=>( - const best::comparable auto& u) const { - if (auto v = err()) return v <=> u; + const best::comparable auto& u) const { + if (auto v = err()) { return v <=> u; } return best::ord::greater; } template U> BEST_INLINE_SYNTHETIC constexpr auto operator<=>(best::ok u) const { - if (auto v = ok()) return v <=> u.args.args[best::index<0>]; + if (auto v = ok()) { return v <=> u.args.args[best::index<0>]; } return best::ord::less; } template U> BEST_INLINE_SYNTHETIC constexpr auto operator<=>(best::err u) const { - if (auto v = ok()) return v <=> u.args.args[best::index<0>]; + if (auto v = ok()) { return v <=> u.args.args[best::index<0>]; } return best::ord::less; } BEST_INLINE_SYNTHETIC constexpr auto operator<=>(best::ok<> u) const { - if (ok()) return best::ord::equal; + if (ok()) { return best::ord::equal; } return best::ord::less; } BEST_INLINE_SYNTHETIC constexpr auto operator<=>(best::err<> u) const { - if (err()) return best::ord::equal; + if (err()) { return best::ord::equal; } return best::ord::greater; } - friend void BestFmt(auto& fmt, const result& res) - requires requires { fmt.format(res.ok()); } && - requires { fmt.format(res.err()); } + friend void BestFmt(auto& fmt, const result& res) requires requires { + fmt.format(res.ok()); + } && requires { fmt.format(res.err()); } { if (auto v = res.ok()) { if constexpr (best::is_void) { @@ -385,11 +381,11 @@ class [[nodiscard( } } template - friend constexpr void BestFmtQuery(Q& query, result*) { + constexpr friend void BestFmtQuery(Q& query, result*) { query.supports_width = query.template of.supports_width || query.template of.supports_width; - query.supports_prec = query.template of.supports_prec || - query.template of.supports_prec; + query.supports_prec = + query.template of.supports_prec || query.template of.supports_prec; query.uses_method = [](auto r) { return (Q::template of.uses_method(r) && Q::template of.uses_method(r)); @@ -421,7 +417,7 @@ constexpr result::result(R&& that) requires best::constructible, R&&>> && best::constructible, R&&>> && cannot_init_from - : BEST_RESULT_IMPL_(best::uninit) { + : BEST_RESULT_IMPL_(best::uninit) { if (auto v = BEST_FWD(that).ok()) { if constexpr (best::is_void>) { std::construct_at(&impl(), best::index<0>); @@ -502,135 +498,135 @@ template constexpr auto result::map(auto&& f) const& { using U = best::call_result; return impl().index_match( - [&](best::index_t<0>, auto&&... args) -> result { - return best::ok(best::call(BEST_FWD(f), BEST_FWD(args)...)); - }, - [&](best::index_t<1>, auto&&... args) -> result { - return best::err(BEST_FWD(args)...); - }); + [&](best::index_t<0>, auto&&... args) -> result { + return best::ok(best::call(BEST_FWD(f), BEST_FWD(args)...)); + }, + [&](best::index_t<1>, auto&&... args) -> result { + return best::err(BEST_FWD(args)...); + }); } template constexpr auto result::map(auto&& f) & { using U = best::call_result; return impl().index_match( - [&](best::index_t<0>, auto&&... args) -> result { - return best::ok(best::call(BEST_FWD(f), BEST_FWD(args)...)); - }, - [&](best::index_t<1>, auto&&... args) -> result { - return best::err(BEST_FWD(args)...); - }); + [&](best::index_t<0>, auto&&... args) -> result { + return best::ok(best::call(BEST_FWD(f), BEST_FWD(args)...)); + }, + [&](best::index_t<1>, auto&&... args) -> result { + return best::err(BEST_FWD(args)...); + }); } template constexpr auto result::map(auto&& f) const&& { using U = best::call_result; return BEST_MOVE(*this).impl().index_match( - [&](best::index_t<0>, auto&&... args) -> result { - return best::ok(best::call(BEST_FWD(f), BEST_FWD(args)...)); - }, - [&](best::index_t<1>, auto&&... args) -> result { - return best::err(BEST_FWD(args)...); - }); + [&](best::index_t<0>, auto&&... args) -> result { + return best::ok(best::call(BEST_FWD(f), BEST_FWD(args)...)); + }, + [&](best::index_t<1>, auto&&... args) -> result { + return best::err(BEST_FWD(args)...); + }); } template constexpr auto result::map(auto&& f) && { using U = best::call_result; return BEST_MOVE(*this).impl().index_match( - [&](best::index_t<0>, auto&&... args) -> result { - return best::ok(best::call(BEST_FWD(f), BEST_FWD(args)...)); - }, - [&](best::index_t<1>, auto&&... args) -> result { - return best::err(BEST_FWD(args)...); - }); + [&](best::index_t<0>, auto&&... args) -> result { + return best::ok(best::call(BEST_FWD(f), BEST_FWD(args)...)); + }, + [&](best::index_t<1>, auto&&... args) -> result { + return best::err(BEST_FWD(args)...); + }); } template constexpr auto result::map_err(auto&& f) const& { using U = best::call_result; return impl().index_match( - [&](best::index_t<1>, auto&&... args) -> result { - return best::err(best::call(BEST_FWD(f), BEST_FWD(args)...)); - }, - [&](best::index_t<0>, auto&&... args) -> result { - return best::ok(BEST_FWD(args)...); - }); + [&](best::index_t<1>, auto&&... args) -> result { + return best::err(best::call(BEST_FWD(f), BEST_FWD(args)...)); + }, + [&](best::index_t<0>, auto&&... args) -> result { + return best::ok(BEST_FWD(args)...); + }); } template constexpr auto result::map_err(auto&& f) & { using U = best::call_result; return impl().index_match( - [&](best::index_t<1>, auto&&... args) -> result { - return best::err(best::call(BEST_FWD(f), BEST_FWD(args)...)); - }, - [&](best::index_t<0>, auto&&... args) -> result { - return best::ok(BEST_FWD(args)...); - }); + [&](best::index_t<1>, auto&&... args) -> result { + return best::err(best::call(BEST_FWD(f), BEST_FWD(args)...)); + }, + [&](best::index_t<0>, auto&&... args) -> result { + return best::ok(BEST_FWD(args)...); + }); } template constexpr auto result::map_err(auto&& f) const&& { using U = best::call_result; return BEST_MOVE(*this).impl().index_match( - [&](best::index_t<1>, auto&&... args) -> result { - return best::err(best::call(BEST_FWD(f), BEST_FWD(args)...)); - }, - [&](best::index_t<0>, auto&&... args) -> result { - return best::ok(BEST_FWD(args)...); - }); + [&](best::index_t<1>, auto&&... args) -> result { + return best::err(best::call(BEST_FWD(f), BEST_FWD(args)...)); + }, + [&](best::index_t<0>, auto&&... args) -> result { + return best::ok(BEST_FWD(args)...); + }); } template constexpr auto result::map_err(auto&& f) && { using U = best::call_result; return BEST_MOVE(*this).impl().index_match( - [&](best::index_t<1>, auto&&... args) -> result { - return best::err(best::call(BEST_FWD(f), BEST_FWD(args)...)); - }, - [&](best::index_t<0>, auto&&... args) -> result { - return best::ok(BEST_FWD(args)...); - }); + [&](best::index_t<1>, auto&&... args) -> result { + return best::err(best::call(BEST_FWD(f), BEST_FWD(args)...)); + }, + [&](best::index_t<0>, auto&&... args) -> result { + return best::ok(BEST_FWD(args)...); + }); } template constexpr auto result::then(auto&& f) const& { using U = best::unref>; return impl().index_match( - [&](best::index_t<0>, auto&&... args) -> U { - return best::call(BEST_FWD(f), BEST_FWD(args)...); - }, - [&](best::index_t<1>, auto&&... args) -> U { - return best::err(BEST_FWD(args)...); - }); + [&](best::index_t<0>, auto&&... args) -> U { + return best::call(BEST_FWD(f), BEST_FWD(args)...); + }, + [&](best::index_t<1>, auto&&... args) -> U { + return best::err(BEST_FWD(args)...); + }); } template constexpr auto result::then(auto&& f) & { using U = best::unref>; return impl().index_match( - [&](best::index_t<0>, auto&&... args) -> U { - return best::call(BEST_FWD(f), BEST_FWD(args)...); - }, - [&](best::index_t<1>, auto&&... args) -> U { - return best::err(BEST_FWD(args)...); - }); + [&](best::index_t<0>, auto&&... args) -> U { + return best::call(BEST_FWD(f), BEST_FWD(args)...); + }, + [&](best::index_t<1>, auto&&... args) -> U { + return best::err(BEST_FWD(args)...); + }); } template constexpr auto result::then(auto&& f) const&& { using U = best::unref>; return BEST_MOVE(*this).impl().index_match( - [&](best::index_t<0>, auto&&... args) -> U { - return best::call(BEST_FWD(f), BEST_FWD(args)...); - }, - [&](best::index_t<1>, auto&&... args) -> U { - return best::err(BEST_FWD(args)...); - }); + [&](best::index_t<0>, auto&&... args) -> U { + return best::call(BEST_FWD(f), BEST_FWD(args)...); + }, + [&](best::index_t<1>, auto&&... args) -> U { + return best::err(BEST_FWD(args)...); + }); } template constexpr auto result::then(auto&& f) && { using U = best::unref>; return BEST_MOVE(*this).impl().index_match( - [&](best::index_t<0>, auto&&... args) -> U { - return best::call(BEST_FWD(f), BEST_FWD(args)...); - }, - [&](best::index_t<1>, auto&&... args) -> U { - return best::err(BEST_FWD(args)...); - }); + [&](best::index_t<0>, auto&&... args) -> U { + return best::call(BEST_FWD(f), BEST_FWD(args)...); + }, + [&](best::index_t<1>, auto&&... args) -> U { + return best::err(BEST_FWD(args)...); + }); } constexpr decltype(auto) BestGuardResidual(auto&&, is_result auto&& r) { diff --git a/best/container/result_test.cc b/best/container/result_test.cc index 4b3c8cb..6ae0a95 100644 --- a/best/container/result_test.cc +++ b/best/container/result_test.cc @@ -117,9 +117,9 @@ best::test Map = [](auto& t) { t.expect_eq(c, 1); best::result, best::str> x4( - x0.map([](int x) { return new int(x); })); + x0.map([](int x) { return new int(x); })); best::result, best::str> x5( - x1.map([](int x) { return new int(x); })); + x1.map([](int x) { return new int(x); })); t.expect_eq(**std::move(x4).map([](auto&& x) { return std::move(x); }), 42); t.expect_eq(std::move(x5).map([](auto&& x) { return std::move(x); }), @@ -155,7 +155,7 @@ best::test Irregular = [](auto& t) { best::test Guard = [](auto& t) { auto cb = - [](best::result x) -> best::result { + [](best::result x) -> best::result { BEST_GUARD(x); return *x == 0; }; diff --git a/best/container/row.h b/best/container/row.h index d818208..cd9f716 100644 --- a/best/container/row.h +++ b/best/container/row.h @@ -65,7 +65,7 @@ struct args final { template T> constexpr operator T() && { return std::move(row).apply( - [](auto&&... args) { return T(BEST_FWD(args)...); }); + [](auto&&... args) { return T(BEST_FWD(args)...); }); } friend void BestFmt(auto& fmt, const args& args) @@ -74,7 +74,7 @@ struct args final { fmt.format(args.row); } - friend constexpr void BestFmtQuery(auto& query, args*) { + constexpr friend void BestFmtQuery(auto& query, args*) { query = query.template of>; } }; @@ -126,7 +126,7 @@ args(Args&&...) -> args; /// - `best::row` is trivial when all of its elements are trivial. template class row final - : row_internal::impl), Elems...> { + : row_internal::impl), Elems...> { public: /// # `row::types` /// @@ -160,7 +160,7 @@ class row final /// such that `U::BestRowKey` is `T`. template static constexpr bool selectable = - row_internal::lookup.template count() > 0; + row_internal::lookup.template count() > 0; private: using impl = row_internal::impl; @@ -185,30 +185,29 @@ class row final /// Constructs a row by initializing each element from the corresponding /// argument. constexpr row(auto&&... args) - requires(best::constructible && ...) && - (!best::same> || ...) - : impl{{best::object(best::in_place, BEST_FWD(args))}...} {} - constexpr row(devoid&&... args) - requires(sizeof...(args) > 0) - : impl{{best::object(best::in_place, BEST_FWD(args))}...} {} + requires (best::constructible && ...) && + (!best::same> || ...) + : impl{{best::object(best::in_place, BEST_FWD(args))}...} {} + constexpr row(devoid&&... args) requires (sizeof...(args) > 0) + : impl{{best::object(best::in_place, BEST_FWD(args))}...} {} constexpr row(best::bind_t, auto&&... args) - requires(best::constructible && ...) && - (!best::same> || ...) - : impl{{best::object(best::in_place, BEST_FWD(args))}...} {} + requires (best::constructible && ...) && + (!best::same> || ...) + : impl{{best::object(best::in_place, BEST_FWD(args))}...} {} constexpr row(best::bind_t, devoid&&... args) - requires(sizeof...(args) > 0) - : impl{{best::object(best::in_place, BEST_FWD(args))}...} {} + requires (sizeof...(args) > 0) + : impl{{best::object(best::in_place, BEST_FWD(args))}...} {} /// # `row::size()` /// /// Returns the number of elements in this row. - constexpr static size_t size() { return types.size(); } + static constexpr size_t size() { return types.size(); } /// # `row::is_empty()` /// /// Returns whether this is the empty row `best::row<>`. - constexpr static bool is_empty() { return types.size() == 0; } + static constexpr bool is_empty() { return types.size() == 0; } /// # `row::as_ref()` /// @@ -506,7 +505,7 @@ class row final } template - friend constexpr void BestFmtQuery(Q& query, row*) { + constexpr friend void BestFmtQuery(Q& query, row*) { query.supports_width = (query.template of.supports_width || ...); query.supports_prec = (query.template of.supports_prec || ...); query.uses_method = [](auto r) { @@ -517,23 +516,21 @@ class row final // Comparisons. template constexpr bool operator==(const row& that) const - requires(best::equatable && ...) + requires (best::equatable && ...) { return indices.apply( - [&](auto... i) { return (... && (get(i) == that.get(i))); }); + [&](auto... i) { return (... && (get(i) == that.get(i))); }); } template constexpr auto operator<=>(const choice& that) const - requires(best::comparable && ...) + requires (best::comparable && ...) { using Output = best::common_ord...>; Output result = Output::equivalent; return indices.each([&](auto i) { - if (result == 0) { - result = get(i) <=> that.get(i); - } + if (result == 0) { result = get(i) <=> that.get(i); } }); return result; @@ -555,28 +552,28 @@ template constexpr auto row::as_args() const& { return apply([](auto&&... args) { return best::args{ - row(BEST_FWD(args)...)}; + row(BEST_FWD(args)...)}; }); } template constexpr auto row::as_args() & { return apply([](auto&&... args) { return best::args{ - row(BEST_FWD(args)...)}; + row(BEST_FWD(args)...)}; }); } template constexpr auto row::as_args() const&& { return BEST_MOVE(*this).apply([](auto&&... args) { return best::args{ - row(BEST_FWD(args)...)}; + row(BEST_FWD(args)...)}; }); } template constexpr auto row::as_args() && { return BEST_MOVE(*this).apply([](auto&&... args) { return best::args{ - row(BEST_FWD(args)...)}; + row(BEST_FWD(args)...)}; }); } @@ -589,7 +586,7 @@ constexpr best::row...> row::as_ref() const& { template constexpr best::row...> row::as_ref() & { apply( - [](auto&&... args) { best::row...>(BEST_FWD(args)...); }); + [](auto&&... args) { best::row...>(BEST_FWD(args)...); }); } template constexpr best::row...> row::as_ref() const&& { @@ -599,15 +596,14 @@ constexpr best::row...> row::as_ref() const&& { } template constexpr best::row...> row::as_ref() && { - BEST_MOVE(*this).apply([](auto&&... args) { - best::row...>(BEST_FWD(args)...); - }); + BEST_MOVE(*this).apply( + [](auto&&... args) { best::row...>(BEST_FWD(args)...); }); } template template constexpr row::cref row::operator[]( - best::index_t idx) const& { + best::index_t idx) const& { return *this->get_impl(idx); } template @@ -618,7 +614,7 @@ constexpr row::ref row::operator[](best::index_t idx) & { template template constexpr row::crref row::operator[]( - best::index_t idx) const&& { + best::index_t idx) const&& { return *BEST_MOVE(this->get_impl(idx)); } template @@ -697,7 +693,7 @@ row::object(best::index_t i) const& { template template constexpr best::object::template type>& row::object( - best::index_t i) & { + best::index_t i) & { return this->get_impl(i); } template @@ -812,74 +808,68 @@ constexpr auto row::select_indices(best::tlist idx) { // XXX: This code tickles a clang-format bug. template -constexpr decltype(auto) row::first() const& - requires(!types.is_empty()) +constexpr decltype(auto) row::first() const& requires (!types.is_empty()) { return at(index<0>); } template - constexpr decltype(auto) row::first() & - requires(!types.is_empty()) { - return at(index<0>); - } template - constexpr decltype(auto) row::first() const&& - requires(!types.is_empty()) + constexpr decltype(auto) row::first() & + requires(!types.is_empty()) { return at(index<0>); } template + constexpr decltype(auto) row::first() const&& requires ( + !types.is_empty()) { return BEST_MOVE(*this).at(index<0>); } template - constexpr decltype(auto) row::first() && - requires(!types.is_empty()) { - return BEST_MOVE(*this).at(index<0>); - } template - constexpr decltype(auto) row::second() const& - requires(types.size() >= 2) + constexpr decltype(auto) row::first() && + requires(!types.is_empty()) { + return BEST_MOVE(*this).at(index<0>); + } template + constexpr decltype(auto) row::second() const& requires (types.size() >= + 2) { return at(index<1>); } template - constexpr decltype(auto) row::second() & - requires(types.size() >= 2) { - return at(index<1>); - } template - constexpr decltype(auto) row::second() const&& - requires(types.size() >= 2) + constexpr decltype(auto) row::second() & + requires(types.size() >= 2) { return at(index<1>); } template + constexpr decltype(auto) row::second() const&& requires (types.size() >= + 2) { return BEST_MOVE(*this).at(index<1>); } template - constexpr decltype(auto) row::second() && - requires(types.size() >= 2) { - return BEST_MOVE(*this).at(index<1>); - } template - constexpr decltype(auto) row::last() const& - requires(!types.is_empty()) + constexpr decltype(auto) row::second() && + requires(types.size() >= 2) { + return BEST_MOVE(*this).at(index<1>); + } template + constexpr decltype(auto) row::last() const& requires (!types.is_empty()) { return at(index); } template - constexpr decltype(auto) row::last() & - requires(!types.is_empty()) { - return at(index); - } template - constexpr decltype(auto) row::last() const&& - requires(!types.is_empty()) + constexpr decltype(auto) row::last() & + requires(!types.is_empty()) { + return at(index); + } template + constexpr decltype(auto) row::last() const&& requires ( + !types.is_empty()) { return BEST_MOVE(*this).at(index); } template - constexpr decltype(auto) row::last() && - requires(!types.is_empty()) { - return BEST_MOVE(*this).at(index); - } + constexpr decltype(auto) row::last() && + requires(!types.is_empty()) { + return BEST_MOVE(*this).at(index); + } #define BEST_ROW_MUST_USE(func_) \ [[nodiscard("best::row::" #func_ \ "() does not mutate its argument; instead, it " \ "returns a new best::row")]] - template - BEST_ROW_MUST_USE(push) - constexpr auto row::push(auto&& that) const& { + template + BEST_ROW_MUST_USE(push) + constexpr auto row::push(auto&& that) const& { return splice(best::types>, best::row(best::bind, BEST_FWD(that)), best::vals); @@ -1392,12 +1382,12 @@ constexpr auto row::scatter(best::tlist those_types, template constexpr decltype(auto) row::apply(auto&& f) const& { return this->apply_impl( - [&](auto&&... p) { return best::call(BEST_FWD(f), p.or_empty()...); }); + [&](auto&&... p) { return best::call(BEST_FWD(f), p.or_empty()...); }); } template constexpr decltype(auto) row::apply(auto&& f) & { return this->apply_impl( - [&](auto&&... p) { return best::call(BEST_FWD(f), p.or_empty()...); }); + [&](auto&&... p) { return best::call(BEST_FWD(f), p.or_empty()...); }); } template constexpr decltype(auto) row::apply(auto&& f) const&& { @@ -1415,12 +1405,12 @@ constexpr decltype(auto) row::apply(auto&& f) && { template constexpr void row::each(auto&& f) const& { return this->apply_impl( - [&](auto&&... p) { (row_internal::object_call(BEST_FWD(f), p), ...); }); + [&](auto&&... p) { (row_internal::object_call(BEST_FWD(f), p), ...); }); } template constexpr void row::each(auto&& f) & { return this->apply_impl( - [&](auto&&... p) { (row_internal::object_call(BEST_FWD(f), p), ...); }); + [&](auto&&... p) { (row_internal::object_call(BEST_FWD(f), p), ...); }); } template constexpr void row::each(auto&& f) const&& { diff --git a/best/container/row_test.cc b/best/container/row_test.cc index 4e18a05..55554fb 100644 --- a/best/container/row_test.cc +++ b/best/container/row_test.cc @@ -124,42 +124,42 @@ best::test Splice = [](auto& t) { t.expect_eq(x1, best::row{1, 2, nullptr, 4, nullptr}); best::row x11 = - x0.push(best::bind, x0[best::index<2>]); + x0.push(best::bind, x0[best::index<2>]); t.expect_eq(x11, best::row{1, 2, nullptr, 4, nullptr}); best::row x12 = - x0.push(best::types, nullptr); + x0.push(best::types, nullptr); t.expect_eq(x12, best::row{1, 2, nullptr, 4, nullptr}); best::row x2 = x0.insert<3>(x0[best::index<2>]); t.expect_eq(x2, best::row{1, 2, nullptr, nullptr, 4}); best::row x21 = - x0.insert<3>(best::bind, x0[best::index<2>]); + x0.insert<3>(best::bind, x0[best::index<2>]); t.expect_eq(x21, best::row{1, 2, nullptr, nullptr, 4}); best::row x22 = - x0.insert<3>(best::types, nullptr); + x0.insert<3>(best::types, nullptr); t.expect_eq(x22, best::row{1, 2, nullptr, nullptr, 4}); best::row x2o = x0.update<3>(x0[best::index<2>]); t.expect_eq(x2o, best::row{1, 2, nullptr, nullptr}); best::row x21o = - x0.update<3>(best::bind, x0[best::index<2>]); + x0.update<3>(best::bind, x0[best::index<2>]); t.expect_eq(x21o, best::row{1, 2, nullptr, nullptr}); best::row x22o = - x0.update<3>(best::types, nullptr); + x0.update<3>(best::types, nullptr); t.expect_eq(x22o, best::row{1, 2, nullptr, nullptr}); best::row x3o = - x0.splice(x0); + x0.splice(x0); t.expect_eq(x3o, best::row{1, 1, 2, nullptr, 4, 4}); best::row x31 = - x0.splice( - best::types, x0); + x0.splice( + best::types, x0); t.expect_eq(x31, best::row{1, 1, 2, nullptr, 4, 4}); best::row x4{MoveOnly(), 42}; diff --git a/best/container/vec.h b/best/container/vec.h index 55cdba7..6403b7a 100644 --- a/best/container/vec.h +++ b/best/container/vec.h @@ -54,8 +54,8 @@ class vec; /// Determines whether `V` is some `best::vec<...>`. template concept is_vec = - best::same, - best::vec>; + best::same, + best::vec>; /// # `best::vec_inline_default()` /// @@ -64,8 +64,8 @@ template constexpr size_t vec_inline_default() { // This is at most 24, so we only need to take a single byte // for the size. - auto best_possible = best::layout::of_struct, size_t>().size() / - best::size_of; + auto best_possible = + best::layout::of_struct, size_t>().size() / best::size_of; return best::saturating_sub(best_possible, 1); } @@ -120,11 +120,11 @@ class vec final { explicit vec(Range&& range) requires best::constructible> && best::constructible - : vec(alloc{}, BEST_FWD(range)) {} + : vec(alloc{}, BEST_FWD(range)) {} template vec(alloc alloc, Range&& range) requires best::constructible> - : vec(std::move(alloc)) { + : vec(std::move(alloc)) { assign(range); // TODO: move optimization? } @@ -136,15 +136,13 @@ class vec final { explicit vec(Iter&& iter) requires best::constructible> && best::constructible - : vec(alloc{}, BEST_FWD(iter)) {} + : vec(alloc{}, BEST_FWD(iter)) {} template vec(alloc alloc, Iter&& iter) requires best::constructible> - : vec(std::move(alloc)) { + : vec(std::move(alloc)) { reserve(iter.size_hint().lower); - for (auto&& elem : iter) { - push(BEST_FWD(elem)); - } + for (auto&& elem : iter) { push(BEST_FWD(elem)); } } /// # `vec::vec{...}` @@ -152,14 +150,13 @@ class vec final { /// Constructs a vector via initializer list. vec(std::initializer_list range) requires best::constructible && best::is_object - : vec(alloc{}, range) {} + : vec(alloc{}, range) {} /// # `vec::vec(alloc, {...})` /// /// Constructs a vector via initializer list, using the given allocator. - vec(alloc alloc, std::initializer_list range) - requires best::is_object - : vec(std::move(alloc)) { + vec(alloc alloc, std::initializer_list range) requires best::is_object + : vec(std::move(alloc)) { assign(range); } @@ -168,7 +165,7 @@ class vec final { /// Constructs a vector from an array box, by taking ownership of the /// allocation. vec(box&& box) - : alloc_(best::in_place, BEST_MOVE(box.allocator())) { + : alloc_(best::in_place, BEST_MOVE(box.allocator())) { raw_ = BEST_MOVE(box).leak(); store_size(~raw_.size()); } @@ -177,8 +174,7 @@ class vec final { /// /// Vectors are copyable if their elements are copyable; vectors are /// always moveable. - vec(const vec& that) - requires best::copyable && best::copyable; + vec(const vec& that) requires best::copyable && best::copyable; vec& operator=(const vec& that) requires best::copyable && best::copyable; vec(vec&& that); @@ -210,9 +206,7 @@ class vec final { /// Returns this vector's capacity (the number of elements it can hold before /// being forced to resize). size_t capacity() const { - if (auto heap = on_heap()) { - return heap->size(); - } + if (auto heap = on_heap()) { return heap->size(); } return max_inline; } @@ -299,15 +293,15 @@ class vec final { /// /// Converts this vector into a box by taking ownership of the allocation. best::box to_box() && { - if (is_empty()) return {}; + if (is_empty()) { return {}; } spill_to_heap(size(), true); auto span = as_span(); store_size(0); // This disables the destructor by marking this as an empty // inlined vector. return best::box( - unsafe("we are marking this vector completely empty in this function"), - *BEST_MOVE(alloc_), span); + unsafe("we are marking this vector completely empty in this function"), + *BEST_MOVE(alloc_), span); } /// # `vec::reserve()`. @@ -320,7 +314,7 @@ class vec final { /// /// Resizes the underlying allocation such that `capacity == size`. void shrink_to_fit() { - if (size() == capacity() || !on_heap()) return; + if (size() == capacity() || !on_heap()) { return; } spill_to_heap(size()); } @@ -338,8 +332,7 @@ class vec final { /// # `vec::push()`. /// /// Constructs a new value at the end of this vector, in-place. - ref push(auto&&... args) - requires best::constructible + ref push(auto&&... args) requires best::constructible { return insert(size(), BEST_FWD(args)...); } @@ -352,8 +345,8 @@ class vec final { ref insert(size_t idx, auto&&... args) requires best::constructible { - auto ptr = insert_uninit(unsafe("we call construct immediately after this"), - idx, 1); + auto ptr = + insert_uninit(unsafe("we call construct immediately after this"), idx, 1); ptr.construct(BEST_FWD(args)...); return *ptr; } @@ -472,9 +465,7 @@ class vec final { void splice_within(size_t idx, size_t start, size_t count); best::option> on_heap() const { - if (best::to_signed(load_size()) < 0) { - return raw_; - } + if (best::to_signed(load_size()) < 0) { return raw_; } return best::none; } @@ -516,7 +507,7 @@ class vec final { // How big the area where inlined values live is, including the size. size_t inlined_region_size() const { return reinterpret_cast( - &size_do_not_use_directly_or_else_ub_) - + &size_do_not_use_directly_or_else_ub_) - reinterpret_cast(this) + best::size_of; } size_t load_size() const { @@ -529,15 +520,15 @@ class vec final { } static constexpr size_t InternalAlignment = - max_inline > 0 && best::align_of > best::align_of> - ? best::align_of - : best::align_of>; + max_inline > 0 && best::align_of > best::align_of> + ? best::align_of + : best::align_of>; alignas(InternalAlignment) best::span raw_; [[no_unique_address]] padding padding_; ssize_t // Signed so we get "reasonable" // debugging prints in gdb. - size_do_not_use_directly_or_else_ub_ = 0; + size_do_not_use_directly_or_else_ub_ = 0; [[no_unique_address]] best::object alloc_; }; @@ -546,7 +537,7 @@ vec(std::initializer_list) -> vec; template vec(Range) -> vec>>; template - requires(!contiguous) +requires (!contiguous) vec(Iter) -> vec>>; } // namespace best @@ -558,12 +549,12 @@ namespace best { template vec::vec(const vec& that) requires best::copyable && best::copyable - : vec(that.allocator()) { + : vec(that.allocator()) { assign(that); } template -auto vec::operator=(const vec& that) -> vec& - requires best::copyable && best::copyable +auto vec::operator=(const vec& that) + -> vec& requires best::copyable && best::copyable { assign(that); return *this; @@ -581,7 +572,7 @@ auto vec::operator=(vec&& that) -> vec& { template void vec::move_construct(vec&& that, bool assign) { - if (best::equal(this, &that)) return; + if (best::equal(this, &that)) { return; } if (auto heap = that.on_heap()) { destroy(); @@ -611,9 +602,7 @@ void vec::move_construct(vec&& that, bool assign) { data().relo(that.data(), new_size); } - if (assign) { - alloc_ = std::move(that.alloc_); - } + if (assign) { alloc_ = std::move(that.alloc_); } that.store_size(0); // This resets `that` to being empty and inlined. } @@ -630,17 +619,13 @@ void vec::destroy() { template best::ptr vec::data() const { - if (auto heap = on_heap()) { - return heap->data(); - } + if (auto heap = on_heap()) { return heap->data(); } return reinterpret_cast::pointee*>(this); } template best::ptr vec::data() { - if (auto heap = on_heap()) { - return heap->data(); - } + if (auto heap = on_heap()) { return heap->data(); } return reinterpret_cast::pointee*>(this); } @@ -687,7 +672,7 @@ void vec::set_size(unsafe, size_t new_size) { template void vec::assign(const contiguous auto& that) { - if (best::equal(this, &that)) return; + if (best::equal(this, &that)) { return; } using Range = best::unref; if constexpr (best::is_vec) { @@ -733,55 +718,53 @@ void vec::splice_within(size_t idx, size_t start, insert_uninit(unsafe("we perform copies immediately after this"), idx, count); unsafe u( - "no bounds checks required; has_subarray and insert_uninit verify " - "all relevant bounds for us"); + "no bounds checks required; has_subarray and insert_uninit verify " + "all relevant bounds for us"); if (idx > end) { // The spliced-from region is before the insertion point, so we have // one loop. as_span() - .at(u, {.start = idx, .count = count}) - .emplace_from(as_span().at(u, {.start = start, .end = end})); + .at(u, {.start = idx, .count = count}) + .emplace_from(as_span().at(u, {.start = start, .end = end})); } else if (idx < start) { // The spliced-from region is after the insertion point. This is the // same as above, but we need to offset the slice operation by // `that.size()`. as_span() - .at(u, {.start = idx, .count = count}) - .emplace_from( - as_span().at(u, {.start = start + count, .end = end + count})); + .at(u, {.start = idx, .count = count}) + .emplace_from( + as_span().at(u, {.start = start + count, .end = end + count})); } else { // The annoying case. We need to do the copy in two parts. size_t before = idx - start; size_t after = count - before; as_span() - .at(u, {.start = idx, .count = before}) - .emplace_from(as_span().at(u, {.start = start, .count = before})); + .at(u, {.start = idx, .count = before}) + .emplace_from(as_span().at(u, {.start = start, .count = before})); as_span() - .at(u, {.start = idx + before, .count = after}) - .emplace_from(as_span().at(u, {.start = idx, .count = after})); + .at(u, {.start = idx + before, .count = after}) + .emplace_from(as_span().at(u, {.start = idx, .count = after})); } } template void vec::truncate(size_t count) { - if (count > size()) return; + if (count > size()) { return; } resize_uninit(count); } template void vec::clear() { if (!best::destructible) { - for (size_t i = 0; i < size(); ++i) { - (data() + i).destroy(); - } + for (size_t i = 0; i < size(); ++i) { (data() + i).destroy(); } } set_size(unsafe("we just destroyed all elements"), 0); } template best::option vec::pop() { - if (is_empty()) return best::none; + if (is_empty()) { return best::none; } return remove(size() - 1); } @@ -801,8 +784,8 @@ void vec::erase(best::bounds bounds) { size_t end = bounds.start + range.size(); size_t len = size() - end; as_span().shift_within( - unsafe("shifting elements over the ones we just destroyed"), start, end, - len); + unsafe("shifting elements over the ones we just destroyed"), start, end, + len); set_size(unsafe("updating length to exclude the range we just deleted"), size() - range.size()); } @@ -811,7 +794,7 @@ template best::ptr vec::insert_uninit(unsafe u, size_t start, size_t count) { (void)as_span()[{.start = start}]; // Trigger a bounds check. - if (count == 0) return data() + start; + if (count == 0) { return data() + start; } // TODO(mcyoung): Ideally, this operation should be fused with reserve, // since there are cases where we can perform a memmove as-we-go when we @@ -820,9 +803,7 @@ best::ptr vec::insert_uninit(unsafe u, size_t start, /// Relocate elements to create an empty space. auto end = start + count; - if (start < size()) { - as_span().shift_within(u, end, start, size() - start); - } + if (start < size()) { as_span().shift_within(u, end, start, size() - start); } set_size(u, size() + count); return data() + start; } @@ -833,8 +814,8 @@ void vec::resize_uninit(size_t new_size) { if (new_size <= capacity()) { if (new_size < old_size) { as_span() - .at(unsafe{"we just did a bounds check (above)"}, {.start = new_size}) - .destroy(); + .at(unsafe{"we just did a bounds check (above)"}, {.start = new_size}) + .destroy(); set_size(unsafe("elements beyond new_size destroyed above"), new_size); } return; @@ -861,9 +842,7 @@ void vec::spill_to_heap(best::option capacity_hint, } // If we don't hint at a capacity, pick 32 as a "good default". - if (size() < 32 && !capacity_hint) { - new_size = 32; - } + if (size() < 32 && !capacity_hint) { new_size = 32; } auto old_layout = best::layout::array(capacity()); auto new_layout = best::layout::array(new_size); @@ -880,11 +859,11 @@ void vec::spill_to_heap(best::option capacity_hint, // In the general case, we need to allocate new memory, relocate the values, // destroy the moved-from values, and free the old buffer if it is on-heap. best::ptr new_data = - static_cast::pointee*>(alloc_->alloc(new_layout)); + static_cast::pointee*>(alloc_->alloc(new_layout)); size_t old_size = size(); new_data.relo(data(), old_size); - if (on_heap()) alloc_->dealloc(data(), old_layout); + if (on_heap()) { alloc_->dealloc(data(), old_layout); } // construct_at instead of assignment, since raw_ may contain garbage, so // calling its operator= is UB. diff --git a/best/func/call.h b/best/func/call.h index 2707dda..b1e20cc 100644 --- a/best/func/call.h +++ b/best/func/call.h @@ -61,8 +61,8 @@ BEST_INLINE_SYNTHETIC constexpr decltype(auto) call_devoid(auto &&...args) } template BEST_INLINE_SYNTHETIC constexpr decltype(auto) call_devoid(auto &&...args) - requires(sizeof...(vs) > 0) && - requires { best::call(BEST_FWD(args)...); } + requires (sizeof...(vs) > 0) && + requires { best::call(BEST_FWD(args)...); } { using Out = decltype(best::call(BEST_FWD(args)...)); if constexpr (best::is_void) { @@ -95,8 +95,8 @@ BEST_INLINE_SYNTHETIC constexpr decltype(auto) call_devoid(auto &&...args) /// Arguments after the function signature are explicit template parameters for /// `operator()`. template -concept callable = call_internal::can_call(call_internal::tag{}, - (Signature *)nullptr); +concept callable = + call_internal::can_call(call_internal::tag{}, (Signature *)nullptr); /// # `best::call_result`. /// @@ -107,7 +107,7 @@ concept callable = call_internal::can_call(call_internal::tag{}, /// arguments. template using call_result = - decltype(call_internal::call_result(call_internal::tag{})); + decltype(call_internal::call_result(call_internal::tag{})); } // namespace best diff --git a/best/func/fnref.h b/best/func/fnref.h index a37c264..62a7a54 100644 --- a/best/func/fnref.h +++ b/best/func/fnref.h @@ -46,10 +46,10 @@ namespace best { /// container. It's not forbidden, but it's something to do carefully. template class fnref final : best::abominable_internal::tame::template apply< - fnref_internal::impl> { + fnref_internal::impl> { private: using impl_t = best::abominable_internal::tame::template apply< - fnref_internal::impl>; + fnref_internal::impl>; public: /// # `fnref::output` diff --git a/best/func/internal/call.h b/best/func/internal/call.h index 3e14115..f32db03 100644 --- a/best/func/internal/call.h +++ b/best/func/internal/call.h @@ -33,7 +33,7 @@ struct tag {}; template BEST_INLINE_SYNTHETIC constexpr decltype(auto) call( - best::is_func auto Class::*member, auto&& self, auto&&... args) + best::is_func auto Class::*member, auto&& self, auto&&... args) requires requires { requires sizeof...(Ts) == 0; (self.*member)(BEST_FWD(args)...); @@ -43,7 +43,7 @@ BEST_INLINE_SYNTHETIC constexpr decltype(auto) call( } template BEST_INLINE_SYNTHETIC constexpr decltype(auto) call( - best::is_func auto Class::*member, auto* self, auto&&... args) + best::is_func auto Class::*member, auto* self, auto&&... args) requires requires { requires sizeof...(Ts) == 0; (self->*member)(BEST_FWD(args)...); @@ -54,21 +54,19 @@ BEST_INLINE_SYNTHETIC constexpr decltype(auto) call( template BEST_INLINE_SYNTHETIC constexpr decltype(auto) call( - best::is_object auto Class::*member, auto&& self) - requires requires { - requires sizeof...(Ts) == 0; - self.*member; - } + best::is_object auto Class::*member, auto&& self) requires requires { + requires sizeof...(Ts) == 0; + self.*member; +} { return BEST_FWD(self).*member; } template BEST_INLINE_SYNTHETIC constexpr decltype(auto) call( - best::is_object auto Class::*member, auto* self) - requires requires { - requires sizeof...(Ts) == 0; - self->*member; - } + best::is_object auto Class::*member, auto* self) requires requires { + requires sizeof...(Ts) == 0; + self->*member; +} { return self->*member; } @@ -112,12 +110,12 @@ constexpr bool can_call(tag, R (*)(Args...)) { return best::is_void || best::convertible( - best::lie, best::lie...))>; + best::lie, best::lie...))>; } template auto call_result(tag) - -> decltype(call_internal::call(best::lie, best::lie...)); + -> decltype(call_internal::call(best::lie, best::lie...)); template auto call_result(tag) -> decltype(call_internal::call(best::lie)); } // namespace best::call_internal diff --git a/best/func/internal/fnref.h b/best/func/internal/fnref.h index ac0ef95..0118ae4 100644 --- a/best/func/internal/fnref.h +++ b/best/func/internal/fnref.h @@ -43,38 +43,38 @@ class impl { constexpr impl(const auto& fn) requires best::callable && - (!no_captures) - : ptr_(best::addr(fn)), - lambda_(+[](const void* captures, Args... args) -> R { - if constexpr (best::is_void) { - best::call( - *reinterpret_cast*>(captures), - BEST_FWD(args)...); - } else { - return best::call( - *reinterpret_cast*>(captures), - BEST_FWD(args)...); - } - }) {} + (!no_captures) + : ptr_(best::addr(fn)), + lambda_(+[](const void* captures, Args... args) -> R { + if constexpr (best::is_void) { + best::call( + *reinterpret_cast*>(captures), + BEST_FWD(args)...); + } else { + return best::call( + *reinterpret_cast*>(captures), + BEST_FWD(args)...); + } + }) {} constexpr impl(best::callable auto&& fn) - requires(!best::is_const_func) && (!no_captures) - : ptr_(best::addr(fn)), - lambda_(+[](const void* captures, Args... args) -> R { - if constexpr (best::is_void) { - best::call(*reinterpret_cast*>( - const_cast(captures)), - BEST_FWD(args)...); - } else { - return best::call(*reinterpret_cast*>( - const_cast(captures)), - BEST_FWD(args)...); - } - }) {} + requires (!best::is_const_func) && (!no_captures) + : ptr_(best::addr(fn)), + lambda_(+[](const void* captures, Args... args) -> R { + if constexpr (best::is_void) { + best::call(*reinterpret_cast*>( + const_cast(captures)), + BEST_FWD(args)...); + } else { + return best::call(*reinterpret_cast*>( + const_cast(captures)), + BEST_FWD(args)...); + } + }) {} constexpr impl(best::callable auto&& fn) requires no_captures - : impl(+fn) {} + : impl(+fn) {} constexpr R operator()(Args... args) const { if (ptr_) { diff --git a/best/func/tap.h b/best/func/tap.h index ed6aec7..f99a071 100644 --- a/best/func/tap.h +++ b/best/func/tap.h @@ -79,7 +79,7 @@ namespace best { /// ``` template class [[nodiscard( - "a best::tap must be called using ->* to have any effect")]] tap final { + "a best::tap must be called using ->* to have any effect")]] tap final { public: BEST_CTAD_GUARD_("best::tap", Guard); diff --git a/best/iter/bounds.cc b/best/iter/bounds.cc index 2487a98..eebbabb 100644 --- a/best/iter/bounds.cc +++ b/best/iter/bounds.cc @@ -32,7 +32,7 @@ namespace best::bounds_internal { if (count == 1 && start >= len) { crash_internal::crash( - {"bounds-check failed: %zu (start) >= %zu (len)", loc}, start, len); + {"bounds-check failed: %zu (start) >= %zu (len)", loc}, start, len); } if (end && *end < start) { @@ -57,14 +57,14 @@ namespace best::bounds_internal { if (count && start + *count > len) { crash_internal::crash( - {"bounds-check failed: %zu + %zu (start + count) > %zu (len)", loc}, - start, *count, len); + {"bounds-check failed: %zu + %zu (start + count) > %zu (len)", loc}, + start, *count, len); } crash_internal::crash( - "failed to print proper bounds check result? this is a bug. %zu %zu/%d " - "%zu/%d %zu/%d %zu", - start, *end, end.has_value(), *including_end, including_end.has_value(), - *count, count.has_value(), len); + "failed to print proper bounds check result? this is a bug. %zu %zu/%d " + "%zu/%d %zu/%d %zu", + start, *end, end.has_value(), *including_end, including_end.has_value(), + *count, count.has_value(), len); } } // namespace best::bounds_internal diff --git a/best/iter/bounds.h b/best/iter/bounds.h index 0683295..4c96f35 100644 --- a/best/iter/bounds.h +++ b/best/iter/bounds.h @@ -197,17 +197,15 @@ struct int_range final { /// fine-grained control so this looks like syntax the user would ordinarily /// write. fmt.write('{'); - if (bounds.start != 0) { - fmt.format(".start = {}", bounds.start); - } + if (bounds.start != 0) { fmt.format(".start = {}", bounds.start); } if (bounds.end) { - if (bounds.start != 0) fmt.write(", "); + if (bounds.start != 0) { fmt.write(", "); } fmt.format(".end = {}", *bounds.end); } else if (bounds.including_end) { - if (bounds.start != 0) fmt.write(", "); + if (bounds.start != 0) { fmt.write(", "); } fmt.format(".including_end = {}", *bounds.including_end); } else if (bounds.count) { - if (bounds.start != 0) fmt.write(", "); + if (bounds.start != 0) { fmt.write(", "); } fmt.format(".count = {}", *bounds.count); } fmt.write('}'); @@ -232,7 +230,7 @@ struct int_range final { return ret; } - if (count_ == 0) return {}; + if (count_ == 0) { return {}; } count_ = (best::overflow(count_) - 1).wrap(); auto ret = start_; @@ -259,7 +257,7 @@ struct int_range final { constexpr int_range normalize(opt_t max_size) const; static constexpr Int Max = - std::make_unsigned_t(-1) >> std::is_signed_v; + std::make_unsigned_t(-1) >> std::is_signed_v; static constexpr Int Min = ~Max; }; template @@ -282,16 +280,16 @@ constexpr auto int_range::iter() const { if (start == Min && ((including_end && *including_end == Max) || (!including_end && !end && !count))) { return best::iter( - iter_impl{.start_ = Min, .count_ = Int(-1), .plus_one_ = true}); + iter_impl{.start_ = Min, .count_ = Int(-1), .plus_one_ = true}); } return best::iter( - iter_impl{.start_ = start, .count_ = *normalize(Max).count}); + iter_impl{.start_ = start, .count_ = *normalize(Max).count}); } template constexpr int_range int_range::wrapping_normalize( - Int max_size) const { + Int max_size) const { if (end) { return {.start = start, .count = (best::overflow(*end) - start).wrap()}; } @@ -303,32 +301,24 @@ constexpr int_range int_range::wrapping_normalize( .count = (best::overflow(*including_end) - start + 1).wrap()}; } - if (count) { - return {.start = start, .count = count}; - } + if (count) { return {.start = start, .count = count}; } return {.start = start, .count = (best::overflow(max_size) - start).wrap()}; } template constexpr int_range int_range::normalize(opt_t max_size) const { - if (max_size && start > *max_size) { - return {.start = start}; - } + if (max_size && start > *max_size) { return {.start = start}; } if (end) { - if (start > *end) { - return {.start = start}; - } + if (start > *end) { return {.start = start}; } return {.start = start, .count = (best::overflow(*end) - start).wrap()}; } if (including_end) { // NOTE: in the case that start = max and including_end = max, this // correctly produces a one-element range that yields `max`. - if (start > *including_end) { - return {.start = start}; - } + if (start > *including_end) { return {.start = start}; } return {.start = start, .count = (best::overflow(*including_end) - start + 1).wrap()}; } @@ -340,9 +330,7 @@ constexpr int_range int_range::normalize(opt_t max_size) const { return {.start = start, .count = count}; } - if (!max_size) { - return {.start = start, .count = Max - start}; - } + if (!max_size) { return {.start = start, .count = Max - start}; } return {.start = start, .count = (best::overflow(*max_size) - start).wrap()}; } @@ -352,26 +340,21 @@ constexpr size_t int_range::compute_count(size_t max_size, best::location loc) const requires std::is_same_v { - if (auto result = try_compute_count(max_size)) { - return *result; - } + if (auto result = try_compute_count(max_size)) { return *result; } bounds_internal::crash(*this, max_size, loc); } template constexpr int_range::opt_t int_range::try_compute_count( - opt_t max_size) const - requires std::is_same_v + opt_t max_size) const requires std::is_same_v { - if (!max_size && !end && !including_end && !count) { - return {}; - } + if (!max_size && !end && !including_end && !count) { return {}; } auto count = normalize(max_size).count; - if (!max_size) return count; + if (!max_size) { return count; } auto [end, of] = (best::overflow(start) + *count); - if (of || end > *max_size) return {}; + if (of || end > *max_size) { return {}; } return count; } diff --git a/best/iter/bounds_test.cc b/best/iter/bounds_test.cc index 74c5de2..1f4219c 100644 --- a/best/iter/bounds_test.cc +++ b/best/iter/bounds_test.cc @@ -97,8 +97,8 @@ best::test Iter = [](auto& t) { t.expect_eq(b.iter().last(), 11); b = { - .start = best::max_of - 1, - .including_end = best::max_of, + .start = best::max_of - 1, + .including_end = best::max_of, }; t.expect_eq(best::vec(b.iter()), {best::max_of - 1, best::max_of}); diff --git a/best/iter/iter.h b/best/iter/iter.h index e145adf..f8b8397 100644 --- a/best/iter/iter.h +++ b/best/iter/iter.h @@ -159,7 +159,7 @@ class iter final { constexpr /*default*/ size_t count() && { BEST_ITER_FWD(count); size_t total = 0; - while (next().has_value()) ++total; + while (next().has_value()) { ++total; } return total; } @@ -170,9 +170,7 @@ class iter final { BEST_ITER_FWD(last); best::option last; - while (auto v = next()) { - last = BEST_MOVE(v); - } + while (auto v = next()) { last = BEST_MOVE(v); } return last; } @@ -334,7 +332,7 @@ class map final { private: constexpr explicit map(best::iter iter, Cb cb) - : iter_(BEST_MOVE(iter)), cb_(BEST_FWD(cb)) {} + : iter_(BEST_MOVE(iter)), cb_(BEST_FWD(cb)) {} friend best::iter; [[no_unique_address]] iter iter_; @@ -352,22 +350,22 @@ constexpr auto iter::map(best::callable auto&& cb) && { template constexpr auto iter::inspect(best::callable auto&& cb) && { return best::iter(iter_internal::map{ - std::move(*this), - [cb = BEST_FWD(cb)](auto&& value) -> decltype(auto) { - best::call(cb); - return BEST_FWD(value); - }, + std::move(*this), + [cb = BEST_FWD(cb)](auto&& value) -> decltype(auto) { + best::call(cb); + return BEST_FWD(value); + }, }); } template constexpr auto iter::inspect(best::callable auto&& cb) && { return best::iter(iter_internal::map{ - std::move(*this), - [cb = BEST_FWD(cb)](auto&& value) -> decltype(auto) { - best::call(cb, value); - return item(value); - }, + std::move(*this), + [cb = BEST_FWD(cb)](auto&& value) -> decltype(auto) { + best::call(cb, value); + return item(value); + }, }); } @@ -386,7 +384,7 @@ class enumerate final { private: constexpr explicit enumerate(best::iter iter) - : iter_(BEST_MOVE(iter)) {} + : iter_(BEST_MOVE(iter)) {} friend best::iter; [[no_unique_address]] iter iter_; @@ -406,7 +404,7 @@ template class take final { public: constexpr best::option::item> next() { - if (count_ == 0) return best::none; + if (count_ == 0) { return best::none; } --count_; return iter_.next(); } @@ -418,7 +416,7 @@ class take final { private: constexpr explicit take(best::iter iter, size_t n) - : iter_(BEST_MOVE(iter)), count_(n) {} + : iter_(BEST_MOVE(iter)), count_(n) {} friend best::iter; [[no_unique_address]] iter iter_; diff --git a/best/iter/iter_test.cc b/best/iter/iter_test.cc index ba8d826..af8903f 100644 --- a/best/iter/iter_test.cc +++ b/best/iter/iter_test.cc @@ -25,9 +25,9 @@ namespace best::iter_test { best::test Map = [](auto& t) { best::vec ints = best::bounds{.start = 5, .count = 7} - .iter() - .map([](int x) { return x * x; }) - .collect(); + .iter() + .map([](int x) { return x * x; }) + .collect(); t.expect_eq(ints, {25, 36, 49, 64, 81, 100, 121}); }; @@ -36,9 +36,9 @@ best::test Count = [](auto& t) { size_t calls = 0; t.expect_eq(best::bounds{.start = 5, .count = 7} - .iter() - .inspect([&] { ++calls; }) - .count(), + .iter() + .inspect([&] { ++calls; }) + .count(), 7); t.expect_eq(calls, 7); }; diff --git a/best/log/internal/crash.cc b/best/log/internal/crash.cc index c680a20..80ff887 100644 --- a/best/log/internal/crash.cc +++ b/best/log/internal/crash.cc @@ -36,9 +36,9 @@ inline constexpr char Red[] = "\N{ESCAPE}[31m"; } // namespace [[noreturn]] void die( - best::location loc, - best::fnref)> - write_message) { + best::location loc, + best::fnref)> + write_message) { // TODO: Make this function non-reentrant. static char buf[512]; diff --git a/best/log/internal/crash.h b/best/log/internal/crash.h index 45208db..409e6d3 100644 --- a/best/log/internal/crash.h +++ b/best/log/internal/crash.h @@ -28,9 +28,9 @@ namespace best::crash_internal { /// Implementation of `best::wtf()`. [[noreturn]] void die( - best::location loc, - best::fnref)> - write_message); + best::location loc, + best::fnref)> + write_message); /// Internal shim for crashing without depending on the logging headers. [[noreturn]] void crash(best::track_location fmt, ...); diff --git a/best/log/location.h b/best/log/location.h index ad7d262..99cf4bc 100644 --- a/best/log/location.h +++ b/best/log/location.h @@ -48,33 +48,31 @@ class track_location { /// Constructs a new location referring to the current context from /// best::here. constexpr track_location( - here_t, std::source_location loc = std::source_location::current()) + here_t, std::source_location loc = std::source_location::current()) requires best::is_void - : value_{}, impl_(loc) {} + : value_{}, impl_(loc) {} template constexpr track_location( - Arg&& arg, std::source_location loc = std::source_location::current()) + Arg&& arg, std::source_location loc = std::source_location::current()) requires std::convertible_to - : value_(BEST_FWD(arg)), impl_(loc) {} + : value_(BEST_FWD(arg)), impl_(loc) {} /// Constructs a new location from the given location. template constexpr track_location(best::track_location loc) requires best::is_void - : value_{}, impl_(loc.impl_) {} + : value_{}, impl_(loc.impl_) {} template constexpr track_location(Arg&& arg, best::track_location loc) requires std::convertible_to - : value_(BEST_FWD(arg)), impl_(loc.impl_) {} + : value_(BEST_FWD(arg)), impl_(loc.impl_) {} /// Returns the file this location refers to. template constexpr best::text file() const { - if (impl_.file_name() == nullptr) { - return ""; - } + if (impl_.file_name() == nullptr) { return ""; } return *best::text::from_nul(impl_.file_name()); } @@ -83,9 +81,7 @@ class track_location { // appear extremely early. typename utf8 = best::utf8> constexpr best::text func() const { - if (impl_.function_name() == nullptr) { - return ""; - } + if (impl_.function_name() == nullptr) { return ""; } return *best::text::from_nul(impl_.function_name()); } @@ -99,23 +95,20 @@ class track_location { constexpr track_location location() const { return *this; } // This makes best::track_location into a smart pointer. - constexpr best::as_ref operator*() const - requires(!best::is_void) + constexpr best::as_ref operator*() const requires (!best::is_void) { return value_; } - constexpr best::as_ref operator*() - requires(!best::is_void) + constexpr best::as_ref operator*() requires (!best::is_void) { return value_; } - constexpr best::as_ptr operator->() const& - requires(!best::is_void) + constexpr best::as_ptr operator->() const& requires ( + !best::is_void) { return best::addr(value_); } - constexpr best::as_ptr operator->() & - requires(!best::is_void) + constexpr best::as_ptr operator->() & requires (!best::is_void) { return best::addr(value_); } diff --git a/best/log/wtf.h b/best/log/wtf.h index 8d5283e..89e3c76 100644 --- a/best/log/wtf.h +++ b/best/log/wtf.h @@ -41,9 +41,7 @@ template const Args&... args) { best::strbuf message = best::format(templ, args...); auto write = [&](char* scratch, size_t scratch_len, auto write) { - if (message.is_empty()) { - message.push("explicit call to best::wtf()"); - } + if (message.is_empty()) { message.push("explicit call to best::wtf()"); } write(message.data(), message.size()); }; diff --git a/best/math/bit.h b/best/math/bit.h index eba4cff..fd2b9f0 100644 --- a/best/math/bit.h +++ b/best/math/bit.h @@ -87,7 +87,7 @@ BEST_INLINE_ALWAYS constexpr uint32_t trailing_ones(integer auto x) { template BEST_INLINE_ALWAYS constexpr Int shift_left(Int x, uint32_t shamt) { auto mask = bits_of - 1; - if (shamt != (shamt & mask)) return 0; + if (shamt != (shamt & mask)) { return 0; } return best::to_unsigned(x) << shamt; } @@ -103,7 +103,7 @@ BEST_INLINE_ALWAYS constexpr Int shift_left(Int x, uint32_t shamt) { template BEST_INLINE_ALWAYS constexpr Int shift_right(Int x, uint32_t shamt) { auto mask = bits_of - 1; - if (shamt != (shamt & mask)) return 0; + if (shamt != (shamt & mask)) { return 0; } return best::to_unsigned(x) >> shamt; } @@ -119,7 +119,7 @@ BEST_INLINE_ALWAYS constexpr Int shift_right(Int x, uint32_t shamt) { template BEST_INLINE_ALWAYS constexpr Int shift_sign(Int x, uint32_t shamt) { auto mask = bits_of - 1; - if (shamt != (shamt & mask)) return -(x < 0); + if (shamt != (shamt & mask)) { return -(x < 0); } return best::to_signed(x) >> shamt; } @@ -153,7 +153,7 @@ BEST_INLINE_ALWAYS constexpr bool is_pow2(unsigned_int auto x) { /// Unlike `best::next_pow2()`, this function cannot overflow. template BEST_INLINE_ALWAYS constexpr Int next_pow2_minus1(Int x) { - if (x == 0) return 0; + if (x == 0) { return 0; } return best::shift_right(best::max_of, best::leading_zeros(x)); } @@ -187,7 +187,7 @@ BEST_INLINE_ALWAYS constexpr Int next_pow2(Int x) { /// inclusive. `best::bits_for(0) == 0`. template BEST_INLINE_ALWAYS constexpr uint32_t bits_for(Int x) { - if (x == 0) return 0; + if (x == 0) { return 0; } return best::trailing_zeros(best::wrapping_next_pow2(x)); } } // namespace best diff --git a/best/math/conv.h b/best/math/conv.h index 14fdb98..5fe1f05 100644 --- a/best/math/conv.h +++ b/best/math/conv.h @@ -50,7 +50,7 @@ constexpr best::result atoi(const string_type auto &str, /// on a prefix, such as `0x`, `0b`, `0o`, or `0`. template constexpr best::result atoi_with_prefix( - const string_type auto &str); + const string_type auto &str); /// # `best::atoi_with_sign()` /// @@ -58,7 +58,7 @@ constexpr best::result atoi_with_prefix( /// argument. template constexpr best::result atoi_with_sign( - const string_type auto &str, bool is_negative, uint32_t radix) { + const string_type auto &str, bool is_negative, uint32_t radix) { if constexpr (best::is_pretext) { // Adapted slightly from the implementation found in Rust's // from_str_radix(). @@ -70,7 +70,7 @@ constexpr best::result atoi_with_sign( auto runes = str.runes(); auto next = runes.next(); - if (!next) return best::atoi_error{}; + if (!next) { return best::atoi_error{}; } // We make an approximation that the number of digits provided by `str` is // no larger than its length in code units. The greatest information density @@ -80,9 +80,9 @@ constexpr best::result atoi_with_sign( // e.g. `80` will overflow `int8_t`. size_t total_codes = best::size(str); size_t maximum_codes_without_overflow = - sizeof(Int) * 2 - best::signed_int; + sizeof(Int) * 2 - best::signed_int; size_t cannot_overflow = - radix <= 16 && total_codes <= maximum_codes_without_overflow; + radix <= 16 && total_codes <= maximum_codes_without_overflow; #define BEST_ATOI_LOOP_(result_, op_) \ do { \ @@ -136,7 +136,7 @@ constexpr best::result atoi(const string_type auto &str_, auto str = str_; bool neg = str.consume_prefix('-'); - if (!neg) str.consume_prefix('+'); + if (!neg) { str.consume_prefix('+'); } return best::atoi_with_sign(str, neg, radix); } else { @@ -146,13 +146,13 @@ constexpr best::result atoi(const string_type auto &str_, template constexpr best::result atoi_with_prefix( - const string_type auto &str_) { + const string_type auto &str_) { if constexpr (best::is_pretext) { auto str = str_; bool neg = str.consume_prefix('-'); - if (!neg) str.consume_prefix('+'); + if (!neg) { str.consume_prefix('+'); } - if (str == "0") return 0; + if (str == "0") { return 0; } int radix = 10; if (str.consume_prefix("0x")) { diff --git a/best/math/int.h b/best/math/int.h index a36cd14..997fff4 100644 --- a/best/math/int.h +++ b/best/math/int.h @@ -112,9 +112,9 @@ BEST_INLINE_ALWAYS constexpr std::strong_ordering int_cmp(integer auto x, /// The maximum value for a particular integer type. template inline constexpr Int max_of = - // If Int is unsigned, this is 0x11...11. - // If Int is signed, this is 0x01...11. - to_unsigned(-1) >> signed_int; + // If Int is unsigned, this is 0x11...11. + // If Int is signed, this is 0x01...11. + to_unsigned(-1) >> signed_int; /// # `best::min_of` /// @@ -137,7 +137,7 @@ BEST_INLINE_ALWAYS constexpr bool int_fits(integer auto x) { /// not be exact. template BEST_INLINE_ALWAYS constexpr best::option checked_cast(integer auto x) { - if (!best::int_fits(x)) return {}; + if (!best::int_fits(x)) { return {}; } return x; } @@ -155,7 +155,7 @@ using common_int = decltype(best::int_internal::common()); /// Computes the minimum from a collection of signed or unsigned integers. template BEST_INLINE_ALWAYS constexpr best::common_int min(Ints... args) - requires(sizeof...(args) > 0) + requires (sizeof...(args) > 0) { BEST_PUSH_GCC_DIAGNOSTIC() BEST_IGNORE_GCC_DIAGNOSTIC("-Wunused-value") @@ -166,7 +166,7 @@ BEST_INLINE_ALWAYS constexpr best::common_int min(Ints... args) } template BEST_INLINE_ALWAYS constexpr best::common_int min(Ints... args) - requires(sizeof...(args) > 0) + requires (sizeof...(args) > 0) { BEST_PUSH_GCC_DIAGNOSTIC() BEST_IGNORE_GCC_DIAGNOSTIC("-Wunused-value") @@ -181,7 +181,7 @@ BEST_INLINE_ALWAYS constexpr best::common_int min(Ints... args) /// Computes the maximum from a collection of signed or unsigned integers. template BEST_INLINE_ALWAYS constexpr best::common_int max(Ints... args) - requires(sizeof...(args) > 0) + requires (sizeof...(args) > 0) { BEST_PUSH_GCC_DIAGNOSTIC() BEST_IGNORE_GCC_DIAGNOSTIC("-Wunused-value") @@ -192,7 +192,7 @@ BEST_INLINE_ALWAYS constexpr best::common_int max(Ints... args) } template BEST_INLINE_ALWAYS constexpr best::common_int max(Ints... args) - requires(sizeof...(args) > 0) + requires (sizeof...(args) > 0) { BEST_PUSH_GCC_DIAGNOSTIC() BEST_IGNORE_GCC_DIAGNOSTIC("-Wunused-value") @@ -207,12 +207,13 @@ BEST_INLINE_ALWAYS constexpr best::common_int max(Ints... args) /// Computes the smallest unsigned integer type that can represent `n`. template using smallest_uint_t = best::select< // - best::int_fits(n), uint8_t, + best::int_fits(n), uint8_t, + best::select< // + best::int_fits(n), uint16_t, best::select< // - best::int_fits(n), uint16_t, - best::select< // - best::int_fits(n), uint32_t, // - uint64_t>>>; + best::int_fits(n), + uint32_t, // + uint64_t>>>; } // namespace best #endif // BEST_MATH_INT_H_ diff --git a/best/math/int_test.cc b/best/math/int_test.cc index 8e09219..7fe4317 100644 --- a/best/math/int_test.cc +++ b/best/math/int_test.cc @@ -59,7 +59,7 @@ static_assert(best::bits_of == 64); static_assert(best::same, long>); static_assert( - best::same, unsigned long>); + best::same, unsigned long>); static_assert(best::unsigned_cmp(-1, 1) > 0); static_assert(best::signed_cmp(~0u, 1) < 0); diff --git a/best/math/internal/common_int.h b/best/math/internal/common_int.h index 338ba9d..e407302 100644 --- a/best/math/internal/common_int.h +++ b/best/math/internal/common_int.h @@ -28,18 +28,18 @@ namespace best::int_internal { template -concept is_int = // - std::is_same_v || // - std::is_same_v || // - std::is_same_v || // - std::is_same_v || // - std::is_same_v || // - std::is_same_v || // - std::is_same_v || // - std::is_same_v || // - std::is_same_v || // - std::is_same_v || // - std::is_same_v; +concept is_int = // + std::is_same_v || // + std::is_same_v || // + std::is_same_v || // + std::is_same_v || // + std::is_same_v || // + std::is_same_v || // + std::is_same_v || // + std::is_same_v || // + std::is_same_v || // + std::is_same_v || // + std::is_same_v; template constexpr auto widest() { diff --git a/best/math/overflow.h b/best/math/overflow.h index 15e7d23..7e832ea 100644 --- a/best/math/overflow.h +++ b/best/math/overflow.h @@ -49,7 +49,7 @@ struct div_t { /// zero. template BEST_INLINE_ALWAYS constexpr best::div_t> div( - overflow a, overflow b, best::location loc = best::here) { + overflow a, overflow b, best::location loc = best::here) { if (best::unlikely(b.value == 0)) { best::crash_internal::crash({"division by zero", loc}); } @@ -66,7 +66,7 @@ BEST_INLINE_ALWAYS constexpr best::div_t> div( } template BEST_INLINE_ALWAYS constexpr best::div_t> div( - A a, B b, best::location loc = best::here) { + A a, B b, best::location loc = best::here) { return best::div(overflow(a), overflow(b)); } @@ -75,7 +75,7 @@ BEST_INLINE_ALWAYS constexpr best::div_t> div( /// Performs division, but rounding towards infinity. template BEST_INLINE_ALWAYS constexpr overflow> ceildiv( - overflow a, overflow b) { + overflow a, overflow b) { auto [q, r] = best::div(a, b); if ((r.value > 0 && b.value > 0) || (r.value < 0 && b.value < 0)) { return q + 1; @@ -178,7 +178,7 @@ struct overflow { /// /// Wraps an integer. constexpr overflow(Int value, bool overflowed = false) - : value(value), overflowed(overflowed) {} + : value(value), overflowed(overflowed) {} /// # `overflow::overflow(overflow)` /// @@ -186,10 +186,10 @@ struct overflow { /// is recorded appropriately. template constexpr overflow(overflow that) - : value(that.value), - overflowed(that.overflowed || - best::int_cmp(best::min_of, that.value) < 0 || - best::int_cmp(best::max_of, that.value) > 0) {} + : value(that.value), + overflowed(that.overflowed || + best::int_cmp(best::min_of, that.value) < 0 || + best::int_cmp(best::max_of, that.value) > 0) {} /// # `overflow::wrap()` /// @@ -200,7 +200,7 @@ struct overflow { /// /// Returns the result, but only if it did not overflow. BEST_INLINE_ALWAYS constexpr best::option checked() const { - if (overflowed) return {}; + if (overflowed) { return {}; } return value; } @@ -208,7 +208,7 @@ struct overflow { /// /// Returns the result, but crashes if overflow occurred. BEST_INLINE_ALWAYS constexpr Int strict( - best::location loc = best::here) const { + best::location loc = best::here) const { if (best::unlikely(overflowed)) { best::crash_internal::crash({"arithmetic overflow", loc}); } @@ -220,7 +220,7 @@ struct overflow { } template BEST_INLINE_ALWAYS constexpr friend overflow> operator+( - overflow a, overflow b) { + overflow a, overflow b) { using C = common_int; C ca = a.value, cb = b.value; @@ -239,7 +239,7 @@ struct overflow { } template BEST_INLINE_ALWAYS constexpr friend overflow> operator-( - overflow a, overflow b) { + overflow a, overflow b) { using C = common_int; C ca = a.value, cb = b.value; @@ -250,7 +250,7 @@ struct overflow { template BEST_INLINE_ALWAYS constexpr friend overflow> operator*( - overflow a, overflow b) { + overflow a, overflow b) { using C = common_int; C ca = a.value, cb = b.value; @@ -261,12 +261,12 @@ struct overflow { template BEST_INLINE_ALWAYS constexpr friend overflow> operator/( - best::track_location a, overflow b) { + best::track_location a, overflow b) { return best::div(*a, b, a).quot; } template BEST_INLINE_ALWAYS constexpr friend overflow> operator%( - best::track_location a, overflow b) { + best::track_location a, overflow b) { return best::div(*a, b, a).rem; } @@ -276,7 +276,7 @@ struct overflow { template BEST_INLINE_ALWAYS constexpr friend overflow> operator&( - overflow a, overflow b) { + overflow a, overflow b) { using C = common_int; C ca = a.value, cb = b.value; @@ -284,7 +284,7 @@ struct overflow { } template BEST_INLINE_ALWAYS constexpr friend overflow> operator|( - overflow a, overflow b) { + overflow a, overflow b) { using C = common_int; C ca = a.value, cb = b.value; @@ -292,7 +292,7 @@ struct overflow { } template BEST_INLINE_ALWAYS constexpr friend overflow> operator^( - overflow a, overflow b) { + overflow a, overflow b) { using C = common_int; C ca = a.value, cb = b.value; @@ -324,11 +324,11 @@ struct overflow { \ template \ BEST_INLINE_ALWAYS constexpr friend overflow& operator opeq_( \ - overflow & a, overflow b) { \ + overflow & a, overflow b) { \ return a = a op_ b; \ } \ BEST_INLINE_ALWAYS constexpr friend overflow& operator opeq_( \ - overflow & a, integer auto b) { \ + overflow & a, integer auto b) { \ return a = a op_ b; \ } diff --git a/best/memory/allocator.cc b/best/memory/allocator.cc index 46a5a71..477badd 100644 --- a/best/memory/allocator.cc +++ b/best/memory/allocator.cc @@ -39,20 +39,18 @@ constexpr bool UseCookies = best::is_debug(); best::layout embiggen(best::layout layout) { size_t new_align = best::max(layout.align(), best::align_of); size_t grow_by = best::size_of; - if (grow_by < new_align) { - grow_by = new_align; - } + if (grow_by < new_align) { grow_by = new_align; } return best::layout( - unsafe("the alignment is a power of two, because it is either `layout`'s " - "alignment or that of `best::layout`"), - layout.size() + grow_by, new_align); + unsafe("the alignment is a power of two, because it is either `layout`'s " + "alignment or that of `best::layout`"), + layout.size() + grow_by, new_align); } // Given a pointer to free and its supposed layout, verifies the layout, and // returns the *actual* pointer to pass to free(). void check_layout(void*& p, best::layout& layout) { - if (!UseCookies) return; + if (!UseCookies) { return; } auto actual = embiggen(layout); p = static_cast(p) - (actual.size() - layout.size()); @@ -61,9 +59,9 @@ void check_layout(void*& p, best::layout& layout) { std::memcpy(&cookie, p, sizeof(cookie)); if (cookie.size() != layout.size() || cookie.align() != layout.align()) { best::crash_internal::crash( - "attempted to free allocation with layout %zu:%zu, but it was actually " - "allocated with %zu:%zu", - layout.size(), layout.align(), cookie.size(), cookie.align()); + "attempted to free allocation with layout %zu:%zu, but it was actually " + "allocated with %zu:%zu", + layout.size(), layout.align(), cookie.size(), cookie.align()); } layout = actual; @@ -77,7 +75,7 @@ void check_addr(void* p) { } if (reinterpret_cast(p) < 0x1000) { best::crash_internal::crash( - "attempted to de/reallocate a dangling pointer"); + "attempted to de/reallocate a dangling pointer"); } } } // namespace @@ -94,8 +92,8 @@ void* malloc::alloc(best::layout layout) { if (best::unlikely(p == nullptr)) { best::crash_internal::crash( - "malloc() returned a null pointer on layout %zu:%zu", layout.size(), - layout.align()); + "malloc() returned a null pointer on layout %zu:%zu", layout.size(), + layout.align()); } if (UseCookies) { @@ -103,9 +101,7 @@ void* malloc::alloc(best::layout layout) { p = static_cast(p) + (actual.size() - layout.size()); } - if (best::is_debug()) { - std::memset(p, 0xcd, layout.size()); - } + if (best::is_debug()) { std::memset(p, 0xcd, layout.size()); } return p; } @@ -116,8 +112,8 @@ void* malloc::zalloc(best::layout layout) { void* p = ::calloc(actual.size(), 1); if (best::unlikely(p == nullptr)) { best::crash_internal::crash( - "calloc() returned a null pointer on layout %zu:%zu", layout.size(), - layout.align()); + "calloc() returned a null pointer on layout %zu:%zu", layout.size(), + layout.align()); } if (UseCookies) { std::memcpy(p, &layout, sizeof(layout)); @@ -140,8 +136,8 @@ void* malloc::realloc(void* ptr, best::layout old, best::layout layout) { void* p = ::realloc(ptr, actual.size()); if (best::unlikely(p == nullptr)) { best::crash_internal::crash( - "realloc() returned a null pointer on layout %zu:%zu -> %zu:%zu", - old.size(), old.align(), layout.size(), layout.align()); + "realloc() returned a null pointer on layout %zu:%zu -> %zu:%zu", + old.size(), old.align(), layout.size(), layout.align()); } if (UseCookies) { std::memcpy(p, &layout, sizeof(layout)); diff --git a/best/memory/allocator.h b/best/memory/allocator.h index 7092db6..c36c117 100644 --- a/best/memory/allocator.h +++ b/best/memory/allocator.h @@ -45,39 +45,39 @@ namespace best { /// other. template concept allocator = // - best::moveable && best::equatable && - requires(A& alloc, best::layout layout, void* ptr) { - /// # `allocator::alloc(layout)` - /// - /// Allocates fresh memory. Returns a non-null pointer to it. - /// Crashes on allocation failure. - { alloc.alloc(layout) } -> std::same_as; - - /// # `allocator::zalloc(layout)` - /// - /// Allocates fresh zeroed memory. Returns a non-null pointer to it. - /// Crashes on allocation failure. - { alloc.zalloc(layout) } -> std::same_as; - - /// # `allocator::realloc(ptr, old, new)` - /// - /// Resizes memory previously allocated with this allocator. - /// Returns a non-null pointer to it. - /// - /// The second argument is the original layout it was allocated with, the - /// third is the desired layout. - /// Crashes on allocation failure. - { alloc.realloc(ptr, layout, layout) } -> std::same_as; - - /// # `allocator::dealloc(ptr, layout)` - /// - /// Deallocates memory previously allocated with this allocator. - /// Returns a non-null pointer to it. - /// - /// The second argument is the original layout it was allocated with. - /// Crashes on allocation failure. - { alloc.dealloc(ptr, layout) }; - }; + best::moveable && best::equatable && + requires(A& alloc, best::layout layout, void* ptr) { + /// # `allocator::alloc(layout)` + /// + /// Allocates fresh memory. Returns a non-null pointer to it. + /// Crashes on allocation failure. + { alloc.alloc(layout) } -> std::same_as; + + /// # `allocator::zalloc(layout)` + /// + /// Allocates fresh zeroed memory. Returns a non-null pointer to it. + /// Crashes on allocation failure. + { alloc.zalloc(layout) } -> std::same_as; + + /// # `allocator::realloc(ptr, old, new)` + /// + /// Resizes memory previously allocated with this allocator. + /// Returns a non-null pointer to it. + /// + /// The second argument is the original layout it was allocated with, the + /// third is the desired layout. + /// Crashes on allocation failure. + { alloc.realloc(ptr, layout, layout) } -> std::same_as; + + /// # `allocator::dealloc(ptr, layout)` + /// + /// Deallocates memory previously allocated with this allocator. + /// Returns a non-null pointer to it. + /// + /// The second argument is the original layout it was allocated with. + /// Crashes on allocation failure. + { alloc.dealloc(ptr, layout) }; + }; /// # `best::malloc` /// diff --git a/best/memory/internal/bytes.h b/best/memory/internal/bytes.h index 7c2aa9a..175fcbe 100644 --- a/best/memory/internal/bytes.h +++ b/best/memory/internal/bytes.h @@ -52,35 +52,33 @@ template struct tag {}; template -void can_memcmp(tag, tag) - requires requires { - requires sizeof(T) == sizeof(U); +void can_memcmp(tag, tag) requires requires { + requires sizeof(T) == sizeof(U); #if BEST_HAS_BUILTIN(__is_trivially_equality_comparable) - requires __is_trivially_equality_comparable(T); - requires __is_trivially_equality_comparable(U); + requires __is_trivially_equality_comparable(T); + requires __is_trivially_equality_comparable(U); #else - requires std::is_integral_v; - requires std::is_integral_v; + requires std::is_integral_v; + requires std::is_integral_v; #endif - }; +}; template -void can_memcmp(tag, tag) - requires requires { - // Function pointer equality is all kinds of messed up. - - requires !best::is_func && !best::is_func; - // libc++ asserts there's something weird about virtual bases. - // This is not perfect detection for that case but allows a - // lot more "reasonable" code than libc++'s constraint. - // - // See: - // https://github.com/llvm/llvm-project/blob/ecf2a53407f517a261ee296e1f922c647a13a503/libcxx/include/__type_traits/is_equality_comparable.h#L41 - requires !std::is_polymorphic_v && !std::is_polymorphic_v; - - // Some platforms are just weird, man. - requires sizeof(T*) == sizeof(U*); - }; +void can_memcmp(tag, tag) requires requires { + // Function pointer equality is all kinds of messed up. + + requires !best::is_func && !best::is_func; + // libc++ asserts there's something weird about virtual bases. + // This is not perfect detection for that case but allows a + // lot more "reasonable" code than libc++'s constraint. + // + // See: + // https://github.com/llvm/llvm-project/blob/ecf2a53407f517a261ee296e1f922c647a13a503/libcxx/include/__type_traits/is_equality_comparable.h#L41 + requires !std::is_polymorphic_v && !std::is_polymorphic_v; + + // Some platforms are just weird, man. + requires sizeof(T*) == sizeof(U*); +}; template concept is_char = best::same, char> || @@ -109,11 +107,11 @@ concept constexpr_byte_comparable = requires { template BEST_INLINE_ALWAYS constexpr bool equate(best::span lhs, best::span rhs) { - if (lhs.size() != rhs.size()) return false; - if (lhs.is_empty()) return true; + if (lhs.size() != rhs.size()) { return false; } + if (lhs.is_empty()) { return true; } if (!std::is_constant_evaluated()) { - if (lhs.data() == rhs.data()) return true; + if (lhs.data() == rhs.data()) { return true; } } return 0 == BEST_memcmp_(lhs.data(), rhs.data(), lhs.size() * sizeof(T)); } @@ -121,54 +119,50 @@ BEST_INLINE_ALWAYS constexpr bool equate(best::span lhs, best::span rhs) { template BEST_INLINE_ALWAYS constexpr best::ord compare(best::span lhs, best::span rhs) { - if (lhs.is_empty() || rhs.is_empty()) return lhs.size() <=> rhs.size(); + if (lhs.is_empty() || rhs.is_empty()) { return lhs.size() <=> rhs.size(); } if (!std::is_constant_evaluated()) { - if (lhs.data() == rhs.data()) return lhs.size() <=> rhs.size(); + if (lhs.data() == rhs.data()) { return lhs.size() <=> rhs.size(); } } auto to_compare = best::min(lhs.size(), rhs.size()) * sizeof(T); int result = BEST_memcmp_(lhs.data(), rhs.data(), to_compare); - if (result == 0) { - return lhs.size() <=> rhs.size(); - } + if (result == 0) { return lhs.size() <=> rhs.size(); } return result <=> 0; } template -best::option constexpr search_byte(best::span haystack, +constexpr best::option search_byte(best::span haystack, best::span needle) { auto* found = - BEST_memchr_(haystack.data().raw(), *needle.data(), haystack.size()); - if (!found) return best::none; + BEST_memchr_(haystack.data().raw(), *needle.data(), haystack.size()); + if (!found) { return best::none; } return found - haystack.data(); } template -best::option constexpr search_memmem(best::span haystack, +constexpr best::option search_memmem(best::span haystack, best::span needle) { if constexpr (sizeof(U) == 1) { - if (needle.size() == 1) { - return search_byte(haystack, needle); - } + if (needle.size() == 1) { return search_byte(haystack, needle); } } auto* data = - reinterpret_cast(static_cast(haystack.data())); + reinterpret_cast(static_cast(haystack.data())); auto* start = data; size_t size = haystack.size() * sizeof(T); while (true) { void* found = bytes_internal::memmem(data, size, needle.data(), needle.size() * sizeof(T)); - if (!found) return best::none; + if (!found) { return best::none; } // Found a potential match. data = static_cast(found); size_t offset = data - start; // If the match is aligned to the stride of `T`, we're done. - if (offset % sizeof(T) == 0) return offset / sizeof(T); + if (offset % sizeof(T) == 0) { return offset / sizeof(T); } // Otherwise, this is a false positive that we found because memmem is // not a striding search. Skip to the next few bytes so that we begin @@ -182,18 +176,16 @@ best::option constexpr search_memmem(best::span haystack, template BEST_INLINE_ALWAYS constexpr best::option search_constexpr( - best::span haystack, best::span needle) { + best::span haystack, best::span needle) { if constexpr (sizeof(U) == 1) { - if (needle.size() == 1) { - return search_byte(haystack, needle); - } + if (needle.size() == 1) { return search_byte(haystack, needle); } } size_t hz = haystack.size(); size_t nz = needle.size(); - if (nz == 0) return 0; - if (hz < nz) return best::none; + if (nz == 0) { return 0; } + if (hz < nz) { return best::none; } T* hp = haystack.data().raw(); U* np = needle.data().raw(); @@ -202,16 +194,14 @@ BEST_INLINE_ALWAYS constexpr best::option search_constexpr( const T* end = hp + hz; while (true) { size_t len = end - hp; - if (len < nz) return best::none; + if (len < nz) { return best::none; } // Skip to the next possible candidate. hp = (T*)BEST_memchr_(hp, first, len); - if (hp == nullptr) return best::none; + if (hp == nullptr) { return best::none; } // Check if we actually found the string. - if (BEST_memcmp_(hp, np, nz) == 0) { - return hp - haystack.data().raw(); - } + if (BEST_memcmp_(hp, np, nz) == 0) { return hp - haystack.data().raw(); } ++hp; } } @@ -227,7 +217,7 @@ BEST_INLINE_ALWAYS constexpr best::option search(best::span haystack, return bytes_internal::search_constexpr(haystack, needle); } else { best::crash_internal::crash( - "cannot call best::search_bytes() in constexpr for this type"); + "cannot call best::search_bytes() in constexpr for this type"); } } diff --git a/best/memory/internal/layout.h b/best/memory/internal/layout.h index 50fc941..5b80c47 100644 --- a/best/memory/internal/layout.h +++ b/best/memory/internal/layout.h @@ -32,7 +32,7 @@ namespace best::layout_internal { template using to_object = best::devoid< - best::select || best::is_void, T, best::as_ptr>>; + best::select || best::is_void, T, best::as_ptr>>; /// Computes the alignment of a struct/union with the given member types. /// @@ -44,7 +44,7 @@ template inline constexpr size_t align_of = [] { size_t align = 1; best::types...>.each( - [&] { align = (align > alignof(T) ? align : alignof(T)); }); + [&] { align = (align > alignof(T) ? align : alignof(T)); }); return align; }(); @@ -54,15 +54,13 @@ inline constexpr size_t align_of = [] { /// a size of 1. template inline constexpr size_t size_of = [] { - if (sizeof...(Types) == 0) return size_t{1}; + if (sizeof...(Types) == 0) { return size_t{1}; } size_t size = 0, align = 1; auto align_to = [&size](size_t align) { auto remainder = size % align; - if (remainder != 0) { - size += align - remainder; - } + if (remainder != 0) { size += align - remainder; } }; best::types...>.each([&] { @@ -82,14 +80,12 @@ inline constexpr size_t size_of = [] { /// a size of 1. template inline constexpr size_t size_of_union = [] { - if (sizeof...(Types) == 0) return size_t{1}; + if (sizeof...(Types) == 0) { return size_t{1}; } size_t size = 0, align = 1; auto align_to = [&](size_t align) { auto remainder = size % align; - if (remainder != 0) { - size += align - remainder; - } + if (remainder != 0) { size += align - remainder; } }; best::types...>.each([&] { diff --git a/best/memory/layout.h b/best/memory/layout.h index 9db6339..fab5dea 100644 --- a/best/memory/layout.h +++ b/best/memory/layout.h @@ -66,7 +66,7 @@ class layout final { /// This must observe two critical requirements: `size % align == 0`, and /// `best::is_pow2(align)`. constexpr explicit layout(unsafe, size_t size, size_t align) - : size_(size), align_(align) {} + : size_(size), align_(align) {} /// # `layout::of()` /// @@ -75,7 +75,7 @@ class layout final { /// Technically, this is the layout for `best::object`, which coincides /// with that of `T` if `T` is an object type. template - constexpr static layout of() { + static constexpr layout of() { return layout(unsafe("manifest from calling size_of and align_of"), size_of, align_of); } @@ -87,11 +87,11 @@ class layout final { /// /// Crashes on overflow. template - constexpr static layout array(size_t n) { + static constexpr layout array(size_t n) { auto [sz, of] = best::overflow(size_of) * n; if (of || sz > best::max_of) { best::crash_internal::crash( - "attempted to allocate more than max_of/2 bytes"); + "attempted to allocate more than max_of/2 bytes"); } return layout(unsafe("manifest from the bounds check above and align_of"), @@ -105,7 +105,7 @@ class layout final { /// This implement the C/C++ standard layout class sizing algorithm. Zero /// types produces the layout of `char`. template - constexpr static layout of_struct() { + static constexpr layout of_struct() { return layout(unsafe("manifest from calling size_of and align_of"), layout_internal::size_of, layout_internal::align_of); @@ -119,7 +119,7 @@ class layout final { /// rounded to the alignment of the most-aligned type. Zero types produces /// the layout of `char`. template - constexpr static layout of_union() { + static constexpr layout of_union() { return layout(unsafe("manifest from calling size_of and align_of"), layout_internal::size_of_union, layout_internal::align_of); diff --git a/best/memory/ptr.h b/best/memory/ptr.h index 559afc7..a823e77 100644 --- a/best/memory/ptr.h +++ b/best/memory/ptr.h @@ -54,7 +54,7 @@ using pointee_for = best::select || best::is_void, T, /// Whether a `best::ptr` is implicitly convertible into a `best::ptr`. template concept ptr_convertible_to = - best::convertible*, best::pointee_for*>; + best::convertible*, best::pointee_for*>; /// # `best::ptr` /// @@ -121,11 +121,10 @@ class ptr final { /// necessary. template U> constexpr ptr(ptr ptr) - : BEST_PTR_( - // NOTE: This const cast won't cast away `const` if this constructor - // is called via implicit conversion, unless T is a void type. - const_cast::pointee>*>( - ptr.raw())) {} + : BEST_PTR_( + // NOTE: This const cast won't cast away `const` if this constructor + // is called via implicit conversion, unless T is a void type. + const_cast::pointee>*>(ptr.raw())) {} /// # `ptr::is_niche()` /// @@ -347,7 +346,7 @@ class ptr final { requires best::constructible> { copy_impl( - that, count); + that, count); } template BEST_INLINE_ALWAYS constexpr void move_assign_overlapping(ptr that, @@ -355,13 +354,13 @@ class ptr final { requires best::constructible> { copy_impl( - that, count); + that, count); } friend void BestFmt(auto& fmt, ptr value) { fmt.format("{:#x}", value.to_addr()); } - friend constexpr void BestFmtQuery(auto& query, ptr*) { + constexpr friend void BestFmtQuery(auto& query, ptr*) { query.requires_debug = false; query.uses_method = [](auto r) { return r == 'p'; }; } @@ -373,7 +372,7 @@ class ptr final { friend class vptr; BEST_INLINE_ALWAYS constexpr void check() const { - if (!best::is_debug() || std::is_constant_evaluated()) return; + if (!best::is_debug() || std::is_constant_evaluated()) { return; } if (*this == nullptr) { best::crash_internal::crash("dereferenced a null `best::ptr`"); } @@ -539,7 +538,7 @@ class vptr final { constexpr vptr(unsafe, U* ptr, const best::vtable* vt) : ptr_(ptr), vt_(vt) {} template U> constexpr vptr(unsafe, ptr ptr, const best::vtable* vt) - : ptr_(ptr), vt_(vt) {} + : ptr_(ptr), vt_(vt) {} /// # `vptr::raw()`, `vptr::thin()` /// @@ -598,8 +597,7 @@ class vptr final { /// /// Destroys the pointed to object in place. This will call the complete /// object destructor. - BEST_INLINE_SYNTHETIC void destroy() const - requires best::destructible; + BEST_INLINE_SYNTHETIC void destroy() const requires best::destructible; /// # `ptr::is_copyable()` /// @@ -617,7 +615,7 @@ class vptr final { friend void BestFmt(auto& fmt, vptr value) { fmt.format("{:#x}/{:#x}", value.to_addr(), value.vtable()); } - friend constexpr void BestFmtQuery(auto& query, vptr*) { + constexpr friend void BestFmtQuery(auto& query, vptr*) { query.requires_debug = false; query.uses_method = [](auto r) { return r == 'p'; }; } @@ -685,7 +683,7 @@ BEST_INLINE_SYNTHETIC constexpr ptr::ref ptr::operator*() const { } template BEST_INLINE_SYNTHETIC constexpr auto ptr::operator->() const - -> best::as_ptr { + -> best::as_ptr { check(); if constexpr (best::is_object || best::is_void) { return raw(); @@ -696,7 +694,7 @@ BEST_INLINE_SYNTHETIC constexpr auto ptr::operator->() const template constexpr ptr ptr::operator+(ptrdiff_t idx) const { - if (idx == 0) return *this; + if (idx == 0) { return *this; } check(); if constexpr (best::is_void) { return *this; @@ -743,8 +741,8 @@ constexpr ptr ptr::operator--(int) { template vptr vptr::operator+(ptrdiff_t idx) const { return { - thin().scaled_offset(idx, layout().size()), - vtable(), + thin().scaled_offset(idx, layout().size()), + vtable(), }; } template @@ -808,8 +806,7 @@ BEST_INLINE_SYNTHETIC constexpr void ptr::construct(Args&&... args) const template template BEST_INLINE_SYNTHETIC constexpr void ptr::construct( - best::args args) const - requires best::constructible + best::args args) const requires best::constructible { args.row.apply([&](auto&&... args) { construct(BEST_FWD(args)...); }); } @@ -846,8 +843,7 @@ BEST_INLINE_SYNTHETIC constexpr void ptr::assign(Args&&... args) const template template BEST_INLINE_SYNTHETIC constexpr void ptr::assign( - best::args args) const - requires best::assignable + best::args args) const requires best::assignable { args.row.apply([&](auto&&... args) { assign(BEST_FWD(args)...); }); } @@ -857,9 +853,7 @@ BEST_INLINE_SYNTHETIC constexpr void ptr::destroy() const requires best::destructible { check(); - if constexpr (best::is_object) { - raw()->~T(); - } + if constexpr (best::is_object) { raw()->~T(); } if (!std::is_constant_evaluated() && best::is_debug()) { BEST_PUSH_GCC_DIAGNOSTIC() BEST_IGNORE_GCC_DIAGNOSTIC("-Wdynamic-class-memaccess") @@ -883,7 +877,7 @@ template template ::how how, typename U> BEST_INLINE_SYNTHETIC constexpr void ptr::copy_impl(ptr that, size_t count) const { - if (count == 0) return; + if (count == 0) { return; } check(); that.check(); if constexpr (how.template can_memcpy()) { @@ -920,9 +914,7 @@ BEST_INLINE_SYNTHETIC constexpr void ptr::copy_impl(ptr that, (*this + i).construct(BEST_MOVE(that[i])); } - if constexpr (how.kind == how::Relo) { - (that + i).destroy(); - } + if constexpr (how.kind == how::Relo) { (that + i).destroy(); } } } @@ -932,7 +924,7 @@ BEST_INLINE_ALWAYS constexpr void ptr::relo_overlapping(ptr that, auto dst = *this; auto src = that; - if (dst == src) return; + if (dst == src) { return; } // We need to handle the following cases. // @@ -1006,7 +998,7 @@ template BEST_INLINE_SYNTHETIC void vptr::copy_to(void* to) const { if (!is_copyable()) { best::crash_internal::crash( - "attempted to copy non-copyable type through a vptr at %p", raw()); + "attempted to copy non-copyable type through a vptr at %p", raw()); } vtable()->copy_(to, raw()); } diff --git a/best/memory/span.h b/best/memory/span.h index 84b77d1..9dfe4b2 100644 --- a/best/memory/span.h +++ b/best/memory/span.h @@ -48,8 +48,7 @@ namespace best { /// # `best::data()` /// /// Returns the data pointer of a contiguous range. -constexpr auto data(auto&& range) - requires requires { range.data(); } +constexpr auto data(auto&& range) requires requires { range.data(); } { return BEST_FWD(range).data(); } @@ -65,8 +64,7 @@ constexpr const T* data(const std::initializer_list& il) { /// # `best::size()` /// /// Returns the size of a contiguous range. -constexpr size_t size(const auto& range) - requires requires { range.size(); } +constexpr size_t size(const auto& range) requires requires { range.size(); } { return range.size(); } @@ -108,7 +106,7 @@ using data_type = best::unref))>; /// applied to this type, or best::none. template inline constexpr best::option static_size = - BestStaticSize(best::types, best::as_ptr{}); + BestStaticSize(best::types, best::as_ptr{}); /// # `best::static_contiguous` /// @@ -121,7 +119,7 @@ concept static_contiguous = static_size.has_value(); /// Represents whether T is best::span for some U, n. template concept is_span = - best::same, best::span, static_size>>; + best::same, best::span, static_size>>; /// # `best::from_nul` /// @@ -142,7 +140,7 @@ constexpr best::span from_nul(T* ptr) { /// default turns out to be very annoying. template constexpr best::span, best::static_size> from_static( - R&& range) { + R&& range) { return {BEST_FWD(range)}; } @@ -227,8 +225,7 @@ class span final { /// Constructs a new empty span. /// /// This span's data pointer is always null. - constexpr span() - requires is_dynamic || (n == 0) + constexpr span() requires is_dynamic || (n == 0) = default; /// # `span::span(span)` @@ -246,7 +243,7 @@ class span final { /// If this span has a statically-known size, this constructor will crash if /// len is not that size. constexpr explicit(is_static) - span(best::ptr data, size_t size, best::location loc = best::here); + span(best::ptr data, size_t size, best::location loc = best::here); /// # `span::span(range)` /// @@ -257,12 +254,12 @@ class span final { /// this constructor can crash, it is explicit. template constexpr explicit(is_static && n != static_size) - span(R&& range, best::location loc = best::here) - requires best::qualifies_to>, T> && - (is_dynamic || // - best::static_size.is_empty() || // - best::static_size == best::static_size) - : span(best::data(range), best::size(range), loc) {} + span(R&& range, best::location loc = best::here) + requires best::qualifies_to>, T> && + (is_dynamic || // + best::static_size.is_empty() || // + best::static_size == best::static_size) + : span(best::data(range), best::size(range), loc) {} /// # `span::span{...}` /// @@ -272,10 +269,10 @@ class span final { /// /// If this would construct a fixed-size span, and the list being constructed /// from has a different length, this constructor will crash. - constexpr explicit(is_static) span(std::initializer_list il, - best::location loc = best::here) - requires is_const - : span(best::data(il), best::size(il), loc) {} + constexpr explicit(is_static) + span(std::initializer_list il, best::location loc = best::here) + requires is_const + : span(best::data(il), best::size(il), loc) {} /// # `span::from_nul()` /// @@ -286,12 +283,12 @@ class span final { /// /// If this is a fixed-with span, this will perform the usual fatal bounds /// check upon construction. - constexpr static span from_nul(T* data); + static constexpr span from_nul(T* data); private: template static constexpr best::option minus = - n ? best::option(best::saturating_sub(*n, m)) : best::none; + n ? best::option(best::saturating_sub(*n, m)) : best::none; public: /// # `span::data()` @@ -302,13 +299,11 @@ class span final { /// # `span::size()` /// /// Returns the size (length) of this span. - constexpr static size_t size() - requires is_static + static constexpr size_t size() requires is_static { return *n; } - constexpr size_t size() const - requires is_dynamic + constexpr size_t size() const requires is_dynamic { return size_; } @@ -339,34 +334,33 @@ class span final { /// Returns the first, or first `m`, elements of this span, and the remaining /// elements, or `best::none` if there are not enough elements. constexpr auto split_first() const - -> best::option>>>; + -> best::option>>>; template constexpr auto split_first(best::index_t = {}) const - -> best::option, span>>>; + -> best::option, span>>>; /// # `span::split_last()` /// /// Returns the last, or last `m`, elements of this span, and the remaining /// elements, or `best::none` if there are not enough elements. constexpr auto split_last() const - -> best::option>>>; + -> best::option>>>; template constexpr auto split_last(best::index_t = {}) const - -> best::option, span>>>; + -> best::option, span>>>; /// # `span::take_first()` /// /// Splits this span at `m`; returns the prefix, and updates this to be the /// rest. If `m > size()`, returns best::none and leaves this span untouched - constexpr best::option> take_first(size_t m) - requires is_dynamic; + constexpr best::option> take_first(size_t m) requires is_dynamic + ; /// # `span::take_last()` /// /// Splits this span at `m`; returns the suffix, and updates this to be the /// rest. If `m > size()`, returns best::none. - constexpr best::option> take_last(size_t m) - requires is_dynamic; + constexpr best::option> take_last(size_t m) requires is_dynamic; /// # `span[idx]` /// @@ -394,7 +388,7 @@ class span final { /// and `this->is_static`, produces a compile time error; otherwise crashes. template constexpr auto operator[](best::vlist) const - requires(range.try_compute_count(best::static_size).has_value()); + requires (range.try_compute_count(best::static_size).has_value()); /// # `span::at(idx)` /// @@ -439,13 +433,12 @@ class span final { /// /// Swaps the elements at indices `a` and `b`. constexpr void swap(size_t a, size_t b, best::location loc = best::here) const - requires(!is_const); + requires (!is_const); /// # `span::reverse()` /// /// Reverses the order of the elements in this span, in-place. - constexpr void reverse() const - requires(!is_const); + constexpr void reverse() const requires (!is_const); /// # `span::split_at()` /// @@ -472,7 +465,7 @@ class span final { constexpr best::option find(const R& needle) const requires best::equatable> && (!best::equatable); constexpr best::option find( - best::callable auto&& pred) const; + best::callable auto&& pred) const; /// # `span::contains()` /// @@ -494,13 +487,13 @@ class span final { /// A pattern for a separator may be as in `span::find()`. template U = T> constexpr best::option, span>> split_once( - const U& needle) const; + const U& needle) const; template > constexpr best::option, span>> split_once( - const R& needle) const + const R& needle) const requires best::equatable> && (!best::equatable); constexpr best::option, span>> split_once( - best::callable auto&& pred) const; + best::callable auto&& pred) const; /// # `span::split()`. /// @@ -541,21 +534,19 @@ class span final { /// returns the rest; otherwise returns `best::none`. template U = const T> constexpr best::option> strip_prefix( - best::span prefix) const; + best::span prefix) const; template U = const T> constexpr best::option> strip_suffix( - best::span suffix) const; + best::span suffix) const; /// # `span::consume_prefix()`, `span::consume_suffix()` /// /// Like `strip_prefix()`/`strip_suffix()`, but returns a bool and updates the /// span in-place. template U = const T> - constexpr bool consume_prefix(best::span prefix) - requires is_dynamic; + constexpr bool consume_prefix(best::span prefix) requires is_dynamic; template U = const T> - constexpr bool consume_suffix(best::span suffix) - requires is_dynamic; + constexpr bool consume_suffix(best::span suffix) requires is_dynamic; /// # `span::sort()` /// @@ -571,26 +562,25 @@ class span final { /// pull in a completely unacceptable amount of stuff, the implementations of /// these functions live in `//best/memory/span_sort.h`, which must be /// included separately. - constexpr void sort() const - requires best::comparable && (!is_const); + constexpr void sort() const requires best::comparable && (!is_const); constexpr void sort(best::callable auto&&) const - requires(!is_const); + requires (!is_const); constexpr void sort( - best::callable auto&&) const - requires(!is_const); + best::callable auto&&) const + requires (!is_const); /// # `span::stable_sort()` /// /// Identical to `sort()`, but uses a stable sort which guarantees that equal /// items are not reordered past each other. This usually means the algorithm /// is slower. - constexpr void stable_sort() const - requires best::comparable && (!is_const); + constexpr void stable_sort() const requires best::comparable && (!is_const) + ; constexpr void stable_sort(best::callable auto&&) const - requires(!is_const); + requires (!is_const); constexpr void stable_sort( - best::callable auto&&) const - requires(!is_const); + best::callable auto&&) const + requires (!is_const); /// # `span::bisect()` /// @@ -606,11 +596,11 @@ class span final { /// If the value is found, its index is returned in `best::ok()`. If not, /// a `best::err()` containing where it should be inserted is returned. constexpr best::result bisect( - const best::comparable auto& sought) const; + const best::comparable auto& sought) const; constexpr best::result bisect( - const auto& sought, best::callable auto&&) const; + const auto& sought, best::callable auto&&) const; constexpr best::result bisect( - best::callable auto&&) const; + best::callable auto&&) const; /// # `span::copy_from()`, `span::emplace_from()` /// @@ -621,11 +611,9 @@ class span final { /// `emplace_from()` additionally assumes the destination is uninitialized, so /// it will call a constructor rather than assignment. template - constexpr void copy_from(best::span src) const - requires(!is_const); + constexpr void copy_from(best::span src) const requires (!is_const); template - constexpr void emplace_from(best::span src) const - requires(!is_const); + constexpr void emplace_from(best::span src) const requires (!is_const); // TODO(mcyoung): Various other iterators, including: // struct chunk_iter, struct window_iter, struct ptr_iter. @@ -634,8 +622,7 @@ class span final { /// /// Destroys the elements of this span, in-place. This does not affect /// whatever the underlying storage for the span is. - constexpr void destroy() const - requires(!is_const); + constexpr void destroy() const requires (!is_const); /// # `vec::shift_within()` /// @@ -646,13 +633,13 @@ class span final { /// *and* that the source range is initialized, except where they overlap. constexpr void shift_within(unsafe u, size_t dst, size_t src, size_t count) const - requires(!is_const) && is_static + requires (!is_const) && is_static { as_dynamic(*this).shift_within(u, dst, src, count); } constexpr void shift_within(unsafe, size_t dst, size_t src, size_t count) const - requires(!is_const) && is_dynamic; + requires (!is_const) && is_dynamic; /// # `span::has_subarray` /// @@ -697,7 +684,7 @@ class span final { private: best::ptr data_ = nullptr; [[no_unique_address]] best::select - size_{}; + size_{}; }; /// # `best::span::iter_impl` @@ -723,7 +710,7 @@ class span::iter_impl final { constexpr iter_impl(T* ptr, size_t idx) : start_(ptr), end_(ptr + idx) {} constexpr best::option next() { - if (start_ == end_) return best::none; + if (start_ == end_) { return best::none; } return best::option(*start_++); } @@ -734,7 +721,7 @@ class span::iter_impl final { constexpr size_t count() && { return end_ - start_; } constexpr best::option last() && { - if (start_ == end_) return best::none; + if (start_ == end_) { return best::none; } return end_[-1]; } @@ -762,10 +749,10 @@ class span::split_impl final { friend best::iter; constexpr explicit split_impl(auto&& pat, best::span span) - : pat_(best::in_place, BEST_FWD(pat)), span_(span) {} + : pat_(best::in_place, BEST_FWD(pat)), span_(span) {} constexpr best::option> next() { - if (done_) return best::none; + if (done_) { return best::none; } if (auto found = span_.split_once(*pat_)) { span_ = found->second(); return found->first(); @@ -777,7 +764,7 @@ class span::split_impl final { } constexpr best::size_hint size_hint() const { - if (done_) return {0, 0}; + if (done_) { return {0, 0}; } return {1, span_.size() + 1}; } @@ -819,7 +806,7 @@ inline constexpr size_t BestStaticSize(auto, std::array*) { template > n> constexpr span::span(best::ptr data, size_t size, best::location loc) - : data_(data) { + : data_(data) { if constexpr (is_static) { best::bounds bounds_check = {.start = this->size(), .count = 0}; bounds_check.compute_count(this->size(), loc); @@ -835,7 +822,7 @@ constexpr best::option span::first() const { template > n> template constexpr best::option> span::first( - best::index_t) const { + best::index_t) const { return at({.end = m}).map(best::ctor>); } template > n> @@ -845,20 +832,20 @@ constexpr best::option span::last() const { template > n> template constexpr best::option> span::last( - best::index_t) const { + best::index_t) const { return at({.start = size() - m}).map(best::ctor>); } template > n> constexpr auto span::split_first() const - -> best::option>>> { - if (is_empty()) return best::none; + -> best::option>>> { + if (is_empty()) { return best::none; } return {{*data(), span>(data() + 1, size() - 1)}}; } template > n> template constexpr auto span::split_first(best::index_t) const - -> best::option, span>>> { + -> best::option, span>>> { return at({.end = m}).map(best::ctor>).map([&](auto ch) { return best::row{ch, span>(operator[]({.start = m}))}; }); @@ -866,33 +853,32 @@ constexpr auto span::split_first(best::index_t) const template > n> constexpr auto span::split_last() const - -> best::option>>> { - if (is_empty()) return best::none; + -> best::option>>> { + if (is_empty()) { return best::none; } return {{data()[size() - 1], span>(data(), size() - 1)}}; } template > n> template constexpr auto span::split_last(best::index_t) const - -> best::option, span>>> { + -> best::option, span>>> { return at({.start = size() - m}) - .map(best::ctor>) - .map([&](auto ch) { - return best::row{ch, - span>(operator[]({.end = size() - m}))}; - }); + .map(best::ctor>) + .map([&](auto ch) { + return best::row{ch, span>(operator[]({.end = size() - m}))}; + }); } template > n> constexpr span span::from_nul(T* data) { - if (data == nullptr) return {data, 0}; + if (data == nullptr) { return {data, 0}; } if constexpr (best::bytes_internal::constexpr_byte_comparable) { if (BEST_CONSTEXPR_MEMCMP_ || !std::is_constant_evaluated()) { size_t len = #if BEST_CONSTEXPR_MEMCMP_ - __builtin_strlen(data); + __builtin_strlen(data); #else - bytes_internal::strlen(data); + bytes_internal::strlen(data); #endif return {data, len}; } @@ -908,7 +894,7 @@ template > n> constexpr best::option> span::take_first(size_t m) requires is_dynamic { - if (m > size()) return best::none; + if (m > size()) { return best::none; } auto [prefix, rest] = *split_at(m); *this = rest; return prefix; @@ -918,7 +904,7 @@ template > n> constexpr best::option> span::take_last(size_t m) requires is_dynamic { - if (m > size()) return best::none; + if (m > size()) { return best::none; } auto [rest, suffix] = *split_at(size() - m); *this = rest; return suffix; @@ -942,7 +928,7 @@ constexpr T& span::operator[](best::index_t) const template > n> constexpr best::span span::operator[]( - best::bounds::with_location range) const { + best::bounds::with_location range) const { auto count = range.compute_count(size()); return as_dynamic{data() + range.start, count}; } @@ -950,7 +936,7 @@ constexpr best::span span::operator[]( template > n> template constexpr auto span::operator[](best::vlist) const - requires(range.try_compute_count(best::static_size).has_value()) + requires (range.try_compute_count(best::static_size).has_value()) { constexpr auto count = range.try_compute_count(best::static_size); return with_extent(data() + range.start, *count); @@ -958,9 +944,7 @@ constexpr auto span::operator[](best::vlist) const template > n> constexpr best::option span::at(size_t idx) const { - if (idx < size()) { - return data()[idx]; - } + if (idx < size()) { return data()[idx]; } return best::none; } @@ -988,24 +972,21 @@ constexpr best::span span::at(unsafe, best::bounds range) const { template > n> constexpr void span::swap(size_t a, size_t b, best::location loc) const - requires(!is_const) + requires (!is_const) { using ::std::swap; swap(operator[]({a, loc}), operator[]({b, loc})); } template > n> -constexpr void span::reverse() const - requires(!is_const) +constexpr void span::reverse() const requires (!is_const) { - for (size_t i = 0; i < size() / 2; ++i) { - swap(i, size() - i - 1); - } + for (size_t i = 0; i < size() / 2; ++i) { swap(i, size() - i - 1); } } template > n> constexpr best::option, 2>> span::split_at( - size_t idx) const { + size_t idx) const { if (auto prefix = at({.end = idx})) { auto rest = *this; rest.data_ += idx; @@ -1027,7 +1008,7 @@ template constexpr best::option span::find(const R& needle) const requires best::equatable> && (!best::equatable) { - if (best::size(needle) == 0) return 0; + if (best::size(needle) == 0) { return 0; } if constexpr (best::is_span) { using U = best::data_type; @@ -1045,10 +1026,10 @@ constexpr best::option span::find(const R& needle) const // Skip to the next possible start. size_t next = 0; for (const auto& x : haystack) { - if (x == first) break; + if (x == first) { break; } ++next; } - if (next == haystack.size()) return best::none; + if (next == haystack.size()) { return best::none; } // Skip forward. haystack = at(unsafe("we just did a bounds check; avoid going through " @@ -1057,7 +1038,7 @@ constexpr best::option span::find(const R& needle) const .start = next + 1}); // Now check if we found the full needle. - if (haystack.starts_with(rest)) return size() - haystack.size() - 1; + if (haystack.starts_with(rest)) { return size() - haystack.size() - 1; } } return best::none; } else { @@ -1069,10 +1050,10 @@ constexpr best::option span::find(const R& needle) const template > n> constexpr best::option span::find( - best::callable auto&& pred) const { + best::callable auto&& pred) const { size_t idx = 0; for (const auto& x : *this) { - if (pred(x)) return idx; + if (pred(x)) { return idx; } ++idx; } return best::none; @@ -1092,42 +1073,42 @@ constexpr bool span::contains(const R& needle) const } template > n> constexpr bool span::contains( - best::callable auto&& pred) const { + best::callable auto&& pred) const { return find(BEST_FWD(pred)).has_value(); } template > n> template U> constexpr best::option, span>> span::split_once( - const U& needle) const { + const U& needle) const { auto idx = find(needle); - if (!idx) return best::none; + if (!idx) { return best::none; } return {{ - best::span(data(), *idx), - best::span(data() + *idx + 1, size() - *idx - 1), + best::span(data(), *idx), + best::span(data() + *idx + 1, size() - *idx - 1), }}; } template > n> template constexpr best::option, span>> span::split_once( - const R& needle) const + const R& needle) const requires best::equatable> && (!best::equatable) { auto idx = find(needle); - if (!idx) return best::none; + if (!idx) { return best::none; } return {{ - best::span(data(), *idx), - best::span(data() + *idx + needle.size(), size() - *idx - needle.size()), + best::span(data(), *idx), + best::span(data() + *idx + needle.size(), size() - *idx - needle.size()), }}; } template > n> constexpr best::option, span>> span::split_once( - best::callable auto&& pred) const { + best::callable auto&& pred) const { auto idx = find(pred); - if (!idx) return best::none; + if (!idx) { return best::none; } return {{ - best::span(data(), idx), - best::span(data() + *idx + 1, size() - *idx - 1), + best::span(data(), idx), + best::span(data() + *idx + 1, size() - *idx - 1), }}; } @@ -1151,9 +1132,9 @@ constexpr auto span::split(const R& needle) const } template > n> constexpr auto span::split( - best::callable auto&& pred) const { + best::callable auto&& pred) const { return split_iter( - split_impl(BEST_FWD(pred), *this)); + split_impl(BEST_FWD(pred), *this)); } template > n> @@ -1170,16 +1151,16 @@ constexpr bool span::ends_with(best::span needle) const { template > n> template U> constexpr best::option> span::strip_prefix( - best::span prefix) const { - if (!starts_with(prefix)) return best::none; + best::span prefix) const { + if (!starts_with(prefix)) { return best::none; } return at({.start = prefix.size()}); } template > n> template U> constexpr best::option> span::strip_suffix( - best::span suffix) const { - if (!ends_with(suffix)) return best::none; + best::span suffix) const { + if (!ends_with(suffix)) { return best::none; } return at({.end = size() - suffix.size()}); } @@ -1188,7 +1169,7 @@ template U> constexpr bool span::consume_prefix(best::span prefix) requires is_dynamic { - if (!starts_with(prefix)) return false; + if (!starts_with(prefix)) { return false; } *this = *at({.start = prefix.size()}); return true; } @@ -1201,25 +1182,25 @@ template U> constexpr bool span::consume_suffix(best::span suffix) requires is_dynamic { - if (!ends_with(suffix)) return false; + if (!ends_with(suffix)) { return false; } *this = *at({.end = size() - suffix.size()}); return true; } template > n> constexpr best::result span::bisect( - const best::comparable auto& sought) const { + const best::comparable auto& sought) const { return bisect([&](auto& that) { return that <=> sought; }); } template > n> constexpr best::result span::bisect( - const auto& sought, best::callable auto&& key) const { + const auto& sought, best::callable auto&& key) const { return bisect( - [&](auto& that) { return best::call(BEST_FWD(key), that) <=> sought; }); + [&](auto& that) { return best::call(BEST_FWD(key), that) <=> sought; }); } template > n> constexpr best::result span::bisect( - best::callable auto&& comparator) const { + best::callable auto&& comparator) const { // Taken from Rust's `<[T]>::binary_search_by()`. size_t size = this->size(); size_t left = 0; @@ -1252,26 +1233,25 @@ constexpr best::result span::bisect( template > n> template constexpr void span::copy_from(best::span src) const - requires(!is_const) + requires (!is_const) { data().copy_assign(src.data(), best::min(size(), src.size())); } template > n> template constexpr void span::emplace_from(best::span src) const - requires(!is_const) + requires (!is_const) { data().copy(src.data(), best::min(size(), src.size())); } template > n> -constexpr void span::destroy() const - requires(!is_const) +constexpr void span::destroy() const requires (!is_const) { - if constexpr (!best::is_debug() && best::destructible) return; - for (size_t i = 0; i < size(); ++i) { - (data() + i).destroy(); + if constexpr (!best::is_debug() && best::destructible) { + return; } + for (size_t i = 0; i < size(); ++i) { (data() + i).destroy(); } } template > n> @@ -1291,11 +1271,9 @@ constexpr bool span::operator==(span that) const } } - if (size() != that.size()) return false; + if (size() != that.size()) { return false; } for (size_t i = 0; i < size(); ++i) { - if (data()[i] != that.data()[i]) { - return false; - } + if (data()[i] != that.data()[i]) { return false; } } return true; @@ -1329,7 +1307,7 @@ constexpr auto span::operator<=>(span that) const template > n> constexpr void span::shift_within(unsafe u, size_t dst, size_t src, size_t count) const - requires(!is_const) && is_dynamic + requires (!is_const) && is_dynamic { (data() + dst).relo_overlapping(data() + src, count); } diff --git a/best/memory/span_sort.h b/best/memory/span_sort.h index 52b86af..09cba9d 100644 --- a/best/memory/span_sort.h +++ b/best/memory/span_sort.h @@ -37,8 +37,7 @@ constexpr void span::sort() const } template > n> constexpr void span::sort( - best::callable auto&& get_key) const - requires(!is_const) + best::callable auto&& get_key) const requires (!is_const) { std::sort(data().raw(), data().raw() + size(), [&](auto& a, auto& b) { return best::call(BEST_FWD(get_key), a) < best::call(BEST_FWD(get_key), b); @@ -46,8 +45,8 @@ constexpr void span::sort( } template > n> constexpr void span::sort( - best::callable auto&& get_key) const - requires(!is_const) + best::callable auto&& get_key) const + requires (!is_const) { std::sort(data().raw(), data().raw() + size(), [&](auto& a, auto& b) { return best::call(BEST_FWD(get_key), a, b) < 0; @@ -61,8 +60,7 @@ constexpr void span::stable_sort() const } template > n> constexpr void span::stable_sort( - best::callable auto&& get_key) const - requires(!is_const) + best::callable auto&& get_key) const requires (!is_const) { std::stable_sort(data().raw(), data().raw() + size(), [&](auto& a, auto& b) { return best::call(BEST_FWD(get_key), a) < best::call(BEST_FWD(get_key), b); @@ -70,8 +68,8 @@ constexpr void span::stable_sort( } template > n> constexpr void span::stable_sort( - best::callable auto&& get_key) const - requires(!is_const) + best::callable auto&& get_key) const + requires (!is_const) { std::stable_sort(data().raw(), data().raw() + size(), [&](auto& a, auto& b) { return best::call(BEST_FWD(get_key), a, b) < 0; diff --git a/best/memory/span_test.cc b/best/memory/span_test.cc index 9d305c9..0bebed0 100644 --- a/best/memory/span_test.cc +++ b/best/memory/span_test.cc @@ -265,8 +265,8 @@ best::test Find = [](auto& t) { best::vec> byte_split(bytes.split('4')); t.expect_eq( - byte_split, - {{'1', '2', '3'}, {}, {}, {}, {}, {'5', '6', '7', '8', '9', '\0'}}); + byte_split, + {{'1', '2', '3'}, {}, {}, {}, {}, {'5', '6', '7', '8', '9', '\0'}}); int a[] = {1, 2, 3, 4, 4, 4, 4, 4, 5, 6, 7, 8, 9, 0}; best::span ints = a; @@ -377,7 +377,7 @@ best::test Shift = [](auto& t) { // This test doesn't work correctly in opt mode, because it relies on doing // UAF reads of destroyed values. // TODO: Make this test sound? - if (!best::is_debug()) return; + if (!best::is_debug()) { return; } unsafe u("test"); diff --git a/best/meta/empty.h b/best/meta/empty.h index c3501c7..d70414c 100644 --- a/best/meta/empty.h +++ b/best/meta/empty.h @@ -83,7 +83,7 @@ class ebo /* not final! */ { /// /// Constructs a new `ebo` by calling the constructor of the wrapped type constexpr ebo(best::in_place_t, auto&&... args) - : BEST_EBO_VALUE_(BEST_FWD(args)...) {} + : BEST_EBO_VALUE_(BEST_FWD(args)...) {} constexpr ebo() = default; constexpr ebo(const ebo&) = default; @@ -107,8 +107,7 @@ class ebo /* not final! */ : T { public: constexpr ebo(best::in_place_t, auto&&... args) : T(BEST_FWD(args)...) {} - constexpr ebo() - requires best::constructible + constexpr ebo() requires best::constructible = default; constexpr ebo(const ebo&) = default; constexpr ebo& operator=(const ebo&) = default; @@ -126,11 +125,10 @@ class ebo /* not final! */ { (void)T(BEST_FWD(args)...); } - constexpr ebo() - requires best::constructible + constexpr ebo() requires best::constructible = default; constexpr ebo() - requires(best::constructible && !best::constructible) + requires (best::constructible && !best::constructible) { (void)T(); }; diff --git a/best/meta/init.h b/best/meta/init.h index 5b76ae9..bb1f02e 100644 --- a/best/meta/init.h +++ b/best/meta/init.h @@ -102,56 +102,56 @@ concept converts_to = convertible; /// Whether `T` is move-constructible. template concept move_constructible = - init_internal::only_trivial && // - best::convertible && - best::convertible>; + init_internal::only_trivial && // + best::convertible && + best::convertible>; /// # `best::copy_constructible` /// /// Whether T is copy-constructible. template concept copy_constructible = - init_internal::only_trivial && // - best::move_constructible && - best::convertible> && - best::convertible>>; + init_internal::only_trivial && // + best::move_constructible && + best::convertible> && + best::convertible>>; /// # `best::move_assignable` /// /// Whether T is move-assignable. template concept move_assignable = - init_internal::only_trivial && // - best::assignable && - best::assignable>; + init_internal::only_trivial && // + best::assignable && + best::assignable>; /// # `best::copy_assignable` /// /// Whether T is copy-assignable. template concept copy_assignable = - init_internal::only_trivial && // - best::move_assignable && - best::assignable> && - best::assignable>>; + init_internal::only_trivial && // + best::move_assignable && + best::assignable> && + best::assignable>>; /// # `best::moveable` /// /// Whether T is moveable. template concept moveable = - init_internal::only_trivial && // - best::move_constructible && best::move_assignable; + init_internal::only_trivial && // + best::move_constructible && best::move_assignable; /// # `best::copyable` /// /// Whether T is copyable. template concept copyable = - init_internal::only_trivial && // - best::copy_constructible && best::copy_assignable; + init_internal::only_trivial && // + best::copy_constructible && best::copy_assignable; /// # `best::relocatable` /// @@ -165,11 +165,11 @@ concept copyable = /// To make a non-trivially-moveable type trivially relocatable, use the /// `BEST_RELOCATABLE` macro. template -concept relocatable = init_internal::only_trivial && // - (!std::is_object_v || // - (init_internal::is_trivial - ? init_internal::trivially_relocatable - : moveable)); +concept relocatable = + init_internal::only_trivial && // + (!std::is_object_v || // + (init_internal::is_trivial ? init_internal::trivially_relocatable + : moveable)); /// # `best::destructible` /// /// Whether T can be destroyed. @@ -180,10 +180,10 @@ concept relocatable = init_internal::only_trivial && // /// For all other types, this is vacuously true. template concept destructible = - init_internal::only_trivial && // - (!std::is_object_v || - (init_internal::is_trivial ? std::is_trivially_destructible_v - : std::is_destructible_v)); + init_internal::only_trivial && // + (!std::is_object_v || + (init_internal::is_trivial ? std::is_trivially_destructible_v + : std::is_destructible_v)); /// A callback that constructs a `T`. /// diff --git a/best/meta/init_test.cc b/best/meta/init_test.cc index c8353fa..e6fff53 100644 --- a/best/meta/init_test.cc +++ b/best/meta/init_test.cc @@ -64,7 +64,7 @@ static_assert(!best::constructible); static_assert(!best::constructible); static_assert(!best::constructible); static_assert( - !best::constructible); + !best::constructible); static_assert(best::constructible); static_assert(best::constructible); @@ -113,9 +113,9 @@ static_assert(best::convertible); static_assert(best::convertible); static_assert(!best::convertible); static_assert( - !best::convertible); + !best::convertible); static_assert( - !best::convertible); + !best::convertible); static_assert(best::convertible); static_assert(best::convertible); @@ -317,7 +317,7 @@ static_assert(best::constructible>); static_assert(best::constructible>); static_assert(best::constructible>); static_assert( - best::constructible>); + best::constructible>); } // namespace best::init_test diff --git a/best/meta/internal/init.h b/best/meta/internal/init.h index 205d4d4..8557851 100644 --- a/best/meta/internal/init.h +++ b/best/meta/internal/init.h @@ -45,16 +45,14 @@ struct tag {}; // ========================================================================== // template -void ctor(tag, tag...) - requires std::is_constructible_v; +void ctor(tag, tag...) requires std::is_constructible_v; template void ctor(tag, tag, tag...) requires std::is_trivially_constructible_v; template -void ctor(tag, tag) - requires std::is_constructible_v; +void ctor(tag, tag) requires std::is_constructible_v; template void ctor(tag, tag, tag) @@ -63,24 +61,21 @@ void ctor(tag, tag, tag) // -------------------------------------------------------------------------- // template -void ctor(tag, tag) - requires std::is_constructible_v; +void ctor(tag, tag) requires std::is_constructible_v; template void ctor(tag, tag, tag) requires std::is_trivially_constructible_v; template -void ctor(tag, tag) - requires std::is_constructible_v; +void ctor(tag, tag) requires std::is_constructible_v; template void ctor(tag, tag, tag) requires std::is_trivially_constructible_v; template -void ctor(tag, tag) - requires std::is_constructible_v; +void ctor(tag, tag) requires std::is_constructible_v; template void ctor(tag, tag, tag) @@ -107,8 +102,7 @@ void ctor(tag, tag, tag) // -------------------------------------------------------------------------- // template -void ctor(tag, tag) - requires std::is_convertible_v>; +void ctor(tag, tag) requires std::is_convertible_v>; template void ctor(tag, tag, tag) @@ -149,8 +143,7 @@ void ctor(tag, tag, tag&&>) // ========================================================================== // template -void conv(tag, tag) - requires std::is_convertible_v; +void conv(tag, tag) requires std::is_convertible_v; template void conv(tag, tag, tag) @@ -158,8 +151,7 @@ void conv(tag, tag, tag) requires { ctor(tag{}, tag{}, tag()); }; template -void conv(tag, tag) - requires std::is_convertible_v; +void conv(tag, tag) requires std::is_convertible_v; template void conv(tag, tag, tag) @@ -167,8 +159,7 @@ void conv(tag, tag, tag) requires { ctor(tag{}, tag{}, tag()); }; template -void conv(tag, tag) - requires std::is_convertible_v; +void conv(tag, tag) requires std::is_convertible_v; template void conv(tag, tag, tag) @@ -176,8 +167,7 @@ void conv(tag, tag, tag) requires { ctor(tag{}, tag{}, tag()); }; template -void conv(tag, tag) - requires std::is_convertible_v; +void conv(tag, tag) requires std::is_convertible_v; template void conv(tag, tag, tag) @@ -186,43 +176,39 @@ void conv(tag, tag, tag) template void conv(tag, tag) - requires(!best::is_object && !best::is_rref) && - requires { ctor(tag{}, tag()); }; + requires (!best::is_object && !best::is_rref) && + requires { ctor(tag{}, tag()); }; template void conv(tag, tag, tag) - requires(!best::is_object && !best::is_rref) && - requires { ctor(tag{}, tag{}, tag()); }; + requires (!best::is_object && !best::is_rref) && + requires { ctor(tag{}, tag{}, tag()); }; // ========================================================================== // template -void assign(tag, tag) - requires std::is_assignable_v; +void assign(tag, tag) requires std::is_assignable_v; template void assign(tag, tag, tag) requires std::is_trivially_assignable_v; template -void assign(tag, tag) - requires std::is_assignable_v; +void assign(tag, tag) requires std::is_assignable_v; template void assign(tag, tag, tag) requires std::is_trivially_assignable_v; template -void assign(tag, tag) - requires std::is_assignable_v; +void assign(tag, tag) requires std::is_assignable_v; template void assign(tag, tag, tag) requires std::is_trivially_assignable_v; template -void assign(tag, tag) - requires std::is_assignable_v; +void assign(tag, tag) requires std::is_assignable_v; template void assign(tag, tag, tag) @@ -232,12 +218,12 @@ void assign(tag, tag, tag) template void assign(tag, tag) - requires(!best::is_object) && requires { ctor(tag{}, tag{}); }; + requires (!best::is_object) && requires { ctor(tag{}, tag{}); }; template void assign(tag, tag, tag) - requires(!best::is_object) && - requires { ctor(tag{}, tag{}, tag{}); }; + requires (!best::is_object) && + requires { ctor(tag{}, tag{}, tag{}); }; // -------------------------------------------------------------------------- // @@ -279,9 +265,9 @@ concept is_trivial = requires { is_triv(tag{}...); }; template concept trivially_relocatable = #if BEST_HAS_BUILTIN(__is_trivially_relocatable) - __is_trivially_relocatable(T); + __is_trivially_relocatable(T); #else - std::is_trivial_v; + std::is_trivial_v; #endif } // namespace init_internal diff --git a/best/meta/internal/names.h b/best/meta/internal/names.h index e0349ef..0764ae8 100644 --- a/best/meta/internal/names.h +++ b/best/meta/internal/names.h @@ -44,12 +44,12 @@ struct priv {}; template constexpr best::span raw_name() { return best::span::from_nul( - std::source_location::current().function_name()); + std::source_location::current().function_name()); } template constexpr best::span raw_name() { return best::span::from_nul( - std::source_location::current().function_name()); + std::source_location::current().function_name()); } // Similar to best::lie, but this materializes a real reference that can be @@ -67,15 +67,15 @@ constexpr T& materialize() { // Needles to search for that are *definitely* gonna be in the target compiler's // pretty printed function names. inline constexpr auto TypeNeedle = - best::span::from_nul("BEST_REFLECT_STRUCT_"); + best::span::from_nul("BEST_REFLECT_STRUCT_"); inline constexpr auto FieldNeedle = best::span::from_nul( - "&BEST_REFLECT_STRUCT_::BEST_REFLECT_FIELD1_"); + "&BEST_REFLECT_STRUCT_::BEST_REFLECT_FIELD1_"); inline constexpr auto FieldNeedle1 = - best::span::from_nul("BEST_REFLECT_FIELD1_"); + best::span::from_nul("BEST_REFLECT_FIELD1_"); inline constexpr auto FieldNeedle2 = - best::span::from_nul("BEST_REFLECT_FIELD2_"); -inline constexpr auto ValueNeedle = best::span::from_nul( - "BEST_REFLECT_STRUCT_::BEST_REFLECT_VALUE_"); + best::span::from_nul("BEST_REFLECT_FIELD2_"); +inline constexpr auto ValueNeedle = + best::span::from_nul("BEST_REFLECT_STRUCT_::BEST_REFLECT_VALUE_"); struct raw_offsets { size_t prefix, suffix; @@ -87,25 +87,25 @@ inline constexpr auto TypeOffsets = [] { auto name = raw_name(); auto idx = *name.find(TypeNeedle); return raw_offsets{ - .prefix = idx, - .suffix = name.size() - idx - TypeNeedle.size(), + .prefix = idx, + .suffix = name.size() - idx - TypeNeedle.size(), }; }(); inline constexpr auto FieldOffsets = [] { auto name = raw_name<&BEST_REFLECT_STRUCT_::BEST_REFLECT_FIELD1_>(); auto idx = *name.find(FieldNeedle1); return raw_offsets{ - .prefix = idx, - .suffix = name.size() - idx - FieldNeedle1.size(), + .prefix = idx, + .suffix = name.size() - idx - FieldNeedle1.size(), }; }(); inline constexpr auto ValueOffsets = [] { auto name = - raw_name(); + raw_name(); auto idx = *name.find(ValueNeedle); return raw_offsets{ - .prefix = idx, - .suffix = name.size() - idx - ValueNeedle.size(), + .prefix = idx, + .suffix = name.size() - idx - ValueNeedle.size(), }; }(); @@ -126,15 +126,13 @@ inline constexpr auto FieldPtrOffsets = [] { auto name = names_internal::raw_name(); auto idx = *name.find(FieldNeedle1); return raw_offsets{ - .prefix = idx, - .suffix = name.size() - idx - FieldNeedle1.size(), + .prefix = idx, + .suffix = name.size() - idx - FieldNeedle1.size(), }; }(); constexpr best::str remove_namespace(best::str path) { - while (auto split = path.split_once("::")) { - path = split->second(); - } + while (auto split = path.split_once("::")) { path = split->second(); } return path; } @@ -143,12 +141,12 @@ constexpr Names parse() { auto offsets = names_internal::TypeOffsets; auto raw = names_internal::raw_name(); return Names( - priv{}, - best::str(unsafe("the compiler made this string, it better be UTF-8"), - raw[{ - .start = offsets.prefix, - .count = raw.size() - offsets.prefix - offsets.suffix, - }])); + priv{}, + best::str(unsafe("the compiler made this string, it better be UTF-8"), + raw[{ + .start = offsets.prefix, + .count = raw.size() - offsets.prefix - offsets.suffix, + }])); } template @@ -156,11 +154,11 @@ constexpr best::str parse() { auto offsets = names_internal::FieldOffsets; auto raw = names_internal::raw_name(); auto name = - best::str(unsafe("the compiler made this string, it better be UTF-8"), - raw[{ - .start = offsets.prefix, - .count = raw.size() - offsets.prefix - offsets.suffix, - }]); + best::str(unsafe("the compiler made this string, it better be UTF-8"), + raw[{ + .start = offsets.prefix, + .count = raw.size() - offsets.prefix - offsets.suffix, + }]); // `name` is going to be scoped, so we need to strip off a leading path. return names_internal::remove_namespace(name); @@ -176,16 +174,14 @@ constexpr best::str parse() { // search for the last `.` or `->` (MSVC uses -> and it feels easier to just // search for *both*...). auto prefix = raw[{ - .end = raw.size() - offsets.suffix, + .end = raw.size() - offsets.suffix, }]; // TODO(mcyoung): Use rfind() once we implement that. size_t i = prefix.size(); for (; i > 0; --i) { unsafe u("already did the bounds check"); - if (prefix.at(u, i - 1) == '.') { - break; - } + if (prefix.at(u, i - 1) == '.') { break; } if (i > 1 && prefix.at(u, i - 1) == '>' && prefix.at(u, i - 2) == '-') { break; } @@ -202,13 +198,13 @@ constexpr best::option parse() { auto offsets = names_internal::ValueOffsets; auto raw = names_internal::raw_name(); auto name = - best::str(unsafe("the compiler made this string, it better be UTF-8"), - raw[{ - .start = offsets.prefix, - .count = raw.size() - offsets.prefix - offsets.suffix, - }]); + best::str(unsafe("the compiler made this string, it better be UTF-8"), + raw[{ + .start = offsets.prefix, + .count = raw.size() - offsets.prefix - offsets.suffix, + }]); - if (name.starts_with('(')) return best::none; + if (name.starts_with('(')) { return best::none; } // `name` is going to be scoped, so we need to strip off a leading path. return names_internal::remove_namespace(name); }; diff --git a/best/meta/internal/ops.h b/best/meta/internal/ops.h index 6ebc0b4..46aedb9 100644 --- a/best/meta/internal/ops.h +++ b/best/meta/internal/ops.h @@ -31,7 +31,7 @@ struct tag {}; #define BEST_OP_FOLD_CASE_(op_, ...) \ template \ BEST_INLINE_SYNTHETIC constexpr auto run(tag, auto&&... args) \ - -> decltype((... __VA_ARGS__ BEST_FWD(args))) { \ + -> decltype((... __VA_ARGS__ BEST_FWD(args))) { \ return (... __VA_ARGS__ BEST_FWD(args)); \ } BEST_OP_FOLD_CASE_(Add, +) @@ -74,7 +74,7 @@ BEST_OP_FOLD_CASE_(ShrAssign, >>=) #define BEST_OP_2_CASE_(op_, ...) \ template \ BEST_INLINE_SYNTHETIC constexpr auto run(tag, auto&& a, auto&& b) \ - -> decltype(BEST_FWD(a) __VA_ARGS__ BEST_FWD(b)) { \ + -> decltype(BEST_FWD(a) __VA_ARGS__ BEST_FWD(b)) { \ return BEST_FWD(a) __VA_ARGS__ BEST_FWD(b); \ } @@ -85,7 +85,7 @@ BEST_OP_2_CASE_(Spaceship, <=>) #define BEST_OP_1_CASE_(op_, ...) \ template \ BEST_INLINE_SYNTHETIC constexpr auto run(tag, auto&& a) \ - -> decltype(__VA_ARGS__ BEST_FWD(a)) { \ + -> decltype(__VA_ARGS__ BEST_FWD(a)) { \ return __VA_ARGS__ BEST_FWD(a); \ } @@ -103,7 +103,7 @@ BEST_OP_1_CASE_(PreDec, --) #define BEST_OP_POST_CASE_(op_, ...) \ template \ BEST_INLINE_SYNTHETIC constexpr auto run(tag, auto&& a) \ - -> decltype(BEST_FWD(a) __VA_ARGS__) { \ + -> decltype(BEST_FWD(a) __VA_ARGS__) { \ return BEST_FWD(a) __VA_ARGS__; \ } @@ -113,7 +113,7 @@ BEST_OP_POST_CASE_(PostDec, --) template BEST_INLINE_SYNTHETIC constexpr auto run(tag, auto&& a) - -> decltype(BEST_FWD(a).operator->()) { + -> decltype(BEST_FWD(a).operator->()) { return BEST_FWD(a).operator->(); } template @@ -124,14 +124,14 @@ constexpr auto run(tag, auto* a) { template BEST_INLINE_SYNTHETIC constexpr auto run(tag, auto&& func, auto&&... args) - -> decltype(BEST_FWD(func)(BEST_FWD(args)...)) { + -> decltype(BEST_FWD(func)(BEST_FWD(args)...)) { return BEST_FWD(func)(BEST_FWD(args)...); } template BEST_INLINE_SYNTHETIC constexpr auto run(tag, auto&& func, auto&& arg) - -> decltype(BEST_FWD(func)[BEST_FWD(arg)]) { + -> decltype(BEST_FWD(func)[BEST_FWD(arg)]) { return BEST_FWD(func)[BEST_FWD(arg)]; } } // namespace best::ops_internal diff --git a/best/meta/internal/reflect.h b/best/meta/internal/reflect.h index eb24fa6..4b3e60c 100644 --- a/best/meta/internal/reflect.h +++ b/best/meta/internal/reflect.h @@ -61,10 +61,10 @@ class vdesc; // Enum value descriptor. // CTAD deduction guides for the descriptors. template tdesc(best::tlist, Items, Tags) - -> tdesc, best::abridge>; + -> tdesc, best::abridge>; template fdesc(best::vlist

, best::tlist, Get, Tags) - -> fdesc>; + -> fdesc>; template vdesc(best::vlist, Tags) -> vdesc>; @@ -76,7 +76,7 @@ enum kind { Field, Value, Type }; template struct validator { static constexpr bool value = - Info::Kind == Type && best::same; + Info::Kind == Type && best::same; }; template concept valid_reflection = validator, For>::value; @@ -132,7 +132,7 @@ inline constexpr struct any_t { constexpr any_t(any_t&&) = delete; template - requires(!best::same) + requires (!best::same) operator T() const; } any; template @@ -169,14 +169,14 @@ template constexpr decltype(auto) bind(auto&& val) { return best::indices.apply([&](auto... i) -> decltype(auto) { return reflect_internal::bind( - BEST_FWD(val), - [&](best::dependent..., auto&& the_one, - auto&&... rest) -> decltype(auto) { - // Structured bindings' type does not preserve value category - // correctly, so we need to adjust its type to match the value - // category of `val`. - return best::refcopy(the_one); - }); + BEST_FWD(val), + [&](best::dependent..., auto&& the_one, + auto&&... rest) -> decltype(auto) { + // Structured bindings' type does not preserve value category + // correctly, so we need to adjust its type to match the value + // category of `val`. + return best::refcopy(the_one); + }); }); } @@ -194,7 +194,7 @@ class fdesc final { inline static constexpr auto Kind = Field; constexpr fdesc(best::vlist

, best::tlist, Get get, Tags tags) - : get_(get), tags_(tags) {} + : get_(get), tags_(tags) {} // Adds tags to this field. constexpr auto add(auto... tags) const { @@ -252,12 +252,12 @@ class tdesc final { using Tags = best::unabridge; constexpr tdesc(best::tlist, Items items, Tags tags) - : items_(items), tags_(tags) {} + : items_(items), tags_(tags) {} template constexpr auto operator+(lifted args) const { return args.args.apply( - [&](auto... tags) { return this->template add(tags...); }); + [&](auto... tags) { return this->template add(tags...); }); } template constexpr auto operator+(lifted) const { @@ -274,7 +274,7 @@ class tdesc final { template constexpr auto find() const { return items_.select_indices( - best::types().*pm))>>); + best::types().*pm))>>); } // Adds a field or updates it by appending tags @@ -283,22 +283,19 @@ class tdesc final { auto found = find(); if constexpr (found.is_empty()) { return reflect_internal::tdesc( + best::types, + items_.push(fdesc( + best::vals().*pm))>, best::types, - items_.push(fdesc( - best::vals().*pm))>, - best::types, - [](auto&& value) -> decltype(auto) { - return BEST_FWD(value).*pm; - }, - best::row(tags...))), - tags_); + [](auto&& value) -> decltype(auto) { return BEST_FWD(value).*pm; }, + best::row(tags...))), + tags_); } else { return reflect_internal::tdesc( - best::types, - items_.update( - items_[best::index>].add(tags...), - best::index>), - tags_); + best::types, + items_.update(items_[best::index>].add(tags...), + best::index>), + tags_); } } @@ -314,15 +311,14 @@ class tdesc final { auto found = find(); if constexpr (found.is_empty()) { return reflect_internal::tdesc( - best::types, items_.push(vdesc(best::vals, best::row(tags...))), - tags_); + best::types, items_.push(vdesc(best::vals, best::row(tags...))), + tags_); } else { return reflect_internal::tdesc( - best::types, - items_.update( - items_[best::index>].add(tags...), - best::index>), - tags_); + best::types, + items_.update(items_[best::index>].add(tags...), + best::index>), + tags_); } } @@ -331,44 +327,42 @@ class tdesc final { constexpr auto hide() const { auto found = find(); return reflect_internal::tdesc( - best::types, items_.remove(best::index>), - tags_); + best::types, items_.remove(best::index>), + tags_); } // Infers the default reflection descriptor for a struct. - static constexpr auto infer_struct() - requires best::is_struct + static constexpr auto infer_struct() requires best::is_struct { auto fields = best::indices>.apply([&] { return best::row{fdesc( - best::vals(materialize())))>, - best::types, - [](auto&& v) -> decltype(auto) { - return reflect_internal::bind(BEST_FWD(v)); - }, - best::row())...}; + best::vals(materialize())))>, + best::types, + [](auto&& v) -> decltype(auto) { + return reflect_internal::bind(BEST_FWD(v)); + }, + best::row())...}; }); return reflect_internal::tdesc(best::types, fields, best::row()); } // Infers the default reflection descriptor for an enum. template - static constexpr auto infer_enum() - requires best::is_enum + static constexpr auto infer_enum() requires best::is_enum { BEST_PUSH_GCC_DIAGNOSTIC() BEST_IGNORE_GCC_DIAGNOSTIC("-Wenum-constexpr-conversion") constexpr auto ok = best::indices.apply([] { constexpr size_t ok_count = - (0 + ... + best::value_name.has_value()); + (0 + ... + best::value_name.has_value()); std::array ok = {}; size_t idx = 0; ((best::value_name.has_value() - ? void(ok[idx++] = T(start + i)) - : void()), + ? void(ok[idx++] = T(start + i)) + : void()), ...); return ok; }); @@ -387,7 +381,7 @@ class tdesc final { struct reify { template static constexpr mirror>, best::abridge>> - empty{{{}, {}, {}}, {}}; + empty{{{}, {}, {}}, {}}; template static constexpr auto ftadle = BestReflect(empty, (T*){}); @@ -412,16 +406,15 @@ struct reify { // variable as a stepladder for moving constexpr values out of the () world // and into the <> world. return m.with_.indices.apply([] { - return ( - m.info_ + ... + - reify::template lift].first()>( - m.with_[best::index][best::vals])); + return (m.info_ + ... + + reify::template lift].first()>( + m.with_[best::index][best::vals])); }); }(); }; template - requires requires { BestReflect(reify::empty, (T*){}); } +requires requires { BestReflect(reify::empty, (T*){}); } inline constexpr auto desc = reify::apply; }; // namespace best::reflect_internal diff --git a/best/meta/internal/tlist.h b/best/meta/internal/tlist.h index 8edeb7d..2c3059c 100644 --- a/best/meta/internal/tlist.h +++ b/best/meta/internal/tlist.h @@ -69,14 +69,14 @@ auto make_indexer(std::index_sequence) { template using fast_nth = #if BEST_TLIST_USE_CLANG_MAGIC_ && BEST_HAS_BUILTIN(__type_pack_element) - __type_pack_element; + __type_pack_element; #else - typename decltype(typename make_indexer(std::make_index_sequence{})( - best::id{}...))::type; + typename decltype(typename make_indexer(std::make_index_sequence{})( + best::id{}...))::type; #endif template - requires(n < sizeof...(Ts)) +requires (n < sizeof...(Ts)) auto nth_impl(tlist) { #if BEST_TLIST_USE_CLANG_MAGIC_ && BEST_HAS_BUILTIN(__type_pack_element) return best::id<__type_pack_element>{}; @@ -86,7 +86,7 @@ auto nth_impl(tlist) { } template - requires(n >= sizeof...(Ts)) +requires (n >= sizeof...(Ts)) auto nth_impl(tlist) { return best::id{}; } @@ -110,64 +110,64 @@ template auto make_slicer(std::index_sequence, std::index_sequence, std::index_sequence) { return []... prefix, splat... infix, splat... suffix>( - id..., id..., id...) { + id..., id..., id...) { return best::id>{}; }; } template - requires(count.has_value()) +requires (count.has_value()) auto slice_impl(tlist) { return make_slicer( - std::make_index_sequence{}, std::make_index_sequence<*count>{}, - std::make_index_sequence{})( - best::id{}...); + std::make_index_sequence{}, std::make_index_sequence<*count>{}, + std::make_index_sequence{})( + best::id{}...); } template - requires(!count.has_value()) +requires (!count.has_value()) best::id slice_impl(tlist); template using slice = - decltype(slice_impl( - best::lie))::type; + decltype(slice_impl( + best::lie))::type; // Inside-out version of make_slicer. template auto make_splicer(std::index_sequence, std::index_sequence, std::index_sequence) { return []... prefix, splat... infix, splat... suffix>( - id..., id..., id...) { + id..., id..., id...) { return best::id>{}; }; } template - requires(count.has_value()) +requires (count.has_value()) auto splice_impl(tlist, tlist) { return make_splicer( - std::make_index_sequence{}, std::make_index_sequence<*count>{}, - std::make_index_sequence{})( - best::id{}...); + std::make_index_sequence{}, std::make_index_sequence<*count>{}, + std::make_index_sequence{})( + best::id{}...); } template - requires(!count.has_value()) +requires (!count.has_value()) best::id splice_impl(tlist, tlist); template using splice = - decltype(splice_impl( - best::lie, best::lie))::type; + decltype(splice_impl( + best::lie, best::lie))::type; template - requires((i < sizeof...(Ts)) && ...) // Fast path for no out-of-bounds. +requires ((i < sizeof...(Ts)) && ...) // Fast path for no out-of-bounds. auto gather_impl(tlist) { return best::tlist...>{}; } template - requires((i >= sizeof...(Ts)) || ...) +requires ((i >= sizeof...(Ts)) || ...) auto gather_impl(tlist) { return best::tlist>...>{}; } @@ -177,7 +177,7 @@ using gather = decltype(gather_impl(best::lie)); template - requires((i < sizeof...(Ts)) && ...) // Fast path for no out-of-bounds. +requires ((i < sizeof...(Ts)) && ...) // Fast path for no out-of-bounds. auto scatter_impl(std::index_sequence, tlist, tlist) { constexpr auto lut = [] { std::array lut{(j - j)...}; @@ -190,9 +190,8 @@ auto scatter_impl(std::index_sequence, tlist, tlist) { } template -using scatter = - decltype(scatter_impl(std::make_index_sequence{}, - best::lie, best::lie)); +using scatter = decltype(scatter_impl( + std::make_index_sequence{}, best::lie, best::lie)); // This generates a lookup table for computing a join. // @@ -200,7 +199,7 @@ using scatter = // for each element of the `n`th list. The odd elements are then `{0, 1, 2, 0, // 1, 2, 0, 1, 2}`: for each list `l`, the sequence `{.count = l.size() - 1}`. template -constexpr inline auto join_lut = [] { +inline constexpr auto join_lut = [] { std::array lut = {}; size_t running_total = 0; @@ -225,7 +224,7 @@ auto join_impl(std::index_sequence, Packs...) { } template using join = decltype(join_impl( - std::make_index_sequence<(0 + ... + Packs::size())>{}, Packs{}...)); + std::make_index_sequence<(0 + ... + Packs::size())>{}, Packs{}...)); template struct entry {}; @@ -268,7 +267,7 @@ concept vt_callable = sizeof...(Elems) > 0 && (... && requires(F f) { best::call(f); }); template concept vts_callable = - sizeof...(Elems) > 0 && requires(F f) { best::call(f); }; + sizeof...(Elems) > 0 && requires(F f) { best::call(f); }; } // namespace best::tlist_internal #endif // BEST_META_INTERNAL_TLIST_H_ diff --git a/best/meta/names.h b/best/meta/names.h index 0fce7f1..cfc910a 100644 --- a/best/meta/names.h +++ b/best/meta/names.h @@ -100,8 +100,8 @@ template inline constexpr best::str field_name = best::names_internal::parse(); template inline constexpr best::option value_name = BEST_PUSH_GCC_DIAGNOSTIC() - BEST_IGNORE_GCC_DIAGNOSTIC("-Wenum-constexpr-conversion") - best::names_internal::parse(); + BEST_IGNORE_GCC_DIAGNOSTIC("-Wenum-constexpr-conversion") + best::names_internal::parse(); BEST_POP_GCC_DIAGNOSTIC() } // namespace best @@ -111,12 +111,12 @@ BEST_POP_GCC_DIAGNOSTIC() namespace best { constexpr type_names::type_names(names_internal::priv, best::str name) - : full_name_(name) { + : full_name_(name) { first_angle_ = full_name_.find('<').value_or(full_name_.size()); - last_colcol_ = first_angle_ - names_internal::remove_namespace( - full_name_[{.end = first_angle_}]) - .size(); + last_colcol_ = + first_angle_ - + names_internal::remove_namespace(full_name_[{.end = first_angle_}]).size(); } } // namespace best diff --git a/best/meta/ops.h b/best/meta/ops.h index 075a1b9..93e02be 100644 --- a/best/meta/ops.h +++ b/best/meta/ops.h @@ -92,8 +92,8 @@ enum class op { /// (... op args), if the operation supports folding. template constexpr auto operate(auto &&...args) - -> decltype(ops_internal::run(ops_internal::tag{}, - BEST_FWD(args)...)) { + -> decltype(ops_internal::run(ops_internal::tag{}, + BEST_FWD(args)...)) { return ops_internal::run(ops_internal::tag{}, BEST_FWD(args)...); } diff --git a/best/meta/reflect.h b/best/meta/reflect.h index 4dc9470..809c9b8 100644 --- a/best/meta/reflect.h +++ b/best/meta/reflect.h @@ -54,10 +54,10 @@ concept reflected = requires { }; template concept is_reflected_struct = - best::is_struct> && best::reflected; + best::is_struct> && best::reflected; template concept is_reflected_enum = - best::is_enum> && best::reflected; + best::is_enum> && best::reflected; /// # `best::reflect` /// @@ -65,7 +65,7 @@ concept is_reflected_enum = /// `best::reflected_type`, although the exact specialization is not nameable. template inline constexpr auto reflect = - best::reflected_type>>{}; + best::reflected_type>>{}; /// # `best::fields()` /// @@ -73,7 +73,7 @@ inline constexpr auto reflect = /// of references of them. constexpr auto fields(best::is_reflected_struct auto&& value) { return best::reflect.apply( - [&](auto... f) { return best::row(best::bind, BEST_FWD(value)->*f...); }); + [&](auto... f) { return best::row(best::bind, BEST_FWD(value)->*f...); }); } /// # `best::mirror` @@ -150,7 +150,7 @@ class mirror final { friend class mirror; constexpr explicit mirror(info_t info, with_t with) - : info_(info), with_(with) {} + : info_(info), with_(with) {} info_t info_; with_t with_; }; @@ -359,10 +359,10 @@ class reflected_type final { static constexpr size_t index_of(best::str name) { best::mark_sort_header_used(); return best::span(Name2Index) - .bisect(name, &entry::k) - .ok() - .map([](auto i) { return Name2Index[i].v; }) - .value_or(0); + .bisect(name, &entry::k) + .ok() + .map([](auto i) { return Name2Index[i].v; }) + .value_or(0); } }; } // namespace best @@ -386,8 +386,8 @@ constexpr auto mirror::infer() const BEST_REFLECT_MAX_FIELDS_)) { return best::mirror{ - reflect_internal::tdesc::infer_struct(), - best::row(), + reflect_internal::tdesc::infer_struct(), + best::row(), }; } @@ -397,9 +397,9 @@ constexpr auto mirror::infer() const requires best::is_enum && (range.try_compute_count({}).has_value()) { return best::mirror{ - reflect_internal::tdesc::template infer_enum< - range.start, *range.try_compute_count({})>(), - best::row(), + reflect_internal::tdesc::template infer_enum< + range.start, *range.try_compute_count({})>(), + best::row(), }; } @@ -493,7 +493,7 @@ constexpr void reflected_type::zip_fields(auto&&... args) const auto row = best::row(best::bind, BEST_FWD(args)...); auto&& cb = BEST_MOVE(row).last(); auto rest = - BEST_MOVE(row).at(best::vals); + BEST_MOVE(row).at(best::vals); return each([&](auto field) -> decltype(auto) { return BEST_MOVE(rest).apply([&](auto&&... structs) -> decltype(auto) { diff --git a/best/meta/reflect_test.cc b/best/meta/reflect_test.cc index 42865cb..b55b651 100644 --- a/best/meta/reflect_test.cc +++ b/best/meta/reflect_test.cc @@ -45,9 +45,9 @@ struct MyType final { constexpr friend auto BestReflect(auto& m, MyType*) { return m.infer() - .with(best::str("foo")) - .with(&MyType::y, MyCallback([] { return 42; })) - .hide(&MyType::transient); + .with(best::str("foo")) + .with(&MyType::y, MyCallback([] { return 42; })) + .hide(&MyType::transient); } constexpr bool operator==(const MyType&) const = default; @@ -81,17 +81,13 @@ best::test FindTag = [](auto& t) { int found = -1; best::reflect.each([&](auto field) { auto tags = field.tags(best::types); - if constexpr (!tags.is_empty()) { - return found = tags.first().callback(); - } + if constexpr (!tags.is_empty()) { return found = tags.first().callback(); } }); t.expect_eq(found, 42); best::reflect.each([&](auto field) { auto tags = field.tags(best::types); - if constexpr (!tags.is_empty()) { - return found = tags.first().callback(); - } + if constexpr (!tags.is_empty()) { return found = tags.first().callback(); } }); t.expect_eq(found, 57); }; @@ -99,16 +95,14 @@ best::test FindTag = [](auto& t) { best::test FindField = [](auto& t) { MyType x0{1, 2, 3, "foo", "bar"}; best::reflect.match( - "x", [&] {}, - [&](auto f) { - if constexpr (best::integer) { - x0->*f = 42; - } - }); + "x", [&] {}, + [&](auto f) { + if constexpr (best::integer) { x0->*f = 42; } + }); t.expect_eq(x0, MyType{42, 2, 3, "foo", "bar"}); auto x1 = best::reflect.match( - "C", [&](auto f) { return f.value; }, [&] { return MyEnum(0); }); + "C", [&](auto f) { return f.value; }, [&] { return MyEnum(0); }); t.expect_eq(x1, MyEnum::C); }; } // namespace best::reflect_test diff --git a/best/meta/taxonomy.h b/best/meta/taxonomy.h index 285d7c3..891fc4d 100644 --- a/best/meta/taxonomy.h +++ b/best/meta/taxonomy.h @@ -237,7 +237,7 @@ concept is_member_ptr = std::is_member_pointer_v; /// Whether this is a function pointer type. template concept is_func_ptr = - best::is_ptr && best::is_func>; + best::is_ptr && best::is_func>; /// # `best::as_ptr` /// @@ -265,15 +265,15 @@ using unptr = std::remove_pointer_t; /// respectively. template concept is_deref = - (is_ptr && (is_void || - std::is_same_v>>)) || - (is_void && requires(T& r) { - operator*(r); - r.operator->(); - }) || requires(const T& cr) { - { operator*(cr) } -> std::convertible_to; - { cr.operator->() } -> std::convertible_to; - }; + (is_ptr && + (is_void || std::is_same_v>>)) || + (is_void && requires(T& r) { + operator*(r); + r.operator->(); + }) || requires(const T& cr) { + { operator*(cr) } -> std::convertible_to; + { cr.operator->() } -> std::convertible_to; + }; /// # `best::addr()` /// diff --git a/best/meta/tlist.h b/best/meta/tlist.h index 6787163..fcc1805 100644 --- a/best/meta/tlist.h +++ b/best/meta/tlist.h @@ -115,8 +115,8 @@ inline constexpr auto indices = [](std::index_sequence) { /// Variadic version of std::is_same/std::same_as. template concept same = - (std::same_as::template type<0, void>> && - ...); + (std::same_as::template type<0, void>> && + ...); /// # `best::tlist<...>` /// @@ -190,8 +190,8 @@ class tlist final { /// specified, in which case that is returned instead. template static constexpr auto value = - type, - tlist_internal::strict, val>>::value; + type, + tlist_internal::strict, val>>::value; /// # `tlist::at()` /// @@ -289,7 +289,7 @@ class tlist final { /// Ouf-of-bounds "writes" are discarded. template static constexpr auto scatter( - best::is_tlist_of_size auto those); + best::is_tlist_of_size auto those); /// # `tlist::trim_prefix()` /// @@ -312,9 +312,7 @@ class tlist final { /// value trait, or a type/value to search for. static constexpr auto find(tlist_internal::t_callable auto pred) { size_t n = -1; - if (((++n, best::call(pred)) || ...)) { - return opt_size(n); - } + if (((++n, best::call(pred)) || ...)) { return opt_size(n); } return opt_size{}; } static constexpr auto find(tlist_internal::v_callable auto pred) { @@ -329,7 +327,7 @@ class tlist final { return find([] { return Trait::value; }); } static constexpr auto find(auto value) - requires(best::equatable && ...) + requires (best::equatable && ...) { return find([&](auto that) { return value == that.value; }); } @@ -338,7 +336,7 @@ class tlist final { /// /// Like `find()`, but requires that the returned element be the unique match. static constexpr auto find_unique( - tlist_internal::t_callable auto pred) { + tlist_internal::t_callable auto pred) { size_t n = -1; size_t found = 0; if ((((found || ++n), best::call(pred) && (++found == 2)) || ...) || @@ -348,7 +346,7 @@ class tlist final { return opt_size{n}; } static constexpr auto find_unique( - tlist_internal::v_callable auto pred) { + tlist_internal::v_callable auto pred) { return find_unique([&] { return best::call(pred, V{}); }); } template @@ -357,12 +355,12 @@ class tlist final { } template