OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

dictionary.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     Foam::dictionary
00027 
00028 Description
00029     A list of keyword definitions, which are a keyword followed by any number
00030     of values (e.g. words and numbers). The keywords can represent patterns
00031     which are matched using Posix regular expressions. The general order for
00032     searching is as follows:
00033     - exact match
00034     - pattern match (in reverse order)
00035     - optional recursion into the enclosing (parent) dictionaries
00036 
00037     The dictionary class is the base class for IOdictionary.
00038     It also serves as a bootstrap dictionary for the objectRegistry data
00039     dictionaries since, unlike the IOdictionary class, it does not use an
00040     objectRegistry itself to work.
00041 
00042 ToDo
00043     A merge() member function with a non-const dictionary parameter.
00044     This would avoid unnecessary cloning in the add(entry*,bool) method
00045 
00046 SourceFiles
00047     dictionary.C
00048     dictionaryIO.C
00049 
00050 \*---------------------------------------------------------------------------*/
00051 
00052 #ifndef dictionary_H
00053 #define dictionary_H
00054 
00055 #include "entry.H"
00056 #include "IDLList.H"
00057 #include "DLList.H"
00058 #include "fileName.H"
00059 #include "ITstream.H"
00060 #include "HashTable.H"
00061 #include "wordList.H"
00062 #include "className.H"
00063 
00064 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00065 
00066 namespace Foam
00067 {
00068 
00069 // Forward declaration of friend functions and operators
00070 class regExp;
00071 class dictionary;
00072 class SHA1Digest;
00073 
00074 Istream& operator>>(Istream&, dictionary&);
00075 Ostream& operator<<(Ostream&, const dictionary&);
00076 
00077 
00078 /*---------------------------------------------------------------------------*\
00079                            Class dictionary Declaration
00080 \*---------------------------------------------------------------------------*/
00081 
00082 class dictionary
00083 :
00084     public IDLList<entry>
00085 {
00086     // Private data
00087 
00088         //- Dictionary name
00089         fileName name_;
00090 
00091         //- HashTable of the entries held on the DL-list for quick lookup
00092         HashTable<entry*> hashedEntries_;
00093 
00094         //- Parent dictionary
00095         const dictionary& parent_;
00096 
00097         //- Entries of matching patterns
00098         DLList<entry*> patternEntries_;
00099 
00100         //- Patterns as precompiled regular expressions
00101         DLList<autoPtr<regExp> > patternRegexps_;
00102 
00103    // Private Member Functions
00104 
00105         //- Search patterns table for exact match or regular expression match
00106         bool findInPatterns
00107         (
00108             const bool patternMatch,
00109             const word& Keyword,
00110             DLList<entry*>::const_iterator& wcLink,
00111             DLList<autoPtr<regExp> >::const_iterator& reLink
00112         ) const;
00113 
00114         //- Search patterns table for exact match or regular expression match
00115         bool findInPatterns
00116         (
00117             const bool patternMatch,
00118             const word& Keyword,
00119             DLList<entry*>::iterator& wcLink,
00120             DLList<autoPtr<regExp> >::iterator& reLink
00121         );
00122 
00123 
00124 public:
00125 
00126         //- Read dictionary from Istream
00127         bool read(Istream&);
00128 
00129         //- Substitute the given keyword prepended by '$' with the
00130         //  corresponding sub-dictionary entries
00131         bool substituteKeyword(const word& keyword);
00132 
00133 
00134 public:
00135 
00136     //- Declare friendship with the entry class for IO
00137     friend class entry;
00138 
00139 
00140     // Declare name of the class and its debug switch
00141     ClassName("dictionary");
00142 
00143 
00144     //- Null dictionary
00145     static const dictionary null;
00146 
00147 
00148     // Constructors
00149 
00150         //- Construct top-level dictionary null
00151         dictionary();
00152 
00153         //- Construct from the parent dictionary and Istream, reading entries
00154         //  until lastEntry or EOF
00155         dictionary
00156         (
00157             const dictionary& parentDict,
00158             Istream&
00159         );
00160 
00161         //- Construct top-level dictionary from Istream, reading entries
00162         //  until EOF
00163         dictionary(Istream&);
00164 
00165         //- Construct as copy given the parent dictionary
00166         dictionary(const dictionary& parentDict, const dictionary&);
00167 
00168         //- Construct top-level dictionary as copy
00169         dictionary(const dictionary&);
00170 
00171         //- Construct top-level dictionary as copy from pointer to dictionary.
00172         //  A null pointer is treated like an empty dictionary.
00173         dictionary(const dictionary*);
00174 
00175         //- Construct by transferring parameter contents given parent dictionary
00176         dictionary(const dictionary& parentDict, const Xfer<dictionary>&);
00177 
00178         //- Construct top-level dictionary by transferring parameter contents
00179         dictionary(const Xfer<dictionary>&);
00180 
00181         //- Construct and return clone
00182         autoPtr<dictionary> clone() const;
00183 
00184         //- Construct top-level dictionary on freestore from Istream
00185         static autoPtr<dictionary> New(Istream&);
00186 
00187 
00188     // Destructor
00189 
00190         ~dictionary();
00191 
00192 
00193     // Member functions
00194 
00195         //- Return the dictionary name
00196         const fileName& name() const
00197         {
00198             return name_;
00199         }
00200 
00201         //- Return the dictionary name
00202         fileName& name()
00203         {
00204             return name_;
00205         }
00206 
00207         //- Return the parent dictionary
00208         const dictionary& parent() const
00209         {
00210             return parent_;
00211         }
00212 
00213         //- Return line number of first token in dictionary
00214         label startLineNumber() const;
00215 
00216         //- Return line number of last token in dictionary
00217         label endLineNumber() const;
00218 
00219         //- Return the SHA1 digest of the dictionary contents
00220         SHA1Digest digest() const;
00221 
00222 
00223         // Search and lookup
00224 
00225             //- Search dictionary for given keyword
00226             //  If recursive, search parent dictionaries
00227             bool found(const word&, bool recursive=false) const;
00228 
00229             //- Find and return an entry data stream pointer if present
00230             //  otherwise return NULL.
00231             //  If recursive, search parent dictionaries.
00232             //  If patternMatch, use regular expressions
00233             const entry* lookupEntryPtr
00234             (
00235                 const word&,
00236                 bool recursive,
00237                 bool patternMatch
00238             ) const;
00239 
00240             //- Find and return an entry data stream pointer for manipulation
00241             //  if present otherwise return NULL.
00242             //  If recursive, search parent dictionaries.
00243             //  If patternMatch, use regular expressions.
00244             entry* lookupEntryPtr
00245             (
00246                 const word&,
00247                 bool recursive,
00248                 bool patternMatch
00249             );
00250 
00251             //- Find and return an entry data stream if present otherwise error.
00252             //  If recursive, search parent dictionaries.
00253             //  If patternMatch, use regular expressions.
00254             const entry& lookupEntry
00255             (
00256                 const word&,
00257                 bool recursive,
00258                 bool patternMatch
00259             ) const;
00260 
00261             //- Find and return an entry data stream
00262             //  If recursive, search parent dictionaries.
00263             //  If patternMatch, use regular expressions.
00264             ITstream& lookup
00265             (
00266                 const word&,
00267                 bool recursive=false,
00268                 bool patternMatch=true
00269             ) const;
00270 
00271             //- Find and return a T,
00272             //  if not found return the given default value
00273             //  If recursive, search parent dictionaries.
00274             //  If patternMatch, use regular expressions.
00275             template<class T>
00276             T lookupOrDefault
00277             (
00278                 const word&,
00279                 const T&,
00280                 bool recursive=false,
00281                 bool patternMatch=true
00282             ) const;
00283 
00284             //- Find and return a T, if not found return the given
00285             //  default value, and add to dictionary.
00286             //  If recursive, search parent dictionaries.
00287             //  If patternMatch, use regular expressions.
00288             template<class T>
00289             T lookupOrAddDefault
00290             (
00291                 const word&,
00292                 const T&,
00293                 bool recursive=false,
00294                 bool patternMatch=true
00295             );
00296 
00297             //- Find an entry if present, and assign to T
00298             //  Returns true if the entry was found.
00299             //  If recursive, search parent dictionaries.
00300             //  If patternMatch, use regular expressions.
00301             template<class T>
00302             bool readIfPresent
00303             (
00304                 const word&,
00305                 T&,
00306                 bool recursive=false,
00307                 bool patternMatch=true
00308             ) const;
00309 
00310             //- Check if entry is a sub-dictionary
00311             bool isDict(const word&) const;
00312 
00313             //- Find and return a sub-dictionary pointer if present
00314             //  otherwise return NULL.
00315             const dictionary* subDictPtr(const word&) const;
00316 
00317             //- Find and return a sub-dictionary
00318             const dictionary& subDict(const word&) const;
00319 
00320             //- Find and return a sub-dictionary for manipulation
00321             dictionary& subDict(const word&);
00322 
00323             //- Return the table of contents
00324             wordList toc() const;
00325 
00326             //- Return the list of available keys or patterns
00327             List<keyType> keys(bool patterns=false) const;
00328 
00329         // Editing
00330 
00331             //- Add a new entry
00332             //  With the merge option, dictionaries are interwoven and
00333             //  primitive entries are overwritten
00334             bool add(entry*, bool mergeEntry=false);
00335 
00336             //- Add an entry
00337             //  With the merge option, dictionaries are interwoven and
00338             //  primitive entries are overwritten
00339             void add(const entry&, bool mergeEntry=false);
00340 
00341             //- Add a word entry
00342             //  optionally overwrite an existing entry
00343             void add(const keyType&, const word&, bool overwrite=false);
00344 
00345             //- Add a string entry
00346             //  optionally overwrite an existing entry
00347             void add(const keyType&, const string&, bool overwrite=false);
00348 
00349             //- Add a label entry
00350             //  optionally overwrite an existing entry
00351             void add(const keyType&, const label, bool overwrite=false);
00352 
00353             //- Add a scalar entry
00354             //  optionally overwrite an existing entry
00355             void add(const keyType&, const scalar, bool overwrite=false);
00356 
00357             //- Add a dictionary entry
00358             //  optionally merge with an existing sub-dictionary
00359             void add
00360             (
00361                 const keyType&,
00362                 const dictionary&,
00363                 bool mergeEntry=false
00364             );
00365 
00366             //- Add a T entry
00367             //  optionally overwrite an existing entry
00368             template<class T>
00369             void add(const keyType&, const T&, bool overwrite=false);
00370 
00371             //- Assign a new entry, overwrite any existing entry
00372             void set(entry*);
00373 
00374             //- Assign a new entry, overwrite any existing entry
00375             void set(const entry&);
00376 
00377             //- Assign a dictionary entry, overwrite any existing entry
00378             void set(const keyType&, const dictionary&);
00379 
00380             //- Assign a T entry, overwrite any existing entry
00381             template<class T>
00382             void set(const keyType&, const T&);
00383 
00384             //- Remove an entry specified by keyword
00385             bool remove(const word&);
00386 
00387             //- Change the keyword for an entry,
00388             //  optionally forcing overwrite of an existing entry
00389             bool changeKeyword
00390             (
00391                 const keyType& oldKeyword,
00392                 const keyType& newKeyword,
00393                 bool forceOverwrite=false
00394             );
00395 
00396             //- Merge entries from the given dictionary.
00397             //  Also merge sub-dictionaries as required.
00398             bool merge(const dictionary&);
00399 
00400             //- Clear the dictionary
00401             void clear();
00402 
00403             //- Transfer the contents of the argument and annull the argument.
00404             void transfer(dictionary&);
00405 
00406             //- Transfer contents to the Xfer container
00407             Xfer<dictionary> xfer();
00408 
00409 
00410         // Write
00411 
00412             void write(Ostream&, bool subDict=true) const;
00413 
00414 
00415     // Member Operators
00416 
00417         //- Find and return entry
00418         ITstream& operator[](const word&) const;
00419 
00420         void operator=(const dictionary&);
00421 
00422         //- Include entries from the given dictionary.
00423         //  Warn, but do not overwrite existing entries.
00424         void operator+=(const dictionary&);
00425 
00426         //- Conditionally include entries from the given dictionary.
00427         //  Do not overwrite existing entries.
00428         void operator|=(const dictionary&);
00429 
00430         //- Unconditionally include entries from the given dictionary.
00431         //  Overwrite existing entries.
00432         void operator<<=(const dictionary&);
00433 
00434 
00435     // IOstream operators
00436 
00437         //- Read dictionary from Istream
00438         friend Istream& operator>>(Istream&, dictionary&);
00439 
00440         //- Write dictionary to Ostream
00441         friend Ostream& operator<<(Ostream&, const dictionary&);
00442 };
00443 
00444 
00445 // Global Operators
00446 
00447 //- Combine dictionaries.
00448 //  Starting from the entries in dict1 and then including those from dict2.
00449 //  Warn, but do not overwrite the entries from dict1.
00450 dictionary operator+(const dictionary& dict1, const dictionary& dict2);
00451 
00452 //- Combine dictionaries.
00453 //  Starting from the entries in dict1 and then including those from dict2.
00454 //  Do not overwrite the entries from dict1.
00455 dictionary operator|(const dictionary& dict1, const dictionary& dict2);
00456 
00457 
00458 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00459 
00460 } // End namespace Foam
00461 
00462 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00463 
00464 #ifdef NoRepository
00465 #   include "dictionaryTemplates.C"
00466 #endif
00467 
00468 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00469 
00470 #endif
00471 
00472 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd