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-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
35template<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
60template<class T, class Key, class Hash>
62(
63 HashPtrTable<T, Key, Hash>&& rhs
64)
65:
66 parent_type(std::move(rhs))
67{}
68
69
70// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
71
72template<class T, class Key, class Hash>
74{
75 clear();
76}
77
78
79// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
80
81template<class T, class Key, class Hash>
83{
84 if (iter.good())
85 {
86 autoPtr<T> old(iter.val());
87 iter.val() = nullptr;
88 return old;
89 }
90
91 return nullptr;
92}
93
94
95template<class T, class Key, class Hash>
97{
98 iterator iter(this->find(key));
99 return this->release(iter);
100}
101
102
103template<class T, class Key, class Hash>
105{
106 if (iter.good())
107 {
108 autoPtr<T> old(iter.val());
109 this->parent_type::erase(iter);
110 return old;
111 }
112
113 return nullptr;
114}
115
116
117template<class T, class Key, class Hash>
119{
120 iterator iter(this->find(key));
121 return this->remove(iter);
122}
123
124
125template<class T, class Key, class Hash>
127{
128 if (iter.good())
129 {
130 T* ptr = iter.val();
131
132 if (this->parent_type::erase(iter))
133 {
134 delete ptr;
135 return true;
137 }
138
139 return false;
140}
142
143template<class T, class Key, class Hash>
145{
146 iterator iter(this->find(key));
147 return this->erase(iter);
148}
149
150
151template<class T, class Key, class Hash>
153{
154 for (iterator iter = this->begin(); iter != this->end(); ++iter)
155 {
156 delete iter.val();
157 }
158
159 this->parent_type::clear();
160}
161
162
163// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
164
165template<class T, class Key, class Hash>
167(
169)
170{
171 if (this == &rhs)
172 {
173 return; // Self-assignment is a no-op
174 }
175
176 this->clear();
177
178 for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
179 {
180 const Key& k = iter.key();
181 const T* ptr = iter.val();
182
183 if (ptr)
184 {
185 this->set(k, new T(*ptr));
186 }
187 else
188 {
189 this->set(k, nullptr);
190 }
191 }
192}
193
194
195template<class T, class Key, class Hash>
197(
199)
200{
201 if (this == &rhs)
202 {
203 return; // Self-assignment is a no-op
204 }
205
206 this->clear();
207 this->transfer(rhs);
208}
209
210
211// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
212
213#include "HashPtrTableIO.C"
214
215// ************************************************************************* //
label k
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Definition: HashPtrTable.H:68
typename parent_type::iterator iterator
Definition: HashPtrTable.H:88
HashPtrTable()=default
Default construct with default table capacity.
bool erase(iterator &iter)
Definition: HashPtrTable.C:126
typename parent_type::const_iterator const_iterator
Definition: HashPtrTable.H:89
void clear()
Clear all entries from table and delete any allocated pointers.
Definition: HashPtrTable.C:152
~HashPtrTable()
Destructor.
Definition: HashPtrTable.C:73
unsigned int remove()
Remove and return the last element.
Definition: PackedListI.H:709
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
std::istream * release() noexcept
Return managed pointer and release ownership.
const volScalarField & T
patchWriters clear()
void set(List< bool > &bools, const labelUList &locations)
Set the listed locations (assign 'true').
Definition: BitOps.C:38
srcOptions erase("case")