From 1e54a73ff53768ee35778e9ab55eab4da76d78c8 Mon Sep 17 00:00:00 2001 From: Elias Pipping <elias.pipping@fu-berlin.de> Date: Sat, 19 Jul 2014 21:43:40 +0200 Subject: [PATCH] [Cleanup] Refactor enums --- src/Makefile.am | 1 + src/enum_friction_model.cc | 15 -------- src/enum_parser.cc | 23 ------------ src/enum_scheme.cc | 15 -------- src/enum_state_model.cc | 15 -------- src/enum_verbosity.cc | 17 --------- src/enumparser.cc | 77 ++++++++++++++++++++++++++++++++++++++ src/enumparser.hh | 35 +++++++++++++++++ src/sand-wedge.cc | 6 +-- 9 files changed, 114 insertions(+), 90 deletions(-) delete mode 100644 src/enum_friction_model.cc delete mode 100644 src/enum_parser.cc delete mode 100644 src/enum_scheme.cc delete mode 100644 src/enum_state_model.cc delete mode 100644 src/enum_verbosity.cc create mode 100644 src/enumparser.cc create mode 100644 src/enumparser.hh diff --git a/src/Makefile.am b/src/Makefile.am index 42c6012a..5908465c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,6 +3,7 @@ bin_PROGRAMS = sand-wedge-2D sand-wedge-3D common_sources = \ assemblers.cc \ boundary_writer.cc \ + enumparser.cc \ friction_writer.cc \ solverfactory.cc \ state.cc \ diff --git a/src/enum_friction_model.cc b/src/enum_friction_model.cc deleted file mode 100644 index e8a7d066..00000000 --- a/src/enum_friction_model.cc +++ /dev/null @@ -1,15 +0,0 @@ -#include <dune/common/exceptions.hh> - -#include "enums.hh" - -template <> struct StringToEnum<Config::FrictionModel> { - static Config::FrictionModel convert(std::string const &s) { - if (s == "Truncated") - return Config::Truncated; - - if (s == "Regularised") - return Config::Regularised; - - DUNE_THROW(Dune::Exception, "failed to parse enum"); - } -}; diff --git a/src/enum_parser.cc b/src/enum_parser.cc deleted file mode 100644 index 041983c9..00000000 --- a/src/enum_parser.cc +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright Carsten Graeser 2012 - -#include <dune/common/exceptions.hh> -#include <dune/common/typetraits.hh> - -template <class Enum> struct StringToEnum : public Dune::NotImplemented {}; - -template <class Enum> -typename Dune::enable_if< - !Dune::IsBaseOf<Dune::NotImplemented, StringToEnum<Enum>>::value, - std::istream &>::type -operator>>(std::istream &lhs, Enum &e) { - std::string s; - lhs >> s; - - try { - e = StringToEnum<Enum>::convert(s); - } - catch (typename Dune::Exception) { - lhs.setstate(std::ios_base::failbit); - } - return lhs; -} diff --git a/src/enum_scheme.cc b/src/enum_scheme.cc deleted file mode 100644 index bcf55059..00000000 --- a/src/enum_scheme.cc +++ /dev/null @@ -1,15 +0,0 @@ -#include <dune/common/exceptions.hh> - -#include "enums.hh" - -template <> struct StringToEnum<Config::scheme> { - static Config::scheme convert(std::string const &s) { - if (s == "newmark") - return Config::Newmark; - - if (s == "backwardEuler") - return Config::BackwardEuler; - - DUNE_THROW(Dune::Exception, "failed to parse enum"); - } -}; diff --git a/src/enum_state_model.cc b/src/enum_state_model.cc deleted file mode 100644 index 37902aac..00000000 --- a/src/enum_state_model.cc +++ /dev/null @@ -1,15 +0,0 @@ -#include <dune/common/exceptions.hh> - -#include "enums.hh" - -template <> struct StringToEnum<Config::stateModel> { - static Config::stateModel convert(std::string const &s) { - if (s == "AgeingLaw") - return Config::AgeingLaw; - - if (s == "SlipLaw") - return Config::SlipLaw; - - DUNE_THROW(Dune::Exception, "failed to parse enum"); - } -}; diff --git a/src/enum_verbosity.cc b/src/enum_verbosity.cc deleted file mode 100644 index eee21da1..00000000 --- a/src/enum_verbosity.cc +++ /dev/null @@ -1,17 +0,0 @@ -#include <dune/solvers/common/numproc.hh> // Solver::VerbosityMode -#include <dune/common/exceptions.hh> - -template <> struct StringToEnum<Solver::VerbosityMode> { - static Solver::VerbosityMode convert(std::string const &s) { - if (s == "full") - return Solver::FULL; - - if (s == "reduced") - return Solver::REDUCED; - - if (s == "quiet") - return Solver::QUIET; - - DUNE_THROW(Dune::Exception, "failed to parse enum"); - } -}; diff --git a/src/enumparser.cc b/src/enumparser.cc new file mode 100644 index 00000000..c081ec49 --- /dev/null +++ b/src/enumparser.cc @@ -0,0 +1,77 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +// Copyright Carsten Graeser 2012 + +#include <dune/common/exceptions.hh> + +#include "enumparser.hh" + +template <class Enum> +typename Dune::enable_if< + !Dune::IsBaseOf<Dune::NotImplemented, StringToEnum<Enum>>::value, + std::istream &>::type +operator>>(std::istream &lhs, Enum &e) { + std::string s; + lhs >> s; + + try { + e = StringToEnum<Enum>::convert(s); + } + catch (typename Dune::Exception) { + lhs.setstate(std::ios_base::failbit); + } + return lhs; +} + +Config::FrictionModel StringToEnum<Config::FrictionModel>::convert( + std::string const &s) { + if (s == "Truncated") + return Config::Truncated; + + if (s == "Regularised") + return Config::Regularised; + + DUNE_THROW(Dune::Exception, "failed to parse enum"); +} + +Config::scheme StringToEnum<Config::scheme>::convert(std::string const &s) { + if (s == "newmark") + return Config::Newmark; + + if (s == "backwardEuler") + return Config::BackwardEuler; + + DUNE_THROW(Dune::Exception, "failed to parse enum"); +} + +Config::stateModel StringToEnum<Config::stateModel>::convert( + std::string const &s) { + if (s == "AgeingLaw") + return Config::AgeingLaw; + + if (s == "SlipLaw") + return Config::SlipLaw; + + DUNE_THROW(Dune::Exception, "failed to parse enum"); +} + +Solver::VerbosityMode StringToEnum<Solver::VerbosityMode>::convert( + std::string const &s) { + if (s == "full") + return Solver::FULL; + + if (s == "reduced") + return Solver::REDUCED; + + if (s == "quiet") + return Solver::QUIET; + + DUNE_THROW(Dune::Exception, "failed to parse enum"); +} + +template std::istream &operator>>(std::istream &lhs, Config::FrictionModel &); +template std::istream &operator>>(std::istream &lhs, Config::stateModel &); +template std::istream &operator>>(std::istream &lhs, Config::scheme &); +template std::istream &operator>>(std::istream &lhs, Solver::VerbosityMode &); diff --git a/src/enumparser.hh b/src/enumparser.hh new file mode 100644 index 00000000..7eaa0fa6 --- /dev/null +++ b/src/enumparser.hh @@ -0,0 +1,35 @@ +#ifndef SRC_ENUMPARSER_HH +#define SRC_ENUMPARSER_HH + +// Copyright Carsten Graeser 2012 + +#include <dune/common/typetraits.hh> + +#include <dune/solvers/solvers/solver.hh> + +#include "enums.hh" + +template <class Enum> struct StringToEnum : public Dune::NotImplemented {}; + +template <> struct StringToEnum<Config::FrictionModel> { + static Config::FrictionModel convert(std::string const &s); +}; + +template <> struct StringToEnum<Config::stateModel> { + static Config::stateModel convert(std::string const &s); +}; + +template <> struct StringToEnum<Config::scheme> { + static Config::scheme convert(std::string const &s); +}; + +template <> struct StringToEnum<Solver::VerbosityMode> { + static Solver::VerbosityMode convert(std::string const &s); +}; + +template <class Enum> +typename Dune::enable_if< + !Dune::IsBaseOf<Dune::NotImplemented, StringToEnum<Enum>>::value, + std::istream &>::type +operator>>(std::istream &lhs, Enum &e); +#endif diff --git a/src/sand-wedge.cc b/src/sand-wedge.cc index 04ed6790..d94813e8 100644 --- a/src/sand-wedge.cc +++ b/src/sand-wedge.cc @@ -68,11 +68,7 @@ #include "assemblers.hh" #include "tobool.hh" -#include "enum_parser.cc" -#include "enum_friction_model.cc" -#include "enum_scheme.cc" -#include "enum_state_model.cc" -#include "enum_verbosity.cc" +#include "enumparser.hh" #include "enums.hh" #include "friction_writer.hh" #include "sand-wedge-data/mybody.hh" -- GitLab