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 
82 public:
83 
84  //- The template instance used for this table
86 
87  //- The template instance used for the parent HashTable
89 
90  using iterator = typename parent_type::iterator;
92 
93 
94  // Constructors
95 
96  //- Default construct with default table capacity
97  HashPtrTable() = default;
98 
99  //- Construct given initial table capacity
100  inline explicit HashPtrTable(const label size);
101 
102  //- Copy construct, making a copy of each element
103  HashPtrTable(const this_type& rhs);
104 
105  //- Move construct
106  inline HashPtrTable(this_type&& rhs);
107 
108  //- Construct from Istream using given Istream constructor class
109  template<class INew>
110  HashPtrTable(Istream& is, const INew& inew);
111 
112  //- Construct from Istream using default Istream constructor class
113  HashPtrTable(Istream& is);
114 
115  //- Construct from dictionary with default dictionary constructor class
116  explicit HashPtrTable(const dictionary& dict);
117 
118 
119  //- Destructor
120  ~HashPtrTable();
121 
122 
123  // Member Functions
124 
125  // Access
126 
127  //- Return const pointer associated with given entry or a nullptr
128  //- if the key does not exist in the table.
129  inline const T* test(const Key& key) const;
130 
131  //- Return const pointer associated with given entry or a nullptr
132  //- if the key does not exist in the table.
133  inline const T* get(const Key& key) const;
134 
135 
136  // Edit
137 
138  //- Release ownership of the pointer and replace with a nullptr
139  // Includes a safeguard against the end-iterator.
140  //
141  // \return the entry's old value as an encapsulated pointer.
142  autoPtr<T> release(iterator& iter);
143 
144  //- Release ownership of the pointer and replace with a nullptr
145  //
146  // \return the entry's old value as an encapsulated pointer.
147  autoPtr<T> release(const Key& key);
148 
149  //- Remove entry specified by given iterator.
150  // Includes a safeguard against the end-iterator.
151  //
152  // \return the entry's old value as an encapsulated pointer.
153  autoPtr<T> remove(iterator& iter);
154 
155  //- Remove entry specified by given key.
156  //
157  // \return the entry's old value as an encapsulated pointer.
158  autoPtr<T> remove(const Key& key);
159 
160  //- Erase entry specified by given iterator and delete the
161  //- allocated pointer.
162  // Includes a safeguard against the end-iterator.
163  //
164  // \return True if item was removed
165  bool erase(iterator& iter);
166 
167  //- Erase entry specified by given key and delete the
168  //- allocated pointer.
169  //
170  // \return True if item was removed
171  bool erase(const Key& key);
172 
173  //- Clear all entries from table and delete any allocated pointers
174  void clear();
175 
176  //- Attempts to extract entries from source parameter and insert them
177  //- into \c this, does not overwrite existing entries.
178  //- The source will contains any items that could not be merged.
179  void merge(HashPtrTable<T, Key, Hash>& source);
180 
181  //- Attempts to extract entries from source parameter and insert them
182  //- into \c this, does not overwrite existing entries.
183  //- The source will contains any items that could not be merged.
184  void merge(HashPtrTable<T, Key, Hash>&& source);
185 
186 
187  // Reading/writing
188 
189  //- Invoke write() on each non-null entry
190  void write(Ostream& os) const;
191 
192 
193  // Member Operators
194 
195  //- Copy assignment
196  void operator=(const this_type& rhs);
197 
198  //- Move assignment
199  void operator=(this_type&& rhs);
200 
201 
202  // IOstream Operators
203 
204  //- Clear table and read from Istream
205  friend Istream& operator>> <T, Key, Hash>
206  (
207  Istream& is,
209  );
210 
211 
212  // Override HashTable methods
213 
214  //- Emplace insert a new entry, not overwriting existing entries.
215  // \return True if the entry did not previously exist in the table.
216  template<class... Args>
217  inline bool emplace(const Key& key, Args&&... args);
218 
219  //- Emplace set an entry, overwriting any existing entries.
220  // \return Reference to the new element.
221  template<class... Args>
222  inline T& emplace_set(const Key& key, Args&&... args);
223 
224  //- No insert() with raw pointers (potential memory leaks).
225  //- Use insert() with autoPtr or set()
226  inline bool insert(const Key&, T*) = delete;
227 
228  //- Insert a new entry, not overwriting existing entries.
229  // \return True if the entry inserted (not previously in table)
230  inline bool insert(const Key& key, std::unique_ptr<T>&& ptr);
231 
232  //- Insert a new entry, not overwriting existing entries.
233  // \return True if the entry inserted (not previously in table)
234  inline bool insert(const Key& key, autoPtr<T>&& ptr);
235 
236  //- Assign a new entry, overwrites existing
237  inline bool set(const Key& key, T* ptr);
238 
239  //- Assign a new entry, overwrites existing
240  inline bool set(const Key& key, std::unique_ptr<T>&& ptr);
241 
242  //- Assign a new entry, overwrites existing
243  inline bool set(const Key& key, autoPtr<T>&& ptr);
244 
245  //- Assign a new entry from refPtr (move or clone), overwrites existing
246  inline bool set(const Key& key, const refPtr<T>& ptr);
247 
248  //- Assign a new entry from tmp (move or clone), overwrites existing
249  inline bool set(const Key& key, const tmp<T>& ptr);
250 
251 
252  // Housekeeping
253 
254  //- Insert a new entry, not overwriting existing entries.
255  // \return True if the entry inserted (not previously in table)
256  bool insert(const Key& key, autoPtr<T>& ptr)
257  {
258  return this->insert(key, std::move(ptr));
259  }
260 
261  //- Assign a new entry, overwrites existing
262  bool set(const Key& key, autoPtr<T>& ptr)
263  {
264  return this->set(key, std::move(ptr));
265  }
266 };
267 
268 
269 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
270 
271 } // End namespace Foam
272 
273 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
274 
275 #include "HashPtrTableI.H"
276 
277 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
278 
279 #ifdef NoRepository
280  #include "HashPtrTable.C"
281 #endif
282 
283 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
284 
285 #endif
286 
287 // ************************************************************************* //
HashPtrTable< T, Key, Hash > this_type
The template instance used for this table.
Definition: HashPtrTable.H:86
dictionary dict
~HashPtrTable()
Destructor.
Definition: HashPtrTable.C:56
Forward iterator with const access.
Definition: HashTable.H:1095
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
Definition: HashPtrTableI.H:75
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:47
void write(Ostream &os) const
Invoke write() on each non-null entry.
Forward iterator with non-const access.
Definition: HashTable.H:1003
label size() const noexcept
The number of elements in table.
Definition: HashTable.H:331
T & emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
Definition: HashPtrTableI.H:92
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:94
A HashTable similar to std::unordered_map.
Definition: HashTable.H:102
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
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:91
void operator=(const this_type &rhs)
Copy assignment.
typename parent_type::iterator iterator
Definition: HashPtrTable.H:93
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
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.