https://mooseframework.inl.gov
UserObjectInterface.h
Go to the documentation of this file.
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 
25 {
26 public:
28 
29  UserObjectInterface(const MooseObject * moose_object);
30 
31 #ifdef MOOSE_KOKKOS_ENABLED
32 
36 #endif
37 
41  UserObjectName getUserObjectName(const std::string & param_name) const;
42 
46  bool hasUserObject(const std::string & param_name) const;
48  template <class T>
49  bool hasUserObject(const std::string & param_name) const;
51 
52  /*
53  * @return Whether or not a UserObject exists with the name \p object_name.
54  */
56  bool hasUserObjectByName(const UserObjectName & object_name) const;
57  template <class T>
58  bool hasUserObjectByName(const UserObjectName & object_name) const;
60 
69  template <class T>
70  const T & getUserObject(const std::string & param_name, bool is_dependency = true) const;
71 
80  template <class T>
81  const T & getUserObjectByName(const UserObjectName & object_name,
82  bool is_dependency = true) const;
83 
92  const UserObjectBase & getUserObjectBase(const std::string & param_name,
93  bool is_dependency = true) const;
94 
103  const UserObjectBase & getUserObjectBaseByName(const UserObjectName & object_name,
104  bool is_dependency = true) const;
105 
106 protected:
111  virtual void addUserObjectDependencyHelper(const UserObjectBase & /* uo */) const {}
112 
113 private:
117  const UserObjectBase & getUserObjectFromFEProblem(const UserObjectName & object_name,
118  const THREAD_ID tid = 0) const;
119 
125  template <class T>
126  const T & castUserObject(const UserObjectBase & uo_base,
127  const std::string & param_name = "") const;
128 
132  void mooseObjectError(const std::string & param_name, std::stringstream & oss) const;
133 
135  const std::string & userObjectType(const UserObjectBase & uo) const;
137  const std::string & userObjectName(const UserObjectBase & uo) const;
138 
141 
144 
147 };
148 
149 template <class T>
150 const T &
152  const std::string & param_name /* = "" */) const
153 {
154  const T * uo = dynamic_cast<const T *>(&uo_base);
155 
156  if (!uo)
157  {
158  std::stringstream oss;
159  oss << "The provided UserObject \"" << userObjectName(uo_base) << "\" of type "
160  << userObjectType(uo_base)
161  << " is not derived from the required type.\n\nThe UserObject must derive from "
162  << MooseUtils::prettyCppType<T>() << ".";
163 
164  mooseObjectError(param_name, oss);
165  }
166 
167  return *uo;
168 }
169 
170 template <class T>
171 const T &
172 UserObjectInterface::getUserObject(const std::string & param_name, const bool is_dependency) const
173 {
174  return castUserObject<T>(getUserObjectBase(param_name, is_dependency), param_name);
175 }
176 
177 template <class T>
178 const T &
179 UserObjectInterface::getUserObjectByName(const UserObjectName & object_name,
180  const bool is_dependency) const
181 {
182  return castUserObject<T>(getUserObjectBaseByName(object_name, is_dependency));
183 }
184 
185 template <class T>
186 bool
187 UserObjectInterface::hasUserObject(const std::string & param_name) const
188 {
189  return hasUserObjectByName<T>(getUserObjectName(param_name));
190 }
191 
192 template <class T>
193 bool
194 UserObjectInterface::hasUserObjectByName(const UserObjectName & object_name) const
195 {
196  if (!hasUserObjectByName(object_name))
197  return false;
198  return dynamic_cast<const T *>(&getUserObjectFromFEProblem(object_name));
199 }
const std::string & userObjectName(const UserObjectBase &uo) const
Gets a UserObject&#39;s name; avoids including UserObject.h in the UserObjectInterface.
UserObjectName getUserObjectName(const std::string &param_name) const
const T & castUserObject(const UserObjectBase &uo_base, const std::string &param_name="") const
Internal helper that casts the UserObject uo_base to the requested type.
const MooseObject & _uoi_moose_object
Moose object using the interface.
virtual void addUserObjectDependencyHelper(const UserObjectBase &) const
Helper for deriving classes to override to add dependencies when a UserObject is requested.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const T & getUserObjectByName(const UserObjectName &object_name, bool is_dependency=true) const
Get an user object with the name object_name.
static InputParameters validParams()
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
const FEProblemBase & _uoi_feproblem
Reference to the FEProblemBase instance.
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
const UserObjectBase & getUserObjectBaseByName(const UserObjectName &object_name, bool is_dependency=true) const
Get an user object with the name object_name.
const THREAD_ID _uoi_tid
Thread ID.
Interface for objects that need to use UserObjects.
const T & getUserObject(const std::string &param_name, bool is_dependency=true) const
Get an user object with a given parameter param_name.
void mooseObjectError(const std::string &param_name, std::stringstream &oss) const
emit an error for the given parameter
const UserObjectBase & getUserObjectFromFEProblem(const UserObjectName &object_name, const THREAD_ID tid=0) const
Go directly to the FEProblem for the requested object_name for thread ID tid.
bool hasUserObjectByName(const UserObjectName &object_name) const
bool hasUserObject(const std::string &param_name) const
const std::string & userObjectType(const UserObjectBase &uo) const
Gets a UserObject&#39;s type; avoids including UserObject.h in the UserObjectInterface.
const UserObjectBase & getUserObjectBase(const std::string &param_name, bool is_dependency=true) const
Get an user object with a given parameter param_name.
unsigned int THREAD_ID
Definition: MooseTypes.h:237
UserObjectInterface(const MooseObject *moose_object)