From 625c75630cd4852d93705f26d0284d362ddae687 Mon Sep 17 00:00:00 2001 From: Mark Olesen 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 33d7fd3723..b6e52f4686 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 -Dictionary::Dictionary() +Foam::Dictionary::Dictionary() {} -// Copy constructor template -Dictionary::Dictionary(const Dictionary& dict) +Foam::Dictionary::Dictionary(const Dictionary& dict) : DictionaryBase, T>(dict) {} @@ -52,10 +45,10 @@ Dictionary::Dictionary(const Dictionary& dict) // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template -bool Dictionary::erase(const word& Keyword) +bool Foam::Dictionary::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::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 3b985482e7..90af1383f4 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 \. + associated with it. + + It is derived from DictionaryBase instantiated on a memory managed form + of intrusive doubly-linked list of \. SourceFiles Dictionary.C @@ -47,7 +49,7 @@ namespace Foam { /*---------------------------------------------------------------------------*\ - Class Dictionary Declaration + Class Dictionary Declaration \*---------------------------------------------------------------------------*/ template @@ -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 4349973c3c..b8ce241f09 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 -void DictionaryBase::addEntries() +void Foam::DictionaryBase::addEntries() { for ( @@ -51,12 +46,15 @@ void DictionaryBase::addEntries() // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template -DictionaryBase::DictionaryBase() +Foam::DictionaryBase::DictionaryBase() {} template -DictionaryBase::DictionaryBase(const DictionaryBase& dict) +Foam::DictionaryBase::DictionaryBase +( + const DictionaryBase& dict +) : IDLListType(dict) { @@ -66,17 +64,20 @@ DictionaryBase::DictionaryBase(const DictionaryBase& dict) template template -DictionaryBase::DictionaryBase(Istream& is, const INew& inewt) +Foam::DictionaryBase::DictionaryBase +( + Istream& is, + const INew& iNew +) : - IDLListType(is, inewt) + IDLListType(is, iNew) { addEntries(); } -// Istream constructor template -DictionaryBase::DictionaryBase(Istream& is) +Foam::DictionaryBase::DictionaryBase(Istream& is) : IDLListType(is) { @@ -88,25 +89,60 @@ DictionaryBase::DictionaryBase(Istream& is) // Find and return T template -bool DictionaryBase::found(const word& keyword) const +bool Foam::DictionaryBase::found(const word& keyword) const { return hashedTs_.found(keyword); } -// Find and return T* +// Find and return T*, return NULL if not found template -const T* DictionaryBase::lookup(const word& keyword) const +const T* Foam::DictionaryBase::lookupPtr +( + const word& keyword +) const +{ + typename HashTable::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 +T* Foam::DictionaryBase::lookupPtr(const word& keyword) +{ + typename HashTable::iterator iter = hashedTs_.find(keyword); + + if (iter != hashedTs_.end()) + { + return *iter; + } + else + { + return NULL; + } +} + + +// Find and return T*, FatalError if keyword not found +template +const T* Foam::DictionaryBase::lookup(const word& keyword) const { typename HashTable::const_iterator iter = hashedTs_.find(keyword); if (iter == hashedTs_.end()) { - // If keyword not found print error message ... FatalErrorIn ( - "DictionaryBase::" - "lookup(const word& keyword) const" + "DictionaryBase::lookup(const word&) const" ) << keyword << " is undefined" << exit(FatalError); } @@ -115,18 +151,17 @@ const T* DictionaryBase::lookup(const word& keyword) const } -// Find and return T* +// Find and return T*, FatalError if keyword not found template -T* DictionaryBase::lookup(const word& keyword) +T* Foam::DictionaryBase::lookup(const word& keyword) { typename HashTable::iterator iter = hashedTs_.find(keyword); if (iter == hashedTs_.end()) { - // If keyword not found print error message ... FatalErrorIn ( - "DictionaryBase::lookup(const word& keyword)" + "DictionaryBase::lookup(const word&)" ) << keyword << " is undefined" << exit(FatalError); } @@ -137,7 +172,7 @@ T* DictionaryBase::lookup(const word& keyword) // Return the table of contents template -wordList DictionaryBase::toc() const +Foam::wordList Foam::DictionaryBase::toc() const { wordList keywords(this->size()); @@ -158,26 +193,28 @@ wordList DictionaryBase::toc() const // Add at head of dictionary template -void DictionaryBase::insert(const word& keyword, T* tPtr) +void Foam::DictionaryBase::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 -void DictionaryBase::append(const word& keyword, T* tPtr) +void Foam::DictionaryBase::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 -T* DictionaryBase::remove(const word& Keyword) +T* Foam::DictionaryBase::remove(const word& keyword) { - typename HashTable::iterator iter = hashedTs_.find(Keyword); + typename HashTable::iterator iter = hashedTs_.find(keyword); if (iter != hashedTs_.end()) { @@ -192,9 +229,8 @@ T* DictionaryBase::remove(const word& Keyword) } -//- Clear the dictionary template -void DictionaryBase::clear() +void Foam::DictionaryBase::clear() { IDLListType::clear(); hashedTs_.clear(); @@ -204,7 +240,7 @@ void DictionaryBase::clear() // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template -void DictionaryBase::operator= +void Foam::DictionaryBase::operator= ( const DictionaryBase& dict ) @@ -218,25 +254,11 @@ void DictionaryBase::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 8288cdf5cf..63566cc4c1 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&); /*---------------------------------------------------------------------------*\ - Class DictionaryBase Declaration + Class DictionaryBase Declaration \*---------------------------------------------------------------------------*/ template @@ -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 hashedTs_; @@ -99,10 +99,10 @@ public: //- Construct from Istream using given Istream constructor class template - 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 15854c515b..6af2b1622d 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 -Ostream& operator<<(Ostream& os, const DictionaryBase& dict) +Foam::Ostream& Foam::operator<< +( + Ostream& os, + const DictionaryBase& dict) { for ( @@ -53,7 +51,7 @@ Ostream& operator<<(Ostream& os, const DictionaryBase& 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& dict) // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -} // End namespace Foam - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Dictionaries/PtrDictionary/PtrDictionary.C b/src/OpenFOAM/containers/Dictionaries/PtrDictionary/PtrDictionary.C index b2b7861eb4..4b0a48ac90 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 -PtrDictionary::PtrDictionary() +Foam::PtrDictionary::PtrDictionary() {} template -PtrDictionary::PtrDictionary(const PtrDictionary& dict) +Foam::PtrDictionary::PtrDictionary(const PtrDictionary& dict) : DictionaryBase, T>(dict) {} @@ -47,14 +42,14 @@ PtrDictionary::PtrDictionary(const PtrDictionary& dict) template template -PtrDictionary::PtrDictionary(Istream& is, const INew& iNew) +Foam::PtrDictionary::PtrDictionary(Istream& is, const INew& iNew) : DictionaryBase, T>(is, iNew) {} template -PtrDictionary::PtrDictionary(Istream& is) +Foam::PtrDictionary::PtrDictionary(Istream& is) : DictionaryBase, T>(is) {} @@ -62,6 +57,4 @@ PtrDictionary::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 3396236515..9c7da2f4de 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 diff --git a/src/OpenFOAM/containers/Dictionaries/UDictionary/UDictionary.C b/src/OpenFOAM/containers/Dictionaries/UDictionary/UDictionary.C index 9712b74735..8f29af262b 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 -UDictionary::UDictionary() +Foam::UDictionary::UDictionary() {} -// Copy constructor template -UDictionary::UDictionary(const UDictionary& dict) +Foam::UDictionary::UDictionary(const UDictionary& dict) : DictionaryBase, T>(dict) {} @@ -49,6 +42,4 @@ UDictionary::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 fb30237d40..5cc4d301f4 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 diff --git a/src/OpenFOAM/containers/Dictionaries/UPtrDictionary/UPtrDictionary.C b/src/OpenFOAM/containers/Dictionaries/UPtrDictionary/UPtrDictionary.C index 3a4686fbf6..6aa7da72ab 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 -UPtrDictionary::UPtrDictionary() +Foam::UPtrDictionary::UPtrDictionary() {} -// Copy constructor template -UPtrDictionary::UPtrDictionary(const UPtrDictionary& dict) +Foam::UPtrDictionary::UPtrDictionary(const UPtrDictionary& dict) : DictionaryBase, T>(dict) {} @@ -49,6 +42,4 @@ UPtrDictionary::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 2caac9efac..99aee0ac56 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 \. + associated with it. + + It is derived from DictionaryBase instantiated on a non-memory managed + form of intrusive doubly-linked list of \. SourceFiles UPtrDictionary.C @@ -47,7 +49,7 @@ namespace Foam { /*---------------------------------------------------------------------------*\ - Class UPtrDictionary Declaration + Class UPtrDictionary Declaration \*---------------------------------------------------------------------------*/ template