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  template <typename T>
200  inline bool haveParameter(const std::string & name) const
201  {
202  return _pars.have_parameter<T>(name);
203  }
204 
209  inline bool isParamValid(const std::string & name) const { return _pars.isParamValid(name); }
210 
215  inline bool isParamSetByUser(const std::string & name) const
216  {
217  return _pars.isParamSetByUser(name);
218  }
219 
228  void connectControllableParams(const std::string & parameter,
229  const std::string & object_type,
230  const std::string & object_name,
231  const std::string & object_parameter) const;
232 
239  template <typename... Args>
240  [[noreturn]] void paramError(const std::string & param, Args... args) const;
241 
248  template <typename... Args>
249  void paramWarning(const std::string & param, Args... args) const;
250 
258  template <typename... Args>
259  void paramInfo(const std::string & param, Args... args) const;
260 
266  std::string messagePrefix(const bool hit_prefix = true) const
267  {
268  return messagePrefix(_pars, hit_prefix);
269  }
270 
274  std::string errorPrefix(const std::string &) const { return messagePrefix(); }
275 
280  template <typename... Args>
281  [[noreturn]] void mooseError(Args &&... args) const
282  {
283  callMooseError(argumentsToString(std::forward<Args>(args)...), /* with_prefix = */ true);
284  }
285 
286  template <typename... Args>
287  [[noreturn]] void mooseDocumentedError(const std::string & repo_name,
288  const unsigned int issue_num,
289  Args &&... args) const
290  {
292  repo_name, issue_num, argumentsToString(std::forward<Args>(args)...)),
293  /* with_prefix = */ true);
294  }
295 
299  template <typename... Args>
300  [[noreturn]] void mooseErrorNonPrefixed(Args &&... args) const
301  {
302  callMooseError(argumentsToString(std::forward<Args>(args)...), /* with_prefix = */ false);
303  }
304 
308  template <typename... Args>
309  void mooseWarning(Args &&... args) const
310  {
311  moose::internal::mooseWarningStream(_console, messagePrefix(true), std::forward<Args>(args)...);
312  }
313 
317  template <typename... Args>
318  void mooseWarningNonPrefixed(Args &&... args) const
319  {
320  moose::internal::mooseWarningStream(_console, std::forward<Args>(args)...);
321  }
322 
326  template <typename... Args>
327  void mooseDeprecated(Args &&... args) const
328  {
330  _console, false, true, true, messagePrefix(true), std::forward<Args>(args)...);
331  }
332 
336  template <typename... Args>
337  void mooseDeprecatedNoTrace(Args &&... args) const
338  {
340  _console, false, true, false, messagePrefix(true), std::forward<Args>(args)...);
341  }
342 
343  template <typename... Args>
344  void mooseInfo(Args &&... args) const
345  {
346  moose::internal::mooseInfoStream(_console, messagePrefix(true), std::forward<Args>(args)...);
347  }
348 
357  [[noreturn]] void callMooseError(std::string msg,
358  const bool with_prefix,
359  const hit::Node * node = nullptr,
360  const bool show_trace = true) const;
361 
376  [[noreturn]] static void callMooseError(MooseApp * const app,
377  const InputParameters & params,
378  std::string msg,
379  const bool with_prefix,
380  const hit::Node * node,
381  const bool show_trace = true);
382 
383 protected:
386 
388  const std::string & _type;
389 
391  const std::string & _name;
392 
395 
396 private:
403  static std::string messagePrefix(const InputParameters & params, const bool hit_prefix);
404 
411  static const hit::Node * getHitNode(const InputParameters & params);
412 };
413 
414 template <typename T>
415 const T &
416 MooseBase::getParam(const std::string & name) const
417 {
418  return InputParameters::getParamHelper<T>(name, _pars);
419 }
420 
421 template <typename T>
422 const T *
423 MooseBase::queryParam(const std::string & name) const
424 {
425  return haveParameter<T>(name) && isParamValid(name) ? &getParam<T>(name) : nullptr;
426 }
427 
428 template <typename T>
429 const T &
430 MooseBase::getRenamedParam(const std::string & old_name, const std::string & new_name) const
431 {
432  // Most important: accept new parameter
433  if (isParamSetByUser(new_name) && !isParamValid(old_name))
434  return getParam<T>(new_name);
435  // Second most: accept old parameter
436  if (isParamValid(old_name) && !isParamSetByUser(new_name))
437  return getParam<T>(old_name);
438  // Third most: accept default for new parameter
439  if (isParamValid(new_name) && !isParamValid(old_name))
440  return getParam<T>(new_name);
441  // Refuse: no default, no value passed
442  if (!isParamValid(old_name) && !isParamValid(new_name))
443  mooseError("parameter '" + new_name +
444  "' is being retrieved without being set.\nDid you misspell it?");
445  // Refuse: both old and new parameters set by user
446  else
447  mooseError("Parameter '" + new_name + "' may not be provided alongside former parameter '" +
448  old_name + "'");
449 }
450 
451 template <typename T1, typename T2>
452 std::vector<std::pair<T1, T2>>
453 MooseBase::getParam(const std::string & param1, const std::string & param2) const
454 {
455  return _pars.get<T1, T2>(param1, param2);
456 }
457 
458 template <typename T>
459 T
460 MooseBase::getCheckedPointerParam(const std::string & name, const std::string & error_string) const
461 {
462  return _pars.getCheckedPointerParam<T>(name, error_string);
463 }
464 
465 template <typename... Args>
466 [[noreturn]] void
467 MooseBase::paramError(const std::string & param, Args... args) const
468 {
469  _pars.paramError(param, std::forward<Args>(args)...);
470 }
471 
472 template <typename... Args>
473 void
474 MooseBase::paramWarning(const std::string & param, Args... args) const
475 {
476  mooseWarning(_pars.paramMessage(param, std::forward<Args>(args)...));
477 }
478 
479 template <typename... Args>
480 void
481 MooseBase::paramInfo(const std::string & param, Args... args) const
482 {
483  mooseInfo(_pars.paramMessage(param, std::forward<Args>(args)...));
484 }
std::string paramMessage(const std::string &param, Args... args) const
void mooseInfo(Args &&... args) const
Definition: MooseBase.h:344
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:394
const std::string & _name
The name of this class.
Definition: MooseBase.h:391
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:467
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition: MooseBase.h:416
bool haveParameter(const std::string &name) const
Test if a parameter of the given name and type exists.
Definition: MooseBase.h:200
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:108
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:287
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:245
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:460
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:300
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
Emits a deprecation warning prefixed with the object name and type, and a stack trace.
Definition: MooseBase.h:327
void mooseWarningNonPrefixed(Args &&... args) const
Emits a warning without the prefixing included in mooseWarning().
Definition: MooseBase.h:318
void mooseDeprecatedStream(S &oss, const bool expired, const bool print_title, const bool show_trace, Args &&... args)
Definition: MooseError.h:252
An inteface for the _console for outputting to the Console object.
void mooseWarningStream(S &oss, Args &&... args)
Definition: MooseError.h:197
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:385
Definition: Moose.h:46
const T * queryParam(const std::string &name) const
Query a parameter for the object.
Definition: MooseBase.h:423
bool have_parameter(std::string_view name) const
A wrapper around the Parameters base class method.
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:309
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:281
virtual ~MooseBase()=default
std::string errorPrefix(const std::string &) const
Deprecated message prefix; the error type is no longer used.
Definition: MooseBase.h:274
void callMooseError(std::string msg, const bool with_prefix, const hit::Node *node=nullptr, const bool show_trace=true) const
External method for calling moose error with added object context.
Definition: MooseBase.C:105
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:209
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:474
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:215
const std::string & _type
The type of this class.
Definition: MooseBase.h:388
std::string formatMooseDocumentedError(const std::string &repo_name, const unsigned int issue_num, const std::string &msg)
Formats a documented error.
Definition: MooseError.C:142
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:430
std::string messagePrefix(const bool hit_prefix=true) const
Definition: MooseBase.h:266
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:481
void mooseDeprecatedNoTrace(Args &&... args) const
Emits a deprecation warning prefixed with the object name and type, and no stack trace.
Definition: MooseBase.h:337
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.