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 -------------------------------------------------------------------------------
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 Class
28  Foam::HashPtrTable
29 
30 Description
31  A HashTable of pointers to objects of type <T>,
32  with deallocation management of the pointers.
33 
34 SourceFiles
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 
49 namespace Foam
50 {
51 
52 // Forward Declarations
53 template<class T> class autoPtr;
54 template<class T> class refPtr;
55 template<class T> class tmp;
56 template<class T, class Key, class Hash> class HashPtrTable;
57 
58 template<class T, class Key, class Hash>
60 
61 
62 /*---------------------------------------------------------------------------*\
63  Class HashPtrTable Declaration
64 \*---------------------------------------------------------------------------*/
65 
66 template<class T, class Key=word, class Hash=Foam::Hash<Key>>
67 class 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 
90 public:
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;
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)
112  :
113  parent_type(initialCapacity)
114  {}
115 
116  //- Copy construct, making a copy of each element
117  HashPtrTable(const this_type& rhs);
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
130  HashPtrTable(Istream& is);
131 
132  //- Construct from dictionary with default dictionary constructor class
133  explicit HashPtrTable(const dictionary& dict);
134 
136  //- Destructor
137  ~HashPtrTable();
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 
153  // Edit
154 
155  //- Release ownership of the pointer and replace with a nullptr
156  // Includes a safeguard against the end-iterator.
157  //
158  // \return the entry's old value as an encapsulated pointer.
159  autoPtr<T> release(iterator& iter);
160 
161  //- Release ownership of the pointer and replace with a nullptr
162  //
163  // \return the entry's old value as an encapsulated pointer.
164  autoPtr<T> release(const Key& key);
165 
166  //- Remove entry specified by given iterator.
167  // Includes a safeguard against the end-iterator.
168  //
169  // \return the entry's old value as an encapsulated pointer.
170  autoPtr<T> remove(iterator& iter);
171 
172  //- Remove entry specified by given key.
173  //
174  // \return the entry's old value as an encapsulated pointer.
175  autoPtr<T> remove(const Key& key);
176 
177  //- Erase entry specified by given iterator and delete the
178  //- allocated pointer.
179  // Includes a safeguard against the end-iterator.
180  //
181  // \return True if item was removed
182  bool erase(iterator& iter);
183 
184  //- Erase entry specified by given key and delete the
185  //- allocated pointer.
186  //
187  // \return True if item was removed
188  bool erase(const Key& key);
189 
190  //- Clear all entries from table and delete any allocated pointers
191  void clear();
192 
193  //- Attempts to extract entries from source parameter and insert them
194  //- into \c this, does not overwrite existing entries.
195  //- The source will contains any items that could not be merged.
196  void merge(HashPtrTable<T, Key, Hash>& source);
197 
198  //- Attempts to extract entries from source parameter and insert them
199  //- into \c this, does not overwrite existing entries.
200  //- The source will contains any items that could not be merged.
201  void merge(HashPtrTable<T, Key, Hash>&& source);
202 
203 
204  // Reading/writing
205 
206  //- Invoke write() on each non-null entry
207  void write(Ostream& os) const;
208 
209 
210  // Member Operators
211 
212  //- Copy assignment
213  void operator=(const this_type& rhs);
214 
215  //- Move assignment
216  void operator=(this_type&& rhs);
217 
218 
219  // IOstream Operators
220 
221  //- Clear table and read from Istream
222  friend Istream& operator>> <T, Key, Hash>
223  (
224  Istream& is,
226  );
227 
228 
229  // Override HashTable methods
230 
231  //- Emplace insert a new entry, not overwriting existing entries.
232  // \return True if the entry did not previously exist in the table.
233  template<class... Args>
234  inline bool emplace(const Key& key, Args&&... args);
235 
236  //- Emplace set an entry, overwriting any existing entries.
237  // \return Reference to the new element.
238  template<class... Args>
239  inline T& emplace_set(const Key& key, Args&&... args);
240 
241  //- Like emplace_set() but will not overwrite an occupied (non-null)
242  //- location.
243  // \param key - the location to set (unless already defined)
244  // \param args arguments to forward to the constructor of the element
245  // \return reference to the existing or the new element.
246  template<class... Args>
247  inline T& try_emplace(const Key& key, Args&&... args);
248 
249  //- No insert() with raw pointers (potential memory leaks).
250  //- Use insert() with autoPtr or set()
251  inline bool insert(const Key&, T*) = delete;
252 
253  //- Insert a new entry, not overwriting existing entries.
254  // \return True if the entry inserted (not previously in table)
255  inline bool insert(const Key& key, std::unique_ptr<T>&& ptr);
256 
257  //- Insert a new entry, not overwriting existing entries.
258  // \return True if the entry inserted (not previously in table)
259  inline bool insert(const Key& key, autoPtr<T>&& ptr);
260 
261  //- Assign a new entry, overwrites existing
262  inline bool set(const Key& key, T* ptr);
263 
264  //- Assign a new entry, overwrites existing
265  inline bool set(const Key& key, std::unique_ptr<T>&& ptr);
266 
267  //- Assign a new entry, overwrites existing
268  inline bool set(const Key& key, autoPtr<T>&& ptr);
269 
270  //- Assign a new entry from refPtr (move or clone), overwrites existing
271  inline bool set(const Key& key, const refPtr<T>& ptr);
272 
273  //- Assign a new entry from tmp (move or clone), overwrites existing
274  inline bool set(const Key& key, const tmp<T>& ptr);
275 
276 
277  // Housekeeping
278 
279  //- Insert a new entry, not overwriting existing entries.
280  // \return True if the entry inserted (not previously in table)
281  bool insert(const Key& key, autoPtr<T>& ptr)
282  {
283  return this->insert(key, std::move(ptr));
284  }
285 
286  //- Assign a new entry, overwrites existing
287  bool set(const Key& key, autoPtr<T>& ptr)
288  {
289  return this->set(key, std::move(ptr));
290  }
291 };
292 
293 
294 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
295 
296 } // End namespace Foam
297 
298 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
299 
300 #include "HashPtrTableI.H"
301 
302 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
303 
304 #ifdef NoRepository
305  #include "HashPtrTable.C"
306 #endif
307 
308 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
309 
310 #endif
311 
312 // ************************************************************************* //
T & try_emplace(const Key &key, Args &&... args)
Like emplace_set() but will not overwrite an occupied (non-null) location.
HashPtrTable< T, Key, Hash > this_type
The template instance used for this table.
Definition: HashPtrTable.H:96
dictionary dict
~HashPtrTable()
Destructor.
Definition: HashPtrTable.C:56
Forward iterator with const access.
Definition: HashTable.H:1132
constexpr HashPtrTable() noexcept=default
Default construct: empty without allocation (capacity=0)
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
constexpr HashPtrTable(Foam::zero) noexcept
Construct empty without allocation (capacity=0)
Definition: HashPtrTable.H:117
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
Definition: HashPtrTableI.H:56
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
bool erase(iterator &iter)
Erase entry specified by given iterator and delete the allocated pointer.
Definition: HashPtrTable.C:109
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers...
Definition: HashPtrTable.H:51
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
void clear()
Clear all entries from table and delete any allocated pointers.
Definition: HashPtrTable.C:135
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...
Definition: HashPtrTableI.H:28
void write(Ostream &os) const
Invoke write() on each non-null entry.
Forward iterator with non-const access.
Definition: HashTable.H:1040
T & emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
autoPtr< T > release(iterator &iter)
Release ownership of the pointer and replace with a nullptr.
Definition: HashPtrTable.C:65
Istream & operator>>(Istream &, directionInfo &)
typename parent_type::const_iterator const_iterator
Definition: HashPtrTable.H:104
auto key(const Type &t) -> std::enable_if_t< std::is_enum_v< Type >, std::underlying_type_t< Type > >
Definition: foamGltfBase.H:103
A HashTable similar to std::unordered_map.
Definition: HashTable.H:108
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: scalarImpl.H:255
OBJstream os(runTime.globalPath()/outputName)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
HashTable< T *, Key, Hash > parent_type
The template instance used for the parent HashTable.
Definition: HashPtrTable.H:101
void operator=(const this_type &rhs)
Copy assignment.
typename parent_type::iterator iterator
Definition: HashPtrTable.H:103
bool insert(const Key &, T *)=delete
No insert() with raw pointers (potential memory leaks). Use insert() with autoPtr or set() ...
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:47
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:46
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
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Foam::argList args(argc, argv)
Namespace for OpenFOAM.