]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Added Convert struct, with its specialisation for integral types
authorLuca Heltai <luca.heltai@sissa.it>
Tue, 18 Jul 2017 17:09:37 +0000 (19:09 +0200)
committerLuca Heltai <luca.heltai@sissa.it>
Sun, 23 Jul 2017 14:57:12 +0000 (16:57 +0200)
include/deal.II/base/patterns_tools.h [new file with mode: 0644]
source/base/CMakeLists.txt
source/base/patterns_tools.cc [new file with mode: 0644]

diff --git a/include/deal.II/base/patterns_tools.h b/include/deal.II/base/patterns_tools.h
new file mode 100644 (file)
index 0000000..cc4bb02
--- /dev/null
@@ -0,0 +1,117 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 1998 - 2017 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+#ifndef dealii__patterns_tools_h
+#define dealii__patterns_tools_h
+
+
+#include <deal.II/base/config.h>
+#include <deal.II/base/exceptions.h>
+#include <deal.II/base/subscriptor.h>
+#include <deal.II/base/point.h>
+#include <deal.II/base/std_cxx14/memory.h>
+
+#include <boost/core/demangle.hpp>
+#include <boost/any.hpp>
+
+
+#include <map>
+#include <vector>
+#include <string>
+#include <memory>
+#include <type_traits>
+#include <sstream>
+
+DEAL_II_NAMESPACE_OPEN
+
+
+/**
+ * Namespace for a few class and functions that act on values and patterns.
+ *
+ * @ingroup input
+ */
+namespace PatternsTools
+{
+  using namespace Patterns;
+  /**
+   * Converter class. This class is used to generate strings and Patterns associated to
+   * the given type, and to convert from a string to the given type and viceversa.
+   */
+  template<class T, class Enable=void>
+  struct Convert
+  {
+
+    /**
+     * Return a std::unique_ptr to a Pattern that can be used to interpret a
+     * string as the type of the template argument, and the other way around.
+     */
+    static std::unique_ptr<PatternBase> to_pattern() = delete;
+
+
+    /**
+     * Return a string containing a textual version of the variable s. Use the pattern
+     * passed to perform the conversion, or create and use a default one.
+     */
+    static std::string to_string(const T &s,
+                                 std::unique_ptr<PatternBase> p = Convert<T>::to_pattern()) = delete;
+
+
+    /**
+     * Convert a string to a value, using the given pattern, or a default one.
+     */
+    static T to_value(const std::string &s,
+                      std::unique_ptr<PatternBase> p = Convert<T>::to_pattern()) = delete;
+  };
+
+
+}
+
+// ---------------------- inline and template functions --------------------
+
+namespace PatternsTools
+{
+  template<class T>
+  struct Convert<T, typename std::enable_if<std::is_integral<T>::value>::type>
+  {
+
+    static std::unique_ptr<PatternBase> to_pattern()
+    {
+      return std_cxx14::make_unique<Integer>(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
+    }
+
+    static std::string to_string(const T &value, std::unique_ptr<PatternBase> p = Convert<T>::to_pattern())
+    {
+      std::stringstream str;
+      str << value;
+      AssertThrow(p->match(str.str()), ExcMessage("No match"));
+      return str.str();
+    }
+
+    static T to_value(const std::string &s,
+                      std::unique_ptr<PatternBase> p = Convert<T>::to_pattern())
+    {
+      AssertThrow(p->match(s), ExcMessage("No match"));
+      std::istringstream is(s);
+      T i;
+      is >> i;
+      AssertThrow(!is.fail(), ExcMessage("Failed to convert"));
+      return i;
+    }
+  };
+}
+
+DEAL_II_NAMESPACE_CLOSE
+
+#endif
index 4625cb74183eead0cac8544f0d0b6f1129010475..434cec8d73751b6feb25e6486d673abf159128cc 100644 (file)
@@ -45,6 +45,7 @@ SET(_src
   parsed_function.cc
   partitioner.cc
   path_search.cc
+  patterns_tools.cc
   polynomial.cc
   polynomials_abf.cc
   polynomials_adini.cc
diff --git a/source/base/patterns_tools.cc b/source/base/patterns_tools.cc
new file mode 100644 (file)
index 0000000..6612fdd
--- /dev/null
@@ -0,0 +1,57 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 1998 - 2017 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+#include <deal.II/base/parameter_handler.h>
+#include <deal.II/base/patterns_tools.h>
+#include <deal.II/base/utilities.h>
+#include <deal.II/base/memory_consumption.h>
+#include <deal.II/base/std_cxx14/memory.h>
+
+DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
+#include <boost/core/demangle.hpp>
+DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
+
+//#include <fstream>
+//#include <iostream>
+//#include <iomanip>
+//#include <cstdlib>
+//#include <algorithm>
+//#include <sstream>
+//#include <cctype>
+//#include <limits>
+//#include <cstring>
+
+
+DEAL_II_NAMESPACE_OPEN
+
+
+
+//TODO[WB]: various functions here could be simplified by using namespace Utilities
+
+namespace Patterns
+{
+
+
+  std::string default_list_separator(unsigned int rank)
+  {
+    static std::array<std::string, 5> seps = {{" ",  ","  ,  ";"  ,  "|"  ,   "%"}};
+    AssertThrow(rank < seps.size(), ExcMessage("I don't know what to use for such "
+                                               "high rank. Bailing out."));
+    return seps[rank];
+  }
+}
+
+DEAL_II_NAMESPACE_CLOSE

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.