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-2021 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.first();
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 
57 inline const Foam::string& Foam::argList::commandLine() const noexcept
58 {
59  return commandLine_;
60 }
61 
62 
63 inline const Foam::fileName& Foam::argList::rootPath() const noexcept
64 {
65  return rootPath_;
66 }
67 
68 
69 inline const Foam::fileName& Foam::argList::caseName() const noexcept
70 {
71  return case_;
72 }
73 
74 
75 inline const Foam::fileName& Foam::argList::globalCaseName() const noexcept
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 
109 
110 inline bool Foam::argList::distributed() const noexcept
111 {
112  return runControl_.distributed();
113 }
114 
115 
116 inline int Foam::argList::dryRun() const noexcept
117 {
118  return runControl_.dryRun();
119 }
120 
121 
122 inline int Foam::argList::dryRun(const int level) noexcept
123 {
124  return runControl_.dryRun(level);
125 }
126 
127 
128 inline int Foam::argList::verbose() const noexcept
129 {
130  return runControl_.verbose();
131 }
132 
133 
134 inline int Foam::argList::verbose(const int level) noexcept
135 {
136  return runControl_.verbose(level);
137 }
138 
139 
141 {
142  return libs_;
143 }
144 
145 
146 inline Foam::label Foam::argList::size() const noexcept
147 {
148  return args_.size();
149 }
150 
151 
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>&
165 Foam::argList::options() const noexcept
166 {
167  return options_;
168 }
169 
170 
173 {
174  return options_;
175 }
176 
177 
178 inline bool Foam::argList::found(const word& optName) const
179 {
180  return options_.found(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  }
208 
209  template<> inline double argList::get<double>(const label index) const
210  {
211  return Foam::readDouble(args_[index]);
212  }
213 
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);
300 
301  return val;
302 }
303 
304 
305 template<class T>
307 (
308  const word& optName,
309  const T& deflt
310 ) const
311 {
312  if (found(optName))
313  {
314  return get<T>(optName);
315  }
316 
317  return deflt;
318 }
319 
320 
321 template<class T>
323 (
324  const word& optName,
325  T& val
326 ) const
327 {
328  if (found(optName))
329  {
330  val = get<T>(optName);
331  return true;
332  }
333 
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  }
350 
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);
365 
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 || found(optName))
380  {
381  ITstream is(options_[optName]);
382 
383  readList(is, list);
384 
385  checkITstream(is, optName);
386  }
387 
388  return list;
389 }
390 
391 
392 template<class T>
394 (
395  const word& optName,
396  List<T>& list
397 ) const
398 {
399  if (found(optName))
400  {
401  ITstream is(options_[optName]);
402 
403  readList(is, list);
404 
405  checkITstream(is, optName);
406 
407  return true;
408  }
409 
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  }
438 
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  }
483 
484  return deflt;
485 }
486 
487 
488 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
489 
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 // ************************************************************************* //
Foam::argList::libs
dlLibraryTable & libs() const noexcept
Mutable access to the loaded dynamic libraries.
Definition: argListI.H:140
Foam::dlLibraryTable
A table of dynamically loaded libraries.
Definition: dlLibraryTable.H:63
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::argList::getOrDefault
T getOrDefault(const word &optName, const T &deflt) const
Get a value from the named option if present, or return default.
Definition: argListI.H:307
Foam::argList::caseName
const fileName & caseName() const noexcept
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:69
Foam::argList::globalCaseName
const fileName & globalCaseName() const noexcept
Return global case name.
Definition: argListI.H:75
Foam::argList::readListIfPresent
bool readListIfPresent(const word &optName, List< T > &list) const
Definition: argListI.H:394
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::argList::get
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:278
Foam::argList::readIfPresent
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:323
Foam::argList::distributed
bool distributed() const noexcept
Definition: argListI.H:110
Foam::argList::readCheck
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:416
Foam::argList::args
const stringList & args() const noexcept
Return arguments.
Definition: argListI.H:152
Foam::argList::lookup
ITstream lookup(const word &optName) const
Return an input stream from the named option.
Definition: argListI.H:184
Foam::argList::dryRun
int dryRun() const noexcept
Return the dry-run flag.
Definition: argListI.H:116
Foam::argList::options
const HashTable< string > & options() const noexcept
Return options.
Definition: argListI.H:165
Foam::ITstream
An input stream of tokens.
Definition: ITstream.H:52
argList.H
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::argList::readCheckIfPresent
bool readCheckIfPresent(const word &optName, T &val, const Predicate &pred) const
Read the named option if present and check its validity.
Definition: argListI.H:445
Foam::argList::path
fileName path() const
Return the full path to the (processor local) case.
Definition: argListI.H:81
Foam::FatalError
error FatalError
Foam::argList::runControl
const ParRunControl & runControl() const noexcept
Return the run control (parallel, dry-run etc)
Definition: argListI.H:104
Foam::argList::verbose
int verbose() const noexcept
Return the verbose flag.
Definition: argListI.H:128
Foam::readInt32
int32_t readInt32(Istream &is)
Read int32_t from stream.
Definition: int32IO.C:81
Foam::fileName::validate
static fileName validate(const std::string &, const bool doClean=true)
Definition: fileName.C:206
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::argList::getCheck
T getCheck(const word &optName, const Predicate &pred) const
Get a value from the named option with additional checking.
Definition: argListI.H:457
Foam::argList::commandLine
const string & commandLine() const noexcept
The command line options and arguments concatenated as a string.
Definition: argListI.H:57
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::HashTable< Foam::string >
found
bool found
Definition: TABSMDCalcMethod2.H:32
Foam::argList::size
label size() const noexcept
The number of arguments.
Definition: argListI.H:146
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::argList::executable
const word & executable() const noexcept
Name of executable without the path.
Definition: argListI.H:51
Foam::List< string >
Foam::argList::globalPath
fileName globalPath() const
Return the full path to the global case.
Definition: argListI.H:87
Foam::input
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
Foam::argList::getList
List< T > getList(const label index) const
Get a List of values from the argument at index.
Foam::ParRunControl
Helper class for initializing parallel jobs from the command arguments, storing 'dry-run' state etc....
Definition: parRun.H:54
Foam::argList::operator[]
const string & operator[](const label index) const
The string corresponding to the argument index.
Definition: argListI.H:490
Foam::argList::getCheckOrDefault
T getCheckOrDefault(const word &optName, const T &deflt, const Predicate &pred) const
Definition: argListI.H:470
Foam::argList::relativePath
fileName relativePath(const fileName &input, const bool caseTag=false) const
Definition: argListI.H:94
Foam::argList::rootPath
const fileName & rootPath() const noexcept
Return root path.
Definition: argListI.H:63
Foam::argList::found
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:178
Foam::readInt64
int64_t readInt64(Istream &is)
Read int64_t from stream.
Definition: int64IO.C:81