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 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::functionEntries::ifeqEntry
28 
29 Description
30  Conditional parsing of dictionary entries.
31 
32  E.g.
33  \verbatim
34  a #calc "0.123";
35  b 1.23e-1;
36 
37  #ifeq $a $b
38  ..
39  #else
40  ..
41  #endif
42  \endverbatim
43 
44  \verbatim
45  ddtSchemes
46  {
47  #ifeq ${FOAM_APPLICATION} simpleFoam
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 two tokens
58  - the comparison is a string comparison for any word/string/variable,
59  integer comparison for two integers and floating point comparison for
60  any floating point number.
61  - parsing of (non)matching \c #else, \c #endif is not very sophisticated
62 
63 See also
64  Foam::functionEntries::ifEntry
65 
66 SourceFiles
67  ifeqEntry.C
68 
69 \*---------------------------------------------------------------------------*/
70 
71 #ifndef ifeqEntry_H
72 #define ifeqEntry_H
73 
74 #include "functionEntry.H"
75 #include "DynamicList.H"
76 #include "Tuple2.H"
77 
78 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
79 
80 namespace Foam
81 {
82 namespace functionEntries
83 {
84 
85 /*---------------------------------------------------------------------------*\
86  Class ifeqEntry Declaration
87 \*---------------------------------------------------------------------------*/
88 
89 class ifeqEntry
90 :
91  public functionEntry
92 {
93 
94 protected:
95 
97 
98  // Protected Member Functions
99 
100  //- Read tokens. Skip dummy tokens
101  static void readToken(token& t, Istream& is);
102 
103  //- Expand a variable (string/word/var starting with '$')
104  static token expand
105  (
106  const dictionary& dict,
107  const string& keyword,
108  const token& t
109  );
110 
111  //- Expand a string/word/var token
112  static token expand
113  (
114  const dictionary& dict,
115  const token& t
116  );
117 
118  static bool equalToken
119  (
120  const token& t1,
121  const token& t2
122  );
123 
124  //- Consume tokens until reached a specific word
125  static void skipUntil
126  (
127  DynamicList<filePos>& stack,
128  const dictionary& parentDict,
129  const word& endWord,
130  Istream& is
131  );
132 
133  static bool evaluate
134  (
135  const bool doIf,
136  DynamicList<filePos>& stack,
137  dictionary& parentDict,
138  Istream& is
139  );
140 
141  //- Main driver: depending on 'equal' starts evaluating or
142  //- skips forward to #else
143  static bool execute
144  (
145  const bool equal,
146  DynamicList<filePos>& stack,
147  dictionary& parentDict,
148  Istream& is
149  );
150 
151  //- Main driver: depending on 'equal' starts evaluating or
152  //- skips forward to #else
153  static bool execute
154  (
155  DynamicList<filePos>& stack,
156  dictionary& parentDict,
157  Istream& is
158  );
159 
160 
161 public:
162 
163  //- Runtime type information
164  ClassName("ifeq");
165 
166  // Member Functions
167 
168  //- Execute the functionEntry in a sub-dict context
169  static bool execute(dictionary& parentDict, Istream& is);
170 };
171 
172 
173 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
174 
175 } // End namespace functionEntries
176 } // End namespace Foam
177 
178 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
179 
180 #endif
181 
182 // ************************************************************************* //
Foam::functionEntries::ifeqEntry::execute
static bool execute(const bool equal, DynamicList< filePos > &stack, dictionary &parentDict, Istream &is)
Definition: ifeqEntry.C:330
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::entry::keyword
const keyType & keyword() const
Return keyword.
Definition: entry.H:187
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::functionEntries::ifeqEntry
Conditional parsing of dictionary entries.
Definition: ifeqEntry.H:88
Foam::functionEntries::ifeqEntry::readToken
static void readToken(token &t, Istream &is)
Read tokens. Skip dummy tokens.
Definition: ifeqEntry.C:56
Foam::token
A token holds an item read from Istream.
Definition: token.H:69
functionEntry.H
Foam::functionEntries::ifeqEntry::expand
static token expand(const dictionary &dict, const string &keyword, const token &t)
Expand a variable (string/word/var starting with '$')
Definition: ifeqEntry.C:76
Foam::functionEntries::ifeqEntry::filePos
Tuple2< fileName, label > filePos
Definition: ifeqEntry.H:95
Foam::primitiveEntry::dict
virtual const dictionary & dict() const
This entry is not a dictionary,.
Definition: primitiveEntry.C:290
Foam::functionEntries::ifeqEntry::evaluate
static bool evaluate(const bool doIf, DynamicList< filePos > &stack, dictionary &parentDict, Istream &is)
Definition: ifeqEntry.C:276
Foam::functionEntries::ifeqEntry::skipUntil
static void skipUntil(DynamicList< filePos > &stack, const dictionary &parentDict, const word &endWord, Istream &is)
Consume tokens until reached a specific word.
Definition: ifeqEntry.C:237
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:121
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:65
Foam::functionEntries::ifeqEntry::ClassName
ClassName("ifeq")
Runtime type information.
Foam::functionEntries::ifeqEntry::equalToken
static bool equalToken(const token &t1, const token &t2)
Definition: ifeqEntry.C:136
DynamicList.H
Foam::Tuple2
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: Tuple2.H:57
Foam::equal
bool equal(const T &s1, const T &s2)
Compare two values for equality.
Definition: doubleFloat.H:46