HashPtrTable.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "error.H"
30 #include "HashPtrTable.H"
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
34 template<class T, class Key, class Hash>
36 (
37  const HashPtrTable<T, Key, Hash>& ht
38 )
39 :
40  parent_type(ht.capacity())
41 {
42  for (const_iterator iter = ht.begin(); iter != ht.end(); ++iter)
43  {
44  const T* ptr = iter.val();
45  if (ptr)
46  {
47  this->set(iter.key(), new T(*ptr));
48  }
49  else
50  {
51  this->set(iter.key(), nullptr);
52  }
53  }
54 }
55 
56 
57 template<class T, class Key, class Hash>
59 (
60  HashPtrTable<T, Key, Hash>&& ht
61 )
62 :
63  parent_type(std::move(ht))
64 {}
65 
66 
67 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
68 
69 template<class T, class Key, class Hash>
71 {
72  clear();
73 }
74 
75 
76 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
77 
78 template<class T, class Key, class Hash>
80 {
81  if (iter.good())
82  {
83  autoPtr<T> aptr(iter.val());
84  this->parent_type::erase(iter);
85  return aptr;
86  }
87 
88  return nullptr;
89 }
90 
91 
92 template<class T, class Key, class Hash>
94 {
95  auto iter = this->find(key);
96  return this->remove(iter);
97 }
98 
99 
100 template<class T, class Key, class Hash>
102 {
103  if (iter.good())
104  {
105  T* ptr = iter.val();
106 
107  if (this->parent_type::erase(iter))
108  {
109  if (ptr)
110  {
111  delete ptr;
112  }
113 
114  return true;
115  }
116  }
117 
118  return false;
119 }
120 
121 
122 template<class T, class Key, class Hash>
124 {
125  auto iter = this->find(key);
126  return this->erase(iter);
127 }
128 
129 
130 template<class T, class Key, class Hash>
132 {
133  for (iterator iter = this->begin(); iter != this->end(); ++iter)
134  {
135  delete iter.val();
136  }
137 
138  this->parent_type::clear();
139 }
140 
141 
142 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
143 
144 template<class T, class Key, class Hash>
145 void Foam::HashPtrTable<T, Key, Hash>::operator=
146 (
147  const HashPtrTable<T, Key, Hash>& rhs
148 )
149 {
150  if (this == &rhs)
151  {
152  return; // Self-assignment is a no-op
153  }
154 
155  this->clear();
156 
157  for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
158  {
159  const T* ptr = iter.val();
160  if (ptr)
161  {
162  this->set(iter.key(), new T(*ptr));
163  }
164  else
165  {
166  this->set(iter.key(), nullptr);
167  }
168  }
169 }
170 
171 
172 template<class T, class Key, class Hash>
174 (
175  HashPtrTable<T, Key, Hash>&& rhs
176 )
177 {
178  if (this == &rhs)
179  {
180  return; // Self-assignment is a no-op
181  }
182 
183  this->clear();
184  this->transfer(rhs);
185 }
186 
187 
188 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
189 
190 #include "HashPtrTableIO.C"
191 
192 // ************************************************************************* //
Foam::HashPtrTable::erase
bool erase(iterator &iter)
Definition: HashPtrTable.C:101
stdFoam::begin
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:91
Foam::HashPtrTable::remove
autoPtr< T > remove(iterator &iter)
Remove entry specified by given iterator.
Definition: HashPtrTable.C:79
Foam::HashPtrTable::~HashPtrTable
~HashPtrTable()
Destructor.
Definition: HashPtrTable.C:70
erase
srcOptions erase("case")
Foam::HashPtrTable::clear
void clear()
Clear all entries from table and delete any allocated pointers.
Definition: HashPtrTable.C:131
error.H
HashPtrTableIO.C
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::HashPtrTable::HashPtrTable
HashPtrTable()
Construct null with default table capacity.
Definition: HashPtrTableI.H:33
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:115
Foam::ListOps::find
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Find index of the first occurrence that satisfies the predicate.
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
clear
patchWriters clear()
Foam::HashPtrTable
A HashTable of pointers to objects of type <T>.
Definition: HashPtrTable.H:54
HashPtrTable.H
Foam::HashPtrTable< exprResult >::iterator
typename parent_type::iterator iterator
Definition: HashPtrTable.H:88