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

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