From ca772e69c55a0c81e8df19496b6b126a49c8a878 Mon Sep 17 00:00:00 2001 From: Luca Heltai Date: Thu, 26 Oct 2017 19:46:47 +0200 Subject: [PATCH] Added variadic versions. --- doc/news/changes/minor/20171026LucaHeltai | 2 +- include/deal.II/base/patterns.h | 87 ++++++++++++++++++++--- tests/base/patterns_10.cc | 38 ++++++++++ tests/base/patterns_10.output | 3 + tests/base/patterns_11.cc | 38 ++++++++++ tests/base/patterns_11.output | 3 + 6 files changed, 162 insertions(+), 9 deletions(-) create mode 100644 tests/base/patterns_10.cc create mode 100644 tests/base/patterns_10.output create mode 100644 tests/base/patterns_11.cc create mode 100644 tests/base/patterns_11.output diff --git a/doc/news/changes/minor/20171026LucaHeltai b/doc/news/changes/minor/20171026LucaHeltai index b74c649a51..0a765a5d1f 100644 --- a/doc/news/changes/minor/20171026LucaHeltai +++ b/doc/news/changes/minor/20171026LucaHeltai @@ -1,4 +1,4 @@ -New:Created a new Patterns::Tuple class, that allows parsing arbitrary tuples +New: Created a new Patterns::Tuple class, that allows parsing arbitrary tuples of objects in parameter files.
(Luca Heltai, 2017/10/26) diff --git a/include/deal.II/base/patterns.h b/include/deal.II/base/patterns.h index 17fb778c2b..537b5093b3 100644 --- a/include/deal.II/base/patterns.h +++ b/include/deal.II/base/patterns.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -689,19 +690,46 @@ namespace Patterns { public: /** - * Constructor. Specify each pattern that the Tuple should contain. + * Constructor. Use a vector of unique pointers to Patterns to construct + * the tuple. * - * Optionally specify a separator string. - */ - - /** - * Constructor. * @param patterns The pattern each object of the Tuple should match * @param separator An optional string used to delimit each element + * Constructor. */ Tuple (const std::vector > &patterns, const std::string &separator = ","); + /** + * Constructor. Creates a Tuple from more than one class derived from + * PatternBase. + * + * @param separator What separator to use. + * @param patterns The list of patterns to use + */ + template + Tuple (const std::string &separator, + const PatternTypes &... patterns); + + /** + * Constructor. This is needed to allow users to specify + * directly the separator without using std::string(";"). + * + * Since we support a pure variadic templates version, without this + * specialization, the compiler will fail with criptic errors. + */ + template + Tuple (const char *separator, + const PatternTypes &... patterns); + + /** + * Constructor. Same as above, using the default separator. + * + * @param patterns The list of patterns to use + */ + template + Tuple (const Patterns &... patterns); + /** * Copy constructor. */ @@ -761,13 +789,12 @@ namespace Patterns const std::string separator; /** - * Initial part of description + * Initial part of description. */ static const char *description_init; }; - /** * This class is much like the Selection class, but it allows the input to * be a comma-separated list of values which each have to be given in the @@ -1216,6 +1243,50 @@ namespace Patterns // ---------------------- inline and template functions -------------------- namespace Patterns { + template + Tuple::Tuple(const char *separator, + const PatternTypes &... ps) : + separator(separator) + { + static_assert(is_base_of_all::value, + "Not all of the input arguments of this function " + "are derived from PatternBase"); + std::initializer_list pss = { &ps... }; + for (auto p : pss) + patterns.push_back(p->clone()); + } + + + + template + Tuple::Tuple(const std::string &separator, + const PatternTypes &... ps) : + separator(separator) + { + static_assert(is_base_of_all::value, + "Not all of the input arguments of this function " + "are derived from PatternBase"); + std::initializer_list pss = { &ps... }; + for (auto p : pss) + patterns.push_back(p->clone()); + } + + + + template + Tuple::Tuple(const PatternTypes &... ps) : + separator(",") + { + static_assert(is_base_of_all::value, + "Not all of the input arguments of this function " + "are derived from PatternBase"); + std::initializer_list pss = { &ps... }; + for (auto p : pss) + patterns.push_back(p->clone()); + } + + + namespace Tools { namespace internal diff --git a/tests/base/patterns_10.cc b/tests/base/patterns_10.cc new file mode 100644 index 0000000000..eb6257999c --- /dev/null +++ b/tests/base/patterns_10.cc @@ -0,0 +1,38 @@ +// --------------------------------------------------------------------- +// +// 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. +// +// --------------------------------------------------------------------- + + +#include "../tests.h" +#include +#include +#include + +int main() +{ + initlog(); + + // create a pattern and match a string + const auto &pattern = Patterns::Tuple(";", Patterns::Integer(), Patterns::Double(), Patterns::Anything()); + const std::string desc = pattern.description(); + + deallog << desc << std::endl; + + std::string test = "5; 3.14; Ciao"; + + if (pattern.match(test)) + deallog << "OK" << std::endl; + else + deallog << "Not OK" << std::endl; +} diff --git a/tests/base/patterns_10.output b/tests/base/patterns_10.output new file mode 100644 index 0000000000..d24657a065 --- /dev/null +++ b/tests/base/patterns_10.output @@ -0,0 +1,3 @@ + +DEAL::[Tuple of <3> elements <[Integer range -2147483648...2147483647 (inclusive)]>, <[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]>, <[Anything]> separated by <;>] +DEAL::OK diff --git a/tests/base/patterns_11.cc b/tests/base/patterns_11.cc new file mode 100644 index 0000000000..9a99dbde0d --- /dev/null +++ b/tests/base/patterns_11.cc @@ -0,0 +1,38 @@ +// --------------------------------------------------------------------- +// +// 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. +// +// --------------------------------------------------------------------- + + +#include "../tests.h" +#include +#include +#include + +int main() +{ + initlog(); + + // create a pattern and match a string + const auto &pattern = Patterns::Tuple(Patterns::Double(), Patterns::Anything()); + const std::string desc = pattern.description(); + + deallog << desc << std::endl; + + std::string test = "3.14, Ciao"; + + if (pattern.match(test)) + deallog << "OK" << std::endl; + else + deallog << "Not OK" << std::endl; +} diff --git a/tests/base/patterns_11.output b/tests/base/patterns_11.output new file mode 100644 index 0000000000..218d612582 --- /dev/null +++ b/tests/base/patterns_11.output @@ -0,0 +1,3 @@ + +DEAL::[Tuple of <2> elements <[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]>, <[Anything]>] +DEAL::OK -- 2.39.5