From dbbcb93955c79d8d457159c24b52b7f6e6d3bcf2 Mon Sep 17 00:00:00 2001 From: Peter Azmanov Date: Thu, 25 Jul 2019 13:30:17 +0300 Subject: [PATCH 1/5] Fix std::string_view support in transforming validators --- include/CLI/TypeTools.hpp | 7 +++++++ include/CLI/Validators.hpp | 2 +- tests/TransformTest.cpp | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/CLI/TypeTools.hpp b/include/CLI/TypeTools.hpp index e2a0e87bb..0b9a40346 100644 --- a/include/CLI/TypeTools.hpp +++ b/include/CLI/TypeTools.hpp @@ -9,6 +9,9 @@ #include #include #include +#if defined(CLI11_CPP17) +#include +#endif namespace CLI { @@ -72,6 +75,10 @@ template struct IsMemberType { using type = T; }; /// The main custom type needed here is const char * should be a string. template <> struct IsMemberType { using type = std::string; }; +#if defined(CLI11_CPP17) +template <> struct IsMemberType { using type = std::string; }; +#endif + namespace detail { // These are utilities for IsMember diff --git a/include/CLI/Validators.hpp b/include/CLI/Validators.hpp index 536f8a65d..96253a489 100644 --- a/include/CLI/Validators.hpp +++ b/include/CLI/Validators.hpp @@ -503,7 +503,7 @@ auto search(const T &set, const V &val, const std::function &filter_functi // if we haven't found it do the longer linear search with all the element translations auto &setref = detail::smart_deref(set); auto it = std::find_if(std::begin(setref), std::end(setref), [&](decltype(*std::begin(setref)) v) { - V a = detail::pair_adaptor::first(v); + V a{detail::pair_adaptor::first(v)}; a = filter_function(a); return (a == val); }); diff --git a/tests/TransformTest.cpp b/tests/TransformTest.cpp index 776e71db9..7cbb917ee 100644 --- a/tests/TransformTest.cpp +++ b/tests/TransformTest.cpp @@ -102,6 +102,21 @@ TEST_F(TApp, SimpleTransformFn) { EXPECT_EQ(value, 1); } +#if defined(CLI11_CPP17) +TEST_F(TApp, StringViewTransformFn) { + std::string value; + std::map map = + { + // key length > std::string().capacity() [SSO length] + {"a-rather-long-argument", "mapped"} + }; + app.add_option("-s", value)->transform(CLI::CheckedTransformer(map)); + args = {"-s", "a-rather-long-argument"}; + run(); + EXPECT_EQ(value, "mapped"); +} +#endif + TEST_F(TApp, SimpleNumericalTransformFn) { int value; auto opt = From 81e539324b1b94d33ed037c84faa26899426291e Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Thu, 25 Jul 2019 11:14:59 -0400 Subject: [PATCH 2/5] Fix single header --- include/CLI/TypeTools.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/CLI/TypeTools.hpp b/include/CLI/TypeTools.hpp index 0b9a40346..c7f57f587 100644 --- a/include/CLI/TypeTools.hpp +++ b/include/CLI/TypeTools.hpp @@ -9,9 +9,12 @@ #include #include #include + +// [CLI11:verbatim] #if defined(CLI11_CPP17) #include #endif +// [CLI11:verbatim] namespace CLI { From 48f22285842889ae952056f3ddde96e7bc86ba29 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Thu, 25 Jul 2019 11:37:27 -0400 Subject: [PATCH 3/5] Fix formatting --- tests/TransformTest.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/TransformTest.cpp b/tests/TransformTest.cpp index 7cbb917ee..5580efa2c 100644 --- a/tests/TransformTest.cpp +++ b/tests/TransformTest.cpp @@ -105,11 +105,8 @@ TEST_F(TApp, SimpleTransformFn) { #if defined(CLI11_CPP17) TEST_F(TApp, StringViewTransformFn) { std::string value; - std::map map = - { - // key length > std::string().capacity() [SSO length] - {"a-rather-long-argument", "mapped"} - }; + std::map map = {// key length > std::string().capacity() [SSO length] + {"a-rather-long-argument", "mapped"}}; app.add_option("-s", value)->transform(CLI::CheckedTransformer(map)); args = {"-s", "a-rather-long-argument"}; run(); From f5e5e4356b60832211cf239029ab238f1c89b216 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Thu, 25 Jul 2019 12:25:18 -0400 Subject: [PATCH 4/5] Be more careful --- include/CLI/TypeTools.hpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/CLI/TypeTools.hpp b/include/CLI/TypeTools.hpp index c7f57f587..3d8a2f953 100644 --- a/include/CLI/TypeTools.hpp +++ b/include/CLI/TypeTools.hpp @@ -12,7 +12,12 @@ // [CLI11:verbatim] #if defined(CLI11_CPP17) +#if defined(__has_include) +#if __has_include() #include +#define CLI11_HAS_STRING_VIEW +#endif +#endif #endif // [CLI11:verbatim] @@ -78,7 +83,7 @@ template struct IsMemberType { using type = T; }; /// The main custom type needed here is const char * should be a string. template <> struct IsMemberType { using type = std::string; }; -#if defined(CLI11_CPP17) +#ifdef CLI11_HAS_STRING_VIEW template <> struct IsMemberType { using type = std::string; }; #endif From f2fa90a25934d7a096582f4cc56b4395adabbc08 Mon Sep 17 00:00:00 2001 From: Peter Azmanov Date: Thu, 25 Jul 2019 22:47:34 +0300 Subject: [PATCH 5/5] fix string_view test --- tests/TransformTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TransformTest.cpp b/tests/TransformTest.cpp index 5580efa2c..4884a3eea 100644 --- a/tests/TransformTest.cpp +++ b/tests/TransformTest.cpp @@ -102,7 +102,7 @@ TEST_F(TApp, SimpleTransformFn) { EXPECT_EQ(value, 1); } -#if defined(CLI11_CPP17) +#if defined(CLI11_HAS_STRING_VIEW) TEST_F(TApp, StringViewTransformFn) { std::string value; std::map map = {// key length > std::string().capacity() [SSO length]