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>
285 inline T Foam::argList::get(const word& optName, const T& deflt) const
286 {
287  if (found(optName))
288  {
289  return get<T>(optName);
290  }
291 
292  return deflt;
293 }
294 
295 
296 template<class T>
298 (
299  const word& optName,
300  T& val
301 ) const
302 {
303  if (found(optName))
304  {
305  val = get<T>(optName);
306  return true;
307  }
308 
309  return false;
310 }
311 
312 
313 template<class T>
315 (
316  const word& optName,
317  T& val,
318  const T& deflt
319 ) const
320 {
321  if (readIfPresent<T>(optName, val))
322  {
323  return true;
324  }
325 
326  val = deflt;
327  return false;
328 }
329 
330 
331 template<class T>
333 (
334  const word& optName,
335  const T& deflt
336 ) const
337 {
338  if (found(optName))
339  {
340  return get<T>(optName);
341  }
342 
343  return deflt;
344 }
345 
346 
347 template<class T>
348 inline Foam::List<T> Foam::argList::getList(const label index) const
349 {
350  ITstream is(Foam::name(index), args_[index]);
351 
352  List<T> list;
353  readList(is, list);
354 
355  checkITstream(is, index);
356 
357  return list;
358 }
359 
360 
361 template<class T>
363 (
364  const word& optName,
365  bool mandatory
366 ) const
367 {
368  List<T> list;
369 
370  if (mandatory || found(optName))
371  {
372  ITstream is(optName, options_[optName]);
373 
374  readList(is, list);
375 
376  checkITstream(is, optName);
377  }
378 
379  return list;
380 }
381 
382 
383 template<class T>
385 (
386  const word& optName,
387  List<T>& list
388 ) const
389 {
390  if (found(optName))
391  {
392  ITstream is(optName, options_[optName]);
393 
394  readList(is, list);
395 
396  checkITstream(is, optName);
397 
398  return true;
399  }
400 
401  return false;
402 }
403 
404 
405 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
406 
407 inline const Foam::string& Foam::argList::operator[](const label index) const
408 {
409  return args_[index];
410 }
411 
412 
413 inline const Foam::string& Foam::argList::operator[](const word& optName) const
414 {
415  return options_[optName];
416 }
417 
418 
419 // ************************************************************************* //
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::dlLibraryTable
A table of dynamically loaded libraries.
Definition: dlLibraryTable.H:51
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::argList::val
bool T &const val
Definition: argList.H:681
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:333
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:385
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:298
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::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::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
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::path
fileName path() const
Return the full path to the (processor local) case.
Definition: argListI.H:81
Foam::readInt32
int32_t readInt32(Istream &is)
Read int32_t from stream.
Definition: int32IO.C:112
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
T
const volScalarField & T
Definition: createFieldRefs.H:2
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::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.
Definition: parRun.H:52
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:407
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:112