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 -------------------------------------------------------------------------------
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 template<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 
72 template<class T, class Key, class Hash>
74 {
75  clear();
76 }
77 
78 
79 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
80 
81 template<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 
95 template<class T, class Key, class Hash>
97 {
98  iterator iter(this->find(key));
99  return this->release(iter);
100 }
101 
102 
103 template<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 
117 template<class T, class Key, class Hash>
119 {
120  iterator iter(this->find(key));
121  return this->remove(iter);
122 }
123 
124 
125 template<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;
136  }
137  }
138 
139  return false;
140 }
141 
142 
143 template<class T, class Key, class Hash>
145 {
146  iterator iter(this->find(key));
147  return this->erase(iter);
148 }
149 
150 
151 template<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 
165 template<class T, class Key, class Hash>
166 void Foam::HashPtrTable<T, Key, Hash>::operator=
167 (
168  const HashPtrTable<T, Key, Hash>& rhs
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 
195 template<class T, class Key, class Hash>
197 (
198  HashPtrTable<T, Key, Hash>&& rhs
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 // ************************************************************************* //
Foam::HashPtrTable::erase
bool erase(iterator &iter)
Definition: HashPtrTable.C:126
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
stdFoam::begin
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
Definition: stdFoam.H:97
Foam::HashPtrTable::remove
autoPtr< T > remove(iterator &iter)
Remove entry specified by given iterator.
Definition: HashPtrTable.C:104
Foam::HashPtrTable::~HashPtrTable
~HashPtrTable()
Destructor.
Definition: HashPtrTable.C:73
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
erase
srcOptions erase("case")
Foam::HashPtrTable::release
autoPtr< T > release(iterator &iter)
Release ownership of the pointer and replace with a nullptr.
Definition: HashPtrTable.C:82
Foam::HashPtrTable::clear
void clear()
Clear all entries from table and delete any allocated pointers.
Definition: HashPtrTable.C:152
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()=default
Default construct with default table capacity.
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
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>, with deallocation management of the pointers.
Definition: HashPtrTable.H:54
HashPtrTable.H
k
label k
Boltzmann constant.
Definition: LISASMDCalcMethod2.H:41
Foam::HashPtrTable< exprResult >::iterator
typename parent_type::iterator iterator
Definition: HashPtrTable.H:88
autoPtr.H