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-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 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 with default table capacity
105  HashPtrTable() = default;
106 
107  //- Construct with zero table capacity
108  explicit HashPtrTable(const Foam::zero) noexcept
109  :
111  {}
112 
113  //- Construct given initial table capacity
114  explicit HashPtrTable(const label initialCapacity)
115  :
116  parent_type(initialCapacity)
117  {}
118 
119  //- Copy construct, making a copy of each element
120  HashPtrTable(const this_type& rhs);
121 
122  //- Move construct
124  :
125  parent_type(std::move(rhs))
126  {}
127 
128  //- Construct from Istream using given Istream constructor class
129  template<class INew>
130  HashPtrTable(Istream& is, const INew& inew);
131 
132  //- Construct from Istream using default Istream constructor class
133  HashPtrTable(Istream& is);
134 
135  //- Construct from dictionary with default dictionary constructor class
136  explicit HashPtrTable(const dictionary& dict);
137 
139  //- Destructor
140  ~HashPtrTable();
141 
142 
143  // Member Functions
144 
145  // Access
146 
147  //- Return const pointer associated with given entry or a nullptr
148  //- if the key does not exist in the table.
149  inline const T* test(const Key& key) const;
150 
151  //- Return const pointer associated with given entry or a nullptr
152  //- if the key does not exist in the table.
153  inline const T* get(const Key& key) const;
154 
155 
156  // Edit
157 
158  //- Release ownership of the pointer and replace with a nullptr
159  // Includes a safeguard against the end-iterator.
160  //
161  // \return the entry's old value as an encapsulated pointer.
162  autoPtr<T> release(iterator& iter);
163 
164  //- Release ownership of the pointer and replace with a nullptr
165  //
166  // \return the entry's old value as an encapsulated pointer.
167  autoPtr<T> release(const Key& key);
168 
169  //- Remove entry specified by given iterator.
170  // Includes a safeguard against the end-iterator.
171  //
172  // \return the entry's old value as an encapsulated pointer.
173  autoPtr<T> remove(iterator& iter);
174 
175  //- Remove entry specified by given key.
176  //
177  // \return the entry's old value as an encapsulated pointer.
178  autoPtr<T> remove(const Key& key);
179 
180  //- Erase entry specified by given iterator and delete the
181  //- allocated pointer.
182  // Includes a safeguard against the end-iterator.
183  //
184  // \return True if item was removed
185  bool erase(iterator& iter);
186 
187  //- Erase entry specified by given key and delete the
188  //- allocated pointer.
189  //
190  // \return True if item was removed
191  bool erase(const Key& key);
192 
193  //- Clear all entries from table and delete any allocated pointers
194  void clear();
195 
196  //- Attempts to extract entries from source parameter and insert them
197  //- into \c this, does not overwrite existing entries.
198  //- The source will contains any items that could not be merged.
199  void merge(HashPtrTable<T, Key, Hash>& source);
200 
201  //- Attempts to extract entries from source parameter and insert them
202  //- into \c this, does not overwrite existing entries.
203  //- The source will contains any items that could not be merged.
204  void merge(HashPtrTable<T, Key, Hash>&& source);
205 
206 
207  // Reading/writing
208 
209  //- Invoke write() on each non-null entry
210  void write(Ostream& os) const;
211 
212 
213  // Member Operators
214 
215  //- Copy assignment
216  void operator=(const this_type& rhs);
217 
218  //- Move assignment
219  void operator=(this_type&& rhs);
220 
221 
222  // IOstream Operators
223 
224  //- Clear table and read from Istream
225  friend Istream& operator>> <T, Key, Hash>
226  (
227  Istream& is,
229  );
230 
231 
232  // Override HashTable methods
233 
234  //- Emplace insert a new entry, not overwriting existing entries.
235  // \return True if the entry did not previously exist in the table.
236  template<class... Args>
237  inline bool emplace(const Key& key, Args&&... args);
238 
239  //- Emplace set an entry, overwriting any existing entries.
240  // \return Reference to the new element.
241  template<class... Args>
242  inline T& emplace_set(const Key& key, Args&&... args);
243 
244  //- Like emplace_set() but will not overwrite an occupied (non-null)
245  //- location.
246  // \param key - the location to set (unless already defined)
247  // \param args arguments to forward to the constructor of the element
248  // \return reference to the existing or the new element.
249  template<class... Args>
250  inline T& try_emplace(const Key& key, Args&&... args);
251 
252  //- No insert() with raw pointers (potential memory leaks).
253  //- Use insert() with autoPtr or set()
254  inline bool insert(const Key&, T*) = delete;
255 
256  //- Insert a new entry, not overwriting existing entries.
257  // \return True if the entry inserted (not previously in table)
258  inline bool insert(const Key& key, std::unique_ptr<T>&& ptr);
259 
260  //- Insert a new entry, not overwriting existing entries.
261  // \return True if the entry inserted (not previously in table)
262  inline bool insert(const Key& key, autoPtr<T>&& ptr);
263 
264  //- Assign a new entry, overwrites existing
265  inline bool set(const Key& key, T* ptr);
266 
267  //- Assign a new entry, overwrites existing
268  inline bool set(const Key& key, std::unique_ptr<T>&& ptr);
269 
270  //- Assign a new entry, overwrites existing
271  inline bool set(const Key& key, autoPtr<T>&& ptr);
272 
273  //- Assign a new entry from refPtr (move or clone), overwrites existing
274  inline bool set(const Key& key, const refPtr<T>& ptr);
275 
276  //- Assign a new entry from tmp (move or clone), overwrites existing
277  inline bool set(const Key& key, const tmp<T>& ptr);
278 
279 
280  // Housekeeping
281 
282  //- Insert a new entry, not overwriting existing entries.
283  // \return True if the entry inserted (not previously in table)
284  bool insert(const Key& key, autoPtr<T>& ptr)
285  {
286  return this->insert(key, std::move(ptr));
287  }
288 
289  //- Assign a new entry, overwrites existing
290  bool set(const Key& key, autoPtr<T>& ptr)
291  {
292  return this->set(key, std::move(ptr));
293  }
294 };
295 
296 
297 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
298 
299 } // End namespace Foam
300 
301 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
302 
303 #include "HashPtrTableI.H"
304 
305 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
306 
307 #ifdef NoRepository
308  #include "HashPtrTable.C"
309 #endif
310 
311 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
312 
313 #endif
314 
315 // ************************************************************************* //
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:1116
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
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:1024
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
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: Scalar.H:258
HashPtrTable()=default
Default construct with default table capacity.
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
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
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.