From c5841dc37b5f01bc3e768b8e6be9a1aaf36aad3f Mon Sep 17 00:00:00 2001
From: Felix Schindler <felix.schindler@wwu.de>
Date: Mon, 8 Sep 2014 16:02:14 +0200
Subject: [PATCH] [common.string] add specialization of Choose for bool

This adds the possibility to convert 'true' and 'false' together with their
uppercase variants to bool. This allows to use an ini file with dune and
python (as it used to be). Fixes #12
---
 dune/stuff/common/string.hh | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/dune/stuff/common/string.hh b/dune/stuff/common/string.hh
index 1e442482a..e2e721a29 100644
--- a/dune/stuff/common/string.hh
+++ b/dune/stuff/common/string.hh
@@ -347,6 +347,35 @@ public:
   } // ... fromString(...)
 }; // class Choose < const char * >
 
+template <>
+class Choose<bool> : ChooseBase<bool>
+{
+public:
+  static inline bool fromString(const std::string ss, const size_t UNUSED_UNLESS_DEBUG(rows) = 0,
+                                const size_t UNUSED_UNLESS_DEBUG(cols) = 0)
+  {
+    assert(rows == 0);
+    assert(cols == 0);
+    std::string ss_lower_case = ss;
+    std::transform(ss_lower_case.begin(), ss_lower_case.end(), ss_lower_case.begin(), ::tolower);
+    if (ss_lower_case == "true")
+      return true;
+    else if (ss_lower_case == "false")
+      return false;
+    else {
+      try {
+        return boost::lexical_cast<bool, std::string>(ss);
+      } catch (boost::bad_lexical_cast& e) {
+        DUNE_THROW(Exceptions::external_error,
+                   "Error in boost while converting the string '" << ss << "' to type 'bool':\n" << e.what());
+      } catch (std::exception& e) {
+        DUNE_THROW(Exceptions::external_error,
+                   "Error in the stl while converting the string '" << ss << "' to type 'bool':\n" << e.what());
+      }
+    }
+  } // ... fromString(...)
+}; // class Choose < bool >
+
 //! get numerical types from string, using std::sto*
 #define DSC_FRSTR(tn, tns)                                                                                             \
   template <>                                                                                                          \
-- 
GitLab