dictionary.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 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 #include "VectorSpace.H"
63 
64 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65 
66 namespace Foam
67 {
68 
69 // Forward declaration of friend functions and operators
70 class regExp;
71 class dictionary;
72 class SHA1Digest;
73 
74 Istream& operator>>(Istream&, dictionary&);
75 Ostream& operator<<(Ostream&, const dictionary&);
76 
77 /*---------------------------------------------------------------------------*\
78  Class dictionaryName Declaration
79 \*---------------------------------------------------------------------------*/
80 
81 class dictionaryName
82 {
83  // Private Data
84 
85  fileName name_;
86 
87 
88 public:
89 
90  // Constructors
91 
92  //- Construct dictionaryName null
94  {}
95 
96  //- Construct dictionaryName as copy of the given fileName
98  :
99  name_(name)
100  {}
101 
102  //- Move constructor
104  :
105  name_(move(name.name_))
106  {}
107 
108 
109  // Member Functions
110 
111  //- Return the dictionary name
112  const fileName& name() const
113  {
114  return name_;
115  }
116 
117  //- Return the dictionary name
118  fileName& name()
119  {
120  return name_;
121  }
122 
123  //- Return the local dictionary name (final part of scoped name)
124  const word dictName() const
125  {
126  const word scopedName = name_.name();
127 
128  string::size_type i = scopedName.rfind('/');
129 
130  if (i == scopedName.npos)
131  {
132  return scopedName;
133  }
134  else
135  {
136  return scopedName.substr(i + 1, scopedName.npos);
137  }
138  }
139 
140 
141  // Member Operators
143  void operator=(const dictionaryName& name)
144  {
145  name_ = name.name_;
146  }
149  {
150  name_ = move(name.name_);
151  }
152 };
153 
154 
155 /*---------------------------------------------------------------------------*\
156  Class dictionary Declaration
157 \*---------------------------------------------------------------------------*/
159 class dictionary
160 :
161  public dictionaryName,
162  public IDLList<entry>
163 {
164  // Private Data
165 
166  //- If true write optional keywords and values
167  // if not present in dictionary
168  static bool writeOptionalEntries;
169 
170  //- HashTable of the entries held on the DL-list for quick lookup
171  HashTable<entry*> hashedEntries_;
172 
173  //- Parent dictionary
174  const dictionary& parent_;
175 
176  //- Entries of matching patterns
177  DLList<entry*> patternEntries_;
178 
179  //- Patterns as precompiled regular expressions
180  DLList<autoPtr<regExp>> patternRegexps_;
181 
182 
183  // Private Member Functions
184 
185  //- Find and return an entry data stream pointer if present
186  // otherwise return nullptr. Supports scoping using '.'
187  const entry* lookupDotScopedSubEntryPtr
188  (
189  const word&,
190  bool recursive,
191  bool patternMatch
192  ) const;
193 
194  //- Find and return an entry data stream pointer if present
195  // otherwise return nullptr. Supports scoping using '/'
196  const entry* lookupSlashScopedSubEntryPtr
197  (
198  const word&,
199  bool recursive,
200  bool patternMatch
201  ) const;
202 
203  //- Find and return an entry data stream pointer if present
204  // otherwise return nullptr. Supports scoping using '/' or '.'
205  const entry* lookupScopedSubEntryPtr
206  (
207  const word&,
208  bool recursive,
209  bool patternMatch
210  ) const;
211 
212  //- Search patterns table for exact match or regular expression match
213  bool findInPatterns
214  (
215  const bool patternMatch,
216  const word& Keyword,
219  ) const;
220 
221  //- Search patterns table for exact match or regular expression match
222  bool findInPatterns
223  (
224  const bool patternMatch,
225  const word& Keyword,
226  DLList<entry*>::iterator& wcLink,
228  );
229 
230 
231 public:
232 
233  //- Declare friendship with the entry class for IO
234  friend class entry;
235 
236 
237  // Declare name of the class and its debug switch
238  ClassName("dictionary");
239 
240 
241  //- Null dictionary
242  static const dictionary null;
243 
244 
245  // Constructors
246 
247  //- Construct top-level dictionary null
248  dictionary();
249 
250  //- Construct top-level empty dictionary with given name
251  dictionary(const fileName& name);
252 
253  //- Construct given the entry name, parent dictionary and Istream,
254  // reading entries until lastEntry or EOF
255  dictionary
256  (
257  const fileName& name,
258  const dictionary& parentDict,
259  Istream&
260  );
261 
262  //- Construct top-level dictionary from Istream,
263  // reading entries until EOF, optionally keeping the header
264  dictionary(Istream&, const bool keepHeader=false);
265 
266  //- Construct as copy given the parent dictionary
267  dictionary(const dictionary& parentDict, const dictionary&);
268 
269  //- Construct top-level dictionary as copy
270  dictionary(const dictionary&);
271 
272  //- Construct top-level dictionary as copy from pointer to dictionary.
273  // A null pointer is treated like an empty dictionary.
274  dictionary(const dictionary*);
275 
276  //- Move constructor transferring parameter contents
277  // given parent dictionary
278  dictionary(const dictionary& parentDict, dictionary&&);
279 
280  //- Move constructor
282 
283  //- Construct and return clone
284  autoPtr<dictionary> clone() const;
285 
286  //- Construct top-level dictionary on freestore from Istream
288 
289 
290  //- Destructor
291  virtual ~dictionary();
292 
293 
294  // Member Functions
295 
296  //- Return the parent dictionary
297  const dictionary& parent() const
298  {
299  return parent_;
300  }
301 
302  //- Return whether this dictionary is null
303  bool isNull() const
304  {
305  return this == &null;
306  }
307 
308  //- Return the top of the tree
309  const dictionary& topDict() const;
310 
311  //- Return line number of first token in dictionary
312  label startLineNumber() const;
313 
314  //- Return line number of last token in dictionary
315  label endLineNumber() const;
316 
317  //- Return the SHA1 digest of the dictionary contents
318  SHA1Digest digest() const;
319 
320  //- Return the dictionary as a list of tokens
321  tokenList tokens() const;
322 
323 
324  // Search and lookup
325 
326  //- Search dictionary for given keyword
327  // If recursive, search parent dictionaries
328  // If patternMatch, use regular expressions
329  bool found
330  (
331  const word&,
332  bool recursive=false,
333  bool patternMatch = true
334  ) const;
335 
336  //- Find and return an entry data stream pointer if present
337  // otherwise return nullptr.
338  // If recursive, search parent dictionaries.
339  // If patternMatch, use regular expressions
340  const entry* lookupEntryPtr
341  (
342  const word&,
343  bool recursive,
344  bool patternMatch
345  ) const;
346 
347  //- Find and return an entry data stream pointer for manipulation
348  // if present otherwise return nullptr.
349  // If recursive, search parent dictionaries.
350  // If patternMatch, use regular expressions.
351  entry* lookupEntryPtr
352  (
353  const word&,
354  bool recursive,
355  bool patternMatch
356  );
357 
358  //- Find and return an entry data stream if present otherwise error.
359  // If recursive, search parent dictionaries.
360  // If patternMatch, use regular expressions.
361  const entry& lookupEntry
362  (
363  const word&,
364  bool recursive,
365  bool patternMatch
366  ) const;
367 
368  //- Find and return an entry data stream
369  // If recursive, search parent dictionaries.
370  // If patternMatch, use regular expressions.
372  (
373  const word&,
374  bool recursive=false,
375  bool patternMatch=true
376  ) const;
377 
378  //- Find and return a T,
379  // if not found throw a fatal error.
380  // If recursive, search parent dictionaries.
381  // If patternMatch, use regular expressions.
382  template<class T>
383  T lookup
384  (
385  const word&,
386  bool recursive=false,
387  bool patternMatch=true
388  ) const;
389 
390  //- Find and return a T,
391  // if not found return the given default value.
392  // If recursive, search parent dictionaries.
393  // If patternMatch, use regular expressions.
394  template<class T>
395  T lookupOrDefault
396  (
397  const word&,
398  const T&,
399  bool recursive=false,
400  bool patternMatch=true
401  ) const;
402 
403  //- Find and return a T, if not found return the given
404  // default value, and add to dictionary.
405  // If recursive, search parent dictionaries.
406  // If patternMatch, use regular expressions.
407  template<class T>
408  T lookupOrAddDefault
409  (
410  const word&,
411  const T&,
412  bool recursive=false,
413  bool patternMatch=true
414  );
415 
416  //- Find an entry if present, and assign to T
417  // Returns true if the entry was found.
418  // If recursive, search parent dictionaries.
419  // If patternMatch, use regular expressions.
420  template<class T>
421  bool readIfPresent
422  (
423  const word&,
424  T&,
425  bool recursive=false,
426  bool patternMatch=true
427  ) const;
428 
429  //- Find and return an entry data stream pointer if present
430  // otherwise return nullptr. Allows scoping using '/' with
431  // special handling for '!' and '..'.
432  const entry* lookupScopedEntryPtr
433  (
434  const word&,
435  bool recursive,
436  bool patternMatch
437  ) const;
438 
439  //- Check if entry is a sub-dictionary
440  bool isDict(const word&) const;
441 
442  //- Find and return a sub-dictionary pointer if present
443  // otherwise return nullptr.
444  const dictionary* subDictPtr(const word&) const;
445 
446  //- Find and return a sub-dictionary pointer if present
447  // otherwise return nullptr.
448  dictionary* subDictPtr(const word&);
449 
450  //- Find and return a sub-dictionary
451  const dictionary& subDict(const word&) const;
452 
453  //- Find and return a sub-dictionary for manipulation
454  dictionary& subDict(const word&);
455 
456  //- Find and return a sub-dictionary as a copy, or
457  // return an empty dictionary if the sub-dictionary does not exist
458  dictionary subOrEmptyDict
459  (
460  const word&,
461  const bool mustRead = false
462  ) const;
463 
464  //- Find and return a sub-dictionary if found
465  // otherwise return this dictionary
466  const dictionary& optionalSubDict(const word&) const;
467 
468  //- Return the table of contents
469  wordList toc() const;
470 
471  //- Return the sorted table of contents
472  wordList sortedToc() const;
473 
474  //- Return the list of available keys or patterns
475  List<keyType> keys(bool patterns=false) const;
476 
477 
478  // Editing
479 
480  //- Substitute the given keyword prepended by '$' with the
481  // corresponding sub-dictionary entries
482  bool substituteKeyword(const word& keyword);
483 
484  //- Substitute the given scoped keyword prepended by '$' with the
485  // corresponding sub-dictionary entries
486  bool substituteScopedKeyword(const word& keyword);
487 
488  //- Add a new entry
489  // With the merge option, dictionaries are interwoven and
490  // primitive entries are overwritten
491  bool add(entry*, bool mergeEntry=false);
492 
493  //- Add an entry
494  // With the merge option, dictionaries are interwoven and
495  // primitive entries are overwritten
496  void add(const entry&, bool mergeEntry=false);
497 
498  //- Add a word entry
499  // optionally overwrite an existing entry
500  void add(const keyType&, const word&, bool overwrite=false);
501 
502  //- Add a string entry
503  // optionally overwrite an existing entry
504  void add(const keyType&, const string&, bool overwrite=false);
505 
506  //- Add a label entry
507  // optionally overwrite an existing entry
508  void add(const keyType&, const label, bool overwrite=false);
509 
510  //- Add a scalar entry
511  // optionally overwrite an existing entry
512  void add(const keyType&, const scalar, bool overwrite=false);
513 
514  //- Add a dictionary entry
515  // optionally merge with an existing sub-dictionary
516  void add
517  (
518  const keyType&,
519  const dictionary&,
520  bool mergeEntry=false
521  );
522 
523  //- Add a T entry
524  // optionally overwrite an existing entry
525  template<class T>
526  void add(const keyType&, const T&, bool overwrite=false);
527 
528  //- Assign a new entry, overwrite any existing entry
529  void set(entry*);
530 
531  //- Assign a new entry, overwrite any existing entry
532  void set(const entry&);
533 
534  //- Assign a dictionary entry, overwrite any existing entry
535  void set(const keyType&, const dictionary&);
536 
537  //- Assign a T entry, overwrite any existing entry
538  template<class T>
539  void set(const keyType&, const T&);
540 
541  //- Remove an entry specified by keyword
542  bool remove(const word&);
543 
544  //- Change the keyword for an entry,
545  // optionally forcing overwrite of an existing entry
546  bool changeKeyword
547  (
548  const keyType& oldKeyword,
549  const keyType& newKeyword,
550  bool forceOverwrite=false
551  );
552 
553  //- Merge entries from the given dictionary.
554  // Also merge sub-dictionaries as required.
555  bool merge(const dictionary&);
556 
557  //- Clear the dictionary
558  void clear();
559 
560  //- Transfer the contents of the argument and annul the argument.
561  void transfer(dictionary&);
562 
563 
564  // Read
565 
566  //- Read dictionary from Istream, optionally keeping the header
567  bool read(Istream&, const bool keepHeader=false);
568 
569 
570  // Write
571 
572  //- Write dictionary, normally with sub-dictionary formatting
573  void write(Ostream&, const bool subDict=true) const;
574 
575 
576  // Member Operators
577 
578  //- Find and return entry
579  ITstream& operator[](const word&) const;
580 
581  void operator=(const dictionary&);
582 
583  void operator=(dictionary&&);
584 
585  //- Include entries from the given dictionary.
586  // Warn, but do not overwrite existing entries.
587  void operator+=(const dictionary&);
588 
589  //- Conditionally include entries from the given dictionary.
590  // Do not overwrite existing entries.
591  void operator|=(const dictionary&);
592 
593  //- Unconditionally include entries from the given dictionary.
594  // Overwrite existing entries.
595  void operator<<=(const dictionary&);
596 
597 
598  // IOstream Operators
599 
600  //- Read dictionary from Istream
601  friend Istream& operator>>(Istream&, dictionary&);
602 
603  //- Write dictionary to Ostream
604  friend Ostream& operator<<(Ostream&, const dictionary&);
605 };
606 
607 
608 // Global Operators
609 
610 //- Combine dictionaries.
611 // Starting from the entries in dict1 and then including those from dict2.
612 // Warn, but do not overwrite the entries from dict1.
613 dictionary operator+(const dictionary& dict1, const dictionary& dict2);
614 
615 //- Combine dictionaries.
616 // Starting from the entries in dict1 and then including those from dict2.
617 // Do not overwrite the entries from dict1.
618 dictionary operator|(const dictionary& dict1, const dictionary& dict2);
619 
620 
621 // Global Functions
622 
623 //- Write a dictionary entry
624 void writeEntry(Ostream& os, const dictionary& dict);
625 
626 //- Helper function to write the keyword and entry
627 template<class EntryType>
628 void writeEntry(Ostream& os, const word& entryName, const EntryType& value);
629 
630 //- Helper function to write the keyword and entry only if the
631 // values are not equal. The value is then output as value2
632 template<class EntryType>
634 (
635  Ostream& os,
636  const word& entryName,
637  const EntryType& value1,
638  const EntryType& value2
639 );
640 
641 
642 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
643 
644 } // End namespace Foam
645 
646 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
647 
648 #ifdef NoRepository
649  #include "dictionaryTemplates.C"
650 #endif
651 
652 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
653 
654 #endif
655 
656 // ************************************************************************* //
Template class for intrusive linked lists.
Definition: ILList.H:50
A class for handling keywords in dictionaries.
Definition: keyType.H:66
dictionary dict
HashSet< Key, Hash > operator|(const HashSet< Key, Hash > &hash1, const HashSet< Key, Hash > &hash2)
Combine entries from HashSets.
tUEqn clear()
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:79
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
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
Template class for non-intrusive linked lists.
Definition: LList.H:51
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:92
void writeEntryIfDifferent(Ostream &os, const word &entryName, const EntryType &value1, const EntryType &value2)
Helper function to write the keyword and entry only if the.
An STL-conforming const_iterator.
Definition: LList.H:297
The SHA1 message digest.
Definition: SHA1Digest.H:62
An STL-conforming iterator.
Definition: LList.H:246
An STL-conforming const_iterator.
Definition: UILList.H:234
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
const word dictName() const
Return the local dictionary name (final part of scoped name)
Definition: dictionary.H:123
#define ClassName(TypeNameString)
Add typeName information from argument TypeNameString to a class.
Definition: className.H:65
bool read(const char *, int32_t &)
Definition: int32IO.C:85
stressControl lookup("compactNormalStress") >> compactNormalStress
T clone(const T &t)
Definition: List.H:54
bool isNull(const T &t)
Return true if t is a reference to the nullObject of type T.
Definition: nullObjectI.H:46
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:111
Intrusive doubly-linked list.
A class for handling words, derived from string.
Definition: word.H:59
Istream & operator>>(Istream &, directionInfo &)
word name() const
Return file name (part beyond last /)
Definition: fileName.C:183
An STL-conforming iterator.
Definition: UILList.H:185
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
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:54
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)
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
void operator=(const dictionaryName &name)
Definition: dictionary.H:142
Macro definitions for declaring ClassName(), NamespaceName(), etc.
Ostream & operator<<(Ostream &, const ensightPart &)
Non-intrusive doubly-linked list.
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
bool found
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