ifeqEntry.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) 2018 OpenFOAM Foundation
9  Copyright (C) 2021 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 Class
28  Foam::functionEntries::ifeqEntry
29 
30 Description
31  Conditional parsing of dictionary entries.
32 
33  E.g.
34  \verbatim
35  a #eval "0.123";
36  b 1.23e-1;
37 
38  #ifeq $a $b
39  ..
40  #else
41  ..
42  #endif
43  \endverbatim
44 
45  \verbatim
46  ddtSchemes
47  {
48  #ifeq ${FOAM_APPLICATION} simpleFoam
49  default steadyState;
50  #else
51  default Euler;
52  #endif
53  }
54  \endverbatim
55 
56  Note:
57  - supports dictionary variables and environment variables
58  - the two arguments should be two tokens
59  - the comparison is a string comparison for any word/string/variable,
60  integer comparison for two integers and floating point comparison for
61  any floating point number.
62  - parsing of (non)matching \c #else, \c #endif is not very sophisticated
63 
64 See also
65  Foam::functionEntries::ifEntry
66 
67 SourceFiles
68  ifeqEntry.C
69 
70 \*---------------------------------------------------------------------------*/
71 
72 #ifndef functionEntries_ifeqEntry_H
73 #define functionEntries_ifeqEntry_H
74 
75 #include "functionEntry.H"
76 #include "DynamicList.H"
77 #include "Tuple2.H"
78 
79 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
80 
81 namespace Foam
82 {
83 namespace functionEntries
84 {
85 
86 /*---------------------------------------------------------------------------*\
87  Class ifeqEntry Declaration
88 \*---------------------------------------------------------------------------*/
89 
90 class ifeqEntry
91 :
92  public functionEntry
93 {
94 protected:
95 
96  // Data Types
97 
99 
100 
101  // Protected Member Functions
102 
103  static bool evaluate
104  (
105  const bool doIf,
106  DynamicList<filePos>& stack,
107  dictionary& parentDict,
108  Istream& is
109  );
110 
111  //- Main driver: depending on 'equal' starts evaluating or
112  //- skips forward to #else
113  static bool execute
114  (
115  const bool equal,
116  DynamicList<filePos>& stack,
117  dictionary& parentDict,
118  Istream& is
119  );
120 
121  //- Main driver: depending on 'equal' starts evaluating or
122  //- skips forward to #else
123  static bool execute
124  (
125  DynamicList<filePos>& stack,
126  dictionary& parentDict,
127  Istream& is
128  );
129 
130 
131 private:
132 
133  // Private Member Functions
134 
135  //- Read tokens. Skip dummy tokens
136  static void readToken(token& t, Istream& is);
137 
138  //- Expand a variable (string/word/var starting with '$')
139  static token expandToken
140  (
141  const dictionary& dict,
142  const string& keyword,
143  const token& t
144  );
145 
146  //- Expand a string/word/var token
147  static token expandToken
148  (
149  const dictionary& dict,
150  const token& t
151  );
152 
153  //- Test for equality
154  static bool equalToken(const token& t1, const token& t2);
155 
156  //- Consume tokens until reached a specific directive
157  static void skipUntil
158  (
159  DynamicList<filePos>& stack,
160  const dictionary& parentDict,
161  const word& endDirective,
162  Istream& is
163  );
164 
165 
166 public:
167 
168  //- Runtime type information
169  ClassName("ifeq");
170 
171  // Member Functions
172 
173  //- Execute the functionEntry in a sub-dict context
174  static bool execute(dictionary& parentDict, Istream& is);
175 };
176 
177 
178 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
179 
180 } // End namespace functionEntries
181 } // End namespace Foam
182 
183 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
184 
185 #endif
186 
187 // ************************************************************************* //
Foam::functionEntries::ifeqEntry::execute
static bool execute(const bool equal, DynamicList< filePos > &stack, dictionary &parentDict, Istream &is)
Definition: ifeqEntry.C:346
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Tuple2.H
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
Foam::entry::keyword
const keyType & keyword() const noexcept
Return keyword.
Definition: entry.H:195
Foam::functionEntries::ifeqEntry
Conditional parsing of dictionary entries.
Definition: ifeqEntry.H:89
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
functionEntry.H
Foam::functionEntries::ifeqEntry::filePos
Tuple2< fileName, label > filePos
Definition: ifeqEntry.H:97
Foam::primitiveEntry::dict
virtual const dictionary & dict() const
This entry is not a dictionary,.
Definition: primitiveEntry.C:299
Foam::functionEntries::ifeqEntry::evaluate
static bool evaluate(const bool doIf, DynamicList< filePos > &stack, dictionary &parentDict, Istream &is)
Definition: ifeqEntry.C:280
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::functionEntry
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
Definition: functionEntry.H:66
Foam::functionEntries::ifeqEntry::ClassName
ClassName("ifeq")
Runtime type information.
DynamicList.H
Foam::Tuple2
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:60
Foam::equal
bool equal(const T &s1, const T &s2)
Compare two values for equality.
Definition: doubleFloat.H:46