From a61955bb09d9490dd08534215a655eb76f801535 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Wed, 24 Sep 2025 16:20:21 +0800 Subject: [PATCH] feat: finish summary and application for clap. - finish summary and application for clap. - add patch for utf8 string in std::format. --- src/CMakeLists.txt | 5 ++ src/yycc/carton/clap/application.cpp | 28 ++++++++++ src/yycc/carton/clap/application.hpp | 31 +++++++++++ src/yycc/carton/clap/option.cpp | 7 ++- src/yycc/carton/clap/summary.cpp | 28 ++++++++++ src/yycc/carton/clap/summary.hpp | 27 ++++++++++ src/yycc/carton/clap/variable.cpp | 1 + src/yycc/string/format.hpp | 78 ++++++++++++++++++++++++++++ 8 files changed, 203 insertions(+), 2 deletions(-) create mode 100644 src/yycc/carton/clap/application.cpp create mode 100644 src/yycc/carton/clap/application.hpp create mode 100644 src/yycc/carton/clap/summary.cpp create mode 100644 src/yycc/carton/clap/summary.hpp create mode 100644 src/yycc/string/format.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cdbf96e..808f250 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -33,6 +33,8 @@ PRIVATE yycc/carton/csconsole.cpp yycc/carton/clap/option.cpp yycc/carton/clap/variable.cpp + yycc/carton/clap/summary.cpp + yycc/carton/clap/application.cpp ) target_sources(YYCCommonplace PUBLIC @@ -53,6 +55,7 @@ FILES yycc/string/reinterpret.hpp yycc/string/op.hpp yycc/string/stream.hpp + yycc/string/format.hpp yycc/patch/ptr_pad.hpp yycc/patch/fopen.hpp yycc/num/parse.hpp @@ -88,6 +91,8 @@ FILES yycc/carton/clap/types.hpp yycc/carton/clap/option.hpp yycc/carton/clap/variable.hpp + yycc/carton/clap/summary.hpp + yycc/carton/clap/application.hpp ) # Setup header infomations target_include_directories(YYCCommonplace diff --git a/src/yycc/carton/clap/application.cpp b/src/yycc/carton/clap/application.cpp new file mode 100644 index 0000000..2175eaa --- /dev/null +++ b/src/yycc/carton/clap/application.cpp @@ -0,0 +1,28 @@ +#include "application.hpp" + +#define NS_YYCC_CLAP ::yycc::carton::clap + +namespace yycc::carton::clap::application { + + using Summary = NS_YYCC_CLAP::summary::Summary; + using OptionCollection = NS_YYCC_CLAP::option::OptionCollection; + using VariableCollection = NS_YYCC_CLAP::variable::VariableCollection; + + Application::Application(Summary &&summary, OptionCollection &&options, VariableCollection &&variables) : + summary(std::move(summary)), options(std::move(options)), variables(std::move(variables)) {} + + Application::~Application() {} + + const Summary &Application::get_summary() const { + return this->summary; + } + + const OptionCollection &Application::get_options() const { + return this->options; + } + + const VariableCollection &Application::get_variables() const { + return this->variables; + } + +} // namespace yycc::carton::clap::application diff --git a/src/yycc/carton/clap/application.hpp b/src/yycc/carton/clap/application.hpp new file mode 100644 index 0000000..58c8ead --- /dev/null +++ b/src/yycc/carton/clap/application.hpp @@ -0,0 +1,31 @@ +#pragma once +#include "summary.hpp" +#include "option.hpp" +#include "variable.hpp" + +#define NS_YYCC_CLAP ::yycc::carton::clap + +namespace yycc::carton::clap::application { + + class Application { + public: + Application(NS_YYCC_CLAP::summary::Summary&& summary, + NS_YYCC_CLAP::option::OptionCollection&& options, + NS_YYCC_CLAP::variable::VariableCollection&& variables); + ~Application(); + YYCC_DEFAULT_COPY_MOVE(Application); + + public: + const NS_YYCC_CLAP::summary::Summary& get_summary() const; + const NS_YYCC_CLAP::option::OptionCollection& get_options() const; + const NS_YYCC_CLAP::variable::VariableCollection& get_variables() const; + + private: + NS_YYCC_CLAP::summary::Summary summary; + NS_YYCC_CLAP::option::OptionCollection options; + NS_YYCC_CLAP::variable::VariableCollection variables; + }; + +} + +#undef NS_YYCC_CLAP diff --git a/src/yycc/carton/clap/option.cpp b/src/yycc/carton/clap/option.cpp index f1c56d3..8a72083 100644 --- a/src/yycc/carton/clap/option.cpp +++ b/src/yycc/carton/clap/option.cpp @@ -1,5 +1,6 @@ #include "option.hpp" #include "../../string/op.hpp" +#include "../../string/format.hpp" #include #include @@ -117,7 +118,8 @@ namespace yycc::carton::clap::option { if (short_name.has_value()) { std::u8string short_name_value(short_name.value()); if (this->long_names.contains(short_name_value)) { - throw std::logic_error(std::format("short name {} is duplicated with same long name", short_name_value)); + throw std::logic_error( + std::format("short name {} is duplicated with same long name", short_name_value)); } auto [_, ok] = this->short_names.try_emplace(short_name_value, token); if (!ok) { @@ -128,7 +130,8 @@ namespace yycc::carton::clap::option { if (long_name.has_value()) { std::u8string long_name_value(long_name.value()); if (this->short_names.contains(long_name_value)) { - throw std::logic_error(std::format("long name {} is duplicated with same short name", long_name_value)); + throw std::logic_error( + std::format("long name {} is duplicated with same short name", long_name_value)); } auto [_, ok] = this->long_names.try_emplace(long_name_value, token); if (!ok) { diff --git a/src/yycc/carton/clap/summary.cpp b/src/yycc/carton/clap/summary.cpp new file mode 100644 index 0000000..69d9567 --- /dev/null +++ b/src/yycc/carton/clap/summary.cpp @@ -0,0 +1,28 @@ +#include "summary.hpp" + +namespace yycc::carton::clap::summary { + + Summary::Summary(const std::u8string_view &name, + const std::u8string_view &author, + const std::u8string_view &version, + const std::u8string_view &description) : name(name), author(author), version(version), description(description) {} + + Summary::~Summary() {} + + std::u8string_view Summary::get_name() const { + return this->name; + } + + std::u8string_view Summary::get_author() const { + return this->author; + } + + std::u8string_view Summary::get_version() const { + return this->version; + } + + std::u8string_view Summary::get_description() const { + return this->description; + } + +} // namespace yycc::carton::clap::summary diff --git a/src/yycc/carton/clap/summary.hpp b/src/yycc/carton/clap/summary.hpp new file mode 100644 index 0000000..5de2a0c --- /dev/null +++ b/src/yycc/carton/clap/summary.hpp @@ -0,0 +1,27 @@ +#pragma once +#include "../../macro/class_copy_move.hpp" +#include +#include + +namespace yycc::carton::clap::summary { + + class Summary { + public: + Summary(const std::u8string_view& name, + const std::u8string_view& author, + const std::u8string_view& version, + const std::u8string_view& description); + ~Summary(); + YYCC_DEFAULT_COPY_MOVE(Summary) + + public: + std::u8string_view get_name() const; + std::u8string_view get_author() const; + std::u8string_view get_version() const; + std::u8string_view get_description() const; + + private: + std::u8string name, author, version, description; + }; + +} diff --git a/src/yycc/carton/clap/variable.cpp b/src/yycc/carton/clap/variable.cpp index 6321788..3a3ed1a 100644 --- a/src/yycc/carton/clap/variable.cpp +++ b/src/yycc/carton/clap/variable.cpp @@ -1,4 +1,5 @@ #include "variable.hpp" +#include "../../string/format.hpp" #include #include diff --git a/src/yycc/string/format.hpp b/src/yycc/string/format.hpp new file mode 100644 index 0000000..b2bf73a --- /dev/null +++ b/src/yycc/string/format.hpp @@ -0,0 +1,78 @@ +/** + * @file + * @brief The patch for \c std::format to allow UTF8 string as arguments. + * @details + * As we known, \c std::format only allow \c char and \c wchar_t as its char type in C++ 23 currently. + * So it is impossible to use UTF8 string for std::format, both format string and argument. + * This namespace give a patch for this shortcoming. + * By including this file directly, you will have abilities that use UTF8 string as argument in \c std::format with \c char char type. +*/ +#pragma once +#include "reinterpret.hpp" +#include +#include +#include + +#define NS_YYCC_STRING_REINTERPRET ::yycc::string::reinterpret + +#pragma region Utf8 Formatter + +// Add std::formatter specialization for "char8_t*" +template<> +struct std::formatter { + constexpr auto parse(auto& ctx) { return underlying_formatter.parse(ctx); } + auto format(char8_t* str, auto& ctx) const { return underlying_formatter.format(NS_YYCC_STRING_REINTERPRET::as_ordinary(str), ctx); } + +private: + std::formatter underlying_formatter{}; +}; + +// Add std::formatter specialization for "const char8_t*" +template<> +struct std::formatter { + constexpr auto parse(auto& ctx) { return underlying_formatter.parse(ctx); } + auto format(const char8_t* str, auto& ctx) const { + return underlying_formatter.format(NS_YYCC_STRING_REINTERPRET::as_ordinary(str), ctx); + } + +private: + std::formatter underlying_formatter{}; +}; + +// Add std::formatter specialization for "char8_t[N]" +template +struct std::formatter { + constexpr auto parse(auto& ctx) { return underlying_formatter.parse(ctx); } + auto format(const char8_t (&str)[N], auto& ctx) const { return underlying_formatter.format(std::basic_string_view(str, N), ctx); } + +private: + std::formatter, char> underlying_formatter{}; +}; + +// Add std::formatter specialization for "std::u8string" +template +struct std::formatter, char> { + constexpr auto parse(auto& ctx) { return underlying_formatter.parse(ctx); } + auto format(const std::u8string& str, auto& ctx) const { + return underlying_formatter.format(NS_YYCC_STRING_REINTERPRET::as_ordinary_view(str), ctx); + } + +private: + std::formatter, char> underlying_formatter{}; +}; + +// Add std::formatter specialization for "std::u8string_view" +template +struct std::formatter, char> { + constexpr auto parse(auto& ctx) { return underlying_formatter.parse(ctx); } + auto format(std::u8string_view sv, auto& ctx) const { + return underlying_formatter.format(NS_YYCC_STRING_REINTERPRET::as_ordinary_view(sv), ctx); + } + +private: + std::formatter, char> underlying_formatter{}; +}; + +#pragma endregion + +#undef NS_YYCC_STRING_REINTERPRET