From: Luca Heltai <luca.heltai@sissa.it> 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 <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 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 <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