]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Specialise Bool pattern.
authorLuca Heltai <luca.heltai@sissa.it>
Fri, 21 Jul 2017 11:01:08 +0000 (13:01 +0200)
committerLuca Heltai <luca.heltai@sissa.it>
Sun, 23 Jul 2017 14:57:31 +0000 (16:57 +0200)
include/deal.II/base/patterns.h
tests/base/pattern_tools_01.cc
tests/base/pattern_tools_01.output

index b94d26861dc5cf8205d35c75530b75248eafc48b..c5d0c4e01554c5f86f718290bea3f5c9c6e4ee81 100644 (file)
@@ -1154,7 +1154,9 @@ namespace Patterns
 
       static std::unique_ptr<Patterns::PatternBase> to_pattern()
       {
-        if (std::is_integral<T>::value)
+        if (std::is_same<T,bool>::value)
+          return std_cxx14::make_unique<Patterns::Bool>();
+        else if (std::is_integral<T>::value)
           return std_cxx14::make_unique<Patterns::Integer>(
                    std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
         else if (std::is_floating_point<T>::value)
@@ -1169,6 +1171,8 @@ namespace Patterns
         std::string str;
         if (std::is_same<T, unsigned char>() || std::is_same<T, char>())
           str = std::to_string((int)value);
+        else  if (std::is_same<T,bool>::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<T>::to_pattern())
       {
         AssertThrow(p->match(s), ExcNoMatch(s, *p));
-        std::istringstream is(s);
         T value;
-        if (std::is_same<T, unsigned char>::value || std::is_same<T, char>::value)
+        if (std::is_same<T,bool>::value)
+          value = (s == "true");
+        else
           {
-            int i;
-            is >> i;
-            value = i;
+            std::istringstream is(s);
+            if (std::is_same<T, unsigned char>::value || std::is_same<T, char>::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<Patterns::PatternBase>
                                    &pattern = Convert<T>::to_pattern())
       {
-
         auto p = dynamic_cast<const Patterns::List *>(pattern.get());
         AssertThrow(p, ExcMessage("I need a List pattern to convert a "
                                   "string to a List type."));
index 8737715a5c468a7f62dde4e70ba94c472efae5aa..0a0542e711213349e4eafbac99abb3eb0f755227 100644 (file)
@@ -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;
 }
index 188c128ea87dc66394155d2080ce41286620fbba..6c2cf15ba74c081446ac2f8620d9d2085c718bbf 100644 (file)
@@ -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

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.