www.mooseframework.org
MooseObject.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 "InputParameters.h"
14 #include "ConsoleStreamInterface.h"
15 #include "Registry.h"
16 
17 #include "libmesh/parallel_object.h"
18 
19 #define usingMooseObjectMembers \
20  using MooseObject::isParamValid; \
21  using MooseObject::paramError
22 
23 class MooseApp;
24 class MooseObject;
25 
26 template <>
28 
29 // needed to avoid #include cycle with MooseApp and MooseObject
30 [[noreturn]] void callMooseErrorRaw(std::string & msg, MooseApp * app);
31 
32 // helper macro to explicitly instantiate AD classes
33 #define adBaseClass(X) \
34  template class X<RESIDUAL>; \
35  template class X<JACOBIAN>
36 
37 #define adGetParam this->template getParam
38 
42 class MooseObject : public ConsoleStreamInterface, public libMesh::ParallelObject
43 {
44 public:
46 
47  virtual ~MooseObject() = default;
48 
53  const std::string & type() const { return _type; }
54 
59  const std::string & name() const { return _name; }
60 
65  const InputParameters & parameters() const { return _pars; }
66 
72  template <typename T>
73  const T & getParam(const std::string & name) const;
74 
79  template <typename T>
80  T getCheckedPointerParam(const std::string & name, const std::string & error_string = "") const
81  {
82  return parameters().getCheckedPointerParam<T>(name, error_string);
83  }
84 
89  inline bool isParamValid(const std::string & name) const { return _pars.isParamValid(name); }
90 
94  MooseApp & getMooseApp() const { return _app; }
95 
99  virtual bool enabled() const { return _enabled; }
100 
107  template <typename... Args>
108  [[noreturn]] void paramError(const std::string & param, Args... args) {
109  auto prefix = param + ": ";
110  if (!_pars.inputLocation(param).empty())
111  prefix = _pars.inputLocation(param) + ": (" + _pars.paramFullpath(param) + "):\n";
112  mooseError(prefix, args...);
113  }
114 
121  template <typename... Args>
122  void paramWarning(const std::string & param, Args... args)
123  {
124  auto prefix = param + ": ";
125  if (!_pars.inputLocation(param).empty())
126  prefix = _pars.inputLocation(param) + ": (" + _pars.paramFullpath(param) + "):\n";
127  mooseWarning(prefix, args...);
128  }
129 
137  template <typename... Args>
138  void paramInfo(const std::string & param, Args... args)
139  {
140  auto prefix = param + ": ";
141  if (!_pars.inputLocation(param).empty())
142  prefix = _pars.inputLocation(param) + ": (" + _pars.paramFullpath(param) + "):\n";
143  mooseInfo(prefix, args...);
144  }
145 
146  template <typename... Args>
147  [[noreturn]] void mooseError(Args &&... args) const {
148  std::ostringstream oss;
149  moose::internal::mooseStreamAll(oss, std::forward<Args>(args)...);
150  std::string msg = oss.str();
151  callMooseErrorRaw(msg, &_app);
152  }
153 
154  template <typename... Args>
155  void mooseWarning(Args &&... args) const
156  {
157  moose::internal::mooseWarningStream(_console, std::forward<Args>(args)...);
158  }
159 
160  template <typename... Args>
161  void mooseDeprecated(Args &&... args) const
162  {
163  moose::internal::mooseDeprecatedStream(_console, false, std::forward<Args>(args)...);
164  }
165 
166  template <typename... Args>
167  void mooseInfo(Args &&... args) const
168  {
169  moose::internal::mooseInfoStream(_console, std::forward<Args>(args)...);
170  }
171 
172 protected:
175 
178 
180  const std::string & _type;
181 
183  const std::string & _name;
184 
186  const bool & _enabled;
187 };
188 
189 template <typename T>
190 const T &
191 MooseObject::getParam(const std::string & name) const
192 {
193  return InputParameters::getParamHelper(name, _pars, static_cast<T *>(0));
194 }
195 
void mooseStreamAll(std::ostringstream &ss)
All of the following are not meant to be called directly - they are called by the normal macros (moos...
Definition: MooseError.C:87
T getCheckedPointerParam(const std::string &name, const std::string &error_string="") const
Verifies that the requested parameter exists and is not NULL and returns it to the caller...
Definition: MooseObject.h:80
static const T & getParamHelper(const std::string &name, const InputParameters &pars, const T *the_type)
void callMooseErrorRaw(std::string &msg, MooseApp *app)
Definition: MooseObject.C:57
virtual ~MooseObject()=default
void mooseWarning(Args &&... args) const
Definition: MooseObject.h:155
T getCheckedPointerParam(const std::string &name, const std::string &error_string="") const
Verifies that the requested parameter exists and is not NULL and returns it to the caller...
MooseObject(const InputParameters &parameters)
Definition: MooseObject.C:48
Base class for MOOSE-based applications.
Definition: MooseApp.h:59
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void mooseError(Args &&... args) const
Definition: MooseObject.h:147
void paramInfo(const std::string &param, Args... args)
Emits an informational message prefixed with the file and line number of the given param (from the in...
Definition: MooseObject.h:138
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseObject.h:191
const std::string & type() const
Get the type of this object.
Definition: MooseObject.h:53
void mooseInfoStream(S &oss, Args &&... args)
Definition: MooseError.h:167
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:42
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseObject.h:65
const std::string & inputLocation(const std::string &param) const
Get/set a string representing the location in the input text the parameter originated from (i...
const bool & _enabled
Reference to the "enable" InputParaemters, used by Controls for toggling on/off MooseObjects.
Definition: MooseObject.h:186
An inteface for the _console for outputting to the Console object.
void mooseWarningStream(S &oss, Args &&... args)
Definition: MooseError.h:138
const InputParameters & _pars
Parameters of this object, references the InputParameters stored in the InputParametersWarehouse.
Definition: MooseObject.h:174
InputParameters validParams< MooseObject >()
Definition: MooseObject.C:25
void paramError(const std::string &param, Args... args)
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition: MooseObject.h:108
const std::string & _name
The name of this object, reference to value stored in InputParameters.
Definition: MooseObject.h:183
void mooseDeprecatedStream(S &oss, bool expired, Args &&... args)
Definition: MooseError.h:179
void mooseDeprecated(Args &&... args) const
Definition: MooseObject.h:161
MooseApp & _app
The MooseApp this object is associated with.
Definition: MooseObject.h:177
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:59
void mooseInfo(Args &&... args) const
Definition: MooseObject.h:167
MooseApp & getMooseApp() const
Get the MooseApp this object is associated with.
Definition: MooseObject.h:94
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
void paramWarning(const std::string &param, Args... args)
Emits a warning prefixed with the file and line number of the given param (from the input file) along...
Definition: MooseObject.h:122
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:89
virtual bool enabled() const
Return the enabled status of the object.
Definition: MooseObject.h:99
const std::string & _type
The type of this object (the Class name)
Definition: MooseObject.h:180
const std::string & paramFullpath(const std::string &param) const
Get/set a string representing the full HIT parameter path from the input file (e.g.
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.