From 625c75630cd4852d93705f26d0284d362ddae687 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@Germany> Date: Wed, 17 Dec 2008 11:14:00 +0100 Subject: [PATCH] Dictionary template class - added lookupPtr - can return NULL pointer instead of FatalError for non-existent entries --- .../Dictionaries/Dictionary/Dictionary.C | 17 +-- .../Dictionaries/Dictionary/Dictionary.H | 16 +-- .../DictionaryBase/DictionaryBase.C | 116 +++++++++++------- .../DictionaryBase/DictionaryBase.H | 31 +++-- .../DictionaryBase/DictionaryBaseIO.C | 14 +-- .../PtrDictionary/PtrDictionary.C | 15 +-- .../PtrDictionary/PtrDictionary.H | 8 +- .../Dictionaries/UDictionary/UDictionary.C | 13 +- .../Dictionaries/UDictionary/UDictionary.H | 2 +- .../UPtrDictionary/UPtrDictionary.C | 13 +- .../UPtrDictionary/UPtrDictionary.H | 8 +- 11 files changed, 124 insertions(+), 129 deletions(-) diff --git a/src/OpenFOAM/containers/Dictionaries/Dictionary/Dictionary.C b/src/OpenFOAM/containers/Dictionaries/Dictionary/Dictionary.C index 33d7fd37232..b6e52f46860 100644 --- a/src/OpenFOAM/containers/Dictionaries/Dictionary/Dictionary.C +++ b/src/OpenFOAM/containers/Dictionaries/Dictionary/Dictionary.C @@ -28,22 +28,15 @@ Description #include "Dictionary.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -namespace Foam -{ - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -// Null constructor template<class T> -Dictionary<T>::Dictionary() +Foam::Dictionary<T>::Dictionary() {} -// Copy constructor template<class T> -Dictionary<T>::Dictionary(const Dictionary& dict) +Foam::Dictionary<T>::Dictionary(const Dictionary& dict) : DictionaryBase<IDLList<T>, T>(dict) {} @@ -52,10 +45,10 @@ Dictionary<T>::Dictionary(const Dictionary& dict) // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template<class T> -bool Dictionary<T>::erase(const word& Keyword) +bool Foam::Dictionary<T>::erase(const word& keyword) { T* tPtr; - if ((tPtr = this->remove(Keyword))) + if (tPtr = this->remove(keyword)) { delete tPtr; return true; @@ -69,6 +62,4 @@ bool Dictionary<T>::erase(const word& Keyword) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -} // End namespace Foam - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Dictionaries/Dictionary/Dictionary.H b/src/OpenFOAM/containers/Dictionaries/Dictionary/Dictionary.H index 3b985482e7b..90af1383f4a 100644 --- a/src/OpenFOAM/containers/Dictionaries/Dictionary/Dictionary.H +++ b/src/OpenFOAM/containers/Dictionaries/Dictionary/Dictionary.H @@ -27,8 +27,10 @@ Class Description Gerneral purpose template dictionary class which manages the storage - associated with it. It is derived from DictionaryBase instantiated on - a memory managed form of intrusive doubly-linked list of \<T\>. + associated with it. + + It is derived from DictionaryBase instantiated on a memory managed form + of intrusive doubly-linked list of \<T\>. SourceFiles Dictionary.C @@ -47,7 +49,7 @@ namespace Foam { /*---------------------------------------------------------------------------*\ - Class Dictionary Declaration + Class Dictionary Declaration \*---------------------------------------------------------------------------*/ template<class T> @@ -69,11 +71,9 @@ public: // Member functions - // Editing - - //- Remove an entry specified by keyword from the dictionary - // and delete it - bool erase(const word& keyword); + //- Remove an entry specified by keyword and delete the pointer. + // Returns true if the keyword was found + bool erase(const word& keyword); }; diff --git a/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBase.C b/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBase.C index 4349973c3ce..b8ce241f094 100644 --- a/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBase.C +++ b/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBase.C @@ -26,15 +26,10 @@ License #include "DictionaryBase.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -namespace Foam -{ - // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // template<class IDLListType, class T> -void DictionaryBase<IDLListType, T>::addEntries() +void Foam::DictionaryBase<IDLListType, T>::addEntries() { for ( @@ -51,12 +46,15 @@ void DictionaryBase<IDLListType, T>::addEntries() // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template<class IDLListType, class T> -DictionaryBase<IDLListType, T>::DictionaryBase() +Foam::DictionaryBase<IDLListType, T>::DictionaryBase() {} template<class IDLListType, class T> -DictionaryBase<IDLListType, T>::DictionaryBase(const DictionaryBase& dict) +Foam::DictionaryBase<IDLListType, T>::DictionaryBase +( + const DictionaryBase& dict +) : IDLListType(dict) { @@ -66,17 +64,20 @@ DictionaryBase<IDLListType, T>::DictionaryBase(const DictionaryBase& dict) template<class IDLListType, class T> template<class INew> -DictionaryBase<IDLListType, T>::DictionaryBase(Istream& is, const INew& inewt) +Foam::DictionaryBase<IDLListType, T>::DictionaryBase +( + Istream& is, + const INew& iNew +) : - IDLListType(is, inewt) + IDLListType(is, iNew) { addEntries(); } -// Istream constructor template<class IDLListType, class T> -DictionaryBase<IDLListType, T>::DictionaryBase(Istream& is) +Foam::DictionaryBase<IDLListType, T>::DictionaryBase(Istream& is) : IDLListType(is) { @@ -88,25 +89,60 @@ DictionaryBase<IDLListType, T>::DictionaryBase(Istream& is) // Find and return T template<class IDLListType, class T> -bool DictionaryBase<IDLListType, T>::found(const word& keyword) const +bool Foam::DictionaryBase<IDLListType, T>::found(const word& keyword) const { return hashedTs_.found(keyword); } -// Find and return T* +// Find and return T*, return NULL if not found +template<class IDLListType, class T> +const T* Foam::DictionaryBase<IDLListType, T>::lookupPtr +( + const word& keyword +) const +{ + typename HashTable<T*>::const_iterator iter = hashedTs_.find(keyword); + + if (iter != hashedTs_.end()) + { + return *iter; + } + else + { + return NULL; + } +} + + +// Find and return T*, return NULL if not found +template<class IDLListType, class T> +T* Foam::DictionaryBase<IDLListType, T>::lookupPtr(const word& keyword) +{ + typename HashTable<T*>::iterator iter = hashedTs_.find(keyword); + + if (iter != hashedTs_.end()) + { + return *iter; + } + else + { + return NULL; + } +} + + +// Find and return T*, FatalError if keyword not found template<class IDLListType, class T> -const T* DictionaryBase<IDLListType, T>::lookup(const word& keyword) const +const T* Foam::DictionaryBase<IDLListType, T>::lookup(const word& keyword) const { typename HashTable<T*>::const_iterator iter = hashedTs_.find(keyword); if (iter == hashedTs_.end()) { - // If keyword not found print error message ... FatalErrorIn ( - "DictionaryBase<IDLListType, T>::" - "lookup(const word& keyword) const" + "DictionaryBase<IDLListType, T>::lookup(const word&) const" ) << keyword << " is undefined" << exit(FatalError); } @@ -115,18 +151,17 @@ const T* DictionaryBase<IDLListType, T>::lookup(const word& keyword) const } -// Find and return T* +// Find and return T*, FatalError if keyword not found template<class IDLListType, class T> -T* DictionaryBase<IDLListType, T>::lookup(const word& keyword) +T* Foam::DictionaryBase<IDLListType, T>::lookup(const word& keyword) { typename HashTable<T*>::iterator iter = hashedTs_.find(keyword); if (iter == hashedTs_.end()) { - // If keyword not found print error message ... FatalErrorIn ( - "DictionaryBase<IDLListType, T>::lookup(const word& keyword)" + "DictionaryBase<IDLListType, T>::lookup(const word&)" ) << keyword << " is undefined" << exit(FatalError); } @@ -137,7 +172,7 @@ T* DictionaryBase<IDLListType, T>::lookup(const word& keyword) // Return the table of contents template<class IDLListType, class T> -wordList DictionaryBase<IDLListType, T>::toc() const +Foam::wordList Foam::DictionaryBase<IDLListType, T>::toc() const { wordList keywords(this->size()); @@ -158,26 +193,28 @@ wordList DictionaryBase<IDLListType, T>::toc() const // Add at head of dictionary template<class IDLListType, class T> -void DictionaryBase<IDLListType, T>::insert(const word& keyword, T* tPtr) +void Foam::DictionaryBase<IDLListType, T>::insert(const word& keyword, T* tPtr) { - IDLListType::insert(tPtr); + // NOTE: we should probably check that HashTable::insert actually worked hashedTs_.insert(keyword, tPtr); + IDLListType::insert(tPtr); } // Add at tail of dictionary template<class IDLListType, class T> -void DictionaryBase<IDLListType, T>::append(const word& keyword, T* tPtr) +void Foam::DictionaryBase<IDLListType, T>::append(const word& keyword, T* tPtr) { - IDLListType::append(tPtr); + // NOTE: we should probably check that HashTable::insert actually worked hashedTs_.insert(keyword, tPtr); + IDLListType::append(tPtr); } template<class IDLListType, class T> -T* DictionaryBase<IDLListType, T>::remove(const word& Keyword) +T* Foam::DictionaryBase<IDLListType, T>::remove(const word& keyword) { - typename HashTable<T*>::iterator iter = hashedTs_.find(Keyword); + typename HashTable<T*>::iterator iter = hashedTs_.find(keyword); if (iter != hashedTs_.end()) { @@ -192,9 +229,8 @@ T* DictionaryBase<IDLListType, T>::remove(const word& Keyword) } -//- Clear the dictionary template<class IDLListType, class T> -void DictionaryBase<IDLListType, T>::clear() +void Foam::DictionaryBase<IDLListType, T>::clear() { IDLListType::clear(); hashedTs_.clear(); @@ -204,7 +240,7 @@ void DictionaryBase<IDLListType, T>::clear() // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template<class IDLListType, class T> -void DictionaryBase<IDLListType, T>::operator= +void Foam::DictionaryBase<IDLListType, T>::operator= ( const DictionaryBase<IDLListType, T>& dict ) @@ -218,25 +254,11 @@ void DictionaryBase<IDLListType, T>::operator= } IDLListType::operator=(dict); - this->hashedTs_.clear(); - - for - ( - typename IDLListType::iterator iter = this->begin(); - iter != this->end(); - ++iter - ) - { - this->hashedTs_.insert((*iter).keyword(), &(*iter)); - } + this->addEntries(); } -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -} // End namespace Foam - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #include "DictionaryBaseIO.C" diff --git a/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBase.H b/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBase.H index 8288cdf5cfb..63566cc4c1e 100644 --- a/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBase.H +++ b/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBase.H @@ -29,12 +29,12 @@ Description Base dictionary class templated on both the form of doubly-linked list it uses as well as the type it holds. - The double templating allows for the instantiation of forms with and + The double templating allows for the instantiation of forms with or without storage management. Note The IDLListType parameter should itself be a template but this confused - gcc 2.95.2 so it has to be instantiated for T when an intantiation of + gcc 2.95.2 so it has to be instantiated for T when an instantiation of DictionaryBase is requested See Also @@ -67,7 +67,7 @@ Ostream& operator<<(Ostream&, const DictionaryBase<IDLListType, T>&); /*---------------------------------------------------------------------------*\ - Class DictionaryBase Declaration + Class DictionaryBase Declaration \*---------------------------------------------------------------------------*/ template<class IDLListType, class T> @@ -77,7 +77,7 @@ class DictionaryBase { // Private data - //- HashTable of the enries held on the DL-list for quick lookup + //- HashTable of the entries held on the IDLListType for quick lookup HashTable<T*> hashedTs_; @@ -99,10 +99,10 @@ public: //- Construct from Istream using given Istream constructor class template<class INew> - DictionaryBase(Istream& is, const INew& inewt); + DictionaryBase(Istream&, const INew&); - //- Construct from Istream - DictionaryBase(Istream& is); + //- Construct from Istream using default Istream constructor class + DictionaryBase(Istream&); // Member functions @@ -110,7 +110,13 @@ public: // Search and lookup //- Search DictionaryBase for given keyword - bool found(const word& keyword) const; + bool found(const word&) const; + + //- Find and return an entry if present, otherwise return NULL + const T* lookupPtr(const word&) const; + + //- Find and return an entry if present, otherwise return NULL + T* lookupPtr(const word&); //- Find and return entry const T* lookup(const word&) const; @@ -125,13 +131,14 @@ public: // Editing //- Add at head of dictionary - void insert(const word& keyword, T*); + void insert(const word&, T*); //- Add at tail of dictionary - void append(const word& keyword, T*); + void append(const word&, T*); - //- Remove and return entry specified by keyword - T* remove(const word& keyword); + //- Remove and return entry specified by keyword. + // Return NULL if the keyword was not found. + T* remove(const word&); //- Clear the dictionary void clear(); diff --git a/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBaseIO.C b/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBaseIO.C index 15854c515bf..6af2b1622dc 100644 --- a/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBaseIO.C +++ b/src/OpenFOAM/containers/Dictionaries/DictionaryBase/DictionaryBaseIO.C @@ -30,15 +30,13 @@ Description #include "DictionaryBase.H" #include "IOstreams.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -namespace Foam -{ - // * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * * // template<class IDLListType, class T> -Ostream& operator<<(Ostream& os, const DictionaryBase<IDLListType, T>& dict) +Foam::Ostream& Foam::operator<< +( + Ostream& os, + const DictionaryBase<IDLListType, T>& dict) { for ( @@ -53,7 +51,7 @@ Ostream& operator<<(Ostream& os, const DictionaryBase<IDLListType, T>& dict) if (!os.good()) { Info - << "operator<<(Ostream& os, const DictionaryBase&) : " + << "operator<<(Ostream&, const DictionaryBase&) : " << "Can't write entry for DictionaryBase" << endl; @@ -67,6 +65,4 @@ Ostream& operator<<(Ostream& os, const DictionaryBase<IDLListType, T>& dict) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -} // End namespace Foam - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Dictionaries/PtrDictionary/PtrDictionary.C b/src/OpenFOAM/containers/Dictionaries/PtrDictionary/PtrDictionary.C index b2b7861eb4f..4b0a48ac900 100644 --- a/src/OpenFOAM/containers/Dictionaries/PtrDictionary/PtrDictionary.C +++ b/src/OpenFOAM/containers/Dictionaries/PtrDictionary/PtrDictionary.C @@ -26,20 +26,15 @@ License #include "PtrDictionary.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -namespace Foam -{ - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template<class T> -PtrDictionary<T>::PtrDictionary() +Foam::PtrDictionary<T>::PtrDictionary() {} template<class T> -PtrDictionary<T>::PtrDictionary(const PtrDictionary& dict) +Foam::PtrDictionary<T>::PtrDictionary(const PtrDictionary& dict) : DictionaryBase<DLPtrList<T>, T>(dict) {} @@ -47,14 +42,14 @@ PtrDictionary<T>::PtrDictionary(const PtrDictionary& dict) template<class T> template<class INew> -PtrDictionary<T>::PtrDictionary(Istream& is, const INew& iNew) +Foam::PtrDictionary<T>::PtrDictionary(Istream& is, const INew& iNew) : DictionaryBase<DLPtrList<T>, T>(is, iNew) {} template<class T> -PtrDictionary<T>::PtrDictionary(Istream& is) +Foam::PtrDictionary<T>::PtrDictionary(Istream& is) : DictionaryBase<DLPtrList<T>, T>(is) {} @@ -62,6 +57,4 @@ PtrDictionary<T>::PtrDictionary(Istream& is) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -} // End namespace Foam - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Dictionaries/PtrDictionary/PtrDictionary.H b/src/OpenFOAM/containers/Dictionaries/PtrDictionary/PtrDictionary.H index 3396236515c..9c7da2f4de3 100644 --- a/src/OpenFOAM/containers/Dictionaries/PtrDictionary/PtrDictionary.H +++ b/src/OpenFOAM/containers/Dictionaries/PtrDictionary/PtrDictionary.H @@ -27,8 +27,10 @@ Class Description Template dictionary class which does not manages the storage - associated with it. It is derived from DictionaryBase instantiated on - a non-memory managed form of intrusive doubly-linked list of T. + associated with it. + + It is derived from DictionaryBase instantiated on a non-memory managed + form of intrusive doubly-linked list of T. SourceFiles PtrDictionary.C @@ -47,7 +49,7 @@ namespace Foam { /*---------------------------------------------------------------------------*\ - Class PtrDictionary Declaration + Class PtrDictionary Declaration \*---------------------------------------------------------------------------*/ template<class T> diff --git a/src/OpenFOAM/containers/Dictionaries/UDictionary/UDictionary.C b/src/OpenFOAM/containers/Dictionaries/UDictionary/UDictionary.C index 9712b747351..8f29af262bc 100644 --- a/src/OpenFOAM/containers/Dictionaries/UDictionary/UDictionary.C +++ b/src/OpenFOAM/containers/Dictionaries/UDictionary/UDictionary.C @@ -26,22 +26,15 @@ License #include "UDictionary.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -namespace Foam -{ - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -// Null constructor template<class T> -UDictionary<T>::UDictionary() +Foam::UDictionary<T>::UDictionary() {} -// Copy constructor template<class T> -UDictionary<T>::UDictionary(const UDictionary& dict) +Foam::UDictionary<T>::UDictionary(const UDictionary& dict) : DictionaryBase<UIDLList<T>, T>(dict) {} @@ -49,6 +42,4 @@ UDictionary<T>::UDictionary(const UDictionary& dict) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -} // End namespace Foam - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Dictionaries/UDictionary/UDictionary.H b/src/OpenFOAM/containers/Dictionaries/UDictionary/UDictionary.H index fb30237d40d..5cc4d301f4e 100644 --- a/src/OpenFOAM/containers/Dictionaries/UDictionary/UDictionary.H +++ b/src/OpenFOAM/containers/Dictionaries/UDictionary/UDictionary.H @@ -49,7 +49,7 @@ namespace Foam { /*---------------------------------------------------------------------------*\ - Class UDictionary Declaration + Class UDictionary Declaration \*---------------------------------------------------------------------------*/ template<class T> diff --git a/src/OpenFOAM/containers/Dictionaries/UPtrDictionary/UPtrDictionary.C b/src/OpenFOAM/containers/Dictionaries/UPtrDictionary/UPtrDictionary.C index 3a4686fbf6a..6aa7da72abd 100644 --- a/src/OpenFOAM/containers/Dictionaries/UPtrDictionary/UPtrDictionary.C +++ b/src/OpenFOAM/containers/Dictionaries/UPtrDictionary/UPtrDictionary.C @@ -26,22 +26,15 @@ License #include "UPtrDictionary.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -namespace Foam -{ - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -// Null constructor template<class T> -UPtrDictionary<T>::UPtrDictionary() +Foam::UPtrDictionary<T>::UPtrDictionary() {} -// Copy constructor template<class T> -UPtrDictionary<T>::UPtrDictionary(const UPtrDictionary& dict) +Foam::UPtrDictionary<T>::UPtrDictionary(const UPtrDictionary& dict) : DictionaryBase<DLList<T*>, T>(dict) {} @@ -49,6 +42,4 @@ UPtrDictionary<T>::UPtrDictionary(const UPtrDictionary& dict) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -} // End namespace Foam - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Dictionaries/UPtrDictionary/UPtrDictionary.H b/src/OpenFOAM/containers/Dictionaries/UPtrDictionary/UPtrDictionary.H index 2caac9efac6..99aee0ac562 100644 --- a/src/OpenFOAM/containers/Dictionaries/UPtrDictionary/UPtrDictionary.H +++ b/src/OpenFOAM/containers/Dictionaries/UPtrDictionary/UPtrDictionary.H @@ -27,8 +27,10 @@ Class Description Template dictionary class which does not manages the storage - associated with it. It is derived from DictionaryBase instantiated on - a non-memory managed form of intrusive doubly-linked list of \<T\>. + associated with it. + + It is derived from DictionaryBase instantiated on a non-memory managed + form of intrusive doubly-linked list of \<T\>. SourceFiles UPtrDictionary.C @@ -47,7 +49,7 @@ namespace Foam { /*---------------------------------------------------------------------------*\ - Class UPtrDictionary Declaration + Class UPtrDictionary Declaration \*---------------------------------------------------------------------------*/ template<class T> -- GitLab