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-2020 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 
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 bool Foam::argList::distributed() const
104 {
105  return parRunControl_.distributed();
106 }
107 
108 
110 {
111  return parRunControl_;
112 }
113 
114 
116 {
117  return libs_;
118 }
119 
120 
121 inline Foam::label Foam::argList::size() const noexcept
122 {
123  return args_.size();
124 }
125 
126 
128 {
129  return args_;
130 }
131 
132 
134 {
135  return args_;
136 }
137 
138 
140 {
141  return options_;
142 }
143 
144 
146 {
147  return options_;
148 }
149 
150 
151 inline bool Foam::argList::found(const word& optName) const
152 {
153  return options_.found(optName);
154 }
155 
156 
157 inline Foam::ITstream Foam::argList::lookup(const word& optName) const
158 {
159  return ITstream(optName, options_[optName]);
160 }
161 
162 
163 // * * * * * * * * * * * * Template Specializations * * * * * * * * * * * * //
164 
165 namespace Foam
166 {
167  template<> inline int32_t argList::get<int32_t>(const label index) const
168  {
169  return Foam::readInt32(args_[index]);
170  }
171 
172  template<> inline int64_t argList::get<int64_t>(const label index) const
173  {
174  return Foam::readInt64(args_[index]);
175  }
176 
177  template<> inline float argList::get<float>(const label index) const
178  {
179  return Foam::readFloat(args_[index]);
180  }
181 
182  template<> inline double argList::get<double>(const label index) const
183  {
184  return Foam::readDouble(args_[index]);
185  }
186 
187 
188  template<> inline int32_t argList::get<int32_t>(const word& optName) const
189  {
190  return Foam::readInt32(options_[optName]);
191  }
192 
193  template<> inline int64_t argList::get<int64_t>(const word& optName) const
194  {
195  return Foam::readInt64(options_[optName]);
196  }
197 
198  template<> inline float argList::get<float>(const word& optName) const
199  {
200  return Foam::readFloat(options_[optName]);
201  }
202 
203  template<> inline double argList::get<double>(const word& optName) const
204  {
205  return Foam::readDouble(options_[optName]);
206  }
207 
208 
209  template<>
210  inline string argList::get<Foam::string>(const label index) const
211  {
212  return args_[index];
213  }
214 
215  template<>
216  inline word argList::get<Foam::word>(const label index) const
217  {
218  return args_[index];
219  }
220 
221  template<>
222  inline fileName argList::get<Foam::fileName>(const label index) const
223  {
224  return args_[index];
225  }
226 
227 
228  template<>
229  inline string argList::get<Foam::string>(const word& optName) const
230  {
231  return options_[optName];
232  }
233 
234  template<>
235  inline word argList::get<Foam::word>(const word& optName) const
236  {
237  return options_[optName];
238  }
239 
240  template<>
241  inline fileName argList::get<Foam::fileName>(const word& optName) const
242  {
243  return options_[optName];
244  }
245 }
246 
247 
248 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
249 
250 template<class T>
251 inline T Foam::argList::get(const label index) const
252 {
253  ITstream is(Foam::name(index), args_[index]);
254 
255  T val;
256  is >> val;
257 
258  checkITstream(is, index);
259 
260  return val;
261 }
262 
263 
264 template<class T>
265 inline T Foam::argList::get(const word& optName) const
266 {
267  ITstream is(optName, options_[optName]);
268 
269  T val;
270  is >> val;
271 
272  checkITstream(is, optName);
273 
274  return val;
275 }
276 
277 
278 template<class T>
280 (
281  const word& optName,
282  const T& deflt
283 ) const
284 {
285  if (found(optName))
286  {
287  return get<T>(optName);
288  }
289 
290  return deflt;
291 }
292 
293 
294 template<class T>
296 (
297  const word& optName,
298  T& val
299 ) const
300 {
301  if (found(optName))
302  {
303  val = get<T>(optName);
304  return true;
305  }
306 
307  return false;
308 }
309 
310 
311 template<class T>
313 (
314  const word& optName,
315  T& val,
316  const T& deflt
317 ) const
318 {
319  if (readIfPresent<T>(optName, val))
320  {
321  return true;
322  }
323 
324  val = deflt;
325  return false;
326 }
327 
328 
329 template<class T>
330 inline Foam::List<T> Foam::argList::getList(const label index) const
331 {
332  ITstream is(Foam::name(index), args_[index]);
333 
334  List<T> list;
335  readList(is, list);
336 
337  checkITstream(is, index);
338 
339  return list;
340 }
341 
342 
343 template<class T>
345 (
346  const word& optName,
347  bool mandatory
348 ) const
349 {
350  List<T> list;
351 
352  if (mandatory || found(optName))
353  {
354  ITstream is(optName, options_[optName]);
355 
356  readList(is, list);
357 
358  checkITstream(is, optName);
359  }
360 
361  return list;
362 }
363 
364 
365 template<class T>
367 (
368  const word& optName,
369  List<T>& list
370 ) const
371 {
372  if (found(optName))
373  {
374  ITstream is(optName, options_[optName]);
375 
376  readList(is, list);
377 
378  checkITstream(is, optName);
379 
380  return true;
381  }
382 
383  return false;
384 }
385 
386 
387 template<class T, class Predicate>
388 inline bool Foam::argList::readCheck
389 (
390  const word& optName,
391  T& val,
392  const Predicate& pred,
393  bool mandatory
394 ) const
395 {
396  if (readIfPresent<T>(optName, val))
397  {
398  if (!pred(val))
399  {
400  raiseBadInput(optName);
401  }
402 
403  return true;
404  }
405  else if (mandatory)
406  {
407  FatalError(executable())
408  << "Option -" << optName << " not specified" << nl
409  << exit(FatalError);
410  }
411 
412  return false;
413 }
414 
415 
416 template<class T, class Predicate>
418 (
419  const word& optName,
420  T& val,
421  const Predicate& pred
422 ) const
423 {
424  return readCheck<T>(optName, val, pred, false);
425 }
426 
427 
428 template<class T, class Predicate>
430 (
431  const word& optName,
432  const Predicate& pred
433 ) const
434 {
435  T val;
436  readCheck<T>(optName, val, pred, true);
437  return val;
438 }
439 
440 
441 template<class T, class Predicate>
443 (
444  const word& optName,
445  const T& deflt,
446  const Predicate& pred
447 ) const
448 {
449  // Could predicate check default as well (for FULLDEBUG)
450 
451  T val;
452  if (readCheck<T>(optName, val, pred, false))
453  {
454  return val;
455  }
456 
457  return deflt;
458 }
459 
460 
461 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
462 
463 inline const Foam::string& Foam::argList::operator[](const label index) const
464 {
465  return args_[index];
466 }
467 
468 
469 inline const Foam::string& Foam::argList::operator[](const word& optName) const
470 {
471  return options_[optName];
472 }
473 
474 
475 // ************************************************************************* //
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:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
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:280
Foam::argList::options
const HashTable< string > & options() const
Return options.
Definition: argListI.H:139
Foam::argList::args
const stringList & args() const
Return arguments.
Definition: argListI.H:127
Foam::argList::readListIfPresent
bool readListIfPresent(const word &optName, List< T > &list) const
Definition: argListI.H:367
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::argList::get
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:251
Foam::argList::readIfPresent
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:296
Foam::argList::rootPath
const fileName & rootPath() const
Return root path.
Definition: argListI.H:63
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:389
Foam::argList::parRunControl
const ParRunControl & parRunControl() const
Return the ParRunControl.
Definition: argListI.H:109
Foam::argList::lookup
ITstream lookup(const word &optName) const
Return an input stream from the named option.
Definition: argListI.H:157
Foam::argList::executable
const word & executable() const
Name of executable without the path.
Definition: argListI.H:51
Foam::ITstream
An input stream of tokens.
Definition: ITstream.H:55
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
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:418
Foam::argList::path
fileName path() const
Return the full path to the (processor local) case.
Definition: argListI.H:81
Foam::FatalError
error FatalError
Foam::readInt32
int32_t readInt32(Istream &is)
Read int32_t from stream.
Definition: int32IO.C:81
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:430
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:121
Foam::nl
constexpr char nl
Definition: Ostream.H:385
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::libs
dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
Definition: argListI.H:115
Foam::argList::commandLine
const string & commandLine() const
The command line options and arguments concatenated as a string.
Definition: argListI.H:57
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. Also handles cleanup of paral...
Definition: parRun.H:50
Foam::argList::caseName
const fileName & caseName() const
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:69
Foam::argList::distributed
bool distributed() const
Definition: argListI.H:103
Foam::argList::operator[]
const string & operator[](const label index) const
The string corresponding to the argument index.
Definition: argListI.H:463
Foam::argList::getCheckOrDefault
T getCheckOrDefault(const word &optName, const T &deflt, const Predicate &pred) const
Definition: argListI.H:443
Foam::argList::globalCaseName
const fileName & globalCaseName() const
Return global case name.
Definition: argListI.H:75
Foam::argList::relativePath
fileName relativePath(const fileName &input, const bool caseTag=false) const
Definition: argListI.H:94
Foam::argList::found
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:151
Foam::readInt64
int64_t readInt64(Istream &is)
Read int64_t from stream.
Definition: int64IO.C:81