argListI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "argList.H"
27 
28 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
29 
31 {
32  return argListStr_;
33 }
34 
35 
37 {
38  return executable_;
39 }
40 
41 
43 {
44  return rootPath_;
45 }
46 
47 
49 {
50  return case_;
51 }
52 
53 
55 {
56  return globalCase_;
57 }
58 
59 
61 {
62  return parRunControl_;
63 }
64 
65 
67 {
68  return rootPath()/caseName();
69 }
70 
71 
73 {
74  return args_;
75 }
76 
77 
79 {
80  return args_;
81 }
82 
83 
84 inline const Foam::string& Foam::argList::arg(const label index) const
85 {
86  return args_[index];
87 }
88 
89 
91 {
92  return args_.size();
93 }
94 
95 
97 {
98  return options_;
99 }
100 
101 
103 {
104  return options_;
105 }
106 
107 
108 inline const Foam::string& Foam::argList::option(const word& opt) const
109 {
110  return options_[opt];
111 }
112 
113 
114 inline bool Foam::argList::optionFound(const word& opt) const
115 {
116  return options_.found(opt);
117 }
118 
119 
121 {
122  return IStringStream(options_[opt]);
123 }
124 
125 
126 // * * * * * * * * * * * * Template Specialisations * * * * * * * * * * * * //
127 
128 namespace Foam
129 {
130  // Template specialisation for string
131  template<>
133  Foam::argList::argRead<Foam::string>(const label index) const
134  {
135  return args_[index];
136  }
137 
138  // Template specialisation for word
139  template<>
140  inline Foam::word
141  Foam::argList::argRead<Foam::word>(const label index) const
142  {
143  return args_[index];
144  }
145 
146  // Template specialisation for fileName
147  template<>
149  Foam::argList::argRead<Foam::fileName>(const label index) const
150  {
151  return args_[index];
152  }
153 
154  // Template specialisation for string
155  template<>
157  Foam::argList::optionRead<Foam::string>(const word& opt) const
158  {
159  return options_[opt];
160  }
161 
162  // Template specialisation for word
163  template<>
164  inline Foam::word
165  Foam::argList::optionRead<Foam::word>(const word& opt) const
166  {
167  return options_[opt];
168  }
169 
170  // Template specialisation for fileName
171  template<>
173  Foam::argList::optionRead<Foam::fileName>(const word& opt) const
174  {
175  return options_[opt];
176  }
177 
178  inline Foam::label
180  {
181  label level = 0;
182  label maxLevel = 0;
183 
184  for
185  (
186  string::const_iterator iter = str.begin();
187  iter != str.end();
188  ++iter
189  )
190  {
191  const char c = *iter;
192 
193  if (c == '(')
194  {
195  level++;
196  }
197  else if (c == ')')
198  {
199  level--;
200  }
201 
202  maxLevel = max(maxLevel, level);
203  }
204 
205  return maxLevel;
206  }
207 
208  template<class T>
209  inline Foam::List<T>
211  {
212  string argstr(optionLookup(opt).str());
213 
214  OStringStream os;
215  const T x;
216  os << x;
217 
218  label level = bracketLevel(os.str()) + 1;
219 
220  while (bracketLevel(argstr) < level)
221  {
222  argstr.insert(0, "(");
223  argstr.insert(argstr.size(), ")");
224  }
225 
226  return readList<T>(IStringStream(argstr)());
227  }
228 }
229 
230 
231 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
232 
233 template<class T>
234 inline T Foam::argList::argRead(const label index) const
235 {
236  T val;
237 
238  IStringStream(args_[index])() >> val;
239  return val;
240 }
241 
242 
243 template<class T>
244 inline T Foam::argList::optionRead(const word& opt) const
245 {
246  T val;
247 
248  optionLookup(opt)() >> val;
249  return val;
250 }
251 
252 
253 template<class T>
255 (
256  const word& opt,
257  T& val
258 ) const
259 {
260  if (optionFound(opt))
261  {
262  val = optionRead<T>(opt);
263  return true;
264  }
265  else
266  {
267  return false;
268  }
269 }
270 
271 
272 template<class T>
274 (
275  const word& opt,
276  T& val,
277  const T& deflt
278 ) const
279 {
280  if (optionReadIfPresent<T>(opt, val))
281  {
282  return true;
283  }
284  else
285  {
286  val = deflt;
287  return false;
288  }
289 }
290 
291 
292 template<class T>
294 (
295  const word& opt,
296  const T& deflt
297 ) const
298 {
299  if (optionFound(opt))
300  {
301  return optionRead<T>(opt);
302  }
303  else
304  {
305  return deflt;
306  }
307 }
308 
309 
310 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
311 
312 inline const Foam::string& Foam::argList::operator[](const label index) const
313 {
314  return args_[index];
315 }
316 
317 
318 inline const Foam::string& Foam::argList::operator[](const word& opt) const
319 {
320  return options_[opt];
321 }
322 
323 
324 // ************************************************************************* //
Input from memory buffer stream.
Definition: IStringStream.H:52
Output to memory buffer stream.
Definition: OStringStream.H:52
string str() const
Return the string.
Helper class for initialising parallel jobs from the command arguments.
Definition: parRun.H:47
const string & commandLine() const
Return the command line string.
Definition: argListI.H:30
const fileName & caseName() const
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:48
const string & arg(const label index) const
Return the argument corresponding to index.
Definition: argListI.H:84
const fileName & globalCaseName() const
Return case name.
Definition: argListI.H:54
const string & operator[](const label index) const
Return the argument corresponding to index.
Definition: argListI.H:312
T optionRead(const word &opt) const
Read a value from the named option.
Definition: argListI.H:244
label bracketLevel(const string &str) const
Count brackets, to support the optionReadList function.
Definition: argListI.H:179
label size() const
Return the number of arguments.
Definition: argListI.H:90
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
const ParRunControl & parRunControl() const
Return parRunControl.
Definition: argListI.H:60
const string & option(const word &opt) const
Return the argument string associated with the named option.
Definition: argListI.H:108
const word & executable() const
Name of executable without the path.
Definition: argListI.H:36
const stringList & args() const
Return arguments.
Definition: argListI.H:72
List< T > optionReadList(const word &opt) const
Read a List of values from the named option.
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:255
const Foam::HashTable< string > & options() const
Return options.
Definition: argListI.H:96
fileName path() const
Return the path to the caseName.
Definition: argListI.H:66
const fileName & rootPath() const
Return root path.
Definition: argListI.H:42
T argRead(const label index) const
Read a value from the argument at index.
Definition: argListI.H:234
IStringStream optionLookup(const word &opt) const
Return an IStringStream from the named option.
Definition: argListI.H:120
T optionLookupOrDefault(const word &opt, const T &deflt) const
Read a value from the named option if present.
Definition: argListI.H:294
A class for handling file names.
Definition: fileName.H:82
A class for handling character strings derived from std::string.
Definition: string.H:79
A class for handling words, derived from string.
Definition: word.H:62
const dimensionedScalar c
Speed of light in a vacuum.
Namespace for OpenFOAM.
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:59
void T(LagrangianPatchField< Type > &f, const LagrangianPatchField< Type > &f1)
layerAndWeight max(const layerAndWeight &a, const layerAndWeight &b)