dictionary.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 | Copyright (C) 2011-2016 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::dictionary
26 
27 Description
28  A list of keyword definitions, which are a keyword followed by any number
29  of values (e.g. words and numbers). The keywords can represent patterns
30  which are matched using Posix regular expressions. The general order for
31  searching is as follows:
32  - exact match
33  - pattern match (in reverse order)
34  - optional recursion into the enclosing (parent) dictionaries
35 
36  The dictionary class is the base class for IOdictionary.
37  It also serves as a bootstrap dictionary for the objectRegistry data
38  dictionaries since, unlike the IOdictionary class, it does not use an
39  objectRegistry itself to work.
40 
41  To add - a merge() member function with a non-const dictionary parameter?
42  This would avoid unnecessary cloning in the add(entry*, bool) method.
43 
44 SourceFiles
45  dictionary.C
46  dictionaryIO.C
47 
48 \*---------------------------------------------------------------------------*/
49 
50 #ifndef dictionary_H
51 #define dictionary_H
52 
53 #include "entry.H"
54 #include "IDLList.H"
55 #include "DLList.H"
56 #include "fileName.H"
57 #include "ITstream.H"
58 #include "HashTable.H"
59 #include "wordList.H"
60 #include "className.H"
61 
62 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
63 
64 namespace Foam
65 {
66 
67 // Forward declaration of friend functions and operators
68 class regExp;
69 class dictionary;
70 class SHA1Digest;
71 
72 Istream& operator>>(Istream&, dictionary&);
73 Ostream& operator<<(Ostream&, const dictionary&);
74 
75 /*---------------------------------------------------------------------------*\
76  Class dictionaryName Declaration
77 \*---------------------------------------------------------------------------*/
78 
79 class dictionaryName
80 {
81  // Private data
82 
83  fileName name_;
84 
85 
86 public:
87 
88  // Constructors
89 
90  //- Construct dictionaryName null
92  {}
93 
94  //- Construct dictionaryName as copy of the given fileName
96  :
97  name_(name)
98  {}
99 
100 
101  // Member functions
102 
103  //- Return the dictionary name
104  const fileName& name() const
105  {
106  return name_;
107  }
108 
109  //- Return the dictionary name
110  fileName& name()
111  {
112  return name_;
113  }
114 
115  //- Return the local dictionary name (final part of scoped name)
116  const word dictName() const
117  {
118  const word scopedName = name_.name();
119 
120  string::size_type i = scopedName.rfind('.');
121 
122  if (i == scopedName.npos)
123  {
124  return scopedName;
125  }
126  else
127  {
128  return scopedName.substr(i + 1, scopedName.npos);
129  }
130  }
131 };
132 
133 
134 /*---------------------------------------------------------------------------*\
135  Class dictionary Declaration
136 \*---------------------------------------------------------------------------*/
138 class dictionary
139 :
140  public dictionaryName,
141  public IDLList<entry>
142 {
143  // Private data
144 
145  //- If true write optional keywords and values
146  // if not present in dictionary
147  static bool writeOptionalEntries;
148 
149  //- HashTable of the entries held on the DL-list for quick lookup
150  HashTable<entry*> hashedEntries_;
151 
152  //- Parent dictionary
153  const dictionary& parent_;
154 
155  //- Entries of matching patterns
156  DLList<entry*> patternEntries_;
157 
158  //- Patterns as precompiled regular expressions
159  DLList<autoPtr<regExp>> patternRegexps_;
160 
161 
162  // Private Member Functions
163 
164  //- Search patterns table for exact match or regular expression match
165  bool findInPatterns
166  (
167  const bool patternMatch,
168  const word& Keyword,
171  ) const;
172 
173  //- Search patterns table for exact match or regular expression match
174  bool findInPatterns
175  (
176  const bool patternMatch,
177  const word& Keyword,
178  DLList<entry*>::iterator& wcLink,
180  );
181 
182 
183 public:
184 
185  //- Declare friendship with the entry class for IO
186  friend class entry;
187 
188 
189  // Declare name of the class and its debug switch
190  ClassName("dictionary");
191 
192 
193  //- Null dictionary
194  static const dictionary null;
195 
196 
197  // Constructors
198 
199  //- Construct top-level dictionary null
200  dictionary();
201 
202  //- Construct top-level empty dictionary with given name
203  dictionary(const fileName& name);
204 
205  //- Construct given the entry name, parent dictionary and Istream,
206  // reading entries until lastEntry or EOF
207  dictionary
208  (
209  const fileName& name,
210  const dictionary& parentDict,
211  Istream&
212  );
213 
214  //- Construct top-level dictionary from Istream,
215  // reading entries until EOF
217 
218  //- Construct top-level dictionary from Istream,
219  // reading entries until EOF, optionally keeping the header
220  dictionary(Istream&, const bool keepHeader);
221 
222  //- Construct as copy given the parent dictionary
223  dictionary(const dictionary& parentDict, const dictionary&);
224 
225  //- Construct top-level dictionary as copy
226  dictionary(const dictionary&);
227 
228  //- Construct top-level dictionary as copy from pointer to dictionary.
229  // A null pointer is treated like an empty dictionary.
230  dictionary(const dictionary*);
231 
232  //- Construct by transferring parameter contents given parent dictionary
233  dictionary(const dictionary& parentDict, const Xfer<dictionary>&);
234 
235  //- Construct top-level dictionary by transferring parameter contents
237 
238  //- Construct and return clone
239  autoPtr<dictionary> clone() const;
240 
241  //- Construct top-level dictionary on freestore from Istream
243 
244 
245  //- Destructor
246  virtual ~dictionary();
247 
248 
249  // Member functions
250 
251  //- Return the parent dictionary
252  const dictionary& parent() const
253  {
254  return parent_;
255  }
256 
257  //- Return the top of the tree
258  const dictionary& topDict() const;
259 
260  //- Return line number of first token in dictionary
261  label startLineNumber() const;
262 
263  //- Return line number of last token in dictionary
264  label endLineNumber() const;
265 
266  //- Return the SHA1 digest of the dictionary contents
267  SHA1Digest digest() const;
268 
269  //- Return the dictionary as a list of tokens
270  tokenList tokens() const;
271 
272 
273  // Search and lookup
274 
275  //- Search dictionary for given keyword
276  // If recursive, search parent dictionaries
277  // If patternMatch, use regular expressions
278  bool found
279  (
280  const word&,
281  bool recursive=false,
282  bool patternMatch = true
283  ) const;
284 
285  //- Find and return an entry data stream pointer if present
286  // otherwise return NULL.
287  // If recursive, search parent dictionaries.
288  // If patternMatch, use regular expressions
289  const entry* lookupEntryPtr
290  (
291  const word&,
292  bool recursive,
293  bool patternMatch
294  ) const;
295 
296  //- Find and return an entry data stream pointer for manipulation
297  // if present otherwise return NULL.
298  // If recursive, search parent dictionaries.
299  // If patternMatch, use regular expressions.
300  entry* lookupEntryPtr
301  (
302  const word&,
303  bool recursive,
304  bool patternMatch
305  );
306 
307  //- Find and return an entry data stream if present otherwise error.
308  // If recursive, search parent dictionaries.
309  // If patternMatch, use regular expressions.
310  const entry& lookupEntry
311  (
312  const word&,
313  bool recursive,
314  bool patternMatch
315  ) const;
316 
317  //- Find and return an entry data stream
318  // If recursive, search parent dictionaries.
319  // If patternMatch, use regular expressions.
321  (
322  const word&,
323  bool recursive=false,
324  bool patternMatch=true
325  ) const;
326 
327  //- Find and return a T,
328  // if not found return the given default value
329  // If recursive, search parent dictionaries.
330  // If patternMatch, use regular expressions.
331  template<class T>
332  T lookupOrDefault
333  (
334  const word&,
335  const T&,
336  bool recursive=false,
337  bool patternMatch=true
338  ) const;
339 
340  //- Find and return a T, if not found return the given
341  // default value, and add to dictionary.
342  // If recursive, search parent dictionaries.
343  // If patternMatch, use regular expressions.
344  template<class T>
345  T lookupOrAddDefault
346  (
347  const word&,
348  const T&,
349  bool recursive=false,
350  bool patternMatch=true
351  );
352 
353  //- Find an entry if present, and assign to T
354  // Returns true if the entry was found.
355  // If recursive, search parent dictionaries.
356  // If patternMatch, use regular expressions.
357  template<class T>
358  bool readIfPresent
359  (
360  const word&,
361  T&,
362  bool recursive=false,
363  bool patternMatch=true
364  ) const;
365 
366  //- Find and return an entry data stream pointer if present
367  // otherwise return NULL. Allows scoping using '.'
368  const entry* lookupScopedEntryPtr
369  (
370  const word&,
371  bool recursive,
372  bool patternMatch
373  ) const;
374 
375  //- Check if entry is a sub-dictionary
376  bool isDict(const word&) const;
377 
378  //- Find and return a sub-dictionary pointer if present
379  // otherwise return NULL.
380  const dictionary* subDictPtr(const word&) const;
381 
382  //- Find and return a sub-dictionary
383  const dictionary& subDict(const word&) const;
384 
385  //- Find and return a sub-dictionary for manipulation
386  dictionary& subDict(const word&);
387 
388  //- Find and return a sub-dictionary as a copy, or
389  // return an empty dictionary if the sub-dictionary does not exist
390  dictionary subOrEmptyDict
391  (
392  const word&,
393  const bool mustRead = false
394  ) const;
395 
396  //- Return the table of contents
397  wordList toc() const;
398 
399  //- Return the sorted table of contents
400  wordList sortedToc() const;
401 
402  //- Return the list of available keys or patterns
403  List<keyType> keys(bool patterns=false) const;
404 
405 
406  // Editing
407 
408  //- Substitute the given keyword prepended by '$' with the
409  // corresponding sub-dictionary entries
410  bool substituteKeyword(const word& keyword);
411 
412  //- Substitute the given scoped keyword prepended by '$' with the
413  // corresponding sub-dictionary entries
414  bool substituteScopedKeyword(const word& keyword);
415 
416  //- Add a new entry
417  // With the merge option, dictionaries are interwoven and
418  // primitive entries are overwritten
419  bool add(entry*, bool mergeEntry=false);
420 
421  //- Add an entry
422  // With the merge option, dictionaries are interwoven and
423  // primitive entries are overwritten
424  void add(const entry&, bool mergeEntry=false);
425 
426  //- Add a word entry
427  // optionally overwrite an existing entry
428  void add(const keyType&, const word&, bool overwrite=false);
429 
430  //- Add a string entry
431  // optionally overwrite an existing entry
432  void add(const keyType&, const string&, bool overwrite=false);
433 
434  //- Add a label entry
435  // optionally overwrite an existing entry
436  void add(const keyType&, const label, bool overwrite=false);
437 
438  //- Add a scalar entry
439  // optionally overwrite an existing entry
440  void add(const keyType&, const scalar, bool overwrite=false);
441 
442  //- Add a dictionary entry
443  // optionally merge with an existing sub-dictionary
444  void add
445  (
446  const keyType&,
447  const dictionary&,
448  bool mergeEntry=false
449  );
450 
451  //- Add a T entry
452  // optionally overwrite an existing entry
453  template<class T>
454  void add(const keyType&, const T&, bool overwrite=false);
455 
456  //- Assign a new entry, overwrite any existing entry
457  void set(entry*);
458 
459  //- Assign a new entry, overwrite any existing entry
460  void set(const entry&);
461 
462  //- Assign a dictionary entry, overwrite any existing entry
463  void set(const keyType&, const dictionary&);
464 
465  //- Assign a T entry, overwrite any existing entry
466  template<class T>
467  void set(const keyType&, const T&);
468 
469  //- Remove an entry specified by keyword
470  bool remove(const word&);
471 
472  //- Change the keyword for an entry,
473  // optionally forcing overwrite of an existing entry
474  bool changeKeyword
475  (
476  const keyType& oldKeyword,
477  const keyType& newKeyword,
478  bool forceOverwrite=false
479  );
480 
481  //- Merge entries from the given dictionary.
482  // Also merge sub-dictionaries as required.
483  bool merge(const dictionary&);
484 
485  //- Clear the dictionary
486  void clear();
487 
488  //- Transfer the contents of the argument and annul the argument.
489  void transfer(dictionary&);
490 
491  //- Transfer contents to the Xfer container
492  Xfer<dictionary> xfer();
493 
494 
495  // Read
496 
497  //- Read dictionary from Istream
498  bool read(Istream&);
499 
500  //- Read dictionary from Istream, optionally keeping the header
501  bool read(Istream&, const bool keepHeader);
502 
503 
504  // Write
505 
506  //- Write dictionary, normally with sub-dictionary formatting
507  void write(Ostream&, const bool subDict=true) const;
508 
509 
510  // Member Operators
511 
512  //- Find and return entry
513  ITstream& operator[](const word&) const;
514 
515  void operator=(const dictionary&);
516 
517  //- Include entries from the given dictionary.
518  // Warn, but do not overwrite existing entries.
519  void operator+=(const dictionary&);
520 
521  //- Conditionally include entries from the given dictionary.
522  // Do not overwrite existing entries.
523  void operator|=(const dictionary&);
524 
525  //- Unconditionally include entries from the given dictionary.
526  // Overwrite existing entries.
527  void operator<<=(const dictionary&);
528 
529 
530  // IOstream operators
531 
532  //- Read dictionary from Istream
533  friend Istream& operator>>(Istream&, dictionary&);
534 
535  //- Write dictionary to Ostream
536  friend Ostream& operator<<(Ostream&, const dictionary&);
537 };
538 
539 
540 // Global Operators
541 
542 //- Combine dictionaries.
543 // Starting from the entries in dict1 and then including those from dict2.
544 // Warn, but do not overwrite the entries from dict1.
545 dictionary operator+(const dictionary& dict1, const dictionary& dict2);
546 
547 //- Combine dictionaries.
548 // Starting from the entries in dict1 and then including those from dict2.
549 // Do not overwrite the entries from dict1.
550 dictionary operator|(const dictionary& dict1, const dictionary& dict2);
551 
552 
553 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
554 
555 } // End namespace Foam
556 
557 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
558 
559 #ifdef NoRepository
560  #include "dictionaryTemplates.C"
561 #endif
562 
563 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
564 
565 #endif
566 
567 // ************************************************************************* //
A class for handling keywords in dictionaries.
Definition: keyType.H:64
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
HashSet< Key, Hash > operator|(const HashSet< Key, Hash > &hash1, const HashSet< Key, Hash > &hash2)
Combine entries from HashSets.
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
A class for handling file names.
Definition: fileName.H:69
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
tUEqn clear()
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
dictionaryName()
Construct dictionaryName null.
Definition: dictionary.H:90
const word dictName() const
Return the local dictionary name (final part of scoped name)
Definition: dictionary.H:115
The SHA1 message digest.
Definition: SHA1Digest.H:62
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
#define ClassName(TypeNameString)
Add typeName information from argument TypeNameString to a class.
Definition: className.H:65
word name() const
Return file name (part beyond last /)
Definition: fileName.C:179
bool read(const char *, int32_t &)
Definition: int32IO.C:85
stressControl lookup("compactNormalStress") >> compactNormalStress
A class for handling words, derived from string.
Definition: word.H:59
Istream & operator>>(Istream &, directionInfo &)
An STL-conforming const_iterator.
Definition: DLListBase.H:228
An STL-conforming iterator.
Definition: DLListBase.H:181
Non-intrusive doubly-linked list.
Definition: DLList.H:47
tmp< fvMatrix< Type > > operator+(const fvMatrix< Type > &, const fvMatrix< Type > &)
An STL-conforming hash table.
Definition: HashTable.H:61
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Intrusive doubly-linked list.
Definition: IDLList.H:47
Macro definitions for declaring ClassName(), NamespaceName(), etc.
Ostream & operator<<(Ostream &, const ensightPart &)
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:53
bool found
runTime write()
Input token stream.
Definition: ITstream.H:49
Namespace for OpenFOAM.
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:65