From 6e555ec6ef2208b8f9a063ec5a6d24db117a9e34 Mon Sep 17 00:00:00 2001
From: Elias Pipping <elias.pipping@fu-berlin.de>
Date: Wed, 16 May 2012 15:17:41 +0200
Subject: [PATCH] Use Carsten's enum parser

---
 src/enum_parser.cc      | 23 +++++++++++++++++++++++
 src/enum_state_model.cc | 13 +++++++++++++
 src/enums.hh            |  6 ++++++
 src/one-body-sample.cc  | 22 ++++++++++++++--------
 4 files changed, 56 insertions(+), 8 deletions(-)
 create mode 100644 src/enum_parser.cc
 create mode 100644 src/enum_state_model.cc
 create mode 100644 src/enums.hh

diff --git a/src/enum_parser.cc b/src/enum_parser.cc
new file mode 100644
index 00000000..d8bcd06c
--- /dev/null
+++ b/src/enum_parser.cc
@@ -0,0 +1,23 @@
+// Copyright Carsten Graeser 2012
+
+#include <dune/common/exceptions.hh>
+#include <dune/common/typetraits.hh>
+
+template <class EnumType> struct StringToEnum : public Dune::NotImplemented {};
+
+template <class EnumType>
+typename Dune::enable_if<
+    !Dune::IsBaseOf<Dune::NotImplemented, StringToEnum<EnumType>>::value,
+    std::istream &>::type
+operator>>(std::istream &lhs, EnumType &e) {
+  std::string s;
+  lhs >> s;
+
+  try {
+    e = StringToEnum<EnumType>::convert(s);
+  }
+  catch (typename Dune::Exception) {
+    lhs.setstate(std::ios_base::failbit);
+  }
+  return lhs;
+}
diff --git a/src/enum_state_model.cc b/src/enum_state_model.cc
new file mode 100644
index 00000000..5982c432
--- /dev/null
+++ b/src/enum_state_model.cc
@@ -0,0 +1,13 @@
+#include <dune/common/exceptions.hh>
+
+template <> struct StringToEnum<Config::state_model> {
+  static Config::state_model convert(std::string const &s) {
+    if (s == "Dieterich")
+      return Config::Dieterich;
+
+    if (s == "Ruina")
+      return Config::Ruina;
+
+    DUNE_THROW(Dune::Exception, "failed to parse enum");
+  }
+};
diff --git a/src/enums.hh b/src/enums.hh
new file mode 100644
index 00000000..ce43a1f0
--- /dev/null
+++ b/src/enums.hh
@@ -0,0 +1,6 @@
+struct Config {
+  enum state_model {
+    Dieterich,
+    Ruina
+  };
+};
diff --git a/src/one-body-sample.cc b/src/one-body-sample.cc
index 0f7b7c38..9716d8a8 100644
--- a/src/one-body-sample.cc
+++ b/src/one-body-sample.cc
@@ -67,6 +67,10 @@
 #include "mysolver.hh"
 #include "vtk.hh"
 
+#include "enums.hh"
+#include "enum_parser.cc"
+#include "enum_state_model.cc"
+
 int const dim = 2;
 
 template <class GridView>
@@ -300,14 +304,16 @@ int main(int argc, char *argv[]) {
               // velocity
               // std::cout << std::log(L/unorm * h) << std::endl;
 
-              auto const model =
-                  parset.get<std::string>("boundary.friction.state.model");
-              if (model == std::string("Dieterich"))
-                (*s4_new)[i] = state_update_dieterich(h, unorm / L, s4_old[i]);
-              else if (model == std::string("Ruina"))
-                (*s4_new)[i] = state_update_ruina(h, unorm / L, s4_old[i]);
-              else
-                assert(false);
+              switch (parset.get<Config::state_model>(
+                  "boundary.friction.state.model")) {
+                case Config::Dieterich:
+                  (*s4_new)[i] =
+                      state_update_dieterich(h, unorm / L, s4_old[i]);
+                  break;
+                case Config::Ruina:
+                  (*s4_new)[i] = state_update_ruina(h, unorm / L, s4_old[i]);
+                  break;
+              }
             }
           }
           if (parset.get<bool>("printProgress")) {
-- 
GitLab