argListI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2013 OpenFOAM Foundation
9  Copyright (C) 2017-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "argList.H"
30 
31 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
32 
33 template<class T>
34 inline void Foam::argList::readList(ITstream& is, List<T>& list)
35 {
36  if (is.size() == 1)
37  {
38  // Single token - treat like List with one entry
39  list.resize(1);
40  is >> list.front();
41  }
42  else
43  {
44  is >> list;
45  }
46 }
47 
48 
49 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
50 
51 inline const Foam::word& Foam::argList::executable() const noexcept
52 {
53  return executable_;
54 }
55 
56 
58 {
59  return commandLine_;
60 }
61 
62 
64 {
65  return rootPath_;
66 }
67 
68 
70 {
71  return case_;
72 }
73 
74 
76 {
77  return globalCase_;
78 }
79 
80 
82 {
83  return rootPath()/caseName();
84 }
85 
86 
88 {
89  return rootPath()/globalCaseName();
90 }
91 
92 
94 (
95  const fileName& input,
96  const bool caseTag
97 ) const
98 {
99  return input.relative(globalPath(), caseTag);
100 }
101 
102 
103 inline const Foam::ParRunControl&
105 {
106  return runControl_;
107 }
108 
110 inline bool Foam::argList::distributed() const noexcept
111 {
112  return runControl_.distributed();
113 }
114 
116 inline int Foam::argList::dryRun() const noexcept
117 {
118  return runControl_.dryRun();
119 }
120 
122 inline int Foam::argList::dryRun(const int level) noexcept
123 {
124  return runControl_.dryRun(level);
125 }
126 
128 inline int Foam::argList::verbose() const noexcept
129 {
130  return runControl_.verbose();
131 }
132 
134 inline int Foam::argList::verbose(const int level) noexcept
135 {
136  return runControl_.verbose(level);
137 }
138 
141 {
142  return libs_;
143 }
144 
146 inline Foam::label Foam::argList::size() const noexcept
147 {
148  return args_.size();
149 }
150 
152 inline const Foam::stringList& Foam::argList::args() const noexcept
153 {
154  return args_;
155 }
156 
157 
159 {
160  return args_;
161 }
162 
163 
164 inline const Foam::HashTable<Foam::string>&
166 {
167  return options_;
168 }
169 
170 
173 {
174  return options_;
175 }
176 
178 inline bool Foam::argList::found(const word& optName) const
179 {
180  return options_.contains(optName);
181 }
182 
183 
184 inline Foam::ITstream Foam::argList::lookup(const word& optName) const
185 {
186  return ITstream(options_[optName]);
187 }
188 
189 
190 // * * * * * * * * * * * * Template Specializations * * * * * * * * * * * * //
191 
192 namespace Foam
193 {
194  template<> inline int32_t argList::get<int32_t>(const label index) const
195  {
196  return Foam::readInt32(args_[index]);
197  }
198 
199  template<> inline int64_t argList::get<int64_t>(const label index) const
200  {
201  return Foam::readInt64(args_[index]);
202  }
203 
204  template<> inline float argList::get<float>(const label index) const
205  {
206  return Foam::readFloat(args_[index]);
207  }
209  template<> inline double argList::get<double>(const label index) const
210  {
211  return Foam::readDouble(args_[index]);
212  }
214 
215  template<> inline int32_t argList::get<int32_t>(const word& optName) const
216  {
217  return Foam::readInt32(options_[optName]);
218  }
219 
220  template<> inline int64_t argList::get<int64_t>(const word& optName) const
221  {
222  return Foam::readInt64(options_[optName]);
223  }
224 
225  template<> inline float argList::get<float>(const word& optName) const
226  {
227  return Foam::readFloat(options_[optName]);
228  }
229 
230  template<> inline double argList::get<double>(const word& optName) const
231  {
232  return Foam::readDouble(options_[optName]);
233  }
234 
235 
236  template<>
237  inline string argList::get<Foam::string>(const label index) const
238  {
239  return args_[index];
240  }
241 
242  template<>
243  inline word argList::get<Foam::word>(const label index) const
244  {
245  return args_[index];
246  }
247 
248  template<>
249  inline fileName argList::get<Foam::fileName>(const label index) const
250  {
251  return fileName::validate(args_[index]);
252  }
253 
254 
255  template<>
256  inline string argList::get<Foam::string>(const word& optName) const
257  {
258  return options_[optName];
259  }
260 
261  template<>
262  inline word argList::get<Foam::word>(const word& optName) const
263  {
264  return options_[optName];
265  }
266 
267  template<>
268  inline fileName argList::get<Foam::fileName>(const word& optName) const
269  {
270  return fileName::validate(options_[optName]);
271  }
272 }
273 
274 
275 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
276 
277 template<class T>
278 inline T Foam::argList::get(const label index) const
279 {
280  ITstream is(args_[index]);
281 
282  T val;
283  is >> val;
284 
285  checkITstream(is, index);
286 
287  return val;
288 }
289 
290 
291 template<class T>
292 inline T Foam::argList::get(const word& optName) const
293 {
294  ITstream is(options_[optName]);
295 
296  T val;
297  is >> val;
298 
299  checkITstream(is, optName);
301  return val;
302 }
303 
304 
305 template<class T>
307 (
308  const word& optName,
309  const T& deflt
310 ) const
311 {
312  if (options_.contains(optName))
313  {
314  return get<T>(optName);
315  }
317  return deflt;
318 }
319 
320 
321 template<class T>
323 (
324  const word& optName,
325  T& val
326 ) const
327 {
328  if (options_.contains(optName))
329  {
330  val = get<T>(optName);
331  return true;
332  }
334  return false;
335 }
336 
337 
338 template<class T>
340 (
341  const word& optName,
342  T& val,
343  const T& deflt
344 ) const
345 {
346  if (readIfPresent<T>(optName, val))
347  {
348  return true;
349  }
351  val = deflt;
352  return false;
353 }
354 
355 
356 template<class T>
357 inline Foam::List<T> Foam::argList::getList(const label index) const
358 {
359  ITstream is(args_[index]);
360 
361  List<T> list;
362  readList(is, list);
363 
364  checkITstream(is, index);
366  return list;
367 }
368 
369 
370 template<class T>
372 (
373  const word& optName,
374  bool mandatory
375 ) const
376 {
377  List<T> list;
378 
379  if (mandatory || options_.contains(optName))
380  {
381  ITstream is(options_[optName]);
382 
383  readList(is, list);
384 
385  checkITstream(is, optName);
386  }
388  return list;
389 }
390 
391 
392 template<class T>
394 (
395  const word& optName,
396  List<T>& list
397 ) const
398 {
399  if (options_.contains(optName))
400  {
401  ITstream is(options_[optName]);
402 
403  readList(is, list);
404 
405  checkITstream(is, optName);
406 
407  return true;
408  }
410  return false;
411 }
412 
413 
414 template<class T, class Predicate>
415 inline bool Foam::argList::readCheck
416 (
417  const word& optName,
418  T& val,
419  const Predicate& pred,
420  bool mandatory
421 ) const
422 {
423  if (readIfPresent<T>(optName, val))
424  {
425  if (!pred(val))
426  {
427  raiseBadInput(optName);
428  }
429 
430  return true;
431  }
432  else if (mandatory)
433  {
434  FatalError(executable())
435  << "Option -" << optName << " not specified" << nl
436  << exit(FatalError);
437  }
439  return false;
440 }
441 
442 
443 template<class T, class Predicate>
445 (
446  const word& optName,
447  T& val,
448  const Predicate& pred
449 ) const
450 {
451  return readCheck<T>(optName, val, pred, false);
452 }
453 
454 
455 template<class T, class Predicate>
457 (
458  const word& optName,
459  const Predicate& pred
460 ) const
461 {
462  T val;
463  readCheck<T>(optName, val, pred, true);
464  return val;
465 }
466 
467 
468 template<class T, class Predicate>
470 (
471  const word& optName,
472  const T& deflt,
473  const Predicate& pred
474 ) const
475 {
476  // Could predicate check default as well (for FULLDEBUG)
477 
478  T val;
479  if (readCheck<T>(optName, val, pred, false))
480  {
481  return val;
482  }
484  return deflt;
485 }
486 
487 
488 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
490 inline const Foam::string& Foam::argList::operator[](const label index) const
491 {
492  return args_[index];
493 }
494 
495 
496 inline const Foam::string& Foam::argList::operator[](const word& optName) const
497 {
498  return options_[optName];
499 }
500 
501 
502 // ************************************************************************* //
int32_t readInt32(Istream &is)
Read int32_t from stream.
Definition: int32IO.C:74
A class for handling file names.
Definition: fileName.H:72
Helper class for initializing parallel jobs from the command arguments, storing &#39;dry-run&#39; state etc...
Definition: parRun.H:48
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
const string & operator[](const label index) const
The string corresponding to the argument index.
Definition: argListI.H:483
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
List< T > getList(const label index) const
Get a List of values from the argument at index.
const fileName & globalCaseName() const noexcept
Return global case name.
Definition: argListI.H:68
T getOrDefault(const word &optName, const T &deflt) const
Get a value from the named option if present, or return default.
Definition: argListI.H:300
const stringList & args() const noexcept
Return arguments.
Definition: argListI.H:145
int64_t readInt64(Istream &is)
Read int64_t from stream.
Definition: int64IO.C:74
A class for handling words, derived from Foam::string.
Definition: word.H:63
const word & executable() const noexcept
Name of executable without the path.
Definition: argListI.H:44
int dryRun() const noexcept
Return the dry-run flag.
Definition: argListI.H:109
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
bool readCheckIfPresent(const word &optName, T &val, const Predicate &pred) const
Read the named option if present and check its validity.
Definition: argListI.H:438
bool distributed() const noexcept
Return distributed flag (i.e. are rootPaths different on different machines)
Definition: argListI.H:103
label size() const noexcept
The number of arguments.
Definition: argListI.H:139
const ParRunControl & runControl() const noexcept
Return the run control (parallel, dry-run etc)
Definition: argListI.H:97
const fileName & caseName() const noexcept
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:62
fileName relativePath(const fileName &input, const bool caseTag=false) const
Return the input relative to the globalPath by stripping off a leading value of the globalPath...
Definition: argListI.H:87
A table of dynamically loaded libraries.
ITstream lookup(const word &optName) const
Return an input stream from the named option.
Definition: argListI.H:177
const direction noexcept
Definition: Scalar.H:258
const fileName & rootPath() const noexcept
Return root path.
Definition: argListI.H:56
bool readListIfPresent(const word &optName, List< T > &list) const
If named option is present, get a List of values treating a single entry like a list of size 1...
Definition: argListI.H:387
T getCheck(const word &optName, const Predicate &pred) const
Get a value from the named option with additional checking.
Definition: argListI.H:450
dlLibraryTable & libs() const noexcept
Mutable access to the loaded dynamic libraries.
Definition: argListI.H:133
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
T getCheckOrDefault(const word &optName, const T &deflt, const Predicate &pred) const
Get a value from the named option with additional checking (if present), or return default...
Definition: argListI.H:463
fileName path() const
Return the full path to the (processor local) case.
Definition: argListI.H:74
bool readCheck(const word &optName, T &val, const Predicate &pred, bool mandatory=true) const
Read the named option and check its validity.
Definition: argListI.H:409
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:271
static fileName validate(const std::string &, const bool doClean=true)
Construct fileName without invalid characters, possibly applying other transformations such as changi...
Definition: fileName.C:199
const HashTable< string > & options() const noexcept
Return options.
Definition: argListI.H:158
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:316
A class for handling character strings derived from std::string.
Definition: string.H:72
int verbose() const noexcept
Return the verbose flag.
Definition: argListI.H:121
const string & commandLine() const noexcept
The command line options and arguments concatenated as a string.
Definition: argListI.H:50
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:171
An input stream of tokens.
Definition: ITstream.H:52
Namespace for OpenFOAM.
fileName globalPath() const
Return the full path to the global case.
Definition: argListI.H:80