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 #ifdef MOOSE_KOKKOS_ENABLED
63  static const std::string kokkos_object_param;
65 #endif
66 
68 
73  MooseBase(const InputParameters & params);
74 
80  MooseBase(MooseApp & app, const InputParameters & params);
81 
82  virtual ~MooseBase() = default;
83 
87  MooseApp & getMooseApp() const { return _app; }
88 
93  const std::string & type() const
94  {
95  mooseAssert(_type.size(), "Empty type");
96  return _type;
97  }
98 
103  const std::string & name() const
104  {
105  mooseAssert(_name.size(), "Empty name");
106  return _name;
107  }
108 
113  std::string typeAndName() const;
114 
119  MooseObjectParameterName uniqueParameterName(const std::string & parameter_name) const;
120 
125  MooseObjectName uniqueName() const;
126 
131  const InputParameters & parameters() const { return _pars; }
132 
136  const hit::Node * getHitNode() const { return getHitNode(_pars); }
137 
142  bool hasBase() const { return _pars.hasBase(); }
143 
147  const std::string & getBase() const { return _pars.getBase(); }
148 
154  template <typename T>
155  const T & getParam(const std::string & name) const;
156 
165  template <typename T>
166  const T * queryParam(const std::string & name) const;
167 
175  template <typename T>
176  const T & getRenamedParam(const std::string & old_name, const std::string & new_name) const;
177 
184  template <typename T1, typename T2>
185  std::vector<std::pair<T1, T2>> getParam(const std::string & param1,
186  const std::string & param2) const;
187 
192  template <typename T>
193  T getCheckedPointerParam(const std::string & name, const std::string & error_string = "") const;
194 
199  inline bool isParamValid(const std::string & name) const { return _pars.isParamValid(name); }
200 
205  inline bool isParamSetByUser(const std::string & name) const
206  {
207  return _pars.isParamSetByUser(name);
208  }
209 
218  void connectControllableParams(const std::string & parameter,
219  const std::string & object_type,
220  const std::string & object_name,
221  const std::string & object_parameter) const;
222 
229  template <typename... Args>
230  [[noreturn]] void paramError(const std::string & param, Args... args) const;
231 
238  template <typename... Args>
239  void paramWarning(const std::string & param, Args... args) const;
240 
248  template <typename... Args>
249  void paramInfo(const std::string & param, Args... args) const;
250 
256  std::string messagePrefix(const bool hit_prefix = true) const
257  {
258  return messagePrefix(_pars, hit_prefix);
259  }
260 
264  std::string errorPrefix(const std::string &) const { return messagePrefix(); }
265 
270  template <typename... Args>
271  [[noreturn]] void mooseError(Args &&... args) const
272  {
273  callMooseError(argumentsToString(std::forward<Args>(args)...), /* with_prefix = */ true);
274  }
275 
276  template <typename... Args>
277  [[noreturn]] void mooseDocumentedError(const std::string & repo_name,
278  const unsigned int issue_num,
279  Args &&... args) const
280  {
282  repo_name, issue_num, argumentsToString(std::forward<Args>(args)...)),
283  /* with_prefix = */ true);
284  }
285 
289  template <typename... Args>
290  [[noreturn]] void mooseErrorNonPrefixed(Args &&... args) const
291  {
292  callMooseError(argumentsToString(std::forward<Args>(args)...), /* with_prefix = */ false);
293  }
294 
298  template <typename... Args>
299  void mooseWarning(Args &&... args) const
300  {
301  moose::internal::mooseWarningStream(_console, messagePrefix(true), std::forward<Args>(args)...);
302  }
303 
307  template <typename... Args>
308  void mooseWarningNonPrefixed(Args &&... args) const
309  {
310  moose::internal::mooseWarningStream(_console, std::forward<Args>(args)...);
311  }
312 
313  template <typename... Args>
314  void mooseDeprecated(Args &&... args) const
315  {
317  _console, false, true, messagePrefix(true), std::forward<Args>(args)...);
318  }
319 
320  template <typename... Args>
321  void mooseInfo(Args &&... args) const
322  {
323  moose::internal::mooseInfoStream(_console, messagePrefix(true), std::forward<Args>(args)...);
324  }
325 
333  [[noreturn]] void
334  callMooseError(std::string msg, const bool with_prefix, const hit::Node * node = nullptr) const;
335 
349  [[noreturn]] static void callMooseError(MooseApp * const app,
350  const InputParameters & params,
351  std::string msg,
352  const bool with_prefix,
353  const hit::Node * node);
354 
355 protected:
358 
360  const std::string & _type;
361 
363  const std::string & _name;
364 
367 
368 private:
375  static std::string messagePrefix(const InputParameters & params, const bool hit_prefix);
376 
383  static const hit::Node * getHitNode(const InputParameters & params);
384 };
385 
386 template <typename T>
387 const T &
388 MooseBase::getParam(const std::string & name) const
389 {
390  return InputParameters::getParamHelper<T>(name, _pars);
391 }
392 
393 template <typename T>
394 const T *
395 MooseBase::queryParam(const std::string & name) const
396 {
397  return isParamValid(name) ? &getParam<T>(name) : nullptr;
398 }
399 
400 template <typename T>
401 const T &
402 MooseBase::getRenamedParam(const std::string & old_name, const std::string & new_name) const
403 {
404  // Most important: accept new parameter
405  if (isParamSetByUser(new_name) && !isParamValid(old_name))
406  return getParam<T>(new_name);
407  // Second most: accept old parameter
408  if (isParamValid(old_name) && !isParamSetByUser(new_name))
409  return getParam<T>(old_name);
410  // Third most: accept default for new parameter
411  if (isParamValid(new_name) && !isParamValid(old_name))
412  return getParam<T>(new_name);
413  // Refuse: no default, no value passed
414  if (!isParamValid(old_name) && !isParamValid(new_name))
415  mooseError("parameter '" + new_name +
416  "' is being retrieved without being set.\nDid you misspell it?");
417  // Refuse: both old and new parameters set by user
418  else
419  mooseError("Parameter '" + new_name + "' may not be provided alongside former parameter '" +
420  old_name + "'");
421 }
422 
423 template <typename T1, typename T2>
424 std::vector<std::pair<T1, T2>>
425 MooseBase::getParam(const std::string & param1, const std::string & param2) const
426 {
427  return _pars.get<T1, T2>(param1, param2);
428 }
429 
430 template <typename T>
431 T
432 MooseBase::getCheckedPointerParam(const std::string & name, const std::string & error_string) const
433 {
434  return _pars.getCheckedPointerParam<T>(name, error_string);
435 }
436 
437 template <typename... Args>
438 [[noreturn]] void
439 MooseBase::paramError(const std::string & param, Args... args) const
440 {
441  _pars.paramError(param, std::forward<Args>(args)...);
442 }
443 
444 template <typename... Args>
445 void
446 MooseBase::paramWarning(const std::string & param, Args... args) const
447 {
448  mooseWarning(_pars.paramMessage(param, std::forward<Args>(args)...));
449 }
450 
451 template <typename... Args>
452 void
453 MooseBase::paramInfo(const std::string & param, Args... args) const
454 {
455  mooseInfo(_pars.paramMessage(param, std::forward<Args>(args)...));
456 }
std::string paramMessage(const std::string &param, Args... args) const
void mooseInfo(Args &&... args) const
Definition: MooseBase.h:321
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:366
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:105
const std::string & _name
The name of this class.
Definition: MooseBase.h:363
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:439
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseBase.h:388
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:131
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:103
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:277
MooseObjectName uniqueName() const
Definition: MooseBase.C:69
bool hasBase() const
const std::string & getBase() const
MooseObjectParameterName uniqueParameterName(const std::string &parameter_name) const
Definition: MooseBase.C:63
MooseApp & getMooseApp() const
Get the MooseApp this class is associated with.
Definition: MooseBase.h:87
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:258
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:432
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:142
void mooseErrorNonPrefixed(Args &&... args) const
Emits an error without the prefixing included in mooseError().
Definition: MooseBase.h:290
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:103
static const std::string moose_base_param
The name of the parameter that contains the moose system base.
Definition: MooseBase.h:61
static const std::string kokkos_object_param
The name of the parameter that indicates an object is a Kokkos functor.
Definition: MooseBase.h:64
void mooseDeprecated(Args &&... args) const
Definition: MooseBase.h:314
void mooseWarningNonPrefixed(Args &&... args) const
Emits a warning without the prefixing included in mooseWarning().
Definition: MooseBase.h:308
An inteface for the _console for outputting to the Console object.
void mooseWarningStream(S &oss, Args &&... args)
Definition: MooseError.h:210
const std::string & type() const
Get the type of this class.
Definition: MooseBase.h:93
std::string typeAndName() const
Get the class&#39;s combined type and name; useful in error handling.
Definition: MooseBase.C:57
MooseApp & _app
The MOOSE application this is associated with.
Definition: MooseBase.h:357
Definition: Moose.h:42
void mooseDeprecatedStream(S &oss, const bool expired, const bool print_title, Args &&... args)
Definition: MooseError.h:265
const T * queryParam(const std::string &name) const
Query a parameter for the object.
Definition: MooseBase.h:395
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:299
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:271
virtual ~MooseBase()=default
std::string errorPrefix(const std::string &) const
Deprecated message prefix; the error type is no longer used.
Definition: MooseBase.h:264
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:199
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
const std::string & getBase() const
Definition: MooseBase.h:147
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:446
const hit::Node * getHitNode() const
Definition: MooseBase.h:136
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:205
const std::string & _type
The type of this class.
Definition: MooseBase.h:360
std::string formatMooseDocumentedError(const std::string &repo_name, const unsigned int issue_num, const std::string &msg)
Formats a documented error.
Definition: MooseError.C:128
static InputParameters validParams()
Definition: MooseBase.C:28
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:77
MooseBase(const InputParameters &params)
Primary constructor for general objects.
Definition: MooseBase.C:51
const T & getRenamedParam(const std::string &old_name, const std::string &new_name) const
Retrieve a renamed parameter for the object.
Definition: MooseBase.h:402
std::string messagePrefix(const bool hit_prefix=true) const
Definition: MooseBase.h:256
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:453
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.