Loading...
Searching...
No Matches
entry.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-2016 OpenFOAM Foundation
9 Copyright (C) 2017-2024 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::entry
29
30Description
31 A keyword and a list of tokens is an 'entry'.
32
33 An entry can be read, written and printed, and the types and values of
34 its tokens analysed. An entry is a high-level building block for data
35 description. It is a front-end for the token parser. A list of entries
36 can be used as a set of keyword syntax elements, for example.
37
38SourceFiles
39 entry.C
40 entryIO.C
41
42\*---------------------------------------------------------------------------*/
43
44#ifndef Foam_entry_H
45#define Foam_entry_H
46
47#include "keyType.H"
48#include "IDLList.H"
49#include "fileName.H"
50#include "autoPtr.H"
51
52// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
53
54namespace Foam
55{
56
57// Forward Declarations
58class ITstream;
59class dictionary;
60class entry;
62
64/*---------------------------------------------------------------------------*\
65 Class entry Declaration
66\*---------------------------------------------------------------------------*/
67
68class entry
69:
70 public IDLList<entry>::link
71{
72public:
73
74 // Enums/Typedefs
75
76 //- The input mode options
77 enum class inputMode
78 {
82 WARN,
83 ERROR,
84 GLOBAL
85 };
86
87
88private:
89
90 // Private Data
91
92 //- Keyword of entry
93 keyType keyword_;
94
95
96 // Private Member Functions
97
98 //- Get the next valid keyword.
99 // \return True if it is a valid keyType.
100 static bool getKeyword
101 (
103 token& keyToken,
104 Istream& is
105 );
106
107 //- Get the next valid keyword.
108 // Warn when an invalid token is encountered that is not EOF or
109 // end-of-block
110 // \return True if it is a valid keyType.
111 static bool getKeyword(keyType& keyword, Istream& is);
112
113 //- Emit IOError about bad input for the entry
114 void raiseBadInput(const ITstream& is) const;
115
116
117protected:
118
119 // Protected Member Functions
120
121 //- Report a read warning (on std::cerr)
122 static void reportReadWarning(const IOstream&, const std::string&);
123
124
125public:
126
127 //- Enable or disable use of function entries and variable expansions.
128 static int disableFunctionEntries;
129
130 //- The current global input-mode.
132
133
134 // Constructors
135
136 //- Construct from keyword
137 entry(const keyType& keyword);
138
139 //- Construct as copy
140 entry(const entry& e);
141
142 //- Construct on freestore as copy with reference to the
143 // dictionary the copy belongs to
145 (
146 const dictionary& parentDict
147 ) const = 0;
148
149 //- Construct on freestore as copy
150 // Note: the parent directory is set to dictionary::null
151 virtual autoPtr<entry> clone() const;
152
153 //- Construct from an Istream and insert into the dictionary
154 // \param parentDict dictionary to insert into
155 // \param is the input stream
156 // \param inpMode the input mode.
157 // The default is to use the currently active #globalInputMode
158 // \param endChar the expected end character (eg, a closing brace).
159 // The endChar is 0 if no expectations are asserted.
160 static bool New
161 (
162 dictionary& parentDict,
163 Istream& is,
165 const int endChar = 0
166 );
167
168 //- Construct an entry from Istream.
169 // The expected input comprises a keyword followed by a
170 // dictionaryEntry or a primitiveEntry.
171 //
172 // - The dictionaryEntry starts with a '{' left brace and ends
173 // with a '}' right brace.
174 // - The primitiveEntry ends with a ';' semi-colon.
175 //
176 // Example input
177 // \verbatim
178 // key1 { ... } // dictionary input
179 // key2 ... ; // primitive input
180 // \endverbatim
181 //
182 // \return The #entry read, or nullptr on error.
183 static autoPtr<entry> New(Istream& is);
184
185 //- Reset the #globalInputMode to %merge
186 static void resetInputMode();
187
188
189 //- Destructor
190 virtual ~entry() = default;
191
192
193 // Member Functions
194
195 //- Return keyword
196 const keyType& keyword() const noexcept { return keyword_; }
197
198 //- Return non-const access to keyword
199 keyType& keyword() noexcept { return keyword_; }
200
201 //- Return the entry name
202 virtual const fileName& name() const = 0;
203
204 //- Return the entry name for modification
205 virtual fileName& name() = 0;
206
207 //- Return the entry name relative to the current case
208 virtual fileName relativeName() const = 0;
209
210 //- Return line number of first token in dictionary
211 virtual label startLineNumber() const = 0;
212
213 //- Return line number of last token in dictionary
214 virtual label endLineNumber() const = 0;
215
216
217 //- True if this entry is a stream
218 virtual bool isStream() const noexcept { return this->streamPtr(); }
219
220 //- Return pointer to token stream, if it is a primitive entry,
221 //- otherwise return nullptr
222 virtual ITstream* streamPtr() const noexcept { return nullptr; }
224 //- Return token stream, if entry is a primitive entry
225 virtual ITstream& stream() const = 0;
226
227
228 //- True if this entry is a dictionary
229 virtual bool isDict() const noexcept { return this->dictPtr(); }
230
231 //- Return pointer to dictionary, if entry is a dictionary,
232 //- otherwise return nullptr.
233 virtual const dictionary* dictPtr() const noexcept { return nullptr; }
234
235 //- Return non-const pointer to dictionary, if entry is a dictionary,
236 //- otherwise return nullptr.
237 virtual dictionary* dictPtr() noexcept { return nullptr; }
238
239 //- Return dictionary, if entry is a dictionary,
240 //- otherwise Fatal.
241 virtual const dictionary& dict() const = 0;
242
243 //- Return non-const access to dictionary, if entry is a dictionary,
244 //- otherwise Fatal.
245 virtual dictionary& dict() = 0;
247
248 // Read
249
250 //- Check after reading if the input token stream has unconsumed
251 //- tokens remaining or if there were no tokens in the first place.
252 // Emits FatalIOError
253 void checkITstream(const ITstream& is) const;
254
255 //- Get a T from the stream,
256 //- FatalIOError if the number of tokens is incorrect.
257 template<class T>
258 T get() const
259 {
260 T val;
262 return val;
263 }
264
265 //- Assign to T val,
266 //- FatalIOError if the number of tokens is incorrect.
267 //
268 // \param val the value to read into
269 template<class T>
270 void readEntry(T& val) const
271 {
272 ITstream& is = this->stream();
273 is >> val;
274
275 checkITstream(is);
276 }
277
278 //- Get a T from the stream,
279 //- FatalIOError if the number of tokens is incorrect.
280 //
281 // \param pred the value check predicate
282 template<class T, class Predicate>
283 T getCheck(const Predicate& pred) const
285 T val;
286 readCheck<T>(val, pred);
287 return val;
288 }
289
290 //- Assign to T val,
291 //- FatalIOError if the number of tokens is incorrect.
292 //
293 // \param val the value to read into
294 // \param pred the value check predicate
295 template<class T, class Predicate>
296 void readCheck(T& val, const Predicate& pred) const
297 {
298 ITstream& is = this->stream();
299 is >> val;
300
301 checkITstream(is);
302 if (!pred(val))
303 {
304 raiseBadInput(is);
305 }
306 }
307
309 // Write
310
311 //- Write
312 virtual void write(Ostream& os) const = 0;
313
314
315 // Member Operators
316
317 void operator=(const entry& e);
318
319 bool operator==(const entry& e) const;
320 bool operator!=(const entry& e) const;
321
322
323 // Ostream Operator
324
325 friend Ostream& operator<<(Ostream& os, const entry& e);
327
328
329// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
330
331} // End namespace Foam
332
333// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
334
335#endif
336
337// ************************************************************************* //
Intrusive doubly-linked list.
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition IOstream.H:85
An input stream of tokens.
Definition ITstream.H:56
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
entry(const keyType &keyword)
Construct from keyword.
Definition entry.C:62
virtual dictionary * dictPtr() noexcept
Return non-const pointer to dictionary, if entry is a dictionary, otherwise return nullptr.
Definition entry.H:296
void readEntry(T &val) const
Assign to T val, FatalIOError if the number of tokens is incorrect.
Definition entry.H:341
virtual ITstream & stream() const =0
Return token stream, if entry is a primitive entry.
static void resetInputMode()
Reset the globalInputMode to merge.
Definition entry.C:54
void operator=(const entry &e)
Definition entry.C:179
virtual ~entry()=default
Destructor.
virtual bool isDict() const noexcept
True if this entry is a dictionary.
Definition entry.H:284
void readCheck(T &val, const Predicate &pred) const
Assign to T val, FatalIOError if the number of tokens is incorrect.
Definition entry.H:373
virtual bool isStream() const noexcept
True if this entry is a stream.
Definition entry.H:267
friend Ostream & operator<<(Ostream &os, const entry &e)
virtual label startLineNumber() const =0
Return line number of first token in dictionary.
virtual autoPtr< entry > clone() const
Construct on freestore as copy.
Definition entry.C:76
bool operator!=(const entry &e) const
Definition entry.C:222
static bool New(dictionary &parentDict, Istream &is, const inputMode inpMode=inputMode::GLOBAL, const int endChar=0)
Construct from an Istream and insert into the dictionary.
Definition entryIO.C:98
virtual fileName & name()=0
Return the entry name for modification.
void checkITstream(const ITstream &is) const
Check after reading if the input token stream has unconsumed tokens remaining or if there were no tok...
Definition entry.C:103
virtual dictionary & dict()=0
Return non-const access to dictionary, if entry is a dictionary, otherwise Fatal.
keyType & keyword() noexcept
Return non-const access to keyword.
Definition entry.H:236
virtual const fileName & name() const =0
Return the entry name.
virtual autoPtr< entry > clone(const dictionary &parentDict) const =0
Construct on freestore as copy with reference to the.
static inputMode globalInputMode
The current global input-mode.
Definition entry.H:144
inputMode
The input mode options.
Definition entry.H:75
@ OVERWRITE
Keep last entry. Silently remove previous ones.
Definition entry.H:77
@ PROTECT
Keep initial entry. Silently ignore subsequent ones.
Definition entry.H:78
@ WARN
Keep initial entry. Warn about subsequent ones.
Definition entry.H:79
@ GLOBAL
Use global value from globalInputMode variable.
Definition entry.H:81
@ MERGE
Merge sub-dictionaries when possible.
Definition entry.H:76
@ ERROR
FatalError for duplicate entries.
Definition entry.H:80
virtual fileName relativeName() const =0
Return the entry name relative to the current case.
const keyType & keyword() const noexcept
Return keyword.
Definition entry.H:231
static void reportReadWarning(const IOstream &, const std::string &)
Report a read warning (on std::cerr).
Definition entry.C:41
virtual const dictionary * dictPtr() const noexcept
Return pointer to dictionary, if entry is a dictionary, otherwise return nullptr.
Definition entry.H:290
virtual ITstream * streamPtr() const noexcept
Return pointer to token stream, if it is a primitive entry, otherwise return nullptr.
Definition entry.H:273
virtual const dictionary & dict() const =0
Return dictionary, if entry is a dictionary, otherwise Fatal.
bool operator==(const entry &e) const
Definition entry.C:190
T getCheck(const Predicate &pred) const
Get a T from the stream, FatalIOError if the number of tokens is incorrect.
Definition entry.H:357
virtual label endLineNumber() const =0
Return line number of last token in dictionary.
virtual void write(Ostream &os) const =0
Write.
T get() const
Get a T from the stream, FatalIOError if the number of tokens is incorrect.
Definition entry.H:326
static int disableFunctionEntries
Enable or disable use of function entries and variable expansions.
Definition entry.H:139
A class for handling file names.
Definition fileName.H:75
A class for handling keywords in dictionaries.
Definition keyType.H:69
A token holds an item read from Istream.
Definition token.H:70
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
ILList< DLListBase, T > IDLList
Definition IDLList.H:39
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
const direction noexcept
Definition scalarImpl.H:265
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
runTime write()
volScalarField & e