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-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::entry
29 
30 Description
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 
38 SourceFiles
39  entry.C
40  entryIO.C
41 
42 \*---------------------------------------------------------------------------*/
43 
44 #ifndef entry_H
45 #define entry_H
46 
47 #include "keyType.H"
48 #include "IDLList.H"
49 #include "fileName.H"
50 #include "autoPtr.H"
51 
52 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
53 
54 namespace Foam
55 {
56 
57 // Forward Declarations
58 class ITstream;
59 class dictionary;
60 class entry;
61 Ostream& operator<<(Ostream& os, const entry& e);
62 
63 
64 /*---------------------------------------------------------------------------*\
65  Class entry Declaration
66 \*---------------------------------------------------------------------------*/
67 
68 class entry
69 :
70  public IDLList<entry>::link
71 {
72 public:
73 
74  // Enums/Typedefs
75 
76  //- The input mode options
77  enum class inputMode
78  {
79  MERGE,
80  OVERWRITE,
81  PROTECT,
82  WARN,
83  ERROR,
84  GLOBAL
85  };
86 
87 
88 private:
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  (
102  keyType& keyword,
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 
117 protected:
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 
125 public:
126 
127  //- Enable or disable use of function entries and variable expansions.
128  static int disableFunctionEntries;
129 
130  //- The current global input-mode.
131  static inputMode globalInputMode;
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
144  virtual autoPtr<entry> clone
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,
164  const inputMode inpMode = inputMode::GLOBAL,
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
197  {
198  return keyword_;
199  }
200 
201  //- Return non-const access to keyword
202  keyType& keyword() noexcept
203  {
204  return keyword_;
205  }
206 
207  //- Return the entry name
208  virtual const fileName& name() const = 0;
209 
210  //- Return the entry name for modification
211  virtual fileName& name() = 0;
212 
213  //- Return the entry name relative to the current case
214  virtual fileName relativeName() const = 0;
215 
216  //- Return line number of first token in dictionary
217  virtual label startLineNumber() const = 0;
218 
219  //- Return line number of last token in dictionary
220  virtual label endLineNumber() const = 0;
221 
222 
223  //- Return true if this entry is a stream
224  virtual bool isStream() const noexcept
225  {
226  return false;
227  }
228 
229  //- Return token stream, if entry is a primitive entry
230  virtual ITstream& stream() const = 0;
231 
232 
233  //- Return true if this entry is a dictionary
234  virtual bool isDict() const noexcept
235  {
236  return this->dictPtr();
237  }
238 
239  //- Return pointer to dictionary, if entry is a dictionary.
240  // Return nullptr if the entry is not a dictionary.
241  virtual const dictionary* dictPtr() const noexcept
242  {
243  return nullptr;
244  }
245 
246  //- Return non-const pointer to dictionary, if entry is a dictionary
247  // Return nullptr if the entry is not a dictionary.
248  virtual dictionary* dictPtr() noexcept
249  {
250  return nullptr;
251  }
252 
253  //- Return dictionary, if entry is a dictionary
254  virtual const dictionary& dict() const = 0;
255 
256  //- Return non-const access to dictionary, if entry is a dictionary
257  virtual dictionary& dict() = 0;
258 
259 
260  // Read
261 
262  //- Check after reading if the input token stream has unconsumed
263  //- tokens remaining or if there were no tokens in the first place.
264  // Emits FatalIOError
265  void checkITstream(const ITstream& is) const;
266 
267  //- Get a T from the stream,
268  //- FatalIOError if the number of tokens is incorrect.
269  template<class T>
270  T get() const
271  {
272  T val;
273  readEntry<T>(val);
274  return val;
275  }
276 
277  //- Assign to T val,
278  //- FatalIOError if the number of tokens is incorrect.
279  //
280  // \param val the value to read into
281  template<class T>
282  void readEntry(T& val) const
283  {
284  ITstream& is = this->stream();
285  is >> val;
286 
287  checkITstream(is);
288  }
289 
290  //- Get a T from the stream,
291  //- FatalIOError if the number of tokens is incorrect.
292  //
293  // \param pred the value check predicate
294  template<class T, class Predicate>
295  T getCheck(const Predicate& pred) const
296  {
297  T val;
298  readCheck<T>(val, pred);
299  return val;
300  }
301 
302  //- Assign to T val,
303  //- FatalIOError if the number of tokens is incorrect.
304  //
305  // \param val the value to read into
306  // \param pred the value check predicate
307  template<class T, class Predicate>
308  void readCheck(T& val, const Predicate& pred) const
309  {
310  ITstream& is = this->stream();
311  is >> val;
312 
313  checkITstream(is);
314  if (!pred(val))
315  {
316  raiseBadInput(is);
317  }
318  }
319 
320 
321  // Write
322 
323  //- Write
324  virtual void write(Ostream& os) const = 0;
325 
326 
327  // Member Operators
328 
329  void operator=(const entry& e);
330 
331  bool operator==(const entry& e) const;
332  bool operator!=(const entry& e) const;
333 
334 
335  // Ostream Operator
336 
337  friend Ostream& operator<<(Ostream& os, const entry& e);
338 };
339 
340 
341 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
342 
343 } // End namespace Foam
344 
345 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
346 
347 #endif
348 
349 // ************************************************************************* //
Foam::entry::entry
entry(const keyType &keyword)
Construct from keyword.
Definition: entry.C:69
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::entry::inputMode::WARN
Keep initial entry. Warn about subsequent ones.
Foam::entry::New
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:105
Foam::entry::inputMode::ERROR
FatalError for duplicate entries.
Foam::entry::keyword
keyType & keyword() noexcept
Return non-const access to keyword.
Definition: entry.H:201
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::entry::stream
virtual ITstream & stream() const =0
Return token stream, if entry is a primitive entry.
Foam::entry::~entry
virtual ~entry()=default
Destructor.
Foam::entry::getCheck
T getCheck(const Predicate &pred) const
Definition: entry.H:294
Foam::entry::isStream
virtual bool isStream() const noexcept
Return true if this entry is a stream.
Definition: entry.H:223
Foam::entry::globalInputMode
static inputMode globalInputMode
The current global input-mode.
Definition: entry.H:130
Foam::entry::isDict
virtual bool isDict() const noexcept
Return true if this entry is a dictionary.
Definition: entry.H:233
Foam::entry::dictPtr
virtual const dictionary * dictPtr() const noexcept
Return pointer to dictionary, if entry is a dictionary.
Definition: entry.H:240
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:79
Foam::entry::keyword
const keyType & keyword() const noexcept
Return keyword.
Definition: entry.H:195
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::entry::inputMode
inputMode
The input mode options.
Definition: entry.H:76
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:68
Foam::ITstream
An input stream of tokens.
Definition: ITstream.H:52
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
fileName.H
Foam::entry::operator=
void operator=(const entry &e)
Definition: entry.C:194
Foam::entry::resetInputMode
static void resetInputMode()
Reset the globalInputMode to merge.
Definition: entry.C:61
Foam::entry::reportReadWarning
static void reportReadWarning(const IOstream &, const std::string &)
Report a read warning (on std::cerr)
Definition: entry.C:48
Foam::entry::startLineNumber
virtual label startLineNumber() const =0
Return line number of first token in dictionary.
Foam::ILList
Template class for intrusive linked lists.
Definition: ILList.H:52
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::entry::disableFunctionEntries
static int disableFunctionEntries
Enable or disable use of function entries and variable expansions.
Definition: entry.H:127
Foam::entry::inputMode::GLOBAL
Use global value from globalInputMode variable.
Foam::entry::dict
virtual const dictionary & dict() const =0
Return dictionary, if entry is a dictionary.
Foam::entry::name
virtual const fileName & name() const =0
Return the entry name.
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::entry::inputMode::MERGE
Merge sub-dictionaries when possible.
Foam::entry::relativeName
virtual fileName relativeName() const =0
Return the entry name relative to the current case.
Foam::entry::clone
virtual autoPtr< entry > clone() const
Construct on freestore as copy.
Definition: entry.C:83
Foam::entry::operator!=
bool operator!=(const entry &e) const
Definition: entry.C:228
Foam::entry::get
T get() const
Definition: entry.H:269
Foam::entry::readCheck
void readCheck(T &val, const Predicate &pred) const
Definition: entry.H:307
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Foam::entry::operator==
bool operator==(const entry &e) const
Definition: entry.C:205
Foam::entry::readEntry
void readEntry(T &val) const
Definition: entry.H:281
Foam::entry::inputMode::PROTECT
Keep initial entry. Silently ignore subsequent ones.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::entry::endLineNumber
virtual label endLineNumber() const =0
Return line number of last token in dictionary.
Foam::entry::checkITstream
void checkITstream(const ITstream &is) const
Definition: entry.C:110
keyType.H
Foam::entry::write
virtual void write(Ostream &os) const =0
Write.
IDLList.H
Intrusive doubly-linked list.
Foam::entry::inputMode::OVERWRITE
Keep last entry. Silently remove previous ones.
Foam::entry::operator<<
friend Ostream & operator<<(Ostream &os, const entry &e)
autoPtr.H