ifeqEntry.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) 2018-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 Class
25  Foam::functionEntries::ifeqEntry
26 
27 Description
28  Conditional parsing of and within dictionary entries.
29 
30  E.g.
31  \verbatim
32  a #calc "0.123";
33  b 1.23e-1;
34 
35  #ifeq ($a $b)
36  ..
37  #else
38  ..
39  #endif
40 
41  c #ifeq ($a $b) 5.53 #else 7.89 #endif;
42  \endverbatim
43 
44  \verbatim
45  ddtSchemes
46  {
47  #ifeq (${FOAM_APPLICATION} foamRun)
48  default steadyState;
49  #else
50  default Euler;
51  #endif
52  }
53  \endverbatim
54 
55  Note:
56  - supports dictionary variables and environment variables
57  - the two arguments should be tokens
58  - the equality test supports any word/string/variable, integers
59  and floating point numbers
60  - parsing of (non)matching \c \#else, \c \#endif is not very sophisticated
61 
62 See also
63  Foam::functionEntries::ifEntry
64 
65 SourceFiles
66  ifeqEntry.C
67 
68 \*---------------------------------------------------------------------------*/
69 
70 #ifndef ifeqEntry_H
71 #define ifeqEntry_H
72 
73 #include "functionEntry.H"
74 #include "DynamicList.H"
75 
76 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
77 
78 namespace Foam
79 {
80 namespace functionEntries
81 {
82 
83 /*---------------------------------------------------------------------------*\
84  Class ifeqEntry Declaration
85 \*---------------------------------------------------------------------------*/
86 
87 class ifeqEntry
88 :
89  public functionEntry
90 {
91 protected:
92 
94 
95 
96 private:
97 
98  // Private Member Functions
99 
100  tokenList readArgList(Istream& is) const;
101 
102  //- Read tokens. Skip dummy tokens
103  void readToken(token& t, Istream& is) const;
104 
105  //- Expand token if it is a variable
106  token expand(const dictionary& dict, const token& t) const;
107 
108  bool equalToken(const token& t1, const token& t2) const;
109 
110  //- Consume tokens until reached a specific word
111  void skipUntil
112  (
113  DynamicList<filePos>& stack,
114  const dictionary& contextDict,
115  const functionName& endWord,
116  Istream& is
117  ) const;
118 
119  template<class Context>
120  bool evaluate
121  (
122  const bool doIf,
123  DynamicList<filePos>& stack,
124  const dictionary& contextDict,
125  Context& context,
126  Istream& is
127  ) const;
128 
129  //- Main driver: depending on 'equal' starts evaluating or
130  // skips forward to #else
131  template<class Context>
132  bool execute
133  (
134  DynamicList<filePos>& stack,
135  const dictionary& contextDict,
136  Context& context,
137  Istream& is
138  ) const;
139 
140 
141 protected:
142 
143  // Protected Member Functions
144 
145  //- Main driver: depending on 'equal' starts evaluating or
146  // skips forward to #else
147  template<class Context>
148  bool execute
149  (
150  const bool equal,
151  DynamicList<filePos>& stack,
152  const dictionary& contextDict,
153  Context& context,
154  Istream& is
155  ) const;
156 
157 
158 public:
159 
160  //- Runtime type information
161  FunctionTypeName("#ifeq");
162 
163 
164  // Constructors
165 
166  //- Construct from function type, dictionary and tokenList
167  ifeqEntry
168  (
169  const functionName& functionType,
170  const label lineNumber,
171  const dictionary& parentDict,
172  const Istream& is,
173  const tokenList& tokens
174  );
175 
176  //- Construct from line number, dictionary and Istream
177  ifeqEntry
178  (
179  const label lineNumber,
180  const dictionary& parentDict,
181  Istream& is
182  );
183 
184  //- Copy construct
185  ifeqEntry(const ifeqEntry&) = default;
186 
187  //- Clone
188  virtual autoPtr<entry> clone(const dictionary&) const
189  {
190  return autoPtr<entry>(new ifeqEntry(*this));
191  }
192 
193 
194  // Member Functions
195 
196  //- Expand the functionEntry into the contextDict
197  virtual bool execute(dictionary& contextDict, Istream& is);
198 
199  //- Expand the functionEntry into the contextEntry
200  static bool execute
201  (
202  const dictionary& contextDict,
203  primitiveEntry& contextEntry,
204  Istream&
205  );
206 
207 
208  // Member Operators
209 
210  //- Disallow default bitwise assignment
211  void operator=(const ifeqEntry&) = delete;
212 };
213 
214 
215 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
216 
217 } // End namespace functionEntries
218 } // End namespace Foam
219 
220 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
221 
222 #endif
223 
224 // ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:78
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:450
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
A 2-tuple for storing two objects of different types.
Definition: Tuple2.H:66
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
virtual autoPtr< entry > clone() const
Construct on freestore as copy.
Definition: entry.C:56
Conditional parsing of and within dictionary entries.
Definition: ifeqEntry.H:89
void operator=(const ifeqEntry &)=delete
Disallow default bitwise assignment.
FunctionTypeName("#ifeq")
Runtime type information.
ifeqEntry(const functionName &functionType, const label lineNumber, const dictionary &parentDict, const Istream &is, const tokenList &tokens)
Construct from function type, dictionary and tokenList.
Definition: ifeqEntry.C:377
Tuple2< fileName, label > filePos
Definition: ifeqEntry.H:92
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
Definition: functionEntry.H:66
A functionName is a word starting with '#'.
Definition: functionName.H:60
A keyword and a list of tokens is a 'primitiveEntry'. An primitiveEntry can be read,...
const dictionary & dict() const
This entry is not a dictionary,.
A token holds items read from Istream.
Definition: token.H:74
Namespace for OpenFOAM.
bool equal(const T &s1, const T &s2)
Definition: doubleFloat.H:62
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