From: Wolfgang Bangerth <bangerth@math.tamu.edu>
Date: Sat, 13 Dec 2014 03:50:36 +0000 (-0600)
Subject: Reorganize code a bit.
X-Git-Tag: v8.2.0-rc1~15^2~2
X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=03938997309d83ec5d8c0e15d7a86f17e1c619f2;p=dealii.git

Reorganize code a bit.

Change the code so that checking whether a function parser object
has already been created now happens at the place where we use
the object, rather than as an early exit in the init_muparser()
function. I find that this makes the code easier to read.

This also allows us to remove the creation of a parser object on the
thread where the object is actually created. In many (most) cases,
we will simply create the parser later on anyway, but not necessarily.
---

diff --git a/source/base/function_parser.cc b/source/base/function_parser.cc
index ccd7e65659..2faa6ea9fa 100644
--- a/source/base/function_parser.cc
+++ b/source/base/function_parser.cc
@@ -1,6 +1,6 @@
 // ---------------------------------------------------------------------
 //
-// Copyright (C) 2005 - 2013 by the deal.II authors
+// Copyright (C) 2005 - 2014 by the deal.II authors
 //
 // This file is part of the deal.II library.
 //
@@ -141,9 +141,13 @@ namespace internal
 template <int dim>
 void FunctionParser<dim>:: init_muparser() const
 {
-  if (fp.get().size()>0)
-    return;
+  // check that we have not already initialized the parser on the
+  // current thread, i.e., that the current function is only called
+  // once per thread
+  Assert (fp.get().size()==0, ExcInternalError());
 
+  // initialize the objects for the current thread (fp.get() and
+  // vars.get())
   fp.get().resize(this->n_components);
   vars.get().resize(var_names.size());
   for (unsigned int component=0; component<this->n_components; ++component)
@@ -308,9 +312,14 @@ void FunctionParser<dim>::initialize (const std::string   &variables,
   else
     n_vars = dim;
 
-  init_muparser();
+  // create a parser object for the current thread we can then query
+  // in value() and vector_value(). this is not strictly necessary
+  // because a user may never call these functions on the current
+  // thread, but it gets us error messages about wrong formulas right
+  // away
+  init_muparser ();
 
-  // Now set the initialization bit.
+  // finally set the initialization bit
   initialized = true;
 }
 
@@ -373,8 +382,9 @@ double FunctionParser<dim>::value (const Point<dim>  &p,
   Assert (component < this->n_components,
           ExcIndexRange(component, 0, this->n_components));
 
-  // initialize if not done so on this thread yet:
-  init_muparser();
+  // initialize the parser if that hasn't happened yet on the current thread
+  if (fp.get().size() == 0)
+    init_muparser();
 
   for (unsigned int i=0; i<dim; ++i)
     vars.get()[i] = p(i);
@@ -408,8 +418,9 @@ void FunctionParser<dim>::vector_value (const Point<dim> &p,
           ExcDimensionMismatch (values.size(), this->n_components));
 
 
-  // initialize if not done so on this thread yet:
-  init_muparser();
+  // initialize the parser if that hasn't happened yet on the current thread
+  if (fp.get().size() == 0)
+    init_muparser();
 
   for (unsigned int i=0; i<dim; ++i)
     vars.get()[i] = p(i);