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

DictionaryBase.C

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 \*---------------------------------------------------------------------------*/
00026 
00027 #include "DictionaryBase.H"
00028 
00029 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00030 
00031 template<class IDLListType, class T>
00032 void Foam::DictionaryBase<IDLListType, T>::addEntries()
00033 {
00034     for
00035     (
00036         typename IDLListType::iterator iter = this->begin();
00037         iter != this->end();
00038         ++iter
00039     )
00040     {
00041         this->hashedTs_.insert((*iter).keyword(), &(*iter));
00042     }
00043 }
00044 
00045 
00046 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00047 
00048 template<class IDLListType, class T>
00049 Foam::DictionaryBase<IDLListType, T>::DictionaryBase()
00050 {}
00051 
00052 
00053 template<class IDLListType, class T>
00054 Foam::DictionaryBase<IDLListType, T>::DictionaryBase
00055 (
00056     const DictionaryBase& dict
00057 )
00058 :
00059     IDLListType(dict)
00060 {
00061     addEntries();
00062 }
00063 
00064 
00065 template<class IDLListType, class T>
00066 template<class INew>
00067 Foam::DictionaryBase<IDLListType, T>::DictionaryBase
00068 (
00069     Istream& is,
00070     const INew& iNew
00071 )
00072 :
00073     IDLListType(is, iNew)
00074 {
00075     addEntries();
00076 }
00077 
00078 
00079 template<class IDLListType, class T>
00080 Foam::DictionaryBase<IDLListType, T>::DictionaryBase(Istream& is)
00081 :
00082     IDLListType(is)
00083 {
00084     addEntries();
00085 }
00086 
00087 
00088 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00089 
00090 // Find and return T
00091 template<class IDLListType, class T>
00092 bool Foam::DictionaryBase<IDLListType, T>::found(const word& keyword) const
00093 {
00094     return hashedTs_.found(keyword);
00095 }
00096 
00097 
00098 // Find and return T*, return NULL if not found
00099 template<class IDLListType, class T>
00100 const T* Foam::DictionaryBase<IDLListType, T>::lookupPtr
00101 (
00102     const word& keyword
00103 ) const
00104 {
00105     typename HashTable<T*>::const_iterator iter = hashedTs_.find(keyword);
00106 
00107     if (iter != hashedTs_.end())
00108     {
00109         return *iter;
00110     }
00111     else
00112     {
00113         return NULL;
00114     }
00115 }
00116 
00117 
00118 // Find and return T*, return NULL if not found
00119 template<class IDLListType, class T>
00120 T* Foam::DictionaryBase<IDLListType, T>::lookupPtr(const word& keyword)
00121 {
00122     typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
00123 
00124     if (iter != hashedTs_.end())
00125     {
00126         return *iter;
00127     }
00128     else
00129     {
00130         return NULL;
00131     }
00132 }
00133 
00134 
00135 // Find and return T*, FatalError if keyword not found
00136 template<class IDLListType, class T>
00137 const T* Foam::DictionaryBase<IDLListType, T>::lookup(const word& keyword) const
00138 {
00139     typename HashTable<T*>::const_iterator iter = hashedTs_.find(keyword);
00140 
00141     if (iter == hashedTs_.end())
00142     {
00143         FatalErrorIn
00144         (
00145             "DictionaryBase<IDLListType, T>::lookup(const word&) const"
00146         )   << keyword << " is undefined"
00147             << exit(FatalError);
00148     }
00149 
00150     return *iter;
00151 }
00152 
00153 
00154 // Find and return T*, FatalError if keyword not found
00155 template<class IDLListType, class T>
00156 T* Foam::DictionaryBase<IDLListType, T>::lookup(const word& keyword)
00157 {
00158     typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
00159 
00160     if (iter == hashedTs_.end())
00161     {
00162         FatalErrorIn
00163         (
00164             "DictionaryBase<IDLListType, T>::lookup(const word&)"
00165         )   << keyword << " is undefined"
00166             << exit(FatalError);
00167     }
00168 
00169     return *iter;
00170 }
00171 
00172 
00173 // Return the table of contents
00174 template<class IDLListType, class T>
00175 Foam::wordList Foam::DictionaryBase<IDLListType, T>::toc() const
00176 {
00177     wordList keywords(this->size());
00178 
00179     label i = 0;
00180     for
00181     (
00182         typename IDLListType::const_iterator iter = this->begin();
00183         iter != this->end();
00184         ++iter
00185     )
00186     {
00187         keywords[i++] = iter().keyword();
00188     }
00189 
00190     return keywords;
00191 }
00192 
00193 
00194 // Add at head of dictionary
00195 template<class IDLListType, class T>
00196 void Foam::DictionaryBase<IDLListType, T>::insert(const word& keyword, T* tPtr)
00197 {
00198     // NOTE: we should probably check that HashTable::insert actually worked
00199     hashedTs_.insert(keyword, tPtr);
00200     IDLListType::insert(tPtr);
00201 }
00202 
00203 
00204 // Add at tail of dictionary
00205 template<class IDLListType, class T>
00206 void Foam::DictionaryBase<IDLListType, T>::append(const word& keyword, T* tPtr)
00207 {
00208     // NOTE: we should probably check that HashTable::insert actually worked
00209     hashedTs_.insert(keyword, tPtr);
00210     IDLListType::append(tPtr);
00211 }
00212 
00213 
00214 template<class IDLListType, class T>
00215 T* Foam::DictionaryBase<IDLListType, T>::remove(const word& keyword)
00216 {
00217     typename HashTable<T*>::iterator iter = hashedTs_.find(keyword);
00218 
00219     if (iter != hashedTs_.end())
00220     {
00221         T* tPtr = IDLListType::remove(iter());
00222         hashedTs_.erase(iter);
00223         return tPtr;
00224     }
00225     else
00226     {
00227         return NULL;
00228     }
00229 }
00230 
00231 
00232 template<class IDLListType, class T>
00233 void Foam::DictionaryBase<IDLListType, T>::clear()
00234 {
00235     IDLListType::clear();
00236     hashedTs_.clear();
00237 }
00238 
00239 
00240 template<class IDLListType, class T>
00241 void Foam::DictionaryBase<IDLListType, T>::transfer
00242 (
00243     DictionaryBase<IDLListType, T>& dict
00244 )
00245 {
00246     IDLListType::transfer(dict);
00247     hashedTs_.transfer(dict.hashedTs_);
00248 }
00249 
00250 
00251 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00252 
00253 template<class IDLListType, class T>
00254 void Foam::DictionaryBase<IDLListType, T>::operator=
00255 (
00256     const DictionaryBase<IDLListType, T>& dict
00257 )
00258 {
00259     // Check for assignment to self
00260     if (this == &dict)
00261     {
00262         FatalErrorIn("DictionaryBase::operator=(const DictionaryBase&)")
00263             << "attempted assignment to self"
00264             << abort(FatalError);
00265     }
00266 
00267     IDLListType::operator=(dict);
00268     this->hashedTs_.clear();
00269     this->addEntries();
00270 }
00271 
00272 
00273 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00274 
00275 #include "DictionaryBaseIO.C"
00276 
00277 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd