From 3fc4805f92b555ea587ddcb6f5c5481612c54df9 Mon Sep 17 00:00:00 2001 From: Jean-Paul Pelteret Date: Wed, 24 Oct 2018 14:10:28 +0200 Subject: [PATCH] Add guarentee that wrapper resets time state of wrapped class --- include/deal.II/base/incremental_function.h | 3 ++ source/base/incremental_function.cc | 46 ++++++++++++++------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/include/deal.II/base/incremental_function.h b/include/deal.II/base/incremental_function.h index a69af70e00..abd963e423 100644 --- a/include/deal.II/base/incremental_function.h +++ b/include/deal.II/base/incremental_function.h @@ -62,6 +62,9 @@ namespace Functions * @note This class stores a non-constant reference to @p base * and will call base.set_time() during evaluation * in order to evaluate the @p base class at any arbitrary time. + * It is guaranteed that the temporal state of @p base is returned + * to its original settings after each function evaluation in this + * class. */ IncrementalFunction(Function &base); diff --git a/source/base/incremental_function.cc b/source/base/incremental_function.cc index 027424e6d9..f5e23b1c40 100644 --- a/source/base/incremental_function.cc +++ b/source/base/incremental_function.cc @@ -43,6 +43,30 @@ namespace Functions + template + RangeNumberType + IncrementalFunction::value(const Point &p, + unsigned int comp) const + { + // Cache the time state of the base class in case it has been changed + // within the user code. We reset the wrapped function to the original + // state once we're done with our own evaluations. + const auto orig_time = base.get_time(); + + base.set_time(this->get_time()); + const RangeNumberType current = base.value(p, comp); + + base.set_time(this->get_time() - delta_t); + const RangeNumberType old = base.value(p, comp); + + // Reset wrapped function time setting + base.set_time(orig_time); + + return current - old; + } + + + template void IncrementalFunction::vector_value( @@ -53,6 +77,11 @@ namespace Functions // the data via a mutex std::lock_guard lock(mutex); + // Cache the time state of the base class in case it has been changed + // within the user code. We reset the wrapped function to the original + // state once we're done with our own evaluations. + const auto orig_time = base.get_time(); + base.set_time(this->get_time()); base.vector_value(p, values); @@ -60,22 +89,9 @@ namespace Functions base.vector_value(p, values_old); values -= values_old; - } - - - template - RangeNumberType - IncrementalFunction::value(const Point &p, - unsigned int comp) const - { - base.set_time(this->get_time()); - const RangeNumberType current = base.value(p, comp); - - base.set_time(this->get_time() - delta_t); - const RangeNumberType old = base.value(p, comp); - - return current - old; + // Reset wrapped function time setting + base.set_time(orig_time); } -- 2.39.5