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

HashPtrTable.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-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 \*---------------------------------------------------------------------------*/
00025 
00026 #include <OpenFOAM/error.H>
00027 #include "HashPtrTable.H"
00028 
00029 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00030 
00031 // Construct given initial table size
00032 template<class T, class Key, class Hash>
00033 Foam::HashPtrTable<T, Key, Hash>::HashPtrTable(const label size)
00034 :
00035     HashTable<T*, Key, Hash>(size)
00036 {}
00037 
00038 
00039 // Construct as copy
00040 template<class T, class Key, class Hash>
00041 Foam::HashPtrTable<T, Key, Hash>::HashPtrTable
00042 (
00043     const HashPtrTable<T, Key, Hash>& ht
00044 )
00045 :
00046     HashTable<T*, Key, Hash>()
00047 {
00048     for (const_iterator iter = ht.begin(); iter != ht.end(); ++iter)
00049     {
00050         this->insert(iter.key(), new T(**iter));
00051     }
00052 }
00053 
00054 
00055 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00056 
00057 template<class T, class Key, class Hash>
00058 Foam::HashPtrTable<T, Key, Hash>::~HashPtrTable()
00059 {
00060     clear();
00061 }
00062 
00063 
00064 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00065 
00066 template<class T, class Key, class Hash>
00067 T* Foam::HashPtrTable<T, Key, Hash>::remove(iterator& it)
00068 {
00069     T* elemPtr = *it;
00070     HashTable<T*, Key, Hash>::erase(it);
00071     return elemPtr;
00072 }
00073 
00074 
00075 template<class T, class Key, class Hash>
00076 bool Foam::HashPtrTable<T, Key, Hash>::erase(iterator& it)
00077 {
00078     T* elemPtr = *it;
00079 
00080     if (HashTable<T*, Key, Hash>::erase(it))
00081     {
00082         if (elemPtr)
00083         {
00084             delete elemPtr;
00085         }
00086 
00087         return true;
00088     }
00089     else
00090     {
00091         return false;
00092     }
00093 }
00094 
00095 
00096 template<class T, class Key, class Hash>
00097 void Foam::HashPtrTable<T, Key, Hash>::clear()
00098 {
00099     for
00100     (
00101         iterator iter = this->begin();
00102         iter != this->end();
00103         ++iter
00104     )
00105     {
00106         delete *iter;
00107     }
00108 
00109     HashTable<T*, Key, Hash>::clear();
00110 }
00111 
00112 
00113 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00114 
00115 template<class T, class Key, class Hash>
00116 void Foam::HashPtrTable<T, Key, Hash>::operator=
00117 (
00118     const HashPtrTable<T, Key, Hash>& rhs
00119 )
00120 {
00121     // Check for assignment to self
00122     if (this == &rhs)
00123     {
00124         FatalErrorIn
00125         (
00126             "HashPtrTable<T, Key, Hash>::operator="
00127             "(const HashPtrTable<T, Key, Hash>&)"
00128         )   << "attempted assignment to self"
00129             << abort(FatalError);
00130     }
00131 
00132     clear();
00133 
00134     for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
00135     {
00136         this->insert(iter.key(), new T(**iter));
00137     }
00138 }
00139 
00140 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00141 
00142 #include "HashPtrTableIO.C"
00143 
00144 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines