Line data Source code
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"
17 : #include "MooseObjectParameterName.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 :
43 : /**
44 : * Base class for everything in MOOSE with a name and a type.
45 : * You will most likely want to inherit instead
46 : * - MooseObject for an object created within a system
47 : * - Action for a class performing a setup task, like creating objects
48 : */
49 : class MooseBase : public ConsoleStreamInterface
50 : {
51 : public:
52 : /// The name of the parameter that contains the object type
53 : static const std::string type_param;
54 : /// The name of the parameter that contains the object name
55 : static const std::string name_param;
56 : /// The name of the parameter that contains the unique object name
57 : static const std::string unique_name_param;
58 : /// The name of the parameter that contains the MooseApp
59 : static const std::string app_param;
60 : /// The name of the parameter that contains the moose system base
61 : static const std::string moose_base_param;
62 :
63 : static InputParameters validParams();
64 :
65 : /**
66 : * Primary constructor for general objects
67 : * @param params The parameters
68 : */
69 : MooseBase(const InputParameters & params);
70 :
71 : /**
72 : * Constructor to only be used by MooseApp.
73 : * @param app The app
74 : * @param params The app params
75 : */
76 : MooseBase(MooseApp & app, const InputParameters & params);
77 :
78 5459101 : virtual ~MooseBase() = default;
79 :
80 : /**
81 : * Get the MooseApp this class is associated with.
82 : */
83 9528171 : MooseApp & getMooseApp() const { return _app; }
84 :
85 : /**
86 : * Get the type of this class.
87 : * @return the name of the type of this class
88 : */
89 8062264 : const std::string & type() const
90 : {
91 : mooseAssert(_type.size(), "Empty type");
92 8062264 : return _type;
93 : }
94 :
95 : /**
96 : * Get the name of the class
97 : * @return The name of the class
98 : */
99 40276718 : const std::string & name() const
100 : {
101 : mooseAssert(_name.size(), "Empty name");
102 40276718 : return _name;
103 : }
104 :
105 : /**
106 : * Get the class's combined type and name; useful in error handling.
107 : * @return The type and name of this class in the form '<type()> "<name()>"'.
108 : */
109 : std::string typeAndName() const;
110 :
111 : /**
112 : * @returns The unique parameter name of a valid parameter of this
113 : * object for accessing parameter controls
114 : */
115 : MooseObjectParameterName uniqueParameterName(const std::string & parameter_name) const;
116 :
117 : /**
118 : * @returns The unique name for accessing input parameters of this object in
119 : * the InputParameterWarehouse
120 : */
121 : MooseObjectName uniqueName() const;
122 :
123 : /**
124 : * Get the parameters of the object
125 : * @return The parameters of the object
126 : */
127 33728374 : const InputParameters & parameters() const { return _pars; }
128 :
129 : /**
130 : * @returns The block-level hit node for this object, if any
131 : */
132 : const hit::Node * getHitNode() const { return getHitNode(_pars); }
133 :
134 : /**
135 : * @returns Whether or not this object has a registered base (set via
136 : * InputParameters::registerBase())
137 : */
138 161599 : bool hasBase() const { return _pars.hasBase(); }
139 :
140 : /**
141 : * @returns The registered base for this object (set via InputParameters::registerBase())
142 : */
143 893141 : const std::string & getBase() const { return _pars.getBase(); }
144 :
145 : /**
146 : * Retrieve a parameter for the object
147 : * @param name The name of the parameter
148 : * @return The value of the parameter
149 : */
150 : template <typename T>
151 : const T & getParam(const std::string & name) const;
152 :
153 : /**
154 : * Query a parameter for the object
155 : *
156 : * If the parameter is not valid, nullptr will be returned
157 : *
158 : * @param name The name of the parameter
159 : * @return A pointer to the parameter value, if it exists
160 : */
161 : template <typename T>
162 : const T * queryParam(const std::string & name) const;
163 :
164 : /**
165 : * Retrieve a renamed parameter for the object. This helper makes sure we
166 : * check both names before erroring, and that only one parameter is passed to avoid
167 : * silent errors
168 : * @param old_name the old name for the parameter
169 : * @param new_name the new name for the parameter
170 : */
171 : template <typename T>
172 : const T & getRenamedParam(const std::string & old_name, const std::string & new_name) const;
173 :
174 : /**
175 : * Retrieve two parameters and provide pair of parameters for the object
176 : * @param param1 The name of first parameter
177 : * @param param2 The name of second parameter
178 : * @return Vector of pairs of first and second parameters
179 : */
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 :
184 : /**
185 : * Verifies that the requested parameter exists and is not NULL and returns it to the caller.
186 : * The template parameter must be a pointer or an error will be thrown.
187 : */
188 : template <typename T>
189 : T getCheckedPointerParam(const std::string & name, const std::string & error_string = "") const;
190 :
191 : /**
192 : * Test if the supplied parameter is valid
193 : * @param name The name of the parameter to test
194 : */
195 18921425 : inline bool isParamValid(const std::string & name) const { return _pars.isParamValid(name); }
196 :
197 : /**
198 : * Test if the supplied parameter is set by a user, as opposed to not set or set to default
199 : * @param name The name of the parameter to test
200 : */
201 3360543 : inline bool isParamSetByUser(const std::string & name) const
202 : {
203 3360543 : return _pars.isParamSetByUser(name);
204 : }
205 :
206 : /**
207 : * Connect controllable parameter of this action with the controllable parameters of the
208 : * objects added by this action.
209 : * @param parameter Name of the controllable parameter of this action
210 : * @param object_type Type of the object added by this action.
211 : * @param object_name Name of the object added by this action.
212 : * @param object_parameter Name of the parameter of the object.
213 : */
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 :
219 : /**
220 : * Emits an error prefixed with the file and line number of the given param (from the input
221 : * file) along with the full parameter path+name followed by the given args as the message.
222 : * If this object's parameters were not created directly by the Parser, then this function falls
223 : * back to the normal behavior of mooseError - only printing a message using the given args.
224 : */
225 : template <typename... Args>
226 : [[noreturn]] void paramError(const std::string & param, Args... args) const;
227 :
228 : /**
229 : * Emits a warning prefixed with the file and line number of the given param (from the input
230 : * file) along with the full parameter path+name followed by the given args as the message.
231 : * If this object's parameters were not created directly by the Parser, then this function falls
232 : * back to the normal behavior of mooseWarning - only printing a message using the given args.
233 : */
234 : template <typename... Args>
235 : void paramWarning(const std::string & param, Args... args) const;
236 :
237 : /**
238 : * Emits an informational message prefixed with the file and line number of the given param
239 : * (from the input file) along with the full parameter path+name followed by the given args as
240 : * the message. If this object's parameters were not created directly by the Parser, then this
241 : * function falls back to the normal behavior of mooseInfo - only printing a message using
242 : * the given args.
243 : */
244 : template <typename... Args>
245 : void paramInfo(const std::string & param, Args... args) const;
246 :
247 : /**
248 : * @returns A prefix to be used in messages that contain the input
249 : * file location associated with this object (if any) and the
250 : * name and type of the object.
251 : */
252 15389 : std::string messagePrefix(const bool hit_prefix = true) const
253 : {
254 15389 : return messagePrefix(_pars, hit_prefix);
255 : }
256 :
257 : /**
258 : * Deprecated message prefix; the error type is no longer used
259 : */
260 : std::string errorPrefix(const std::string &) const { return messagePrefix(); }
261 :
262 : /**
263 : * Emits an error prefixed with object name and type and optionally a file path
264 : * to the top-level block parameter if available.
265 : */
266 : template <typename... Args>
267 1666 : [[noreturn]] void mooseError(Args &&... args) const
268 : {
269 1666 : callMooseError(argumentsToString(std::forward<Args>(args)...), /* with_prefix = */ true);
270 : }
271 :
272 : template <typename... Args>
273 5 : [[noreturn]] void mooseDocumentedError(const std::string & repo_name,
274 : const unsigned int issue_num,
275 : Args &&... args) const
276 : {
277 7 : callMooseError(moose::internal::formatMooseDocumentedError(
278 : repo_name, issue_num, argumentsToString(std::forward<Args>(args)...)),
279 : /* with_prefix = */ true);
280 : }
281 :
282 : /**
283 : * Emits an error without the prefixing included in mooseError().
284 : */
285 : template <typename... Args>
286 : [[noreturn]] void mooseErrorNonPrefixed(Args &&... args) const
287 : {
288 : callMooseError(argumentsToString(std::forward<Args>(args)...), /* with_prefix = */ false);
289 : }
290 :
291 : /**
292 : * Emits a warning prefixed with object name and type.
293 : */
294 : template <typename... Args>
295 2665 : void mooseWarning(Args &&... args) const
296 : {
297 2665 : moose::internal::mooseWarningStream(_console, messagePrefix(true), std::forward<Args>(args)...);
298 2492 : }
299 :
300 : /**
301 : * Emits a warning without the prefixing included in mooseWarning().
302 : */
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 1451 : void mooseDeprecated(Args &&... args) const
311 : {
312 2902 : moose::internal::mooseDeprecatedStream(
313 1451 : _console, false, true, messagePrefix(true), std::forward<Args>(args)...);
314 1450 : }
315 :
316 : template <typename... Args>
317 11273 : void mooseInfo(Args &&... args) const
318 : {
319 11273 : moose::internal::mooseInfoStream(_console, messagePrefix(true), std::forward<Args>(args)...);
320 11273 : }
321 :
322 : /**
323 : * External method for calling moose error with added object context.
324 : * @param msg The message
325 : * @param with_prefix If true, add the prefix from messagePrefix(), which is the object
326 : * information (type, name, etc)
327 : * @param node Optional hit node to add file path context as a prefix
328 : */
329 : [[noreturn]] void
330 : callMooseError(std::string msg, const bool with_prefix, const hit::Node * node = nullptr) const;
331 :
332 : /**
333 : * External method for calling moose error with added object context.
334 : *
335 : * Needed so that objects without the MooseBase context (InputParameters)
336 : * can call errors with context
337 : *
338 : * @param app The app pointer (if available); adds multiapp context and clears the console
339 : * @param params The parameters, needed to obtain object information
340 : * @param msg The message
341 : * @param with_prefix If true, add the prefix from messagePrefix(), which is the object
342 : * information (type, name, etc)
343 : * @param node Optional hit node to add file path context as a prefix
344 : */
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:
352 : /// The MOOSE application this is associated with
353 : MooseApp & _app;
354 :
355 : /// The type of this class
356 : const std::string & _type;
357 :
358 : /// The name of this class
359 : const std::string & _name;
360 :
361 : /// The object's parameters
362 : const InputParameters & _pars;
363 :
364 : private:
365 : /**
366 : * Internal method for getting the message prefix for an object (object type, name, etc).
367 : *
368 : * Needs to be static so that we can call it externally from InputParameters for
369 : * errors that do not have context of the MooseBase
370 : */
371 : static std::string messagePrefix(const InputParameters & params, const bool hit_prefix);
372 :
373 : /**
374 : * Internal method for getting a hit node (if available) given a set of parameters
375 : *
376 : * Needs to be static so that we can call it externally from InputParameters for
377 : * errors that do not have context of the MooseBase
378 : */
379 : static const hit::Node * getHitNode(const InputParameters & params);
380 : };
381 :
382 : template <typename T>
383 : const T &
384 46431730 : MooseBase::getParam(const std::string & name) const
385 : {
386 46431730 : return InputParameters::getParamHelper<T>(name, _pars);
387 : }
388 :
389 : template <typename T>
390 : const T *
391 2344 : MooseBase::queryParam(const std::string & name) const
392 : {
393 2344 : return isParamValid(name) ? &getParam<T>(name) : nullptr;
394 : }
395 :
396 : template <typename T>
397 : const T &
398 50721 : MooseBase::getRenamedParam(const std::string & old_name, const std::string & new_name) const
399 : {
400 : // Most important: accept new parameter
401 50721 : if (isParamSetByUser(new_name) && !isParamValid(old_name))
402 49148 : return getParam<T>(new_name);
403 : // Second most: accept old parameter
404 1573 : if (isParamValid(old_name) && !isParamSetByUser(new_name))
405 1055 : return getParam<T>(old_name);
406 : // Third most: accept default for new parameter
407 518 : if (isParamValid(new_name) && !isParamValid(old_name))
408 518 : return getParam<T>(new_name);
409 : // Refuse: no default, no value passed
410 0 : if (!isParamValid(old_name) && !isParamValid(new_name))
411 0 : 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 0 : 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 68484 : MooseBase::getParam(const std::string & param1, const std::string & param2) const
422 : {
423 68484 : return _pars.get<T1, T2>(param1, param2);
424 : }
425 :
426 : template <typename T>
427 : T
428 5961159 : MooseBase::getCheckedPointerParam(const std::string & name, const std::string & error_string) const
429 : {
430 5961159 : return _pars.getCheckedPointerParam<T>(name, error_string);
431 : }
432 :
433 : template <typename... Args>
434 : [[noreturn]] void
435 1525 : MooseBase::paramError(const std::string & param, Args... args) const
436 : {
437 1525 : _pars.paramError(param, std::forward<Args>(args)...);
438 : }
439 :
440 : template <typename... Args>
441 : void
442 80 : MooseBase::paramWarning(const std::string & param, Args... args) const
443 : {
444 80 : mooseWarning(_pars.paramMessage(param, std::forward<Args>(args)...));
445 64 : }
446 :
447 : template <typename... Args>
448 : void
449 133 : MooseBase::paramInfo(const std::string & param, Args... args) const
450 : {
451 133 : mooseInfo(_pars.paramMessage(param, std::forward<Args>(args)...));
452 133 : }
|