FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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