1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
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:
33 - pattern match (in reverse order)
34 - optional recursion into the enclosing (parent) dictionaries
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.
42 A merge() member function with a non-const dictionary parameter.
43 This would avoid unnecessary cloning in the add(entry*, bool) method.
49 \*---------------------------------------------------------------------------*/
59 #include "HashTable.H"
61 #include "className.H"
63 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
68 // Forward declaration of friend functions and operators
73 Istream& operator>>(Istream&, dictionary&);
74 Ostream& operator<<(Ostream&, const dictionary&);
76 /*---------------------------------------------------------------------------*\
77 Class dictionaryName Declaration
78 \*---------------------------------------------------------------------------*/
91 //- Construct dictionaryName null
95 //- Construct dictionaryName as copy of the given fileName
96 dictionaryName(const fileName& name)
104 //- Return the dictionary name
105 const fileName& name() const
110 //- Return the dictionary name
118 /*---------------------------------------------------------------------------*\
119 Class dictionary Declaration
120 \*---------------------------------------------------------------------------*/
124 public dictionaryName,
125 public IDLList<entry>
129 //- HashTable of the entries held on the DL-list for quick lookup
130 HashTable<entry*> hashedEntries_;
132 //- Parent dictionary
133 const dictionary& parent_;
135 //- Entries of matching patterns
136 DLList<entry*> patternEntries_;
138 //- Patterns as precompiled regular expressions
139 DLList<autoPtr<regExp> > patternRegexps_;
142 // Private Member Functions
144 //- Search patterns table for exact match or regular expression match
147 const bool patternMatch,
149 DLList<entry*>::const_iterator& wcLink,
150 DLList<autoPtr<regExp> >::const_iterator& reLink
153 //- Search patterns table for exact match or regular expression match
156 const bool patternMatch,
158 DLList<entry*>::iterator& wcLink,
159 DLList<autoPtr<regExp> >::iterator& reLink
165 //- Declare friendship with the entry class for IO
169 // Declare name of the class and its debug switch
170 ClassName("dictionary");
174 static const dictionary null;
179 //- Construct top-level dictionary null
182 //- Construct top-level empty dictionary with given name
183 dictionary(const fileName& name);
185 //- Construct given the entry name, parent dictionary and Istream,
186 // reading entries until lastEntry or EOF
189 const fileName& name,
190 const dictionary& parentDict,
194 //- Construct top-level dictionary from Istream, reading entries
196 dictionary(Istream&);
198 //- Construct as copy given the parent dictionary
199 dictionary(const dictionary& parentDict, const dictionary&);
201 //- Construct top-level dictionary as copy
202 dictionary(const dictionary&);
204 //- Construct top-level dictionary as copy from pointer to dictionary.
205 // A null pointer is treated like an empty dictionary.
206 dictionary(const dictionary*);
208 //- Construct by transferring parameter contents from
209 // given parent dictionary
210 dictionary(const dictionary& parentDict, const Xfer<dictionary>&);
212 //- Construct top-level dictionary by transferring parameter contents
213 dictionary(const Xfer<dictionary>&);
215 //- Construct and return clone
216 autoPtr<dictionary> clone() const;
218 //- Construct top-level dictionary on freestore from Istream
219 static autoPtr<dictionary> New(Istream&);
229 //- Return the parent dictionary
230 const dictionary& parent() const
235 //- Return line number of first token in dictionary
236 label startLineNumber() const;
238 //- Return line number of last token in dictionary
239 label endLineNumber() const;
241 //- Return the SHA1 digest of the dictionary contents
242 SHA1Digest digest() const;
247 //- Search dictionary for given keyword
248 // If recursive, search parent dictionaries
249 bool found(const word&, bool recursive = false) const;
251 //- Find and return an entry data stream pointer if present
252 // otherwise return NULL.
253 // If recursive, search parent dictionaries.
254 // If patternMatch, use regular expressions
255 const entry* lookupEntryPtr
262 //- Find and return an entry data stream pointer for manipulation
263 // if present otherwise return NULL.
264 // If recursive, search parent dictionaries.
265 // If patternMatch, use regular expressions.
266 entry* lookupEntryPtr
273 //- Find and return an entry data stream
274 // if present otherwise error.
275 // If recursive, search parent dictionaries.
276 // If patternMatch, use regular expressions.
277 const entry& lookupEntry
284 //- Find and return an entry data stream
285 // If recursive, search parent dictionaries.
286 // If patternMatch, use regular expressions.
290 bool recursive = false,
291 bool patternMatch = true
294 //- Find and return a T,
295 // if not found return the given default value
296 // If recursive, search parent dictionaries.
297 // If patternMatch, use regular expressions.
303 bool recursive = false,
304 bool patternMatch = true
307 //- Find and return a T, if not found return the given
308 // default value, and add to dictionary.
309 // If recursive, search parent dictionaries.
310 // If patternMatch, use regular expressions.
316 bool recursive = false,
317 bool patternMatch = true
320 //- Find an entry if present, and assign to T
321 // Returns true if the entry was found.
322 // If recursive, search parent dictionaries.
323 // If patternMatch, use regular expressions.
329 bool recursive = false,
330 bool patternMatch = true
333 //- Check if entry is a sub-dictionary
334 bool isDict(const word&) const;
336 //- Find and return a sub-dictionary pointer if present
337 // otherwise return NULL.
338 const dictionary* subDictPtr(const word&) const;
340 //- Find and return a sub-dictionary
341 const dictionary& subDict(const word&) const;
343 //- Find and return a sub-dictionary for manipulation
344 dictionary& subDict(const word&);
346 //- Find and return a sub-dictionary as a copy, or
347 // return an empty dictionary if the sub-dictionary does not exist
348 dictionary subOrEmptyDict(const word&) const;
350 //- Return the table of contents
351 wordList toc() const;
353 //- Return the list of available keys or patterns
354 List<keyType> keys(bool patterns = false) const;
359 //- Substitute the given keyword prepended by '$' with the
360 // corresponding sub-dictionary entries
361 bool substituteKeyword(const word& keyword);
364 // With the merge option, dictionaries are interwoven and
365 // primitive entries are overwritten
366 bool add(entry*, bool mergeEntry = false);
369 // With the merge option, dictionaries are interwoven and
370 // primitive entries are overwritten
371 void add(const entry&, bool mergeEntry = false);
374 // optionally overwrite an existing entry
375 void add(const keyType&, const word&, bool overwrite = false);
377 //- Add a string entry
378 // optionally overwrite an existing entry
379 void add(const keyType&, const string&, bool overwrite = false);
381 //- Add a label entry
382 // optionally overwrite an existing entry
383 void add(const keyType&, const label, bool overwrite = false);
385 //- Add a scalar entry
386 // optionally overwrite an existing entry
387 void add(const keyType&, const scalar, bool overwrite = false);
389 //- Add a dictionary entry
390 // optionally merge with an existing sub-dictionary
395 bool mergeEntry = false
399 // optionally overwrite an existing entry
401 void add(const keyType&, const T&, bool overwrite = false);
403 //- Assign a new entry, overwrite any existing entry
406 //- Assign a new entry, overwrite any existing entry
407 void set(const entry&);
409 //- Assign a dictionary entry, overwrite any existing entry
410 void set(const keyType&, const dictionary&);
412 //- Assign a T entry, overwrite any existing entry
414 void set(const keyType&, const T&);
416 //- Remove an entry specified by keyword
417 bool remove(const word&);
419 //- Change the keyword for an entry,
420 // optionally forcing overwrite of an existing entry
423 const keyType& oldKeyword,
424 const keyType& newKeyword,
425 bool forceOverwrite = false
428 //- Merge entries from the given dictionary.
429 // Also merge sub-dictionaries as required.
430 bool merge(const dictionary&);
432 //- Clear the dictionary
435 //- Transfer the contents of the argument and annul the argument.
436 void transfer(dictionary&);
438 //- Transfer contents to the Xfer container
439 Xfer<dictionary> xfer();
444 //- Read dictionary from Istream
450 void write(Ostream&, bool subDict = true) const;
455 //- Find and return entry
456 ITstream& operator[](const word&) const;
458 void operator=(const dictionary&);
460 //- Include entries from the given dictionary.
461 // Warn, but do not overwrite existing entries.
462 void operator+=(const dictionary&);
464 //- Conditionally include entries from the given dictionary.
465 // Do not overwrite existing entries.
466 void operator|=(const dictionary&);
468 //- Unconditionally include entries from the given dictionary.
469 // Overwrite existing entries.
470 void operator<<=(const dictionary&);
473 // IOstream operators
475 //- Read dictionary from Istream
476 friend Istream& operator>>(Istream&, dictionary&);
478 //- Write dictionary to Ostream
479 friend Ostream& operator<<(Ostream&, const dictionary&);
485 //- Combine dictionaries.
486 // Starting from the entries in dict1 and then including those from dict2.
487 // Warn, but do not overwrite the entries from dict1.
488 dictionary operator+(const dictionary& dict1, const dictionary& dict2);
490 //- Combine dictionaries.
491 // Starting from the entries in dict1 and then including those from dict2.
492 // Do not overwrite the entries from dict1.
493 dictionary operator|(const dictionary& dict1, const dictionary& dict2);
496 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
498 } // End namespace Foam
500 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
503 # include "dictionaryTemplates.C"
506 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
510 // ************************************************************************* //