www.mooseframework.org
UserObjectInterface.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 "FEProblemBase.h"
15 
16 // Forward declarations
17 class UserObject;
18 
23 {
24 public:
26 
33  UserObjectInterface(const MooseObject * moose_object);
34 
38  UserObjectName getUserObjectName(const std::string & param_name) const;
39 
43  bool hasUserObject(const std::string & param_name) const;
45  template <class T>
46  bool hasUserObject(const std::string & param_name) const;
48 
49  /*
50  * @return Whether or not a UserObject exists with the name \p object_name.
51  */
53  bool hasUserObjectByName(const UserObjectName & object_name) const;
54  template <class T>
55  bool hasUserObjectByName(const UserObjectName & object_name) const;
57 
66  template <class T>
67  const T & getUserObject(const std::string & param_name, bool is_dependency = true) const;
68 
77  template <class T>
78  const T & getUserObjectByName(const UserObjectName & object_name,
79  bool is_dependency = true) const;
80 
89  const UserObject & getUserObjectBase(const std::string & param_name,
90  bool is_dependency = true) const;
91 
100  const UserObject & getUserObjectBaseByName(const UserObjectName & object_name,
101  bool is_dependency = true) const;
102 
103 protected:
108  virtual void addUserObjectDependencyHelper(const UserObject & /* uo */) const {}
109 
110 private:
116  template <class T>
117  const T & castUserObject(const UserObject & uo_base, const std::string & param_name = "") const;
118 
120  const std::string & userObjectType(const UserObject & uo) const;
122  const std::string & userObjectName(const UserObject & uo) const;
123 
126 
129 
132 };
133 
134 template <class T>
135 const T &
137  const std::string & param_name /* = "" */) const
138 {
139  const T * uo = dynamic_cast<const T *>(&uo_base);
140 
141  if (!uo)
142  {
143  std::stringstream oss;
144  oss << "The provided UserObject \"" << userObjectName(uo_base) << "\" of type "
145  << userObjectType(uo_base)
146  << " is not derived from the required type.\n\nThe UserObject must derive from "
147  << MooseUtils::prettyCppType<T>() << ".";
148 
149  if (_uoi_moose_object.parameters().isParamValid(param_name))
150  _uoi_moose_object.paramError(param_name, oss.str());
151  else
152  _uoi_moose_object.mooseError(oss.str());
153  }
154 
155  return *uo;
156 }
157 
158 template <class T>
159 const T &
160 UserObjectInterface::getUserObject(const std::string & param_name, const bool is_dependency) const
161 {
162  return castUserObject<T>(getUserObjectBase(param_name, is_dependency), param_name);
163 }
164 
165 template <class T>
166 const T &
167 UserObjectInterface::getUserObjectByName(const UserObjectName & object_name,
168  const bool is_dependency) const
169 {
170  return castUserObject<T>(getUserObjectBaseByName(object_name, is_dependency));
171 }
172 
173 template <class T>
174 bool
175 UserObjectInterface::hasUserObject(const std::string & param_name) const
176 {
177  return hasUserObjectByName<T>(getUserObjectName(param_name));
178 }
179 
180 template <class T>
181 bool
182 UserObjectInterface::hasUserObjectByName(const UserObjectName & object_name) const
183 {
184  if (!hasUserObjectByName(object_name))
185  return false;
186  return dynamic_cast<const T *>(&_uoi_feproblem.getUserObjectBase(object_name));
187 }
UserObjectName getUserObjectName(const std::string &param_name) const
const MooseObject & _uoi_moose_object
Moose object using the interface.
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()
const T & castUserObject(const UserObject &uo_base, const std::string &param_name="") const
Internal helper that casts the UserObject uo_base to the reqested type.
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:33
const THREAD_ID _uoi_tid
Thread ID.
Interface for objects that need to use UserObjects.
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
const T & getUserObject(const std::string &param_name, bool is_dependency=true) const
Get an user object with a given parameter param_name.
const UserObject & getUserObjectBase(const std::string &param_name, bool is_dependency=true) const
Get an user object with a given parameter param_name.
const std::string & userObjectType(const UserObject &uo) const
Gets a UserObject&#39;s type; avoids including UserObject.h in the UserObjectInterface.
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
const InputParameters & parameters() const
Get the parameters of the object.
const UserObject & getUserObjectBase(const std::string &name, const THREAD_ID tid=0) const
Get the user object by its name.
const UserObject & getUserObjectBaseByName(const UserObjectName &object_name, bool is_dependency=true) const
Get an user object with the name object_name.
virtual void addUserObjectDependencyHelper(const UserObject &) const
Helper for deriving classes to override to add dependencies when a UserObject is requested.
bool hasUserObjectByName(const UserObjectName &object_name) const
bool hasUserObject(const std::string &param_name) const
Base class for user-specific data.
Definition: UserObject.h:39
const std::string & userObjectName(const UserObject &uo) const
Gets a UserObject&#39;s name; avoids including UserObject.h in the UserObjectInterface.
unsigned int THREAD_ID
Definition: MooseTypes.h:198
UserObjectInterface(const MooseObject *moose_object)
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.