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-------------------------------------------------------------------------------
11License
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
33template<class T>
34inline 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
103inline const Foam::ParRunControl&
105{
106 return runControl_;
107}
108
109
111{
112 return runControl_.distributed();
113}
114
115
117{
118 return runControl_.dryRun();
119}
120
121
122inline int Foam::argList::dryRun(const int level) noexcept
123{
124 return runControl_.dryRun(level);
125}
126
127
129{
130 return runControl_.verbose();
131}
132
133
134inline int Foam::argList::verbose(const int level) noexcept
135{
136 return runControl_.verbose(level);
137}
138
139
141{
142 return libs_;
143}
144
145
146inline Foam::label Foam::argList::size() const noexcept
147{
148 return args_.size();
149}
150
151
153{
154 return args_;
155}
156
157
159{
160 return args_;
161}
162
163
166{
167 return options_;
168}
169
170
173{
174 return options_;
175}
176
177
178inline bool Foam::argList::found(const word& optName) const
179{
180 return options_.found(optName);
181}
182
183
184inline Foam::ITstream Foam::argList::lookup(const word& optName) const
185{
186 return ITstream(options_[optName]);
187}
188
189
190// * * * * * * * * * * * * Template Specializations * * * * * * * * * * * * //
191
192namespace 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
277template<class T>
278inline 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
291template<class T>
292inline 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
305template<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
321template<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
338template<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
356template<class T>
357inline 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
370template<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
392template<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
414template<class T, class Predicate>
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
443template<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
455template<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
468template<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
490inline const Foam::string& Foam::argList::operator[](const label index) const
491{
492 return args_[index];
493}
494
495
496inline const Foam::string& Foam::argList::operator[](const word& optName) const
497{
498 return options_[optName];
499}
500
501
502// ************************************************************************* //
bool found
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
An input stream of tokens.
Definition: ITstream.H:56
unsigned int get() const
Get value as unsigned, no range-checking.
Definition: PackedListI.H:302
Helper class for initializing parallel jobs from the command arguments, storing 'dry-run' state etc....
Definition: parRun.H:55
int dryRun() const noexcept
Return the dry-run flag.
Definition: argListI.H:116
const stringList & args() const noexcept
Return arguments.
Definition: argListI.H:152
bool distributed() const noexcept
Definition: argListI.H:110
const string & operator[](const label index) const
The string corresponding to the argument index.
Definition: argListI.H:490
const HashTable< string > & options() const noexcept
Return options.
Definition: argListI.H:165
int verbose() const noexcept
Return the verbose flag.
Definition: argListI.H:128
const ParRunControl & runControl() const noexcept
Return the run control (parallel, dry-run etc)
Definition: argListI.H:104
const fileName & rootPath() const noexcept
Return root path.
Definition: argListI.H:63
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
fileName relativePath(const fileName &input, const bool caseTag=false) const
Definition: argListI.H:94
bool readListIfPresent(const word &optName, List< T > &list) const
Definition: argListI.H:394
label size() const noexcept
The number of arguments.
Definition: argListI.H:146
bool readIfPresent(const word &optName, T &val) const
Read a value from the named option if present.
Definition: argListI.H:323
const fileName & globalCaseName() const noexcept
Return global case name.
Definition: argListI.H:75
fileName path() const
Return the full path to the (processor local) case.
Definition: argListI.H:81
T getCheck(const word &optName, const Predicate &pred) const
Get a value from the named option with additional checking.
Definition: argListI.H:457
List< T > getList(const label index) const
Get a List of values from the argument at index.
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
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
const word & executable() const noexcept
Name of executable without the path.
Definition: argListI.H:51
const string & commandLine() const noexcept
The command line options and arguments concatenated as a string.
Definition: argListI.H:57
T getCheckOrDefault(const word &optName, const T &deflt, const Predicate &pred) const
Definition: argListI.H:470
dlLibraryTable & libs() const noexcept
Mutable access to the loaded dynamic libraries.
Definition: argListI.H:140
const fileName & caseName() const noexcept
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:69
fileName globalPath() const
Return the full path to the global case.
Definition: argListI.H:87
A table of dynamically loaded libraries.
A class for handling file names.
Definition: fileName.H:76
virtual void validate()
Validate the turbulence fields after construction.
Definition: kkLOmega.C:597
Lookup type of boundary radiation properties.
Definition: lookup.H:66
A class for handling character strings derived from std::string.
Definition: string.H:79
A class for handling words, derived from Foam::string.
Definition: word.H:68
const volScalarField & T
Namespace for OpenFOAM.
fileName argList::get< Foam::fileName >(const label index) const
Definition: argListI.H:249
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
string argList::get< Foam::string >(const label index) const
Definition: argListI.H:237
int32_t readInt32(Istream &is)
Read int32_t from stream.
Definition: int32IO.C:81
const direction noexcept
Definition: Scalar.H:223
error FatalError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
int64_t readInt64(Istream &is)
Read int64_t from stream.
Definition: int64IO.C:81
word argList::get< Foam::word >(const label index) const
Definition: argListI.H:243
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53