From: Luca Heltai Date: Wed, 19 Jul 2017 19:08:48 +0000 (+0200) Subject: Added convert for std::complex types. X-Git-Tag: v9.0.0-rc1~1391^2~13 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e5d0284ec5db7118d8115fb866c8e966c2da274d;p=dealii.git Added convert for std::complex types. --- diff --git a/include/deal.II/base/patterns_tools.h b/include/deal.II/base/patterns_tools.h index b15f5569d9..cc61706fa1 100644 --- a/include/deal.II/base/patterns_tools.h +++ b/include/deal.II/base/patterns_tools.h @@ -352,6 +352,13 @@ namespace PatternsTools template struct RankInfo>:RankInfo> {}; + // Rank of complex types + template + struct RankInfo> + { + static constexpr int list_rank = RankInfo::list_rank+1; + static constexpr int map_rank = RankInfo::map_rank; + }; } @@ -560,6 +567,59 @@ namespace PatternsTools }; + + template + struct Convert> + { + typedef std::complex T; + + static std::unique_ptr to_pattern() + { + return std_cxx14::make_unique(*Convert::to_pattern(), + 2, 2, + internal::default_list_separator[internal::RankInfo::list_rank]); + } + + 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 List pattern to convert a string to a List type.")); + auto base_p = p->get_base_pattern().clone(); + std::string s = + Convert::to_string(t.real(), base_p) + + p->get_separator() + " " + + Convert::to_string(t.imag(), base_p); + + AssertThrow(pattern->match(s), ExcNoMatch(s, *p)); + return s; + } + + /** + * Convert a string to a value, using the given pattern, or a default one. + */ + 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 List pattern to convert a string to a List type.")); + + auto base_p = p->get_base_pattern().clone(); + + auto v = Utilities::split_string_list(s,p->get_separator()); + AssertDimension(v.size(), 2); + T t(Convert::to_value(v[0], base_p), + Convert::to_value(v[1], base_p)); + return t; + } + }; + + + template void add_parameter(const std::string &entry, ParameterType ¶meter,