]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Added to_string and to_value
authorLuca Heltai <luca.heltai@sissa.it>
Fri, 6 Apr 2018 11:00:17 +0000 (13:00 +0200)
committerLuca Heltai <luca.heltai@sissa.it>
Wed, 11 Apr 2018 21:44:56 +0000 (23:44 +0200)
doc/news/changes/minor/20180406LucaHeltai [new file with mode: 0644]
include/deal.II/base/patterns.h
tests/parameter_handler/pattern_tools_12.cc [new file with mode: 0644]
tests/parameter_handler/pattern_tools_12.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20180406LucaHeltai b/doc/news/changes/minor/20180406LucaHeltai
new file mode 100644 (file)
index 0000000..120b5af
--- /dev/null
@@ -0,0 +1,5 @@
+New: Patterns::Tools::to_string() and Patterns::Tools::to_value() simplify the conversion to and from
+strings of arbirarily complex types. 
+<br>
+(Luca Heltai, 2018/04/06)
+
index 276f06964bb05e07e121c919a51059eeebf7feb6..161c3338370e1c3b86c861fffba8fc054d92901a 100644 (file)
@@ -1265,7 +1265,43 @@ namespace Patterns
                           Convert<T>::to_pattern()) = delete;
     };
 
+    /**
+     * A utility function that simplify the convertion to strings of arbitrarily
+     * complex types.
+     *
+     * This function calls the method Convert<T>::to_string() with the default
+     * pattern. An example usage is the following:
+     *
+     * @code
+     * auto t = std::make_tuple(1.0, std::make_pair(1, "ciao"));
+     * auto s = Patterns::Tools::to_string(t);
+     *
+     * std::cout << s; // will print "1 % 1 : ciao
+     * @endcode
+     *
+     * @author Luca Heltai, 2018
+     */
+    template<typename T>
+    std::string to_string(const T &t);
 
+    /**
+     * A utility function that simplify the convertion from strings to arbitrary
+     * types.
+     *
+     * This function calls the method Convert<T>::to_value() with the default
+     * pattern. An example usage is the following:
+     *
+     * @code
+     * auto t = std::make_tuple(1.0, std::make_pair(1, "ciao"));
+     * to_value("2 % 3 : mondo", t);
+     * auto s = Patterns::Tools::to_string(t);
+     * std::cout << s; // will print "2 % 3 : mondo
+     * @endcode
+     *
+     * @author Luca Heltai, 2018
+     */
+    template<typename T>
+    void to_value(const std::string &s, T &t);
 
     /**
      * @addtogroup Exceptions
@@ -2019,6 +2055,19 @@ namespace Patterns
       }
     };
 
+    // Utility function with default Pattern
+    template<typename T>
+    std::string to_string(const T &t)
+    {
+      return Convert<T>::to_string(t);
+    }
+
+    // Utility function with default Pattern
+    template<typename T>
+    void to_value(const std::string &s, T &t)
+    {
+      t = Convert<T>::to_value(s);
+    }
   }
 }
 
diff --git a/tests/parameter_handler/pattern_tools_12.cc b/tests/parameter_handler/pattern_tools_12.cc
new file mode 100644 (file)
index 0000000..ba65283
--- /dev/null
@@ -0,0 +1,37 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2005 - 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.
+//
+// ---------------------------------------------------------------------
+
+// Check to_string and to_value
+
+#include "../tests.h"
+#include <deal.II/base/parameter_handler.h>
+#include <deal.II/base/std_cxx14/memory.h>
+#include <memory>
+
+using dealii::Patterns::Tools::to_string;
+using dealii::Patterns::Tools::to_value;
+
+int main()
+{
+  initlog();
+
+  auto a = std::make_tuple(1, std::string("ciao"));
+
+  auto s = to_string(a);
+  to_value("2 : mondo", a);
+
+  deallog << "From: " << s
+          << " to " << to_string(a) << std::endl;
+}
diff --git a/tests/parameter_handler/pattern_tools_12.output b/tests/parameter_handler/pattern_tools_12.output
new file mode 100644 (file)
index 0000000..ceb8797
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::From: 1 : ciao to 2 : mondo

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.