1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
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.
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.
48 \*---------------------------------------------------------------------------*/
58 #include "HashTable.H"
60 #include "className.H"
62 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
67 // Forward declaration of friend functions and operators
72 Istream& operator>>(Istream&, dictionary&);
73 Ostream& operator<<(Ostream&, const dictionary&);
75 /*---------------------------------------------------------------------------*\
76 Class dictionaryName Declaration
77 \*---------------------------------------------------------------------------*/
90 //- Construct dictionaryName null
94 //- Construct dictionaryName as copy of the given fileName
95 dictionaryName(const fileName& name)
103 //- Return the dictionary name
104 const fileName& name() const
109 //- Return the dictionary name
115 //- Return the local dictionary name (final part of scoped name)
116 const word dictName() const
118 const word scopedName = name_.name();
120 string::size_type i = scopedName.rfind(':');
122 if (i == scopedName.npos)
128 return scopedName.substr(i + 1, scopedName.npos);
134 /*---------------------------------------------------------------------------*\
135 Class dictionary Declaration
136 \*---------------------------------------------------------------------------*/
140 public dictionaryName,
141 public IDLList<entry>
145 //- HashTable of the entries held on the DL-list for quick lookup
146 HashTable<entry*> hashedEntries_;
148 //- Parent dictionary
149 const dictionary& parent_;
151 //- Entries of matching patterns
152 DLList<entry*> patternEntries_;
154 //- Patterns as precompiled regular expressions
155 DLList<autoPtr<regExp> > patternRegexps_;
158 // Private Member Functions
160 //- Search patterns table for exact match or regular expression match
163 const bool patternMatch,
165 DLList<entry*>::const_iterator& wcLink,
166 DLList<autoPtr<regExp> >::const_iterator& reLink
169 //- Search patterns table for exact match or regular expression match
172 const bool patternMatch,
174 DLList<entry*>::iterator& wcLink,
175 DLList<autoPtr<regExp> >::iterator& reLink
181 //- Declare friendship with the entry class for IO
185 // Declare name of the class and its debug switch
186 ClassName("dictionary");
190 static const dictionary null;
195 //- Construct top-level dictionary null
198 //- Construct top-level empty dictionary with given name
199 dictionary(const fileName& name);
201 //- Construct given the entry name, parent dictionary and Istream,
202 // reading entries until lastEntry or EOF
205 const fileName& name,
206 const dictionary& parentDict,
210 //- Construct top-level dictionary from Istream,
211 // reading entries until EOF
212 dictionary(Istream&);
214 //- Construct top-level dictionary from Istream,
215 // reading entries until EOF, optionally keeping the header
216 dictionary(Istream&, const bool keepHeader);
218 //- Construct as copy given the parent dictionary
219 dictionary(const dictionary& parentDict, const dictionary&);
221 //- Construct top-level dictionary as copy
222 dictionary(const dictionary&);
224 //- Construct top-level dictionary as copy from pointer to dictionary.
225 // A null pointer is treated like an empty dictionary.
226 dictionary(const dictionary*);
228 //- Construct by transferring parameter contents given parent dictionary
229 dictionary(const dictionary& parentDict, const Xfer<dictionary>&);
231 //- Construct top-level dictionary by transferring parameter contents
232 dictionary(const Xfer<dictionary>&);
234 //- Construct and return clone
235 autoPtr<dictionary> clone() const;
237 //- Construct top-level dictionary on freestore from Istream
238 static autoPtr<dictionary> New(Istream&);
242 virtual ~dictionary();
247 //- Return the parent dictionary
248 const dictionary& parent() const
253 //- Return line number of first token in dictionary
254 label startLineNumber() const;
256 //- Return line number of last token in dictionary
257 label endLineNumber() const;
259 //- Return the SHA1 digest of the dictionary contents
260 SHA1Digest digest() const;
265 //- Search dictionary for given keyword
266 // If recursive, search parent dictionaries
267 // If patternMatch, use regular expressions
271 bool recursive=false,
272 bool patternMatch = true
275 //- Find and return an entry data stream pointer if present
276 // otherwise return NULL.
277 // If recursive, search parent dictionaries.
278 // If patternMatch, use regular expressions
279 const entry* lookupEntryPtr
286 //- Find and return an entry data stream pointer for manipulation
287 // if present otherwise return NULL.
288 // If recursive, search parent dictionaries.
289 // If patternMatch, use regular expressions.
290 entry* lookupEntryPtr
297 //- Find and return an entry data stream if present otherwise error.
298 // If recursive, search parent dictionaries.
299 // If patternMatch, use regular expressions.
300 const entry& lookupEntry
307 //- Find and return an entry data stream
308 // If recursive, search parent dictionaries.
309 // If patternMatch, use regular expressions.
313 bool recursive=false,
314 bool patternMatch=true
317 //- Find and return a T,
318 // if not found return the given default value
319 // If recursive, search parent dictionaries.
320 // If patternMatch, use regular expressions.
326 bool recursive=false,
327 bool patternMatch=true
330 //- Find and return a T, if not found return the given
331 // default value, and add to dictionary.
332 // If recursive, search parent dictionaries.
333 // If patternMatch, use regular expressions.
339 bool recursive=false,
340 bool patternMatch=true
343 //- Find an entry if present, and assign to T
344 // Returns true if the entry was found.
345 // If recursive, search parent dictionaries.
346 // If patternMatch, use regular expressions.
352 bool recursive=false,
353 bool patternMatch=true
356 //- Check if entry is a sub-dictionary
357 bool isDict(const word&) const;
359 //- Find and return a sub-dictionary pointer if present
360 // otherwise return NULL.
361 const dictionary* subDictPtr(const word&) const;
363 //- Find and return a sub-dictionary
364 const dictionary& subDict(const word&) const;
366 //- Find and return a sub-dictionary for manipulation
367 dictionary& subDict(const word&);
369 //- Find and return a sub-dictionary as a copy, or
370 // return an empty dictionary if the sub-dictionary does not exist
371 dictionary subOrEmptyDict
374 const bool mustRead = false
377 //- Return the table of contents
378 wordList toc() const;
380 //- Return the list of available keys or patterns
381 List<keyType> keys(bool patterns=false) const;
386 //- Substitute the given keyword prepended by '$' with the
387 // corresponding sub-dictionary entries
388 bool substituteKeyword(const word& keyword);
391 // With the merge option, dictionaries are interwoven and
392 // primitive entries are overwritten
393 bool add(entry*, bool mergeEntry=false);
396 // With the merge option, dictionaries are interwoven and
397 // primitive entries are overwritten
398 void add(const entry&, bool mergeEntry=false);
401 // optionally overwrite an existing entry
402 void add(const keyType&, const word&, bool overwrite=false);
404 //- Add a string entry
405 // optionally overwrite an existing entry
406 void add(const keyType&, const string&, bool overwrite=false);
408 //- Add a label entry
409 // optionally overwrite an existing entry
410 void add(const keyType&, const label, bool overwrite=false);
412 //- Add a scalar entry
413 // optionally overwrite an existing entry
414 void add(const keyType&, const scalar, bool overwrite=false);
416 //- Add a dictionary entry
417 // optionally merge with an existing sub-dictionary
422 bool mergeEntry=false
426 // optionally overwrite an existing entry
428 void add(const keyType&, const T&, bool overwrite=false);
430 //- Assign a new entry, overwrite any existing entry
433 //- Assign a new entry, overwrite any existing entry
434 void set(const entry&);
436 //- Assign a dictionary entry, overwrite any existing entry
437 void set(const keyType&, const dictionary&);
439 //- Assign a T entry, overwrite any existing entry
441 void set(const keyType&, const T&);
443 //- Remove an entry specified by keyword
444 bool remove(const word&);
446 //- Change the keyword for an entry,
447 // optionally forcing overwrite of an existing entry
450 const keyType& oldKeyword,
451 const keyType& newKeyword,
452 bool forceOverwrite=false
455 //- Merge entries from the given dictionary.
456 // Also merge sub-dictionaries as required.
457 bool merge(const dictionary&);
459 //- Clear the dictionary
462 //- Transfer the contents of the argument and annul the argument.
463 void transfer(dictionary&);
465 //- Transfer contents to the Xfer container
466 Xfer<dictionary> xfer();
471 //- Read dictionary from Istream
474 //- Read dictionary from Istream, optionally keeping the header
475 bool read(Istream&, const bool keepHeader);
480 //- Write dictionary, normally with sub-dictionary formatting
481 void write(Ostream&, const bool subDict=true) const;
486 //- Find and return entry
487 ITstream& operator[](const word&) const;
489 void operator=(const dictionary&);
491 //- Include entries from the given dictionary.
492 // Warn, but do not overwrite existing entries.
493 void operator+=(const dictionary&);
495 //- Conditionally include entries from the given dictionary.
496 // Do not overwrite existing entries.
497 void operator|=(const dictionary&);
499 //- Unconditionally include entries from the given dictionary.
500 // Overwrite existing entries.
501 void operator<<=(const dictionary&);
504 // IOstream operators
506 //- Read dictionary from Istream
507 friend Istream& operator>>(Istream&, dictionary&);
509 //- Write dictionary to Ostream
510 friend Ostream& operator<<(Ostream&, const dictionary&);
516 //- Combine dictionaries.
517 // Starting from the entries in dict1 and then including those from dict2.
518 // Warn, but do not overwrite the entries from dict1.
519 dictionary operator+(const dictionary& dict1, const dictionary& dict2);
521 //- Combine dictionaries.
522 // Starting from the entries in dict1 and then including those from dict2.
523 // Do not overwrite the entries from dict1.
524 dictionary operator|(const dictionary& dict1, const dictionary& dict2);
527 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
529 } // End namespace Foam
531 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
534 # include "dictionaryTemplates.C"
537 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
541 // ************************************************************************* //