From 35d244109572f370fb32e3cfd23e163182182474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rimas=20Misevi=C4=8Dius?= Date: Thu, 24 Oct 2024 00:07:20 +0300 Subject: [PATCH] Simplify str_arg_char template specialization * One string class parameter is used instead of the parameter pack * Simplified use of the str_arg constructor with string pointer and length parameters - length can now be any integer. --- include/upa/str_arg.h | 86 ++++++++++++++------------------------- include/upa/url_for_atl.h | 2 +- include/upa/url_for_qt.h | 6 +-- test/test-str_arg.cpp | 47 ++++++++++----------- 4 files changed, 57 insertions(+), 84 deletions(-) diff --git a/include/upa/str_arg.h b/include/upa/str_arg.h index 9bd550a6..8d3595ce 100644 --- a/include/upa/str_arg.h +++ b/include/upa/str_arg.h @@ -6,9 +6,9 @@ /************************************************************** // Usage example: -template = 0> -inline void procfn(Args&&... args) { - const auto inp = make_str_arg(std::forward(args)...); +template = 0> +inline void procfn(StrT&& str) { + const auto inp = make_str_arg(std::forward(str)); const auto* first = inp.begin(); const auto* last = inp.end(); // do something with first ... last @@ -86,18 +86,17 @@ class str_arg { : first_(s) , last_(s + traits_type::length(s)) {} - str_arg(const CharT* s, std::size_t length) - : first_(s) - , last_(s + length) - {} - str_arg(const CharT* s, std::ptrdiff_t length) + + template ::value, int>::type = 0> + str_arg(const CharT* s, SizeT length) : first_(s) , last_(s + length) - {} + { assert(length >= 0); } + str_arg(const CharT* first, const CharT* last) : first_(first) , last_(last) - {} + { assert(first <= last); } // destructor ~str_arg() noexcept = default; @@ -160,33 +159,10 @@ namespace detail { // Requirements for string arguments -template +template struct str_arg_char {}; -// two pointers -template -struct str_arg_char : std::remove_cv { - - template - static str_arg to_str_arg(const T* first, const T* last) { - assert(first <= last); - return { first, last }; - } -}; - -// pointer and size -template -struct str_arg_char : std::enable_if< - is_size_type::value, - typename std::remove_cv::type> { - - template - static str_arg to_str_arg(const T* s, std::size_t length) { - return { s, length }; - } -}; - -// one pointer (null terminated string) +// Null terminated string template struct str_arg_char : std::remove_cv { @@ -196,7 +172,7 @@ struct str_arg_char : std::remove_cv { } }; -// one string class argument +// String that has data() and length() members template struct str_arg_char : std::enable_if< std::is_pointer>::value && @@ -212,24 +188,24 @@ struct str_arg_char : std::enable_if< // String arguments helper types -template -using str_arg_char_s = str_arg_char::type...>; +template +using str_arg_char_s = str_arg_char::type>; -template -using str_arg_char_t = typename str_arg_char_s::type; +template +using str_arg_char_t = typename str_arg_char_s::type; -template +template using enable_if_str_arg_t = typename std::enable_if< - is_char_type>::value, + is_char_type>::value, int>::type; // String arguments helper function -template -inline auto make_str_arg(Args&&... args) -> str_arg> { - return str_arg_char_s::to_str_arg(std::forward(args)...); +template +inline auto make_str_arg(StrT&& str) -> str_arg> { + return str_arg_char_s::to_str_arg(std::forward(str)); } @@ -243,9 +219,9 @@ struct is_char8_type : std::integral_constant {}; -template +template using enable_if_str_arg_to_char8_t = typename std::enable_if< - is_char8_type>::value, + is_char8_type>::value, int>::type; template @@ -255,9 +231,9 @@ struct is_charW_type : std::integral_constant::value > {}; -template +template using enable_if_str_arg_to_charW_t = typename std::enable_if< - is_charW_type>::value, + is_charW_type>::value, int>::type; @@ -265,15 +241,15 @@ inline std::string&& make_string(std::string&& str) { return std::move(str); } -template = 0> -inline string_view make_string(Args&&... args) { - const auto inp = make_str_arg(std::forward(args)...); +template = 0> +inline string_view make_string(StrT&& str) { + const auto inp = make_str_arg(std::forward(str)); return { inp.data(), inp.length() }; } -template = 0> -inline std::string make_string(Args&&... args) { - const auto inp = make_str_arg(std::forward(args)...); +template = 0> +inline std::string make_string(StrT&& str) { + const auto inp = make_str_arg(std::forward(str)); return url_utf::to_utf8_string(inp.begin(), inp.end()); } diff --git a/include/upa/url_for_atl.h b/include/upa/url_for_atl.h index bd19e4e6..9b08dbfc 100644 --- a/include/upa/url_for_atl.h +++ b/include/upa/url_for_atl.h @@ -24,7 +24,7 @@ struct str_arg_char_for_atl { using type = typename StrT::XCHAR; static str_arg to_str_arg(const StrT& str) { - return { str.GetString(), static_cast(str.GetLength()) }; + return { str.GetString(), str.GetLength() }; } }; diff --git a/include/upa/url_for_qt.h b/include/upa/url_for_qt.h index bcaa01a4..ea031a1d 100644 --- a/include/upa/url_for_qt.h +++ b/include/upa/url_for_qt.h @@ -25,7 +25,6 @@ #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) # include #endif -#include // std::ptrdiff_t namespace upa { @@ -36,10 +35,7 @@ struct str_arg_char_for_qt { using type = CharT; static str_arg to_str_arg(const StrT& str) { - return { - reinterpret_cast(str.data()), - static_cast(str.length()) - }; + return { reinterpret_cast(str.data()), str.length() }; } }; diff --git a/test/test-str_arg.cpp b/test/test-str_arg.cpp index 623a3a1c..76fbd8e0 100644 --- a/test/test-str_arg.cpp +++ b/test/test-str_arg.cpp @@ -13,11 +13,9 @@ // Function to test -using namespace upa; - -template = 0> -inline std::size_t procfn(Args&&... args) { - const auto inp = make_str_arg(std::forward(args)...); +template = 0> +inline std::size_t procfn(StrT&& str) { + const auto inp = upa::make_str_arg(std::forward(str)); return std::distance(inp.begin(), inp.end()); } @@ -54,7 +52,7 @@ struct str_arg_char> { template inline void test_char() { - const size_t N = 3; + const std::size_t N = 3; CharT arr[] = { '1', '2', '3', 0 }; const CharT carr[] = { '1', '2', '3', 0 }; CharT* ptr = arr; @@ -67,30 +65,33 @@ inline void test_char() { procfn(cptr); procfn(vptr); - procfn(arr, N); - procfn(carr, N); - procfn(ptr, N); - procfn(cptr, N); - procfn(vptr, N); - + // upa::str_arg + upa::str_arg arg(arr); + procfn(arg); + const upa::str_arg carg(arr); + procfn(carg); + + procfn(upa::str_arg{arr, N}); + procfn(upa::str_arg{carr, N}); + procfn(upa::str_arg{ptr, N}); + procfn(upa::str_arg{cptr, N}); + procfn(upa::str_arg{vptr, N}); // int size - procfn(arr, int(N)); - procfn(cptr, int(N)); + procfn(upa::str_arg(arr, static_cast(N))); + procfn(upa::str_arg(cptr, static_cast(N))); - procfn(arr, arr + N); - procfn(carr, carr + N); - procfn(ptr, ptr + N); - procfn(cptr, cptr + N); - procfn(vptr, vptr + N); + procfn(upa::str_arg{arr, arr + N}); + procfn(upa::str_arg{carr, carr + N}); + procfn(upa::str_arg{ptr, ptr + N}); + procfn(upa::str_arg{cptr, cptr + N}); + procfn(upa::str_arg{vptr, vptr + N}); + // std::basic_string const std::basic_string str(arr); procfn(str); procfn(std::basic_string(arr)); - upa::str_arg arg(arr); - procfn(arg); - - // test custom string + // custom string procfn(CustomString{cptr, N}); }