LCOV - code coverage report
Current view: top level - include/userobjects - UserObjectInterface.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #32971 (54bef8) with base c6cf66 Lines: 19 21 90.5 %
Date: 2026-05-29 20:35:17 Functions: 70 72 97.2 %
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             : #include "MooseUtils.h"
      15             : 
      16             : // Forward declarations
      17             : class MooseObject;
      18             : class UserObjectBase;
      19             : class FEProblemBase;
      20             : 
      21             : /**
      22             :  * Interface for objects that need to use UserObjects.
      23             :  */
      24             : class UserObjectInterface
      25             : {
      26             : public:
      27             :   static InputParameters validParams();
      28             : 
      29             :   UserObjectInterface(const MooseObject * moose_object);
      30             : 
      31             : #ifdef MOOSE_KOKKOS_ENABLED
      32             :   /**
      33             :    * Special constructor used for Kokkos functor copy during parallel dispatch
      34             :    */
      35             :   UserObjectInterface(const UserObjectInterface & object, const Moose::Kokkos::FunctorCopy & key);
      36             : #endif
      37             : 
      38             :   /**
      39             :    * @return The name of the user object associated with the parameter \p param_name
      40             :    */
      41             :   UserObjectName getUserObjectName(const std::string & param_name) const;
      42             : 
      43             :   /**
      44             :    * @return Whether or not a UserObject exists with the name given by the parameter \p param_name.
      45             :    */
      46             :   ///@{
      47             :   bool hasUserObject(const std::string & param_name) const;
      48             :   template <class T>
      49             :   bool hasUserObject(const std::string & param_name) const;
      50             :   ///@}
      51             : 
      52             :   /*
      53             :    * @return Whether or not a UserObject exists with the name \p object_name.
      54             :    */
      55             :   ///@{
      56             :   bool hasUserObjectByName(const UserObjectName & object_name) const;
      57             :   template <class T>
      58             :   bool hasUserObjectByName(const UserObjectName & object_name) const;
      59             :   ///@}
      60             : 
      61             :   /**
      62             :    * Get an user object with a given parameter \p param_name
      63             :    * @param param_name The name of the parameter key of the user object to retrieve
      64             :    * @param is_dependency Whether the user object we are retrieving should be viewed as a
      65             :    * dependency, e.g. whether the retrieved user object should be sorted and executed before this
      66             :    * object (if we are a user object)
      67             :    * @return The user object with name associated with the parameter \p param_name
      68             :    */
      69             :   template <class T>
      70             :   const T & getUserObject(const std::string & param_name, bool is_dependency = true) const;
      71             : 
      72             :   /**
      73             :    * Get an user object with the name \p object_name
      74             :    * @param object_name The name of the user object to retrieve
      75             :    * @param is_dependency Whether the user object we are retrieving should be viewed as a
      76             :    * dependency, e.g. whether the retrieved user object should be sorted and executed before this
      77             :    * object (if we are a user object)
      78             :    * @return The user object with the name \p object_name
      79             :    */
      80             :   template <class T>
      81             :   const T & getUserObjectByName(const UserObjectName & object_name,
      82             :                                 bool is_dependency = true) const;
      83             : 
      84             :   /**
      85             :    * Get an user object with a given parameter \p param_name
      86             :    * @param param_name The name of the parameter key of the user object to retrieve
      87             :    * @param is_dependency Whether the user object we are retrieving should be viewed as a
      88             :    * dependency, e.g. whether the retrieved user object should be sorted and executed before this
      89             :    * object (if we are a user object)
      90             :    * @return The user object with name associated with the parameter \p param_name
      91             :    */
      92             :   const UserObjectBase & getUserObjectBase(const std::string & param_name,
      93             :                                            bool is_dependency = true) const;
      94             : 
      95             :   /**
      96             :    * Get an user object with the name \p object_name
      97             :    * @param object_name The name of the user object to retrieve
      98             :    * @param is_dependency Whether the user object we are retrieving should be viewed as a
      99             :    * dependency, e.g. whether the retrieved user object should be sorted and executed before this
     100             :    * object (if we are a user object)
     101             :    * @return The user object with the name \p object_name
     102             :    */
     103             :   const UserObjectBase & getUserObjectBaseByName(const UserObjectName & object_name,
     104             :                                                  bool is_dependency = true) const;
     105             : 
     106             : protected:
     107             :   /**
     108             :    * Helper for deriving classes to override to add dependencies when a UserObject
     109             :    * is requested.
     110             :    */
     111         811 :   virtual void addUserObjectDependencyHelper(const UserObjectBase & /* uo */) const {}
     112             : 
     113             : private:
     114             :   /**
     115             :    * Go directly to the FEProblem for the requested \p object_name for thread ID \p tid
     116             :    */
     117             :   const UserObjectBase & getUserObjectFromFEProblem(const UserObjectName & object_name,
     118             :                                                     const THREAD_ID tid = 0) const;
     119             : 
     120             :   /**
     121             :    * Internal helper that casts the UserObject \p uo_base to the requested type. Exits with
     122             :    * a useful error if the casting failed. If the parameter \p param_name is provided and
     123             :    * is valid, a paramError() will be used instead.
     124             :    */
     125             :   template <class T>
     126             :   const T & castUserObject(const UserObjectBase & uo_base,
     127             :                            const std::string & param_name = "") const;
     128             : 
     129             :   /**
     130             :    * emit an error for the given parameter
     131             :    */
     132             :   void mooseObjectError(const std::string & param_name, std::stringstream & oss) const;
     133             : 
     134             :   /// Gets a UserObject's type; avoids including UserObject.h in the UserObjectInterface
     135             :   const std::string & userObjectType(const UserObjectBase & uo) const;
     136             :   /// Gets a UserObject's name; avoids including UserObject.h in the UserObjectInterface
     137             :   const std::string & userObjectName(const UserObjectBase & uo) const;
     138             : 
     139             :   /// Moose object using the interface
     140             :   const MooseObject & _uoi_moose_object;
     141             : 
     142             :   /// Reference to the FEProblemBase instance
     143             :   const FEProblemBase & _uoi_feproblem;
     144             : 
     145             :   /// Thread ID
     146             :   const THREAD_ID _uoi_tid;
     147             : };
     148             : 
     149             : template <class T>
     150             : const T &
     151        6251 : UserObjectInterface::castUserObject(const UserObjectBase & uo_base,
     152             :                                     const std::string & param_name /* = "" */) const
     153             : {
     154        6251 :   const T * uo = dynamic_cast<const T *>(&uo_base);
     155             : 
     156        6251 :   if (!uo)
     157             :   {
     158           6 :     std::stringstream oss;
     159          12 :     oss << "The provided UserObject \"" << userObjectName(uo_base) << "\" of type "
     160          12 :         << userObjectType(uo_base)
     161             :         << " is not derived from the required type.\n\nThe UserObject must derive from "
     162           6 :         << MooseUtils::prettyCppType<T>() << ".";
     163             : 
     164           6 :     mooseObjectError(param_name, oss);
     165           0 :   }
     166             : 
     167        6245 :   return *uo;
     168             : }
     169             : 
     170             : template <class T>
     171             : const T &
     172        6217 : UserObjectInterface::getUserObject(const std::string & param_name, const bool is_dependency) const
     173             : {
     174        6217 :   return castUserObject<T>(getUserObjectBase(param_name, is_dependency), param_name);
     175             : }
     176             : 
     177             : template <class T>
     178             : const T &
     179          34 : UserObjectInterface::getUserObjectByName(const UserObjectName & object_name,
     180             :                                          const bool is_dependency) const
     181             : {
     182          65 :   return castUserObject<T>(getUserObjectBaseByName(object_name, is_dependency));
     183             : }
     184             : 
     185             : template <class T>
     186             : bool
     187          27 : UserObjectInterface::hasUserObject(const std::string & param_name) const
     188             : {
     189          27 :   return hasUserObjectByName<T>(getUserObjectName(param_name));
     190             : }
     191             : 
     192             : template <class T>
     193             : bool
     194          69 : UserObjectInterface::hasUserObjectByName(const UserObjectName & object_name) const
     195             : {
     196          69 :   if (!hasUserObjectByName(object_name))
     197           0 :     return false;
     198          69 :   return dynamic_cast<const T *>(&getUserObjectFromFEProblem(object_name));
     199             : }

Generated by: LCOV version 1.14