Loading...
Searching...
No Matches
HashPtrTable.H
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-2025 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
27Class
28 Foam::HashPtrTable
29
30Description
31 A HashTable of pointers to objects of type <T>,
32 with deallocation management of the pointers.
33
34SourceFiles
35 HashPtrTable.C
36 HashPtrTableI.H
37 HashPtrTableIO.C
38
39\*---------------------------------------------------------------------------*/
40
41#ifndef Foam_HashPtrTable_H
42#define Foam_HashPtrTable_H
43
44#include "HashTable.H"
45#include <memory>
46
47// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48
49namespace Foam
50{
51
52// Forward Declarations
53template<class T> class autoPtr;
54template<class T> class refPtr;
55template<class T> class tmp;
56template<class T, class Key, class Hash> class HashPtrTable;
57
58template<class T, class Key, class Hash>
60
61
62/*---------------------------------------------------------------------------*\
63 Class HashPtrTable Declaration
64\*---------------------------------------------------------------------------*/
65
66template<class T, class Key=word, class Hash=Foam::Hash<Key>>
67class HashPtrTable
68:
69 public HashTable<T*, Key, Hash>
70{
71 // Private Member Functions
72
73 //- Read from Istream using Istream constructor class
74 template<class INew>
75 void readIstream(Istream& is, const INew& inew);
76
77 //- Read from dictionary using Istream constructor class
78 template<class INew>
79 void read(const dictionary& dict, const INew& inew);
80
81 //- Implementation for emplace_set and try_emplace
82 template<class... Args>
83 inline T& emplaceImpl
84 (
85 const bool overwrite,
86 const Key& key,
87 Args&&... args
88 );
89
90public:
91
92 //- The template instance used for this table
94
95 //- The template instance used for the parent HashTable
97
98 using iterator = typename parent_type::iterator;
99 using const_iterator = typename parent_type::const_iterator;
100
102 // Constructors
104 //- Default construct: empty without allocation (capacity=0)
105 constexpr HashPtrTable() noexcept = default;
106
107 //- Construct empty without allocation (capacity=0)
108 explicit constexpr HashPtrTable(Foam::zero) noexcept : this_type() {}
109
110 //- Construct given initial table capacity
111 explicit HashPtrTable(const label initialCapacity)
113 parent_type(initialCapacity)
114 {}
115
116 //- Copy construct, making a copy of each element
118
119 //- Move construct
121 :
122 parent_type(std::move(rhs))
123 {}
124
125 //- Construct from Istream using given Istream constructor class
126 template<class INew>
127 HashPtrTable(Istream& is, const INew& inew);
128
129 //- Construct from Istream using default Istream constructor class
131
132 //- Construct from dictionary with default dictionary constructor class
133 explicit HashPtrTable(const dictionary& dict);
134
136 //- Destructor
138
139
140 // Member Functions
141
142 // Access
143
144 //- Return const pointer associated with given entry or a nullptr
145 //- if the key does not exist in the table.
146 inline const T* test(const Key& key) const;
147
148 //- Return const pointer associated with given entry or a nullptr
149 //- if the key does not exist in the table.
150 inline const T* get(const Key& key) const;
151
152 //- Return pointer associated with given entry or a nullptr
153 //- if the key does not exist in the table.
154 inline T* get(const Key& key);
155
156
157 // Edit
158
159 //- Release ownership of the pointer and replace with a nullptr
160 // Includes a safeguard against the end-iterator.
161 //
162 // \return the entry's old value as an encapsulated pointer.
164
165 //- Release ownership of the pointer and replace with a nullptr
166 //
167 // \return the entry's old value as an encapsulated pointer.
168 autoPtr<T> release(const Key& key);
169
170 //- Remove entry specified by given iterator.
171 // Includes a safeguard against the end-iterator.
172 //
173 // \return the entry's old value as an encapsulated pointer.
175
176 //- Remove entry specified by given key.
177 //
178 // \return the entry's old value as an encapsulated pointer.
179 autoPtr<T> remove(const Key& key);
180
181 //- Erase entry specified by given iterator and delete the
182 //- allocated pointer.
183 // Includes a safeguard against the end-iterator.
184 //
185 // \return True if item was removed
186 bool erase(iterator& iter);
187
188 //- Erase entry specified by given key and delete the
189 //- allocated pointer.
190 //
191 // \return True if item was removed
192 bool erase(const Key& key);
193
194 //- Clear all entries from table and delete any allocated pointers
195 void clear();
196
197 //- Attempts to extract entries from source parameter and insert them
198 //- into \c this, does not overwrite existing entries.
199 //- The source will contains any items that could not be merged.
200 void merge(HashPtrTable<T, Key, Hash>& source);
201
202 //- Attempts to extract entries from source parameter and insert them
203 //- into \c this, does not overwrite existing entries.
204 //- The source will contains any items that could not be merged.
205 void merge(HashPtrTable<T, Key, Hash>&& source);
206
207
208 // Reading/writing
209
210 //- Invoke write() on each non-null entry
211 void write(Ostream& os) const;
213
214 // Member Operators
215
216 //- Copy assignment
217 void operator=(const this_type& rhs);
218
219 //- Move assignment
221
222
223 // IOstream Operators
224
225 //- Clear table and read from Istream
226 friend Istream& operator>> <T, Key, Hash>
227 (
228 Istream& is,
230 );
231
232
233 // Override HashTable methods
234
235 //- Emplace insert a new entry, not overwriting existing entries.
236 // \return True if the entry did not previously exist in the table.
237 template<class... Args>
238 inline bool emplace(const Key& key, Args&&... args);
240 //- Emplace set an entry, overwriting any existing entries.
241 // \param key - the location to set
242 // \param args arguments to forward to the constructor of the element
243 // \return reference to the new element.
244 template<class... Args>
245 inline T& emplace_set(const Key& key, Args&&... args);
246
247 //- Like emplace_set() but will not overwrite an occupied (non-null)
248 //- location.
249 // \param key - the location to set (unless already defined)
250 // \param args arguments to forward to the constructor of the element
251 // \return reference to the existing or the new element.
252 template<class... Args>
253 inline T& try_emplace(const Key& key, Args&&... args);
254
255 //- No insert() with raw pointers (potential memory leaks).
256 //- Use insert() with autoPtr or set()
257 inline bool insert(const Key&, T*) = delete;
259 //- Insert a new entry, not overwriting existing entries.
260 // \return True if the entry inserted (not previously in table)
261 inline bool insert(const Key& key, std::unique_ptr<T>&& ptr);
262
263 //- Insert a new entry, not overwriting existing entries.
264 // \return True if the entry inserted (not previously in table)
265 inline bool insert(const Key& key, autoPtr<T>&& ptr);
267 //- Assign a new entry, overwrites existing
268 inline bool set(const Key& key, T* ptr);
269
270 //- Assign a new entry, overwrites existing
271 inline bool set(const Key& key, std::unique_ptr<T>&& ptr);
272
273 //- Assign a new entry, overwrites existing
274 inline bool set(const Key& key, autoPtr<T>&& ptr);
275
276 //- Assign a new entry from refPtr (move or clone), overwrites existing
277 inline bool set(const Key& key, const refPtr<T>& ptr);
278
279 //- Assign a new entry from tmp (move or clone), overwrites existing
280 inline bool set(const Key& key, const tmp<T>& ptr);
281
282
283 // Housekeeping
284
285 //- Insert a new entry, not overwriting existing entries.
286 // \return True if the entry inserted (not previously in table)
287 bool insert(const Key& key, autoPtr<T>& ptr)
288 {
289 return this->insert(key, std::move(ptr));
290 }
291
292 //- Assign a new entry, overwrites existing
293 bool set(const Key& key, autoPtr<T>& ptr)
294 {
295 return this->set(key, std::move(ptr));
296 }
297};
298
299
300// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
301
302} // End namespace Foam
303
304// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
305
306#include "HashPtrTableI.H"
307
308// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
309
310#ifdef NoRepository
311 #include "HashPtrTable.C"
312#endif
313
314// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
315
316#endif
317
318// ************************************************************************* //
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
void merge(HashPtrTable< T, Key, Hash > &&source)
Attempts to extract entries from source parameter and insert them into this, does not overwrite exist...
HashPtrTable(const label initialCapacity)
Construct given initial table capacity.
bool set(const Key &key, const refPtr< T > &ptr)
Assign a new entry from refPtr (move or clone), overwrites existing.
bool insert(const Key &key, std::unique_ptr< T > &&ptr)
Insert a new entry, not overwriting existing entries.
bool set(const Key &key, autoPtr< T > &&ptr)
Assign a new entry, overwrites existing.
const T * get(const Key &key) const
Return const pointer associated with given entry or a nullptr if the key does not exist in the table.
autoPtr< T > remove(const Key &key)
Remove entry specified by given key.
bool insert(const Key &key, autoPtr< T > &ptr)
Insert a new entry, not overwriting existing entries.
typename parent_type::iterator iterator
HashPtrTable(Istream &is, const INew &inew)
Construct from Istream using given Istream constructor class.
bool erase(const Key &key)
Erase entry specified by given key and delete the allocated pointer.
constexpr HashPtrTable() noexcept=default
Default construct: empty without allocation (capacity=0).
const T * test(const Key &key) const
Return const pointer associated with given entry or a nullptr if the key does not exist in the table.
bool insert(const Key &key, autoPtr< T > &&ptr)
Insert a new entry, not overwriting existing entries.
constexpr HashPtrTable(Foam::zero) noexcept
Construct empty without allocation (capacity=0).
autoPtr< T > release(const Key &key)
Release ownership of the pointer and replace with a nullptr.
T & emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
HashPtrTable(const dictionary &dict)
Construct from dictionary with default dictionary constructor class.
autoPtr< T > remove(iterator &iter)
Remove entry specified by given iterator.
bool set(const Key &key, const tmp< T > &ptr)
Assign a new entry from tmp (move or clone), overwrites existing.
void operator=(this_type &&rhs)
Move assignment.
bool insert(const Key &, T *)=delete
No insert() with raw pointers (potential memory leaks). Use insert() with autoPtr or set().
void write(Ostream &os) const
Invoke write() on each non-null entry.
void merge(HashPtrTable< T, Key, Hash > &source)
Attempts to extract entries from source parameter and insert them into this, does not overwrite exist...
bool set(const Key &key, autoPtr< T > &ptr)
Assign a new entry, overwrites existing.
bool erase(iterator &iter)
Erase entry specified by given iterator and delete the allocated pointer.
HashPtrTable(const this_type &rhs)
Copy construct, making a copy of each element.
HashPtrTable< T, Key, Hash > this_type
The template instance used for this table.
bool set(const Key &key, T *ptr)
Assign a new entry, overwrites existing.
T * get(const Key &key)
Return pointer associated with given entry or a nullptr if the key does not exist in the table.
typename parent_type::const_iterator const_iterator
T & try_emplace(const Key &key, Args &&... args)
Like emplace_set() but will not overwrite an occupied (non-null) location.
HashPtrTable(this_type &&rhs) noexcept
Move construct.
bool set(const Key &key, std::unique_ptr< T > &&ptr)
Assign a new entry, overwrites existing.
void operator=(const this_type &rhs)
Copy assignment.
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
void clear()
Clear all entries from table and delete any allocated pointers.
autoPtr< T > release(iterator &iter)
Release ownership of the pointer and replace with a nullptr.
HashTable< T *, Key, Hash > parent_type
HashPtrTable(Istream &is)
Construct from Istream using default Istream constructor class.
~HashPtrTable()
Destructor.
constexpr HashTable() noexcept
Default construct: empty without allocation (capacity=0).
Definition HashTable.C:33
A helper class when constructing from an Istream or dictionary.
Definition INew.H:47
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A class for managing references or pointers (no reference counting).
Definition refPtr.H:54
A class for managing temporary objects.
Definition tmp.H:75
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition zero.H:58
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
Istream & operator>>(Istream &, directionInfo &)
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
const direction noexcept
Definition scalarImpl.H:265
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
runTime write()
dictionary dict
Foam::argList args(argc, argv)
nonInt insert("surfaceSum(((S|magSf)*S)")
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition Hash.H:48