From: Luca Heltai Date: Tue, 18 Jul 2017 17:09:37 +0000 (+0200) Subject: Added Convert struct, with its specialisation for integral types X-Git-Tag: v9.0.0-rc1~1391^2~20 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=00a9e846a3815ef77dc56714214afadc5c98bdd8;p=dealii.git Added Convert struct, with its specialisation for integral types --- diff --git a/include/deal.II/base/patterns_tools.h b/include/deal.II/base/patterns_tools.h new file mode 100644 index 0000000000..cc4bb028a1 --- /dev/null +++ b/include/deal.II/base/patterns_tools.h @@ -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 +#include +#include +#include +#include + +#include +#include + + +#include +#include +#include +#include +#include +#include + +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 + 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 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 p = Convert::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 p = Convert::to_pattern()) = delete; + }; + + +} + +// ---------------------- inline and template functions -------------------- + +namespace PatternsTools +{ + template + struct Convert::value>::type> + { + + static std::unique_ptr to_pattern() + { + return std_cxx14::make_unique(std::numeric_limits::min(), std::numeric_limits::max()); + } + + static std::string to_string(const T &value, std::unique_ptr p = Convert::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 p = Convert::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 diff --git a/source/base/CMakeLists.txt b/source/base/CMakeLists.txt index 4625cb7418..434cec8d73 100644 --- a/source/base/CMakeLists.txt +++ b/source/base/CMakeLists.txt @@ -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 index 0000000000..6612fdd0c4 --- /dev/null +++ b/source/base/patterns_tools.cc @@ -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 +#include +#include +#include +#include + +DEAL_II_DISABLE_EXTRA_DIAGNOSTICS +#include +DEAL_II_ENABLE_EXTRA_DIAGNOSTICS + +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include + + +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 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