https://mooseframework.inl.gov
MooseBaseParameterInterface.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 
14 #include "MooseBase.h"
15 #include "InputParameters.h"
16 #include "MooseUtils.h"
18 
19 #include <string>
20 
21 #define usingMooseBaseParameterInterfaceMembers \
22  using MooseBaseParameterInterface::parameters; \
23  using MooseBaseParameterInterface::uniqueName; \
24  using MooseBaseParameterInterface::isParamValid; \
25  using MooseBaseParameterInterface::isParamSetByUser; \
26  using MooseBaseParameterInterface::paramError; \
27  using MooseBaseParameterInterface::_pars; \
28  using MooseBaseParameterInterface::_factory; \
29  using MooseBaseParameterInterface::_action_factory
30 
37 std::string paramErrorPrefix(const InputParameters & params, const std::string & param);
38 
43 {
44 public:
46 
47  virtual ~MooseBaseParameterInterface() = default;
48 
52  MooseObjectParameterName uniqueParameterName(const std::string & parameter_name) const
53  {
55  _pars.get<std::string>("_moose_base"), _moose_base.name(), parameter_name);
56  }
57 
62  const InputParameters & parameters() const { return _pars; }
63 
68  {
69  return MooseObjectName(_pars.get<std::string>("_unique_name"));
70  }
71 
77  template <typename T>
78  const T & getParam(const std::string & name) const;
79 
88  template <typename T>
89  const T * queryParam(const std::string & name) const;
90 
98  template <typename T>
99  const T & getRenamedParam(const std::string & old_name, const std::string & new_name) const;
100 
107  template <typename T1, typename T2>
108  std::vector<std::pair<T1, T2>> getParam(const std::string & param1,
109  const std::string & param2) const;
110 
115  template <typename T>
116  T getCheckedPointerParam(const std::string & name, const std::string & error_string = "") const;
117 
122  inline bool isParamValid(const std::string & name) const { return _pars.isParamValid(name); }
123 
128  inline bool isParamSetByUser(const std::string & nm) const { return _pars.isParamSetByUser(nm); }
129 
136  template <typename... Args>
137  [[noreturn]] void paramError(const std::string & param, Args... args) const;
138 
145  template <typename... Args>
146  void paramWarning(const std::string & param, Args... args) const;
147 
155  template <typename... Args>
156  void paramInfo(const std::string & param, Args... args) const;
157 
166  void connectControllableParams(const std::string & parameter,
167  const std::string & object_type,
168  const std::string & object_name,
169  const std::string & object_parameter) const;
170 
171 protected:
174 
177 
180 
181 private:
184 
185  template <typename... Args>
186  std::string paramErrorMsg(const std::string & param, Args... args) const
187  {
188  auto param_prefix = paramErrorPrefix(_pars, param);
189 
190  // With no input location information, append object info (name + type)
191  const std::string object_prefix =
192  _pars.inputLocation(param).empty() ? _moose_base.errorPrefix("parameter error") : "";
193 
194  std::ostringstream oss;
195  moose::internal::mooseStreamAll(oss, std::forward<Args>(args)...);
196  std::string msg = oss.str();
197 
198  // Wrap error message to a separate line from param_prefix if it is about to
199  // blow past 100 chars. But only wrap if the param_prefix is long enough (12
200  // chars) for the wrap to buy us much extra length. We don't check object_prefix
201  // here because it will go before the parameter error
202  if ((param_prefix.size() > 12 && msg.size() + param_prefix.size() > 99) ||
203  msg.find("\n") != std::string::npos)
204  {
205  if (param_prefix.size() > 0 && param_prefix[param_prefix.size() - 1] != ':')
206  param_prefix += ":";
207  return object_prefix + param_prefix + "\n " + MooseUtils::replaceAll(msg, "\n", "\n ");
208  }
209  return object_prefix + param_prefix + " " + msg;
210  }
211 };
212 
213 template <typename T>
214 const T &
215 MooseBaseParameterInterface::getParam(const std::string & name) const
216 {
217  return InputParameters::getParamHelper(name, _pars, static_cast<T *>(0), &_moose_base);
218 }
219 
220 template <typename T>
221 const T *
222 MooseBaseParameterInterface::queryParam(const std::string & name) const
223 {
224  return isParamValid(name) ? &getParam<T>(name) : nullptr;
225 }
226 
227 template <typename T>
228 const T &
229 MooseBaseParameterInterface::getRenamedParam(const std::string & old_name,
230  const std::string & new_name) const
231 {
232  // this enables having a default on the new parameter but bypassing it with the old one
233  // Most important: accept new parameter
234  if (isParamSetByUser(new_name) && !isParamValid(old_name))
235  return InputParameters::getParamHelper(new_name, _pars, static_cast<T *>(0), &_moose_base);
236  // Second most: accept old parameter
237  else if (isParamValid(old_name) && !isParamSetByUser(new_name))
238  return InputParameters::getParamHelper(old_name, _pars, static_cast<T *>(0), &_moose_base);
239  // Third most: accept default for new parameter
240  else if (isParamValid(new_name) && !isParamValid(old_name))
241  return InputParameters::getParamHelper(new_name, _pars, static_cast<T *>(0), &_moose_base);
242  // Refuse: no default, no value passed
243  else if (!isParamValid(old_name) && !isParamValid(new_name))
244  mooseError(_pars.blockFullpath() + ": parameter '" + new_name +
245  "' is being retrieved without being set.\n"
246  "Did you misspell it?");
247  // Refuse: both old and new parameters set by user
248  else
249  mooseError(_pars.blockFullpath() + ": parameter '" + new_name +
250  "' may not be provided alongside former parameter '" + old_name + "'");
251 }
252 
253 template <typename... Args>
254 [[noreturn]] void
255 MooseBaseParameterInterface::paramError(const std::string & param, Args... args) const
256 {
257  Moose::show_trace = false;
258  _moose_base.callMooseError(paramErrorMsg(param, std::forward<Args>(args)...),
259  /* with_prefix = */ false);
260  Moose::show_trace = true;
261 }
262 
263 template <typename... Args>
264 void
265 MooseBaseParameterInterface::paramWarning(const std::string & param, Args... args) const
266 {
267  mooseWarning(paramErrorMsg(param, std::forward<Args>(args)...));
268 }
269 
270 template <typename... Args>
271 void
272 MooseBaseParameterInterface::paramInfo(const std::string & param, Args... args) const
273 {
274  mooseInfo(paramErrorMsg(param, std::forward<Args>(args)...));
275 }
276 
277 template <typename T1, typename T2>
278 std::vector<std::pair<T1, T2>>
279 MooseBaseParameterInterface::getParam(const std::string & param1, const std::string & param2) const
280 {
281  return _pars.get<T1, T2>(param1, param2);
282 }
283 
284 template <typename T>
285 T
287  const std::string & error_string) const
288 {
289  return parameters().getCheckedPointerParam<T>(name, error_string);
290 }
std::string name(const ElemQuality q)
void mooseStreamAll(std::ostringstream &ss)
All of the following are not meant to be called directly - they are called by the normal macros (moos...
Definition: MooseError.C:94
bool show_trace
Set to true (the default) to print the stack trace with error and warning messages - false to omit it...
Definition: Moose.C:761
Every object that can be built by the factory should be derived from this class.
const MooseBase & _moose_base
The MooseBase object that inherits this class.
Base class for everything in MOOSE with a name and a type.
Definition: MooseBase.h:32
Generic factory class for build all sorts of objects.
Definition: Factory.h:28
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
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.
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:336
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...
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:57
virtual ~MooseBaseParameterInterface()=default
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Factory & _factory
The Factory associated with the MooseApp.
MooseBaseParameterInterface(const MooseBase &base, const InputParameters &parameters)
MooseObjectParameterName uniqueParameterName(const std::string &parameter_name) const
The unique parameter name of a valid parameter of this object for accessing parameter controls...
void mooseInfo(Args &&... args)
Emit an informational message with the given stringified, concatenated args.
Definition: MooseError.h:369
MooseObjectName uniqueName() const
The unique name for accessing input parameters of this object in the InputParameterWarehouse.
const T * queryParam(const std::string &name) const
Query a parameter for the object.
std::string errorPrefix(const std::string &error_type) const
Definition: MooseBase.C:43
Specialized factory for generic Action System objects.
Definition: ActionFactory.h:50
std::string paramErrorMsg(const std::string &param, Args... args) const
const T & getParam(const std::string &name) const
Retrieve a parameter for the object.
std::string inputLocation(const std::string &param) const
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 ...
ActionFactory & _action_factory
Builds Actions.
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...
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 isParamSetByUser(const std::string &name) const
Method returns true if the parameter was set by the user.
const T & getRenamedParam(const std::string &old_name, const std::string &new_name) const
Retrieve a renamed parameter for the object.
bool isParamSetByUser(const std::string &nm) const
Test if the supplied parameter is set by a user, as opposed to not set or set to default.
void callMooseError(std::string msg, const bool with_prefix) const
Calls moose error with the message msg.
Definition: MooseBase.C:33
std::string blockFullpath() const
const InputParameters & _pars
Parameters of this object, references the InputParameters stored in the InputParametersWarehouse.
const InputParameters & parameters() const
Get the parameters of the object.
A class for storing an input parameter name.
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...
A class for storing the names of MooseObject by tag and object name.
std::string replaceAll(std::string str, const std::string &from, const std::string &to)
Replaces all occurrences of from in str with to and returns the result.
Definition: MooseUtils.C:141
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...
std::string paramErrorPrefix(const InputParameters &params, const std::string &param)
Get canonical paramError prefix for param-related error/warning/info messages.
bool isParamValid(const std::string &name) const
This method returns parameters that have been initialized in one fashion or another, i.e.
static const T & getParamHelper(const std::string &name, const InputParameters &pars, const T *the_type, const MooseBase *moose_base=nullptr)