https://mooseframework.inl.gov
MooseBase.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 #include "ConsoleStreamInterface.h"
13 #include "StreamArguments.h"
14 #include "InputParameters.h"
15 #include "MooseError.h"
16 #include "MooseObjectName.h"
18 
19 class MooseApp;
20 
21 namespace hit
22 {
23 class Node;
24 }
25 
26 #define usingMooseBaseMembers \
27  using MooseBase::getMooseApp; \
28  using MooseBase::type; \
29  using MooseBase::name; \
30  using MooseBase::typeAndName; \
31  using MooseBase::uniqueName; \
32  using MooseBase::parameters; \
33  using MooseBase::isParamValid; \
34  using MooseBase::isParamSetByUser; \
35  using MooseBase::paramError; \
36  using MooseBase::paramWarning; \
37  using MooseBase::paramInfo; \
38  using MooseBase::_app; \
39  using MooseBase::_type; \
40  using MooseBase::_name; \
41  using MooseBase::_pars
42 
50 {
51 public:
53  static const std::string type_param;
55  static const std::string name_param;
57  static const std::string unique_name_param;
59  static const std::string app_param;
61  static const std::string moose_base_param;
62 
64 
69  MooseBase(const InputParameters & params);
70 
76  MooseBase(MooseApp & app, const InputParameters & params);
77 
78  virtual ~MooseBase() = default;
79 
83  MooseApp & getMooseApp() const { return _app; }
84 
89  const std::string & type() const
90  {
91  mooseAssert(_type.size(), "Empty type");
92  return _type;
93  }
94 
99  const std::string & name() const
100  {
101  mooseAssert(_name.size(), "Empty name");
102  return _name;
103  }
104 
109  std::string typeAndName() const;
110 
115  MooseObjectParameterName uniqueParameterName(const std::string & parameter_name) const;
116 
121  MooseObjectName uniqueName() const;
122 
127  const InputParameters & parameters() const { return _pars; }
128 
132  const hit::Node * getHitNode() const { return getHitNode(_pars); }
133 
138  bool hasBase() const { return _pars.hasBase(); }
139 
143  const std::string & getBase() const { return _pars.getBase(); }
144 
150  template <typename T>
151  const T & getParam(const std::string & name) const;
152 
161  template <typename T>
162  const T * queryParam(const std::string & name) const;
163 
171  template <typename T>
172  const T & getRenamedParam(const std::string & old_name, const std::string & new_name) const;
173 
180  template <typename T1, typename T2>
181  std::vector<std::pair<T1, T2>> getParam(const std::string & param1,
182  const std::string & param2) const;
183 
188  template <typename T>
189  T getCheckedPointerParam(const std::string & name, const std::string & error_string = "") const;
190 
195  inline bool isParamValid(const std::string & name) const { return _pars.isParamValid(name); }
196 
201  inline bool isParamSetByUser(const std::string & name) const
202  {
203  return _pars.isParamSetByUser(name);
204  }
205 
214  void connectControllableParams(const std::string & parameter,
215  const std::string & object_type,
216  const std::string & object_name,
217  const std::string & object_parameter) const;
218 
225  template <typename... Args>
226  [[noreturn]] void paramError(const std::string & param, Args... args) const;
227 
234  template <typename... Args>
235  void paramWarning(const std::string & param, Args... args) const;
236 
244  template <typename... Args>
245  void paramInfo(const std::string & param, Args... args) const;
246 
252  std::string messagePrefix(const bool hit_prefix = true) const
253  {
254  return messagePrefix(_pars, hit_prefix);
255  }
256 
260  std::string errorPrefix(const std::string &) const { return messagePrefix(); }
261 
266  template <typename... Args>
267  [[noreturn]] void mooseError(Args &&... args) const
268  {
269  callMooseError(argumentsToString(std::forward<Args>(args)...), /* with_prefix = */ true);
270  }
271 
272  template <typename... Args>
273  [[noreturn]] void mooseDocumentedError(const std::string & repo_name,
274  const unsigned int issue_num,
275  Args &&... args) const
276  {
278  repo_name, issue_num, argumentsToString(std::forward<Args>(args)...)),
279  /* with_prefix = */ true);
280  }
281 
285  template <typename... Args>
286  [[noreturn]] void mooseErrorNonPrefixed(Args &&... args) const
287  {
288  callMooseError(argumentsToString(std::forward<Args>(args)...), /* with_prefix = */ false);
289  }
290 
294  template <typename... Args>
295  void mooseWarning(Args &&... args) const
296  {
297  moose::internal::mooseWarningStream(_console, messagePrefix(true), std::forward<Args>(args)...);
298  }
299 
303  template <typename... Args>
304  void mooseWarningNonPrefixed(Args &&... args) const
305  {
306  moose::internal::mooseWarningStream(_console, std::forward<Args>(args)...);
307  }
308 
309  template <typename... Args>
310  void mooseDeprecated(Args &&... args) const
311  {
313  _console, false, true, messagePrefix(true), std::forward<Args>(args)...);
314  }
315 
316  template <typename... Args>
317  void mooseInfo(Args &&... args) const
318  {
319  moose::internal::mooseInfoStream(_console, messagePrefix(true), std::forward<Args>(args)...);
320  }
321 
329  [[noreturn]] void
330  callMooseError(std::string msg, const bool with_prefix, const hit::Node * node = nullptr) const;
331 
345  [[noreturn]] static void callMooseError(MooseApp * const app,
346  const InputParameters & params,
347  std::string msg,
348  const bool with_prefix,
349  const hit::Node * node);
350 
351 protected:
354 
356  const std::string & _type;
357 
359  const std::string & _name;
360 
363 
364 private:
371  static std::string messagePrefix(const InputParameters & params, const bool hit_prefix);
372 
379  static const hit::Node * getHitNode(const InputParameters & params);
380 };
381 
382 template <typename T>
383 const T &
384 MooseBase::getParam(const std::string & name) const
385 {
386  return InputParameters::getParamHelper<T>(name, _pars);
387 }
388 
389 template <typename T>
390 const T *
391 MooseBase::queryParam(const std::string & name) const
392 {
393  return isParamValid(name) ? &getParam<T>(name) : nullptr;
394 }
395 
396 template <typename T>
397 const T &
398 MooseBase::getRenamedParam(const std::string & old_name, const std::string & new_name) const
399 {
400  // Most important: accept new parameter
401  if (isParamSetByUser(new_name) && !isParamValid(old_name))
402  return getParam<T>(new_name);
403  // Second most: accept old parameter
404  if (isParamValid(old_name) && !isParamSetByUser(new_name))
405  return getParam<T>(old_name);
406  // Third most: accept default for new parameter
407  if (isParamValid(new_name) && !isParamValid(old_name))
408  return getParam<T>(new_name);
409  // Refuse: no default, no value passed
410  if (!isParamValid(old_name) && !isParamValid(new_name))
411  mooseError("parameter '" + new_name +
412  "' is being retrieved without being set.\nDid you misspell it?");
413  // Refuse: both old and new parameters set by user
414  else
415  mooseError("Parameter '" + new_name + "' may not be provided alongside former parameter '" +
416  old_name + "'");
417 }
418 
419 template <typename T1, typename T2>
420 std::vector<std::pair<T1, T2>>
421 MooseBase::getParam(const std::string & param1, const std::string & param2) const
422 {
423  return _pars.get<T1, T2>(param1, param2);
424 }
425 
426 template <typename T>
427 T
428 MooseBase::getCheckedPointerParam(const std::string & name, const std::string & error_string) const
429 {
430  return _pars.getCheckedPointerParam<T>(name, error_string);
431 }
432 
433 template <typename... Args>
434 [[noreturn]] void
435 MooseBase::paramError(const std::string & param, Args... args) const
436 {
437  _pars.paramError(param, std::forward<Args>(args)...);
438 }
439 
440 template <typename... Args>
441 void
442 MooseBase::paramWarning(const std::string & param, Args... args) const
443 {
444  mooseWarning(_pars.paramMessage(param, std::forward<Args>(args)...));
445 }
446 
447 template <typename... Args>
448 void
449 MooseBase::paramInfo(const std::string & param, Args... args) const
450 {
451  mooseInfo(_pars.paramMessage(param, std::forward<Args>(args)...));
452 }
std::string paramMessage(const std::string &param, Args... args) const
void mooseInfo(Args &&... args) const
Definition: MooseBase.h:317
static const std::string name_param
The name of the parameter that contains the object name.
Definition: MooseBase.h:55
static const std::string app_param
The name of the parameter that contains the MooseApp.
Definition: MooseBase.h:59
const InputParameters & _pars
The object&#39;s parameters.
Definition: MooseBase.h:362
void callMooseError(std::string msg, const bool with_prefix, const hit::Node *node=nullptr) const
External method for calling moose error with added object context.
Definition: MooseBase.C:102
const std::string & _name
The name of this class.
Definition: MooseBase.h:359
Base class for everything in MOOSE with a name and a type.
Definition: MooseBase.h:49
static const std::string type_param
The name of the parameter that contains the object type.
Definition: MooseBase.h:53
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 ...
Definition: MooseBase.h:435
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseBase.h:384
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:127
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...
Base class for MOOSE-based applications.
Definition: MooseApp.h:96
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
void mooseDocumentedError(const std::string &repo_name, const unsigned int issue_num, Args &&... args) const
Definition: MooseBase.h:273
MooseObjectName uniqueName() const
Definition: MooseBase.C:66
bool hasBase() const
const std::string & getBase() const
MooseObjectParameterName uniqueParameterName(const std::string &parameter_name) const
Definition: MooseBase.C:60
MooseApp & getMooseApp() const
Get the MooseApp this class is associated with.
Definition: MooseBase.h:83
static const std::string unique_name_param
The name of the parameter that contains the unique object name.
Definition: MooseBase.h:57
void mooseInfoStream(S &oss, Args &&... args)
Definition: MooseError.h:268
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: MooseBase.h:428
void paramError(const std::string &param, Args... args) const
Emits a parameter error prefixed with the parameter location and object information if available...
bool hasBase() const
Definition: MooseBase.h:138
void mooseErrorNonPrefixed(Args &&... args) const
Emits an error without the prefixing included in mooseError().
Definition: MooseBase.h:286
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:99
static const std::string moose_base_param
The name of the parameter that contains the moose system base.
Definition: MooseBase.h:61
void mooseDeprecated(Args &&... args) const
Definition: MooseBase.h:310
void mooseWarningNonPrefixed(Args &&... args) const
Emits a warning without the prefixing included in mooseWarning().
Definition: MooseBase.h:304
An inteface for the _console for outputting to the Console object.
void mooseWarningStream(S &oss, Args &&... args)
Definition: MooseError.h:220
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:89
std::string typeAndName() const
Get the class&#39;s combined type and name; useful in error handling.
Definition: MooseBase.C:54
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:353
Definition: Moose.h:42
void mooseDeprecatedStream(S &oss, const bool expired, const bool print_title, Args &&... args)
Definition: MooseError.h:275
const T * queryParam(const std::string &name) const
Query a parameter for the object.
Definition: MooseBase.h:391
bool isParamSetByUser(const std::string &name) const
Method returns true if the parameter was set by the user.
void mooseWarning(Args &&... args) const
Emits a warning prefixed with object name and type.
Definition: MooseBase.h:295
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition: MooseBase.h:267
virtual ~MooseBase()=default
std::string errorPrefix(const std::string &) const
Deprecated message prefix; the error type is no longer used.
Definition: MooseBase.h:260
A class for storing an input parameter name.
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseBase.h:195
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
const std::string & getBase() const
Definition: MooseBase.h:143
void paramWarning(const std::string &param, Args... args) const
Emits a warning prefixed with the file and line number of the given param (from the input file) along...
Definition: MooseBase.h:442
const hit::Node * getHitNode() const
Definition: MooseBase.h:132
A class for storing the names of MooseObject by tag and object name.
bool isParamSetByUser(const std::string &name) const
Test if the supplied parameter is set by a user, as opposed to not set or set to default.
Definition: MooseBase.h:201
const std::string & _type
The type of this class.
Definition: MooseBase.h:356
std::string formatMooseDocumentedError(const std::string &repo_name, const unsigned int issue_num, const std::string &msg)
Formats a documented error.
Definition: MooseError.C:105
static InputParameters validParams()
Definition: MooseBase.C:25
void connectControllableParams(const std::string &parameter, const std::string &object_type, const std::string &object_name, const std::string &object_parameter) const
Connect controllable parameter of this action with the controllable parameters of the objects added b...
Definition: MooseBase.C:74
MooseBase(const InputParameters &params)
Primary constructor for general objects.
Definition: MooseBase.C:48
const T & getRenamedParam(const std::string &old_name, const std::string &new_name) const
Retrieve a renamed parameter for the object.
Definition: MooseBase.h:398
std::string messagePrefix(const bool hit_prefix=true) const
Definition: MooseBase.h:252
void paramInfo(const std::string &param, Args... args) const
Emits an informational message prefixed with the file and line number of the given param (from the in...
Definition: MooseBase.h:449
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.