From 5b402e48ddde35aa6623fa4d67297f9bfb7b31d7 Mon Sep 17 00:00:00 2001 From: sha512sum Date: Sat, 24 Aug 2024 23:52:08 +0000 Subject: [PATCH] Fixes for fmt --- CMakeLists.txt | 2 - examples/src/menu.cpp | 13 ---- src/constexpr_string.cpp | 23 ------ src/menu.cpp | 147 --------------------------------------- src/utempl.cpp | 1 - tests/menu.cpp | 23 ------ 6 files changed, 209 deletions(-) delete mode 100644 examples/src/menu.cpp delete mode 100644 src/menu.cpp delete mode 100644 tests/menu.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bf49331..3aa1d5b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,7 +48,6 @@ CPMAddPackage( URL "https://github.com/linuxnyasha/fmt_import/archive/refs/heads/linuxnyasha-patch-1.zip" # Fixes for fmt module and constexpr fmt::formatted_size - OPTIONS "CMAKE_CXX_FLAGS -DFMT_ATTACH_TO_GLOBAL_MODULE" ) set(CPM_USE_LOCAL_PACKAGES ${TMP}) @@ -98,7 +97,6 @@ endif() if(BUILD_STATIC_LIBS) add_library(utempl_static) if(NOT BUILD_SHARED_LIBS) - add_library(utempl ALIAS utempl_static) add_library(utempl::utempl ALIAS utempl_static) endif() configure(utempl_static) diff --git a/examples/src/menu.cpp b/examples/src/menu.cpp deleted file mode 100644 index 495f882..0000000 --- a/examples/src/menu.cpp +++ /dev/null @@ -1,13 +0,0 @@ -import utempl; -import std; - -auto main() -> int { - utempl::menu::Menu{} - .With<{"This is 0"}>([] { - std::cout << "You entered 0" << std::endl; - }) - .With<{"Some Long", "S"}>([] { - std::cout << "It aligns the output to the longest element" << std::endl; - }) - .Run<"[{0}]{2} - |{1}|\n">(); -}; diff --git a/src/constexpr_string.cpp b/src/constexpr_string.cpp index 5753fe2..910aecc 100644 --- a/src/constexpr_string.cpp +++ b/src/constexpr_string.cpp @@ -1,25 +1,7 @@ -module; -#include export module utempl.string; import std; // import fmt; -namespace utempl { -export template -struct ConstexprString; -} // namespace utempl - -export { - template - struct fmt::formatter> : public fmt::formatter { - constexpr auto parse(format_parse_context& ctx) const { - return ctx.begin(); - }; - constexpr auto format(const utempl::ConstexprString& str, auto& ctx) const { - return fmt::formatter::format({str.begin()}, ctx); - }; - }; -}; export namespace utempl { template @@ -77,11 +59,6 @@ struct ConstexprString { constexpr ConstexprString(ConstexprString&&) = default; }; -template -consteval auto GetFmtCompiledString() { - return FMT_COMPILE(Str.data.begin()); -}; - template constexpr auto operator<<(std::ostream& stream, const ConstexprString& str) -> std::ostream& { stream << static_cast(str); diff --git a/src/menu.cpp b/src/menu.cpp deleted file mode 100644 index db83b80..0000000 --- a/src/menu.cpp +++ /dev/null @@ -1,147 +0,0 @@ -module; -#include -export module utempl.menu; - -import fmt; -import std; -import utempl.string; -import utempl.optional; -import utempl.tuple; -import utempl.utils; -import utempl.type_list; - -namespace utempl { - -constexpr auto CountDigits(std::size_t num) -> std::size_t { - std::size_t count = 0; - do { // NOLINT - ++count; - num /= 10; // NOLINT - } while(num != 0); - return count; -}; - -constexpr auto GetDigit(std::size_t num, std::size_t index) -> std::size_t { - for(std::size_t i = 0; i < index; ++i) { - num /= 10; // NOLINT - } - return num % 10; // NOLINT -}; - -template -consteval auto ToString() { - constexpr std::size_t digits = CountDigits(num); - return [&](std::index_sequence) { - return ConstexprString{std::array{static_cast('0' + GetDigit(num, digits - 1 - Is))..., '\0'}}; - }(std::make_index_sequence()); -}; - -template -constexpr auto GetMax(Range&& range) { - std::remove_cvref_t response = 0; - for(const auto& element : range) { - response = element > response ? element : response; - }; - return response; -}; - -namespace menu { - -template -struct CallbackMessage { - ConstexprString message; - Optional> need; - consteval CallbackMessage(const char (&need)[N2], const char (&message)[N1]) : // NOLINT - message(std::move(message)), need(std::move(need)) {}; - consteval CallbackMessage(const char (&message)[N1]) : message(std::move(message)), need(std::nullopt) {}; // NOLINT -}; -template -CallbackMessage(const char (&)[N1], const char (&)[N2]) -> CallbackMessage; // NOLINT - -template -CallbackMessage(const char (&)[N1]) -> CallbackMessage; // NOLINT - -export template -struct Menu { - private: - template - static consteval auto FormatMessage() { - constexpr auto fmtlib = GetFmtCompiledString(); - // + 1 - NULL Terminator - constexpr auto size = fmt::formatted_size(fmtlib, - static_cast(neededInput), - static_cast(message), - static_cast(alignment)) + - 1; - std::array data{}; - fmt::format_to(data.begin(), - fmtlib, - static_cast(neededInput), - static_cast(message), - static_cast(alignment)); - return ConstexprString(data); - }; - template - static consteval auto FormatMessageFor() { - constexpr ConstexprString message = Get(storage).message; - constexpr ConstexprString neededInput = [&] { - if constexpr(Get(storage).need) { - return *Get(storage).need; - } else { - return ToString(); - }; - }(); - constexpr ConstexprString alignment = - CreateStringWith(storage).need ? Get(storage).need->size() : CountDigits(I))>(' '); - return FormatMessage(); - }; - - public: - Tuple functionStorage; - - static consteval auto GetMaxSize() -> std::size_t { - return [&](std::index_sequence) { - constexpr auto list = ListFromTuple(storage); - return GetMax(std::array{(std::remove_cvref_t(list))>().need)>::kSize != 0 - ? std::remove_cvref_t(list))>().need)>::kSize - : CountDigits(Is))...}); - }(std::index_sequence_for()); - }; - template - constexpr auto With(F&& f) const { - return Menu>{.functionStorage = - this->functionStorage + Tuple(std::forward(f))}; - }; - template - constexpr auto Run(std::istream& in = std::cin, std::FILE* out = stdout) const -> std::size_t { - return [&](std::index_sequence) -> std::size_t { - constexpr auto message = ((FormatMessageFor() + ...) + enter); - auto result = std::fwrite(message.begin(), 1, message.size(), out); - if(result < message.size()) { - return EOF; - }; - if(std::fflush(out) != 0) { - return EOF; - }; - std::string input; - std::getline(in, input); - ( - [&](storage)>(Wrapper) { - if constexpr(message.need) { - if(*message.need == input) { - Get(this->functionStorage)(); - }; - } else { - if(ToString() == input) { - Get(this->functionStorage)(); - }; - }; - }(Wrapper{}), - ...); - return 0; - }(std::index_sequence_for()); - }; -}; - -} // namespace menu -} // namespace utempl diff --git a/src/utempl.cpp b/src/utempl.cpp index 0d5c55e..ee0a829 100644 --- a/src/utempl.cpp +++ b/src/utempl.cpp @@ -2,7 +2,6 @@ export module utempl; export import utempl.type_list; export import utempl.tuple; export import utempl.string; -export import utempl.menu; export import utempl.loopholes; export import utempl.attributes; export import utempl.meta_info; diff --git a/tests/menu.cpp b/tests/menu.cpp deleted file mode 100644 index 3cd2aae..0000000 --- a/tests/menu.cpp +++ /dev/null @@ -1,23 +0,0 @@ -module; -#include -export module tests.menu; -import utempl; - -namespace utempl { - -TEST(Menu, Basic) { - testing::internal::CaptureStdout(); - std::istringstream stream("t\n"); - int value = 0; - menu::Menu{} - .With<{"t", "This is t"}>([&] { - std::cout << "Success!" << std::endl; - value = 1; - }) - .Run<"[{0}]{2} - ({1})\n">(stream); - auto captured = testing::internal::GetCapturedStdout(); - EXPECT_EQ(captured, "[t] - (This is t)\n|> Success!\n"); - EXPECT_EQ(value, 1); -}; - -} // namespace utempl