From: Luca Heltai Date: Fri, 27 Oct 2017 12:03:11 +0000 (+0200) Subject: Add Convert X-Git-Tag: v9.0.0-rc1~848^2~3 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3f47b24fac6f59ba2e86334fa401e4b33aad6773;p=dealii.git Add Convert --- diff --git a/include/deal.II/base/patterns.h b/include/deal.II/base/patterns.h index 184d2a352b..e4e8097125 100644 --- a/include/deal.II/base/patterns.h +++ b/include/deal.II/base/patterns.h @@ -1517,6 +1517,14 @@ namespace Patterns static constexpr int list_rank = std_cxx14::max(RankInfo::list_rank, RankInfo::list_rank); static constexpr int map_rank = std_cxx14::max(RankInfo::map_rank, RankInfo::map_rank)+1; }; + + + template + struct RankInfo> + { + static constexpr int list_rank = max_list_rank(); + static constexpr int map_rank = max_map_rank()+1; + }; } // stl containers @@ -1878,6 +1886,97 @@ namespace Patterns return *m.begin(); } }; + + // Tuples + template + struct Convert> + { + typedef std::tuple T; + + static std::unique_ptr to_pattern() + { + static_assert(internal::RankInfo::map_rank > 0, + "Cannot use this class for non tuple-compatible types."); + return std_cxx14::make_unique( + internal::default_map_separator[internal::RankInfo::map_rank-1], + *Convert::to_pattern()...); + } + + template + static + decltype(auto) to_string_internal_1(const T &t, + const Patterns::Tuple &pattern, + std_cxx14::index_sequence) + { + std::array::value> a + = {{ + Convert::type>:: + to_string(std::get(t), pattern.get_pattern(I).clone())... + } + }; + return a; + } + + static + decltype(auto) to_string_internal_2(const T &t, + const Patterns::Tuple &pattern) + { + return Convert::to_string_internal_1(t, pattern, + std_cxx14::make_index_sequence::value> {}); + } + + static std::string to_string(const T &t, + const std::unique_ptr + &pattern = Convert::to_pattern()) + { + auto p = dynamic_cast(pattern.get()); + AssertThrow(p,ExcMessage("I need a Tuple pattern to convert a tuple " + "to a string.")); + + const auto string_array = Convert::to_string_internal_2(t,*p); + std::string str; + for (unsigned int i=0; i< string_array.size(); ++i) + str += (i ? " " + p->get_separator() + " " : "") +string_array[i]; + AssertThrow(p->match(str), ExcNoMatch(str, *p)); + return str; + } + + + template + static + T to_value_internal_1(const std::vector &s, + const Patterns::Tuple &pattern, + std_cxx14::index_sequence) + { + return std::make_tuple(Convert::type>:: + to_value(s[I], pattern.get_pattern(I).clone())...); + } + + static + T to_value_internal_2(const std::vector &s, + const Patterns::Tuple &pattern) + { + return Convert::to_value_internal_1(s, pattern, + std_cxx14::make_index_sequence::value> {}); + } + + + static T to_value(const std::string &s, + const std::unique_ptr &pattern = + Convert::to_pattern()) + { + AssertThrow(pattern->match(s), ExcNoMatch(s, *pattern)); + + auto p = dynamic_cast(pattern.get()); + AssertThrow(p,ExcMessage("I need a Tuple pattern to convert a string " + "to a tuple type.")); + + auto v = Utilities::split_string_list(s, p->get_separator()); + + return Convert::to_value_internal_2(v,*p); + } + }; + } } diff --git a/tests/base/pattern_tools_08.cc b/tests/base/pattern_tools_08.cc new file mode 100644 index 0000000000..2d50f94438 --- /dev/null +++ b/tests/base/pattern_tools_08.cc @@ -0,0 +1,31 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2005 - 2015 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 Convert::to_pattern() + +#include "../tests.h" +#include +#include +#include + +int main() +{ + initlog(); + + auto a = std::make_tuple(Point<3>(), double(3.5), std::string("ciao")); + const auto &pattern = Patterns::Tools::Convert::to_pattern(); + + deallog << pattern->description() << std::endl; +} diff --git a/tests/base/pattern_tools_08.output b/tests/base/pattern_tools_08.output new file mode 100644 index 0000000000..042b14be3a --- /dev/null +++ b/tests/base/pattern_tools_08.output @@ -0,0 +1,2 @@ + +DEAL::[Tuple of <3> elements <[List of <[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]> of length 3...3 (inclusive)]>, <[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]>, <[Anything]>] diff --git a/tests/base/pattern_tools_09.cc b/tests/base/pattern_tools_09.cc new file mode 100644 index 0000000000..7adb76d77b --- /dev/null +++ b/tests/base/pattern_tools_09.cc @@ -0,0 +1,34 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2005 - 2015 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 Convert::to_string. + +#include "../tests.h" +#include +#include +#include + +int main() +{ + initlog(); + + auto a = std::make_tuple(Point<3>(), double(3.5), std::string("ciao")); + const auto &pattern = Patterns::Tools::Convert::to_pattern(); + + deallog << pattern->description() << std::endl; + + deallog << Patterns::Tools::Convert::to_string(a) << std::endl; + +} diff --git a/tests/base/pattern_tools_09.output b/tests/base/pattern_tools_09.output new file mode 100644 index 0000000000..f58e4e5b73 --- /dev/null +++ b/tests/base/pattern_tools_09.output @@ -0,0 +1,3 @@ + +DEAL::[Tuple of <3> elements <[List of <[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]> of length 3...3 (inclusive)]>, <[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]>, <[Anything]>] +DEAL::0.000000, 0.000000, 0.000000 : 3.500000 : ciao diff --git a/tests/base/pattern_tools_10.cc b/tests/base/pattern_tools_10.cc new file mode 100644 index 0000000000..4244d34dde --- /dev/null +++ b/tests/base/pattern_tools_10.cc @@ -0,0 +1,37 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2005 - 2015 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 Convert::to_value. + +#include "../tests.h" +#include +#include +#include + +int main() +{ + initlog(); + + auto a = std::make_tuple(Point<3>(), double(3.5), std::string("ciao")); + const auto &pattern = Patterns::Tools::Convert::to_pattern(); + + deallog << pattern->description() << std::endl; + + deallog << Patterns::Tools::Convert::to_string(a) << std::endl; + + a = Patterns::Tools::Convert::to_value("1.0, 2.0, 3.0 : 3.14 : mondo"); + + deallog << Patterns::Tools::Convert::to_string(a) << std::endl; +} diff --git a/tests/base/pattern_tools_10.output b/tests/base/pattern_tools_10.output new file mode 100644 index 0000000000..3f6232b0be --- /dev/null +++ b/tests/base/pattern_tools_10.output @@ -0,0 +1,4 @@ + +DEAL::[Tuple of <3> elements <[List of <[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]> of length 3...3 (inclusive)]>, <[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]>, <[Anything]>] +DEAL::0.000000, 0.000000, 0.000000 : 3.500000 : ciao +DEAL::1.000000, 2.000000, 3.000000 : 3.140000 : mondo