From ee374a679e53938c51f044c0dc7bce6013b4c738 Mon Sep 17 00:00:00 2001 From: Luca Heltai Date: Fri, 21 Jul 2017 13:01:08 +0200 Subject: [PATCH] Specialise Bool pattern. --- include/deal.II/base/patterns.h | 48 +++++++++++++++++------------- tests/base/pattern_tools_01.cc | 4 +++ tests/base/pattern_tools_01.output | 8 +++++ 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/include/deal.II/base/patterns.h b/include/deal.II/base/patterns.h index b94d26861d..c5d0c4e015 100644 --- a/include/deal.II/base/patterns.h +++ b/include/deal.II/base/patterns.h @@ -1154,7 +1154,9 @@ namespace Patterns static std::unique_ptr to_pattern() { - if (std::is_integral::value) + if (std::is_same::value) + return std_cxx14::make_unique(); + else if (std::is_integral::value) return std_cxx14::make_unique( std::numeric_limits::min(), std::numeric_limits::max()); else if (std::is_floating_point::value) @@ -1169,6 +1171,8 @@ namespace Patterns std::string str; if (std::is_same() || std::is_same()) str = std::to_string((int)value); + else if (std::is_same::value) + str = value ? "true" : "false"; else str = std::to_string(value); AssertThrow(p->match(str), ExcNoMatch(str, *p)); @@ -1180,27 +1184,32 @@ namespace Patterns Convert::to_pattern()) { AssertThrow(p->match(s), ExcNoMatch(s, *p)); - std::istringstream is(s); T value; - if (std::is_same::value || std::is_same::value) + if (std::is_same::value) + value = (s == "true"); + else { - int i; - is >> i; - value = i; + std::istringstream is(s); + if (std::is_same::value || std::is_same::value) + { + int i; + is >> i; + value = i; + } + else + is >> value; + + // If someone passes "123 abc" to the function, the method yelds an + // integer 123 alright, but the space terminates the read from the string + // although there is more to come. This case, however, is checked for in + // the call p->match(s) at the beginning of this function, and would + // throw earlier. Here it is safe to assume that if we didn't fail the + // conversion with the operator >>, then we are good to go. + AssertThrow(!is.fail(), + ExcMessage("Failed to convert from \"" + s + + "\" to the type \"" + + boost::core::demangle(typeid(T).name()) + "\"")); } - else - is >> value; - - // If someone passes "123 abc" to the function, the method yelds an - // integer 123 alright, but the space terminates the read from the string - // although there is more to come. This case, however, is checked for in - // the call p->match(s) at the beginning of this function, and would - // throw earlier. Here it is safe to assume that if we didn't fail the - // conversion with the operator >>, then we are good to go. - AssertThrow(!is.fail(), - ExcMessage("Failed to convert from \"" + s + - "\" to the type \"" + - boost::core::demangle(typeid(T).name()) + "\"")); return value; } }; @@ -1318,7 +1327,6 @@ namespace Patterns const std::unique_ptr &pattern = Convert::to_pattern()) { - auto p = dynamic_cast(pattern.get()); AssertThrow(p, ExcMessage("I need a List pattern to convert a " "string to a List type.")); diff --git a/tests/base/pattern_tools_01.cc b/tests/base/pattern_tools_01.cc index 8737715a5c..0a0542e711 100644 --- a/tests/base/pattern_tools_01.cc +++ b/tests/base/pattern_tools_01.cc @@ -49,12 +49,16 @@ int main() types::boundary_id t2 = 3; std::string t3 = "Ciao"; double t4 = 4.0; + bool t5 = true; + float t6 = 5.5; test(t0); test(t1); test(t2); test(t3); test(t4); + test(t5); + test(t6); return 0; } diff --git a/tests/base/pattern_tools_01.output b/tests/base/pattern_tools_01.output index 188c128ea8..6c2cf15ba7 100644 --- a/tests/base/pattern_tools_01.output +++ b/tests/base/pattern_tools_01.output @@ -18,3 +18,11 @@ DEAL::Type : double DEAL::Pattern : [Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)] DEAL::To String: 4.000000 DEAL::To value : 4.000000 +DEAL::Type : bool +DEAL::Pattern : [Bool] +DEAL::To String: true +DEAL::To value : true +DEAL::Type : float +DEAL::Pattern : [Double -3.40282e+38...3.40282e+38 (inclusive)] +DEAL::To String: 5.500000 +DEAL::To value : 5.500000 -- 2.39.5