LCOV - code coverage report
Current view: top level - include/reporters - ReporterInterface.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 1f9d31 Lines: 29 31 93.5 %
Date: 2025-09-02 20:01:20 Functions: 49 51 96.1 %
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             : #pragma once
      10             : 
      11             : #include "MooseTypes.h"
      12             : #include "ReporterData.h"
      13             : #include "MooseObject.h"
      14             : 
      15             : // Forward declarations
      16             : class FEProblemBase;
      17             : class InputParameters;
      18             : 
      19             : /**
      20             :  * Interface to allow object to consume Reporter values.
      21             :  */
      22             : class ReporterInterface
      23             : {
      24             : public:
      25             :   static InputParameters validParams();
      26             :   ReporterInterface(const MooseObject * moose_object);
      27             : 
      28             : protected:
      29             :   ///@{
      30             :   /**
      31             :    * doco-normal-methods-begin
      32             :    * Returns read-only reference to a Reporter value that is provided by an input parameter.
      33             :    * @tparam T The C++ type of the Reporter value being consumed
      34             :    * @param param_name The name of the parameter that gives the name of the Reporter, which
      35             :    *                   must be a ReporterName parameter (i.e., getParam<ReporterName>(param_name)).
      36             :    * @param mode The mode that the object will consume the Reporter value
      37             :    * @pararm time_index (optional) If zero is provided the current value is returned. Use a positive
      38             :    *                    index to return previous values (1 = older, 2 = older, etc.). The maximum
      39             :    *                    number of old values is dictated by the ReporterData object.
      40             :    */
      41             :   template <typename T>
      42             :   const T & getReporterValue(const std::string & param_name, const std::size_t time_index = 0);
      43             :   template <typename T>
      44             :   const T & getReporterValue(const std::string & param_name,
      45             :                              ReporterMode mode,
      46             :                              const std::size_t time_index = 0);
      47             :   // doco-normal-methods-end
      48             :   ///@}
      49             : 
      50             :   ///@{
      51             :   /**
      52             :    * Returns read-only reference to a Reporter value that is provided by name directly.
      53             :    * @tparam T The C++ type of the Reporter value being consumed
      54             :    * @param reporter_name A ReporterName object that for the desired Reporter value.
      55             :    * @param mode The mode that the object will consume the Reporter value
      56             :    * @pararm time_index (optional) If zero is provided the current value is returned. Use a positive
      57             :    *                    index to return previous values (1 = older, 2 = older, etc.). The maximum
      58             :    *                    number of old values is dictated by the ReporterData object.
      59             :    */
      60             :   template <typename T>
      61             :   const T & getReporterValueByName(const ReporterName & reporter_name,
      62             :                                    const std::size_t time_index = 0);
      63             :   template <typename T>
      64             :   const T & getReporterValueByName(const ReporterName & reporter_name,
      65             :                                    ReporterMode mode,
      66             :                                    const std::size_t time_index = 0);
      67             :   ///@}
      68             : 
      69             :   ///@{
      70             :   /**
      71             :    * Return True if the Reporter value exists.
      72             :    * @tparam T The C++ type of the Reporter value being consumed
      73             :    * @param reporter_name A ReporterName object that for the desired Reporter value.
      74             :    */
      75             :   bool hasReporterValue(const std::string & param_name) const;
      76             :   bool hasReporterValueByName(const ReporterName & reporter_name) const;
      77             :   template <typename T>
      78             :   bool hasReporterValue(const std::string & param_name) const;
      79             :   template <typename T>
      80             :   bool hasReporterValueByName(const ReporterName & reporter_name) const;
      81             : 
      82             :   ///@}
      83             : 
      84             :   /**
      85             :    * Get the reporter context to allow non-typed operations with the data
      86             :    * @param reporter_name A ReporterName object that for the desired Reporter context.
      87             :    * @returns ReporterContextBase of the reporter value
      88             :    */
      89             :   const ReporterContextBase &
      90             :   getReporterContextBaseByName(const ReporterName & reporter_name) const;
      91             : 
      92             :   /**
      93             :    * @returns The ReporterName associated with the parametre \p param_name.
      94             :    *
      95             :    * Performs error checking to mak sure that the parameter is valid.
      96             :    */
      97             :   const ReporterName & getReporterName(const std::string & param_name) const;
      98             : 
      99             :   /**
     100             :    * A method that can be overridden to update the UO dependencies.
     101             :    *
     102             :    * This is needed because the get methods for this interface cannot be virtual because of the
     103             :    * template parameter. See GeneralUserObject for how it is utilized.
     104             :    */
     105         970 :   virtual void addReporterDependencyHelper(const ReporterName & /*state_name*/) {}
     106             : 
     107             : private:
     108             :   /**
     109             :    * @returns True if all Reporters have been added (the task associated with adding them is
     110             :    * complete)
     111             :    */
     112             :   bool reportersAdded() const;
     113             : 
     114             :   /**
     115             :    * Helpers for "possibly" checking if a Reporter value exists. This is only
     116             :    * able to check for existance after all Reporters have been added (after
     117             :    * the task creating them has been called). If called before said task, this
     118             :    * will do nothing, hence the "possibly". This allows us to have errors reported
     119             :    * directly by the object requesting the Reporter instead of through a system with
     120             :    * less context.
     121             :    */
     122             :   template <typename T>
     123             :   void possiblyCheckHasReporter(const ReporterName & reporter_name,
     124             :                                 const std::string & param_name = "") const;
     125             : 
     126             :   /// Parameters for the MooseObject inherting from this interface
     127             :   const InputParameters & _ri_params;
     128             : 
     129             :   /// Provides access to FEProblemBase::getReporterData
     130             :   FEProblemBase & _ri_fe_problem_base;
     131             : 
     132             :   /// The ReporterData
     133             :   const ReporterData & _ri_reporter_data;
     134             : 
     135             :   /// The MooseObject needing this interface
     136             :   const MooseObject & _ri_moose_object;
     137             : };
     138             : 
     139             : template <typename T>
     140             : const T &
     141         291 : ReporterInterface::getReporterValue(const std::string & param_name, const std::size_t time_index)
     142             : {
     143         291 :   return getReporterValue<T>(param_name, REPORTER_MODE_UNSET, time_index);
     144             : }
     145             : 
     146             : template <typename T>
     147             : const T &
     148        1003 : ReporterInterface::getReporterValue(const std::string & param_name,
     149             :                                     ReporterMode mode,
     150             :                                     const std::size_t time_index)
     151             : {
     152        1003 :   const auto & reporter_name = getReporterName(param_name);
     153             : 
     154         995 :   possiblyCheckHasReporter<T>(reporter_name, param_name);
     155             : 
     156         991 :   return getReporterValueByName<T>(reporter_name, mode, time_index);
     157             : }
     158             : 
     159             : template <typename T>
     160             : const T &
     161         403 : ReporterInterface::getReporterValueByName(const ReporterName & reporter_name,
     162             :                                           const std::size_t time_index)
     163             : {
     164         403 :   return getReporterValueByName<T>(reporter_name, REPORTER_MODE_UNSET, time_index);
     165             : }
     166             : 
     167             : template <typename T>
     168             : const T &
     169        1394 : ReporterInterface::getReporterValueByName(const ReporterName & reporter_name,
     170             :                                           ReporterMode mode,
     171             :                                           const std::size_t time_index)
     172             : {
     173        1394 :   possiblyCheckHasReporter<T>(reporter_name);
     174             : 
     175        1390 :   addReporterDependencyHelper(reporter_name);
     176             : 
     177        1390 :   return _ri_reporter_data.getReporterValue<T>(reporter_name, _ri_moose_object, mode, time_index);
     178             : }
     179             : 
     180             : template <typename T>
     181             : bool
     182           4 : ReporterInterface::hasReporterValue(const std::string & param_name) const
     183             : {
     184           4 :   if (!reportersAdded())
     185           4 :     _ri_moose_object.mooseError(
     186             :         "Cannot call hasReporterValue() until all Reporters have been constructed.");
     187             : 
     188           0 :   return hasReporterValueByName<T>(getReporterName(param_name));
     189             : }
     190             : 
     191             : template <typename T>
     192             : bool
     193        1548 : ReporterInterface::hasReporterValueByName(const ReporterName & reporter_name) const
     194             : {
     195        1548 :   if (!reportersAdded())
     196           4 :     _ri_moose_object.mooseError(
     197             :         "Cannot call hasReporterValueByName() until all Reporters have been constructed.");
     198             : 
     199        1544 :   return _ri_reporter_data.hasReporterValue<T>(reporter_name);
     200             : }
     201             : 
     202             : template <typename T>
     203             : void
     204        2389 : ReporterInterface::possiblyCheckHasReporter(const ReporterName & reporter_name,
     205             :                                             const std::string & param_name /* = "" */) const
     206             : {
     207        2389 :   if (reportersAdded() && !hasReporterValueByName<T>(reporter_name))
     208             :   {
     209           8 :     std::stringstream oss;
     210           8 :     oss << "A Reporter value with the name \"" << reporter_name << "\" and type \""
     211           8 :         << MooseUtils::prettyCppType<T>() << "\" was not found.";
     212             : 
     213           8 :     if (_ri_params.isParamValid(param_name))
     214           4 :       _ri_moose_object.paramError(param_name, oss.str());
     215             :     else
     216           4 :       _ri_moose_object.mooseError(oss.str());
     217           0 :   }
     218        2381 : }

Generated by: LCOV version 1.14