]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add ParameterHandler::set
authorWolfgang Bangerth <bangerth@math.tamu.edu>
Tue, 3 Jan 2006 17:39:04 +0000 (17:39 +0000)
committerWolfgang Bangerth <bangerth@math.tamu.edu>
Tue, 3 Jan 2006 17:39:04 +0000 (17:39 +0000)
git-svn-id: https://svn.dealii.org/trunk@11941 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/base/include/base/parameter_handler.h
deal.II/base/source/parameter_handler.cc
deal.II/doc/news/changes.html
tests/bits/parameter_handler_6.cc [new file with mode: 0644]
tests/bits/parameter_handler_6/cmp/generic [new file with mode: 0644]

index 11e1e5a3c42c3364b43c3e4c98b5bcf1e32922d4..81223ececa13118ba8e0805b3d06e5d85ad6b9f9 100644 (file)
@@ -2,7 +2,7 @@
 //    $Id$
 //    Version: $Name$
 //
-//    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 by the deal.II authors
+//    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 by the deal.II authors
 //
 //    This file is subject to QPL and may not be  distributed
 //    without copyright and license information. Please refer
@@ -1368,20 +1368,76 @@ class ParameterHandler
     
                                     /**
                                      * Return value of entry
-                                     * <tt>entry_string</tt> as
+                                     * <tt>entry_name</tt> as
                                      * <tt>double</tt>.
                                      */
-    double         get_double (const std::string &entry_string) const;
+    double         get_double (const std::string &entry_name) const;
 
                                     /**
                                      * Return value of entry
-                                     * <tt>entry_string</tt> as <tt>bool</tt>.
+                                     * <tt>entry_name</tt> as <tt>bool</tt>.
                                       * The entry may be "true" or "yes" 
                                       * for <tt>true</tt>, "false" or
                                       * "no" for <tt>false</tt> respectively.
                                       */
-    bool           get_bool (const std::string &entry_string) const;
+    bool           get_bool (const std::string &entry_name) const;
+
+                                     /**
+                                      * Change the value presently stored for
+                                      * <tt>entry_name</tt> to the one given
+                                      * in the second argument.
+                                      *
+                                      * The parameter must already exist in
+                                      * the present subsection.
+                                      */
+    void           set (const std::string &entry_name,
+                        const std::string &new_value);
 
