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-2022 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 
177  // Reading/writing
178 
179  //- Invoke write() on each non-null entry
180  void write(Ostream& os) const;
181 
182 
183  // Member Operators
184 
185  //- Copy assignment
186  void operator=(const this_type& rhs);
187 
188  //- Move assignment
189  void operator=(this_type&& rhs);
190 
191 
192  // IOstream Operators
193 
194  //- Clear table and read from Istream
195  friend Istream& operator>> <T, Key, Hash>
196  (
197  Istream& is,
199  );
200 
201 
202  // Override HashTable methods
203 
204  //- Emplace insert a new entry, not overwriting existing entries.
205  // \return True if the entry did not previously exist in the table.
206  template<class... Args>
207  inline bool emplace(const Key& key, Args&&... args);
208 
209  //- Emplace set an entry, overwriting any existing entries.
210  // \return True, since it always overwrites any entries.
211  template<class... Args>
212  inline bool emplace_set(const Key& key, Args&&... args);
213 
214  //- No insert() with raw pointers (potential memory leaks).
215  //- Use insert() with autoPtr or set()
216  inline bool insert(const Key&, T*) = delete;
217 
218  //- Insert a new entry, not overwriting existing entries.
219  // \return True if the entry inserted (not previously in table)
220  inline bool insert(const Key& key, std::unique_ptr<T>&& ptr);
221 
222  //- Insert a new entry, not overwriting existing entries.
223  // \return True if the entry inserted (not previously in table)
224  inline bool insert(const Key& key, autoPtr<T>&& ptr);
225 
226  //- Assign a new entry, overwrites existing
227  inline bool set(const Key& key, T* ptr);
228 
229  //- Assign a new entry, overwrites existing
230  inline bool set(const Key& key, std::unique_ptr<T>&& ptr);
231 
232  //- Assign a new entry, overwrites existing
233  inline bool set(const Key& key, autoPtr<T>&& ptr);
234 
235  //- Assign a new entry from refPtr (move or clone), overwrites existing
236  inline bool set(const Key& key, const refPtr<T>& ptr);
237 
238  //- Assign a new entry from tmp (move or clone), overwrites existing
239  inline bool set(const Key& key, const tmp<T>& ptr);
240 
241 
242  // Housekeeping
243 
244  //- Insert a new entry, not overwriting existing entries.
245  // \return True if the entry inserted (not previously in table)
246  bool insert(const Key& key, autoPtr<T>& ptr)
247  {
248  return this->insert(key, std::move(ptr));
249  }
250 
251  //- Assign a new entry, overwrites existing
252  bool set(const Key& key, autoPtr<T>& ptr)
253  {
254  return this->set(key, std::move(ptr));
255  }
256 };
257 
258 
259 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
260 
261 } // End namespace Foam
262 
263 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
264 
265 #include "HashPtrTableI.H"
266 
267 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
268 
269 #ifdef NoRepository
270  #include "HashPtrTable.C"
271 #endif
272 
273 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
274 
275 #endif
276 
277 // ************************************************************************* //
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:1070
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
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:45
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:978
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
bool emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
Definition: HashPtrTableI.H:92
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.