From 1fe1fe337d8284f6e6eb35d721ade964f24438e0 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 7 Feb 2022 20:11:55 +0000 Subject: Remove all pre-C++20 compile time formatter support --- libadhocutil/compileTimeFormatter.h | 79 +++++----------------- libadhocutil/ctf-impl/printf-compat.h | 4 +- .../unittests/testCompileTimeFormatter.cpp | 43 +----------- 3 files changed, 21 insertions(+), 105 deletions(-) diff --git a/libadhocutil/compileTimeFormatter.h b/libadhocutil/compileTimeFormatter.h index 33fe677..be57fee 100644 --- a/libadhocutil/compileTimeFormatter.h +++ b/libadhocutil/compileTimeFormatter.h @@ -1,25 +1,17 @@ #ifndef ADHOCUTIL_COMPILE_TIME_FORMATTER_H #define ADHOCUTIL_COMPILE_TIME_FORMATTER_H +#include #include // IWYU pragma: keep #include // IWYU pragma: keep +#include +#include #include #include // IWYU pragma: export #include // Mapped for for BOOST_PP_VARIADIC_SIZE, BOOST_PP... in tests // IWYU pragma: no_include -#ifdef __cpp_nontype_template_parameter_class -# define USE_FIXED_STRING -#endif - -#ifdef USE_FIXED_STRING -# define CtfString const auto -# include -# include -#else -# define CtfString const auto & -#endif namespace AdHoc { // Template char utils template @@ -37,7 +29,7 @@ namespace AdHoc { } // Template string utils - template + template static constexpr auto strlen() { @@ -59,7 +51,7 @@ namespace AdHoc { return off; } - template()> + template()> static constexpr std::optional strchr() { @@ -74,7 +66,7 @@ namespace AdHoc { return off; } - template()> + template()> static constexpr decltype(L) strchrnul() { @@ -85,10 +77,10 @@ namespace AdHoc { return off; } - template class FormatterDetail; + template class FormatterDetail; /// Template used to apply parameters to a stream. - template struct StreamWriter { + template struct StreamWriter { /// Write parameters to stream. template static void @@ -99,7 +91,7 @@ namespace AdHoc { }; /// Helper to simplify implementations of StreamWriter. - template struct StreamWriterBase { + template struct StreamWriterBase { /// Continue processing parameters. template static inline void @@ -110,12 +102,12 @@ namespace AdHoc { }; #define StreamWriterT(...) \ - template \ + template \ struct StreamWriter : \ public StreamWriterBase #define StreamWriterTP(P, ...) \ - template \ + template \ struct StreamWriter : \ public StreamWriterBase @@ -159,10 +151,10 @@ namespace AdHoc { * Compile time string formatter. * @param S the format string. */ - template class FormatterDetail { + template class FormatterDetail { private: using strlen_t = decltype(strlen()); - template friend struct StreamWriterBase; + template friend struct StreamWriterBase; public: /// The derived charater type of the format string. @@ -246,18 +238,18 @@ namespace AdHoc { }; }; -#ifdef USE_FIXED_STRING - // New C++20 implementation namespace support { template class basic_fixed_string : public std::array { public: // cppcheck-suppress noExplicitConstructor + // NOLINTNEXTLINE(hicpp-avoid-c-arrays,modernize-avoid-c-arrays,hicpp-explicit-conversions) constexpr basic_fixed_string(const CharT (&str)[N + 1]) { for (decltype(N) x = 0; x < N; x++) { this->at(x) = str[x]; } } + // NOLINTNEXTLINE(hicpp-avoid-c-arrays,modernize-avoid-c-arrays) constexpr basic_fixed_string(const CharT * str, decltype(N) len) { for (decltype(N) x = 0; x < len; x++) { @@ -267,7 +259,8 @@ namespace AdHoc { }; template - basic_fixed_string(const CharT (&str)[N]) -> basic_fixed_string; + // NOLINTNEXTLINE(hicpp-avoid-c-arrays,modernize-avoid-c-arrays) + basic_fixed_string(const CharT (&str)[N])->basic_fixed_string; } template class LiteralFormatter : public FormatterDetail { @@ -278,7 +271,7 @@ namespace AdHoc { public FormatterDetail::type, L>(S, L), L> { }; -# define AdHocFormatter(name, str) using name = ::AdHoc::LiteralFormatter +#define AdHocFormatter(name, str) using name = ::AdHoc::LiteralFormatter template inline auto @@ -301,47 +294,11 @@ namespace AdHoc { return scprintf(std::cout, pn...); } -#else - // Classic pre-C++20 implementation -# include "unique.h" - template()) L = strlen()> class Formatter : public FormatterDetail { - }; - -# define AdHocFormatterTypedef(name, str, id) \ - inline constexpr auto id = str; \ - using name = ::AdHoc::Formatter -# define AdHocFormatter(name, str) AdHocFormatterTypedef(name, str, MAKE_UNIQUE(name)) - -#endif - namespace literals { -#ifdef USE_FIXED_STRING template constexpr inline auto operator""_fmt() noexcept { return AdHoc::FormatterDetail(); } -#else -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wpedantic" -# ifdef __clang__ -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wgnu-string-literal-operator-template" -# endif - /// CTF format string holder - template struct FMT { - /// CTF format string - // NOLINTNEXTLINE(hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - static constexpr char fmtstr[] = {t...}; - }; - template inline auto operator""_fmt() noexcept - { - return AdHoc::FormatterDetail::fmtstr, sizeof...(t)>(); - } -# ifdef __clang__ -# pragma clang diagnostic pop -# endif -# pragma GCC diagnostic pop -#endif } } diff --git a/libadhocutil/ctf-impl/printf-compat.h b/libadhocutil/ctf-impl/printf-compat.h index 1ca30c3..40508f7 100644 --- a/libadhocutil/ctf-impl/printf-compat.h +++ b/libadhocutil/ctf-impl/printf-compat.h @@ -131,7 +131,7 @@ namespace AdHoc { BOOST_PP_CAT(data, n) #define ISDIGIT(z, n, data) &&isdigit(BOOST_PP_CAT(data, BOOST_PP_ADD(n, 1))) #define FMTWIDTH(unused, d, data) \ - template \ struct StreamWriter::type, '%', \ @@ -147,7 +147,7 @@ namespace AdHoc { }; BOOST_PP_REPEAT(6, FMTWIDTH, void) #define FMTPRECISION(unused, d, data) \ - template \ struct StreamWriter::type, '%', '.', \ diff --git a/libadhocutil/unittests/testCompileTimeFormatter.cpp b/libadhocutil/unittests/testCompileTimeFormatter.cpp index fc45dba..a79e369 100644 --- a/libadhocutil/unittests/testCompileTimeFormatter.cpp +++ b/libadhocutil/unittests/testCompileTimeFormatter.cpp @@ -10,9 +10,9 @@ #include #include #include +#include #include #include -#include #include #include #include @@ -93,45 +93,6 @@ namespace AdHoc { }; } -#ifndef __cpp_nontype_template_parameter_class -// Compile string util assertions -static_assert(strlen() == 0); -static_assert(strlen() == 1); -static_assert(strlen() == 2); -static_assert(strlen() == 7); -static_assert(strlen() == 246); - -static_assert(!strchr()); -static_assert(!strchr()); -static_assert(*strchr() == 0); -static_assert(*strchr() == 0); -static_assert(*strchr() == 1); -static_assert(*strchr() == 3); -static_assert(!strchr()); -static_assert(*strchr() == 3); -static_assert(!strchr()); -static_assert(!strchr()); - -static_assert(strchrnul() == 0); -static_assert(strchrnul() == 1); -static_assert(strchrnul() == 0); -static_assert(strchrnul() == 0); -static_assert(strchrnul() == 1); -static_assert(strchrnul() == 3); -static_assert(strchrnul() == 7); -static_assert(strchrnul() == 3); -static_assert(strchrnul() == 7); -static_assert(strchrnul() == 7); - -static_assert(strchrnul() == 0); -static_assert(strchrnul() == 1); -static_assert(strchrnul() == 0); -static_assert(strchrnul() == 0); -static_assert(strchrnul() == 1); -static_assert(strchrnul() == 3); -static_assert(strchrnul() == 7); -#endif - BOOST_FIXTURE_TEST_SUITE(TestStreamWrite, std::stringstream) BOOST_AUTO_TEST_CASE(empty) @@ -464,7 +425,6 @@ BOOST_AUTO_TEST_CASE(smartptr) smartptr_fmt::get(shrd); } -#ifdef __cpp_nontype_template_parameter_class BOOST_AUTO_TEST_CASE(literal_format_string) { std::stringstream str; @@ -511,7 +471,6 @@ BOOST_AUTO_TEST_CASE(scprintf_get_no_args) auto stng = scprintf<"Some literal format string.">(); BOOST_CHECK_EQUAL(stng, "Some literal format string."); } -#endif using namespace AdHoc::literals; -- cgit v1.2.3