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 UserObject;
18 class FEProblemBase;
19 class MooseObject;
20 
25 {
26 public:
28 
35  UserObjectInterface(const MooseObject * moose_object);
36 
40  UserObjectName getUserObjectName(const std::string & param_name) const;
41 
45  bool hasUserObject(const std::string & param_name) const;
47  template <class T>
48  bool hasUserObject(const std::string & param_name) const;
50 
51  /*
52  * @return Whether or not a UserObject exists with the name \p object_name.
53  */
55  bool hasUserObjectByName(const UserObjectName & object_name) const;
56  template <class T>
57  bool hasUserObjectByName(const UserObjectName & object_name) const;
59 
68  template <class T>
69  const T & getUserObject(const std::string & param_name, bool is_dependency = true) const;
70 
79  template <class T>
80  const T & getUserObjectByName(const UserObjectName & object_name,
81  bool is_dependency = true) const;
82 
91  const UserObject & getUserObjectBase(const std::string & param_name,
92  bool is_dependency = true) const;
93 
102  const UserObject & getUserObjectBaseByName(const UserObjectName & object_name,
103  bool is_dependency = true) const;
104 
105 protected:
110  virtual void addUserObjectDependencyHelper(const UserObject & /* uo */) const {}
111 
112 private:
116  const UserObject & getUserObjectFromFEProblem(const UserObjectName & object_name) const;
117 
123  template <class T>
124  const T & castUserObject(const UserObject & uo_base, const std::string & param_name = "") const;
125 
129  void mooseObjectError(const std::string & param_name, std::stringstream & oss) const;
130 
132  const std::string & userObjectType(const UserObject & uo) const;
134  const std::string & userObjectName(const UserObject & uo) const;
135 
138 
141 
144 };
145 
146 template <class T>
147 const T &
149  const std::string & param_name /* = "" */) const
150 {
151  const T * uo = dynamic_cast<const T *>(&uo_base);
152 
153  if (!uo)
154  {
155  std::stringstream oss;
156  oss << "The provided UserObject \"" << userObjectName(uo_base) << "\" of type "
157  << userObjectType(uo_base)
158  << " is not derived from the required type.\n\nThe UserObject must derive from "
159  << MooseUtils::prettyCppType<T>() << ".";
160 
161  mooseObjectError(param_name, oss);
162  }
163 
164  return *uo;
165 }
166 
167 template <class T>
168 const T &
169 UserObjectInterface::getUserObject(const std::string & param_name, const bool is_dependency) const
170 {
171  return castUserObject<T>(getUserObjectBase(param_name, is_dependency), param_name);
172 }
173 
174 template <class T>
175 const T &
176 UserObjectInterface::getUserObjectByName(const UserObjectName & object_name,
177  const bool is_dependency) const
178 {
179  return castUserObject<T>(getUserObjectBaseByName(object_name, is_dependency));
180 }
181 
182 template <class T>
183 bool
184 UserObjectInterface::hasUserObject(const std::string & param_name) const
185 {
186  return hasUserObjectByName<T>(getUserObjectName(param_name));
187 }
188 
189 template <class T>
190 bool
191 UserObjectInterface::hasUserObjectByName(const UserObjectName & object_name) const
192 {
193  if (!hasUserObjectByName(object_name))
194  return false;
195  return dynamic_cast<const T *>(&getUserObjectFromFEProblem(object_name));
196 }
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 requested 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:28
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 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.
const UserObject & getUserObjectFromFEProblem(const UserObjectName &object_name) const
Go directly to the FEProblem for the requested UserObject.
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:40
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:209
UserObjectInterface(const MooseObject *moose_object)