https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
13#include "StreamArguments.h"
14#include "InputParameters.h"
15#include "MooseError.h"
16#include "MooseObjectName.h"
18
19class MooseApp;
20
21namespace hit
22{
23class 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{
51public:
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
64 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; }
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
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 {
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
316 template <typename... Args>
317 void mooseDeprecated(Args &&... args) const
318 {
320 _console, false, true, true, messagePrefix(true), std::forward<Args>(args)...);
321 }
322
326 template <typename... Args>
327 void mooseDeprecatedNoTrace(Args &&... args) const
328 {
330 _console, false, true, false, messagePrefix(true), std::forward<Args>(args)...);
331 }
332
333 template <typename... Args>
334 void mooseInfo(Args &&... args) const
335 {
336 moose::internal::mooseInfoStream(_console, messagePrefix(true), std::forward<Args>(args)...);
337 }
338
347 [[noreturn]] void callMooseError(std::string msg,
348 const bool with_prefix,
349 const hit::Node * node = nullptr,
350 const bool show_trace = true) const;
351
366 [[noreturn]] static void callMooseError(MooseApp * const app,
367 const InputParameters & params,
368 std::string msg,
369 const bool with_prefix,
370 const hit::Node * node,
371 const bool show_trace = true);
372
373protected:
376
378 const std::string & _type;
379
381 const std::string & _name;
382
385
386private:
393 static std::string messagePrefix(const InputParameters & params, const bool hit_prefix);
394
401 static const hit::Node * getHitNode(const InputParameters & params);
402};
403
404template <typename T>
405const T &
406MooseBase::getParam(const std::string & name) const
407{
408 return InputParameters::getParamHelper<T>(name, _pars);
409}
410
411template <typename T>
412const T *
413MooseBase::queryParam(const std::string & name) const
414{
415 return _pars.queryParam<T>(name);
416}
417
418template <typename T>
419const T &
420MooseBase::getRenamedParam(const std::string & old_name, const std::string & new_name) const
421{
422 // Most important: accept new parameter
423 if (isParamSetByUser(new_name) && !isParamValid(old_name))
424 return getParam<T>(new_name);
425 // Second most: accept old parameter
426 if (isParamValid(old_name) && !isParamSetByUser(new_name))
427 return getParam<T>(old_name);
428 // Third most: accept default for new parameter
429 if (isParamValid(new_name) && !isParamValid(old_name))
430 return getParam<T>(new_name);
431 // Refuse: no default, no value passed
432 if (!isParamValid(old_name) && !isParamValid(new_name))
433 mooseError("parameter '" + new_name +
434 "' is being retrieved without being set.\nDid you misspell it?");
435 // Refuse: both old and new parameters set by user
436 else
437 mooseError("Parameter '" + new_name + "' may not be provided alongside former parameter '" +
438 old_name + "'");
439}
440
441template <typename T1, typename T2>
442std::vector<std::pair<T1, T2>>
443MooseBase::getParam(const std::string & param1, const std::string & param2) const
444{
445 return _pars.get<T1, T2>(param1, param2);
446}
447
448template <typename T>
449T
450MooseBase::getCheckedPointerParam(const std::string & name, const std::string & error_string) const
451{
452 return _pars.getCheckedPointerParam<T>(name, error_string);
453}
454
455template <typename... Args>
456[[noreturn]] void
457MooseBase::paramError(const std::string & param, Args... args) const
458{
459 _pars.paramError(param, std::forward<Args>(args)...);
460}
461
462template <typename... Args>
463void
464MooseBase::paramWarning(const std::string & param, Args... args) const
465{
466 mooseWarning(_pars.paramMessage(param, std::forward<Args>(args)...));
467}
468
469template <typename... Args>
470void
471MooseBase::paramInfo(const std::string & param, Args... args) const
472{
473 mooseInfo(_pars.paramMessage(param, std::forward<Args>(args)...));
474}
An inteface for the _console for outputting to the Console object.
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
bool isParamSetByUser(const std::string &name) const
Method returns true if the parameter was set by the user.
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.
std::string paramMessage(const std::string &param, Args... args) const
void paramError(const std::string &param, Args... args) const
Emits a parameter error prefixed with the parameter location and object information if available.
const std::string & getBase() const
bool hasBase() const
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.
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another,...
const T * queryParam(const std::string &name) const
Query a parameter.
Base class for MOOSE-based applications.
Definition MooseApp.h:110
Base class for everything in MOOSE with a name and a type.
Definition MooseBase.h:50
const T & getRenamedParam(const std::string &old_name, const std::string &new_name) const
Retrieve a renamed parameter for the object.
Definition MooseBase.h:420
const InputParameters & parameters() const
Get the parameters of the object.
Definition MooseBase.h:131
const std::string & getBase() const
Definition MooseBase.h:147
MooseObjectParameterName uniqueParameterName(const std::string &parameter_name) const
Definition MooseBase.C:63
const std::string & type() const
Get the type of this class.
Definition MooseBase.h:93
bool hasBase() const
Definition MooseBase.h:142
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 mooseErrorNonPrefixed(Args &&... args) const
Emits an error without the prefixing included in mooseError().
Definition MooseBase.h:290
std::string errorPrefix(const std::string &) const
Deprecated message prefix; the error type is no longer used.
Definition MooseBase.h:264
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:471
const hit::Node * getHitNode() const
Definition MooseBase.h:136
std::string typeAndName() const
Get the class's combined type and name; useful in error handling.
Definition MooseBase.C:57
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:464
static InputParameters validParams()
Definition MooseBase.C:28
static const std::string unique_name_param
The name of the parameter that contains the unique object name.
Definition MooseBase.h:57
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
Emits a deprecation warning prefixed with the object name and type, and a stack trace.
Definition MooseBase.h:317
void mooseDeprecatedNoTrace(Args &&... args) const
Emits a deprecation warning prefixed with the object name and type, and no stack trace.
Definition MooseBase.h:327
static const std::string name_param
The name of the parameter that contains the object name.
Definition MooseBase.h:55
static const std::string type_param
The name of the parameter that contains the object type.
Definition MooseBase.h:53
MooseObjectName uniqueName() const
Definition MooseBase.C:69
MooseApp & _app
The MOOSE application this is associated with.
Definition MooseBase.h:375
void mooseDocumentedError(const std::string &repo_name, const unsigned int issue_num, Args &&... args) const
Definition MooseBase.h:277
virtual ~MooseBase()=default
const std::string & name() const
Get the name of the class.
Definition MooseBase.h:103
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:457
const T * queryParam(const std::string &name) const
Query a parameter for the object.
Definition MooseBase.h:413
void mooseWarningNonPrefixed(Args &&... args) const
Emits a warning without the prefixing included in mooseWarning().
Definition MooseBase.h:308
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
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
const std::string & _type
The type of this class.
Definition MooseBase.h:378
void mooseWarning(Args &&... args) const
Emits a warning prefixed with object name and type.
Definition MooseBase.h:299
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
const InputParameters & _pars
The object's parameters.
Definition MooseBase.h:384
const std::string & _name
The name of this class.
Definition MooseBase.h:381
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:450
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
Definition MooseBase.h:406
void mooseInfo(Args &&... args) const
Definition MooseBase.h:334
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition MooseBase.h:199
MooseApp & getMooseApp() const
Get the MooseApp this class is associated with.
Definition MooseBase.h:87
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
std::string messagePrefix(const bool hit_prefix=true) const
Definition MooseBase.h:256
static const std::string app_param
The name of the parameter that contains the MooseApp.
Definition MooseBase.h:59
A class for storing the names of MooseObject by tag and object name.
A class for storing an input parameter name.
Definition Moose.h:48
void mooseWarningStream(S &oss, Args &&... args)
Definition MooseError.h:197
void mooseDeprecatedStream(S &oss, const bool expired, const bool print_title, const bool show_trace, Args &&... args)
Definition MooseError.h:252
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
void mooseInfoStream(S &oss, Args &&... args)
Definition MooseError.h:245