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