+    void           set (const std::string &entry_name,
+                        const char        *new_value);
+    
+                                     /**
+                                      * Change the value presently stored for
+                                      * <tt>entry_name</tt> to the one given
+                                      * in the second argument.
+                                      *
+                                      * The parameter must already exist in
+                                      * the present subsection.
+                                      */
+    void           set (const std::string &entry_name,
+                        const long int    &new_value);
+
+                                     /**
+                                      * Change the value presently stored for
+                                      * <tt>entry_name</tt> to the one given
+                                      * in the second argument.
+                                      *
+                                      * The parameter must already exist in
+                                      * the present subsection.
+                                      *
+                                      * For internal purposes, the new value
+                                      * needs to be converted to a
+                                      * string. This is done using 16 digits
+                                      * of accuracy, so the set value and the
+                                      * one you can get back out using
+                                      * get_double() may differ in the 16th
+                                      * digit.
+                                      */
+    void           set (const std::string &entry_name,
+                        const double      &new_value);
+
+                                     /**
+                                      * Change the value presently stored for
+                                      * <tt>entry_name</tt> to the one given
+                                      * in the second argument.
+                                      *
+                                      * The parameter must already exist in
+                                      * the present subsection.
+                                      */
+    void           set (const std::string &entry_name,
+                        const bool        &new_value);
+    
+    
                                     /**
                                      * Print all parameters with the
                                      * given style to
index 604f9ff79dd33020a8121fdc118964f90e16068f..ccb3a5eeebea4936d34f3adc910307a64efff271 100644 (file)
@@ -2,7 +2,7 @@
 //    $Id$
 //    Version: $Name$
 //
-//    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 by the deal.II authors
+//    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 by the deal.II authors
 //
 //    This file is subject to QPL and may not be  distributed
 //    without copyright and license information. Please refer
@@ -727,13 +727,14 @@ bool ParameterHandler::leave_subsection ()
 
 
 
-const std::string & ParameterHandler::get (const std::string &entry_string) const
+const std::string &
+ParameterHandler::get (const std::string &entry_string) const
 {
   const Section* pd = get_present_defaults_subsection ();
   const Section* pc = get_present_changed_subsection ();
 
-                                  // assert that the according entry is already
-                                  // declared in the defaults tree
+                                  // assert that the according entry is
+                                  // already declared in the defaults tree
   Assert (pd->entries.find (entry_string) != pd->entries.end(),
          ExcEntryUndeclared(entry_string));
   
@@ -790,7 +791,9 @@ bool ParameterHandler::get_bool (const std::string &entry_string) const
 {
   std::string s = get(entry_string);
 
-  AssertThrow ((s=="true") || (s=="false") || (s=="yes") || (s=="no"), ExcConversionError(s));
+  AssertThrow ((s=="true") || (s=="false") ||
+               (s=="yes") || (s=="no"),
+               ExcConversionError(s));
   if (s=="true" || s=="yes")
     return true;
   else
@@ -799,6 +802,101 @@ bool ParameterHandler::get_bool (const std::string &entry_string) const
 
 
 
+void
+ParameterHandler::set (const std::string &entry_string,
+                       const std::string &new_value)
+{
+  const Section* pd = get_present_defaults_subsection ();
+  Section* pc = get_present_changed_subsection ();
+
+                                  // assert that the according entry is
+                                  // already declared in the defaults tree
+  Assert (pd->entries.find (entry_string) != pd->entries.end(),
+         ExcEntryUndeclared(entry_string));
+  
+                                   // entry exists; now set it (if the entry
+                                   // in pc hasn't existed yet, create it and
+                                   // set the pattern to the null pointer
+                                   // since it isn't used)
+  if (pc->entries.find (entry_string) == pc->entries.end())
+    pc->entries[entry_string].pattern = 0;
+  pc->entries[entry_string].value = new_value;
+}
+
+
+void
+ParameterHandler::set (const std::string &entry_string,
+                       const char        *new_value)
+{
+                                   // simply forward
+  set (entry_string, std::string(new_value));
+}
+
+  
+void
+ParameterHandler::set (const std::string &entry_string,
+                       const double      &new_value)
+{
+#ifdef HAVE_STD_STRINGSTREAM
+  std::ostringstream s;
+#else
+  std::ostrstream s;
+#endif
+  s << std::setprecision(16);
+  s << new_value;
+#ifndef HAVE_STD_STRINGSTREAM
+  s << std::ends;
+#endif
+
+                                   // hand this off to the function that
+                                   // actually sets the value as a string
+  set (entry_string, s.str());
+}
+
+
+
+void
+ParameterHandler::set (const std::string &entry_string,
+                       const long int    &new_value)
+{
+#ifdef HAVE_STD_STRINGSTREAM
+  std::ostringstream s;
+#else
+  std::ostrstream s;
+#endif
+  s << new_value;
+#ifndef HAVE_STD_STRINGSTREAM
+  s << std::ends;
+#endif
+
+                                   // hand this off to the function that
+                                   // actually sets the value as a string
+  set (entry_string, s.str());
+}
+
+
+
+void
+ParameterHandler::set (const std::string &entry_string,
+                       const bool        &new_value)
+{
+#ifdef HAVE_STD_STRINGSTREAM
+  std::ostringstream s;
+#else
+  std::ostrstream s;
+#endif
+  s << new_value;
+#ifndef HAVE_STD_STRINGSTREAM
+  s << std::ends;
+#endif
+
+                                   // hand this off to the function that
+                                   // actually sets the value as a string
+  set (entry_string, s.str());
+}
+
+
+
 std::ostream &
 ParameterHandler::print_parameters (std::ostream     &out,
                                     const OutputStyle style)
index 8fc34c6c7749ba013a61e9e7b527204e70c7d99a..fc91f2879bb928c6deda7e4ebcdc88b7d163b072 100644 (file)
@@ -5,7 +5,7 @@
   <link href="../screen.css" rel="StyleSheet">
   <title>The deal.II news page</title>
   <meta name="author" content="the deal.II authors <authors@dealii.org>">
-  <meta name="copyright" content="Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 by the deal.II authors">
+  <meta name="copyright" content="Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 by the deal.II authors">
   <meta name="date" content="$Date$">
   <meta name="keywords" content="deal.II"></head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
@@ -150,6 +150,14 @@ inconvenience this causes.
 <h3>base</h3>
 
 <ol>
+  <li> <p>
+       New: There are now functions
+       <code>ParameterHandler::set</code> that allow to set the value of a
+       parameter to something different later on.
+       <br>
+       (Marc Secanell, WB 2006/1/2)
+       </p>
+
   <li> <p>
        New: There are new functions
        <code>Utilities::match_at_string_start</code> and 
diff --git a/tests/bits/parameter_handler_6.cc b/tests/bits/parameter_handler_6.cc
new file mode 100644 (file)
index 0000000..31e0c3d
--- /dev/null
@@ -0,0 +1,88 @@
+//----------------------------  parameter_handler_3.cc  ---------------------------
+//    $Id$
+//    Version: $Name$ 
+//
+//    Copyright (C) 2003, 2004, 2005, 2006 by the deal.II authors
+//
+//    This file is subject to QPL and may not be  distributed
+//    without copyright and license information. Please refer
+//    to the file deal.II/doc/license.html for the  text  and
+//    further information on this license.
+//
+//----------------------------  parameter_handler_3.cc  ---------------------------
+
+
+// test ParameterHandler::set(Text)
+
+#include "../tests.h"
+#include <base/logstream.h>
+#include <base/parameter_handler.h>
+#include <fstream>
+#include <iostream>
+
+
+int main () 
+{
+  try 
+    {
+      std::ofstream logfile("parameter_handler_6/output");
+      deallog.attach(logfile);
+      deallog.depth_console(0);
+      deallog.threshold_double(1.e-10);
+
+                                       // same as parameter_handler_3
+      ParameterHandler prm;
+      prm.enter_subsection ("Testing");
+      prm.declare_entry ("string list",
+                         "a",
+                         Patterns::List(Patterns::Selection("a|b|c|d|e|f|g|h")),
+                         "docs 1");
+      prm.declare_entry ("int",
+                         "1",
+                         Patterns::Integer());
+      prm.declare_entry ("double",
+                         "3.1415926",
+                         Patterns::Double(),
+                         "docs 3");
+      prm.leave_subsection ();
+      
+      prm.read_input("parameter_handler_3.prm");
+
+                                       // now set some of the entries to
+                                       // different values
+      prm.enter_subsection ("Testing");
+      prm.set ("string list", "a, c, b");
+      prm.set ("int", "5");
+      prm.set ("double", "2.71828");
+      prm.leave_subsection ();
+      
+                                       // then write
+      prm.print_parameters (logfile, ParameterHandler::Text);
+    }
+  catch (std::exception &exc)
+    {
+      std::cerr << std::endl << std::endl
+               << "----------------------------------------------------"
+               << std::endl;
+      std::cerr << "Exception on processing: " << std::endl
+               << exc.what() << std::endl
+               << "Aborting!" << std::endl
+               << "----------------------------------------------------"
+               << std::endl;
+      
+      return 1;
+    }
+  catch (...) 
+    {
+      std::cerr << std::endl << std::endl
+               << "----------------------------------------------------"
+               << std::endl;
+      std::cerr << "Unknown exception!" << std::endl
+               << "Aborting!" << std::endl
+               << "----------------------------------------------------"
+               << std::endl;
+      return 1;
+    };
+  
+  return 0;
+}
diff --git a/tests/bits/parameter_handler_6/cmp/generic b/tests/bits/parameter_handler_6/cmp/generic
new file mode 100644 (file)
index 0000000..db35431
--- /dev/null
@@ -0,0 +1,13 @@
+
+# Listing of Parameters
+# ---------------------
+subsection Testing
+  # docs 3
+  set double      = 2.71828 # default: 3.1415926
+  set int         = 5       # default: 1
+
+  # docs 1
+  set string list = a, c, b # default: a
+end
+
+

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.