From 6f50a5301b1de13787db833a00d418bf7e99cae6 Mon Sep 17 00:00:00 2001 From: Luca Heltai Date: Fri, 6 Apr 2018 13:00:17 +0200 Subject: [PATCH] Added to_string and to_value --- doc/news/changes/minor/20180406LucaHeltai | 5 ++ include/deal.II/base/patterns.h | 49 +++++++++++++++++++ tests/parameter_handler/pattern_tools_12.cc | 37 ++++++++++++++ .../parameter_handler/pattern_tools_12.output | 2 + 4 files changed, 93 insertions(+) create mode 100644 doc/news/changes/minor/20180406LucaHeltai create mode 100644 tests/parameter_handler/pattern_tools_12.cc create mode 100644 tests/parameter_handler/pattern_tools_12.output diff --git a/doc/news/changes/minor/20180406LucaHeltai b/doc/news/changes/minor/20180406LucaHeltai new file mode 100644 index 0000000000..120b5aff4d --- /dev/null +++ b/doc/news/changes/minor/20180406LucaHeltai @@ -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. +
+(Luca Heltai, 2018/04/06) + diff --git a/include/deal.II/base/patterns.h b/include/deal.II/base/patterns.h index 276f06964b..161c333837 100644 --- a/include/deal.II/base/patterns.h +++ b/include/deal.II/base/patterns.h @@ -1265,7 +1265,43 @@ namespace Patterns Convert::to_pattern()) = delete; }; + /** + * A utility function that simplify the convertion to strings of arbitrarily + * complex types. + * + * This function calls the method Convert::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 + 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::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 + void to_value(const std::string &s, T &t); /** * @addtogroup Exceptions @@ -2019,6 +2055,19 @@ namespace Patterns } }; + // Utility function with default Pattern + template + std::string to_string(const T &t) + { + return Convert::to_string(t); + } + + // Utility function with default Pattern + template + void to_value(const std::string &s, T &t) + { + t = Convert::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 index 0000000000..ba652839d8 --- /dev/null +++ b/tests/parameter_handler/pattern_tools_12.cc @@ -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 +#include +#include + +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 index 0000000000..ceb87975e0 --- /dev/null +++ b/tests/parameter_handler/pattern_tools_12.output @@ -0,0 +1,2 @@ + +DEAL::From: 1 : ciao to 2 : mondo -- 2.39.5