LCOV - code coverage report
Current view: top level - include/utils - SwapBackSentinel.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 13 13 100.0 %
Date: 2025-07-17 01:28:37 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       3             : //*
       4             : //* All rights reserved, see COPYRIGHT for full restrictions
       5             : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
       6             : //*
       7             : //* Licensed under LGPL 2.1, please see LICENSE for details
       8             : //* https://www.gnu.org/licenses/lgpl-2.1.html
       9             : 
      10             : #pragma once
      11             : 
      12             : // MOOSE includes
      13             : #include "MooseTypes.h"
      14             : 
      15             : // Forward declarations
      16             : class FEProblemBase;
      17             : 
      18             : /**
      19             :  * The "SwapBackSentinel" class's destructor guarantees that
      20             :  * FEProblemBase::swapBackMaterials{Face,Neighbor}() is called even when
      21             :  * an exception is thrown from FEProblemBase::reinitMaterials{Face,Neighbor}.
      22             :  * This is because stack unwinding (for caught exceptions) guarantees
      23             :  * that object destructors are called.  The typical way of using this
      24             :  * object is to construct it in the same scope where reinitMaterials
      25             :  * is called:
      26             :  *
      27             :  * {
      28             :  *   SwapBackSentinel sentinel(_fe_problem, &FEProblemBase::swapBackMaterials, _tid);
      29             :  *   _fe_problem.reinitMaterials(_subdomain, _tid);
      30             :  * }
      31             :  */
      32             : class SwapBackSentinel
      33             : {
      34             : public:
      35             :   /**
      36             :    * SwapBackFunction is a typedef for a pointer to an FEProblemBase
      37             :    * member function taking a THREAD_ID and returning void.  All the
      38             :    * FEProblemBase::swapBackMaterialXXX() members have this signature.
      39             :    */
      40             :   using SwapBackFunction = void (FEProblemBase::*)(THREAD_ID);
      41             : 
      42             :   /**
      43             :    * Constructor taking an FEProblemBase reference, a function to call,
      44             :    * and the THREAD_ID argument.
      45             :    */
      46    11099719 :   SwapBackSentinel(FEProblemBase & fe_problem,
      47             :                    SwapBackFunction func,
      48             :                    THREAD_ID tid,
      49             :                    const bool & predicate,
      50             :                    const bool store_pred_as_value = true)
      51    11099719 :     : _fe_problem(fe_problem),
      52    11099719 :       _func(func),
      53    11099719 :       _thread_id(tid),
      54    11099719 :       _predicate(store_pred_as_value ? (predicate ? _always_true : _always_false) : predicate)
      55             :   {
      56    11099719 :   }
      57             : 
      58   377133448 :   SwapBackSentinel(FEProblemBase & fe_problem, SwapBackFunction func, THREAD_ID tid)
      59   377133448 :     : _fe_problem(fe_problem), _func(func), _thread_id(tid), _predicate(_always_true)
      60             :   {
      61   377133448 :   }
      62             : 
      63             :   /**
      64             :    * The destructor calls swap back function only if the predicate is true.
      65             :    */
      66   388233091 :   ~SwapBackSentinel()
      67             :   {
      68   388233091 :     if (_predicate)
      69   386478263 :       (_fe_problem.*_func)(_thread_id);
      70   388233091 :   }
      71             : 
      72             : private:
      73             :   FEProblemBase & _fe_problem;
      74             :   SwapBackFunction _func;
      75             :   THREAD_ID _thread_id;
      76             :   static constexpr bool _always_true = true;
      77             :   static constexpr bool _always_false = false;
      78             :   const bool & _predicate;
      79             : };

Generated by: LCOV version 1.14