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-2023 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 "HashPtrTable.H"
30 #include "autoPtr.H"
31 #include "error.H"
32 
33 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
34 
35 template<class T, class Key, class Hash>
37 (
38  const HashPtrTable<T, Key, Hash>& rhs
39 )
40 :
41  parent_type(rhs.capacity())
42 {
43  for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
44  {
45  const Key& k = iter.key();
46  const T* ptr = iter.val();
47 
48  if (ptr)
49  {
50  this->set(k, new T(*ptr));
51  }
52  else
53  {
54  this->set(k, nullptr);
55  }
56  }
57 }
58 
59 
60 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
61 
62 template<class T, class Key, class Hash>
64 {
65  clear();
66 }
67 
68 
69 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
70 
71 template<class T, class Key, class Hash>
73 {
74  if (iter.good())
75  {
76  autoPtr<T> old(iter.val());
77  iter.val() = nullptr;
78  return old;
79  }
80 
81  return nullptr;
82 }
83 
84 
85 template<class T, class Key, class Hash>
87 {
88  iterator iter(this->find(key));
89  return this->release(iter);
90 }
91 
92 
93 template<class T, class Key, class Hash>
95 {
96  if (iter.good())
97  {
98  autoPtr<T> old(iter.val());
99  this->parent_type::erase(iter);
100  return old;
101  }
102 
103  return nullptr;
104 }
105 
106 
107 template<class T, class Key, class Hash>
109 {
110  iterator iter(this->find(key));
111  return this->remove(iter);
112 }
113 
114 
115 template<class T, class Key, class Hash>
116 bool Foam::HashPtrTable<T, Key, Hash>::erase(iterator& iter)
117 {
118  if (iter.good())
119  {
120  T* ptr = iter.val();
121 
122  if (this->parent_type::erase(iter))
123  {
124  delete ptr;
125  return true;
126  }
127  }
128 
129  return false;
130 }
131 
132 
133 template<class T, class Key, class Hash>
135 {
136  iterator iter(this->find(key));
137  return this->erase(iter);
138 }
139 
140 
141 template<class T, class Key, class Hash>
143 {
144  for (iterator iter = this->begin(); iter != this->end(); ++iter)
145  {
146  delete iter.val();
147  }
149  this->parent_type::clear();
150 }
151 
152 
153 template<class T, class Key, class Hash>
155 (
157 )
158 {
159  // Use parent merge (of raw pointer entries)
160  // and by-pass any pointer deletions etc.
161  parent_type::merge(static_cast<parent_type&>(source));
162 }
163 
164 
165 template<class T, class Key, class Hash>
167 (
169 )
170 {
171  // Use parent merge (of raw pointer entries)
172  // and by-pass any pointer deletions etc.
173  parent_type::merge(static_cast<parent_type&>(source));
174 }
175 
176 
177 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
178 
179 template<class T, class Key, class Hash>
181 (
182  const HashPtrTable<T, Key, Hash>& rhs
183 )
184 {
185  if (this == &rhs)
186  {
187  return; // Self-assignment is a no-op
188  }
189 
190  this->clear();
191 
192  for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
193  {
194  const Key& k = iter.key();
195  const T* ptr = iter.val();
196 
197  if (ptr)
198  {
199  this->set(k, new T(*ptr));
200  }
201  else
202  {
203  this->set(k, nullptr);
204  }
205  }
206 }
207 
208 
209 template<class T, class Key, class Hash>
210 void Foam::HashPtrTable<T, Key, Hash>::operator=
211 (
212  HashPtrTable<T, Key, Hash>&& rhs
213 )
214 {
215  if (this == &rhs)
216  {
217  return; // Self-assignment is a no-op
218  }
219 
220  this->clear();
221  this->transfer(rhs);
222 }
223 
224 
225 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
226 
227 #include "HashPtrTableIO.C"
228 
229 // ************************************************************************* //
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition: ListOps.H:795
~HashPtrTable()
Destructor.
Definition: HashPtrTable.C:56
srcOptions erase("case")
bool erase(iterator &iter)
Erase entry specified by given iterator and delete the allocated pointer.
Definition: HashPtrTable.C:109
label k
Boltzmann constant.
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers...
Definition: HashPtrTable.H:51
void clear()
Clear all entries from table and delete any allocated pointers.
Definition: HashPtrTable.C:135
autoPtr< T > remove(iterator &iter)
Remove entry specified by given iterator.
Definition: HashPtrTable.C:87
autoPtr< T > release(iterator &iter)
Release ownership of the pointer and replace with a nullptr.
Definition: HashPtrTable.C:65
patchWriters clear()
HashPtrTable()=default
Default construct with default table capacity.
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:201
const volScalarField & T
typename parent_type::iterator iterator
Definition: HashPtrTable.H:103
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
void merge(HashPtrTable< T, Key, Hash > &source)
Attempts to extract entries from source parameter and insert them into this, does not overwrite exist...
Definition: HashPtrTable.C:148
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:168