]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Added variadic versions.
authorLuca Heltai <luca.heltai@sissa.it>
Thu, 26 Oct 2017 17:46:47 +0000 (19:46 +0200)
committerLuca Heltai <luca.heltai@sissa.it>
Thu, 26 Oct 2017 17:46:47 +0000 (19:46 +0200)
doc/news/changes/minor/20171026LucaHeltai
include/deal.II/base/patterns.h
tests/base/patterns_10.cc [new file with mode: 0644]
tests/base/patterns_10.output [new file with mode: 0644]
tests/base/patterns_11.cc [new file with mode: 0644]
tests/base/patterns_11.output [new file with mode: 0644]

index b74c649a51ca9c498d7fd9580853e90829fe5927..0a765a5d1f46207ad2e2972af89c41f553e4f14b 100644 (file)
@@ -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. 
 <br> 
 (Luca Heltai, 2017/10/26)
index 17fb778c2b14b2d76ead5c6131c104b853fa5574..537b5093b33fb370f495e9174365a81aa94364b2 100644 (file)
@@ -25,6 +25,7 @@
 #include <deal.II/base/subscriptor.h>
 #include <deal.II/base/utilities.h>
 #include <deal.II/base/std_cxx14/algorithm.h>
+#include <deal.II/base/template_constraints.h>
 
 #include <boost/archive/basic_archive.hpp>
 #include <boost/core/demangle.hpp>
@@ -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<std::unique_ptr<PatternBase> > &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<class... PatternTypes>
+    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<class... PatternTypes>
+    Tuple (const char *separator,
+           const PatternTypes &... patterns);
+
+    /**
+     * Constructor. Same as above, using the default separator.
+     *
+     * @param patterns The list of patterns to use
+     */
+    template<typename... Patterns>
+    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<class... PatternTypes>
+  Tuple::Tuple(const char *separator,
+               const PatternTypes &... ps) :
+    separator(separator)
+  {
+    static_assert(is_base_of_all<PatternBase, PatternTypes...>::value,
+                  "Not all of the input arguments of this function "
+                  "are derived from PatternBase");
+    std::initializer_list<const PatternBase *> pss = { &ps... };
+    for (auto p : pss)
+      patterns.push_back(p->clone());
+  }
+
+
+
+  template<class... PatternTypes>
+  Tuple::Tuple(const std::string &separator,
+               const PatternTypes &... ps) :
+    separator(separator)
+  {
+    static_assert(is_base_of_all<PatternBase, PatternTypes...>::value,
+                  "Not all of the input arguments of this function "
+                  "are derived from PatternBase");
+    std::initializer_list<const PatternBase *> pss = { &ps... };
+    for (auto p : pss)
+      patterns.push_back(p->clone());
+  }
+
+
+
+  template<class... PatternTypes>
+  Tuple::Tuple(const PatternTypes &... ps) :
+    separator(",")
+  {
+    static_assert(is_base_of_all<PatternBase, PatternTypes...>::value,
+                  "Not all of the input arguments of this function "
+                  "are derived from PatternBase");
+    std::initializer_list<const PatternBase *> 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 (file)
index 0000000..eb62579
--- /dev/null
@@ -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 <deal.II/base/parameter_handler.h>
+#include <deal.II/base/std_cxx14/memory.h>
+#include <memory>
+
+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 (file)
index 0000000..d24657a
--- /dev/null
@@ -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 (file)
index 0000000..9a99dbd
--- /dev/null
@@ -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 <deal.II/base/parameter_handler.h>
+#include <deal.II/base/std_cxx14/memory.h>
+#include <memory>
+
+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 (file)
index 0000000..218d612
--- /dev/null
@@ -0,0 +1,3 @@
+
+DEAL::[Tuple of <2> elements <[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]>, <[Anything]>]
+DEAL::OK

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.