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-2021 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 HashPtrTable_H
42#define HashPtrTable_H
43
44#include "HashTable.H"
45#include <memory>
46
47// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48
49namespace Foam
50{
51
52// Forward Declarations
53
54template<class T> class autoPtr;
55template<class T, class Key, class Hash> class HashPtrTable;
56
57template<class T, class Key, class Hash>
59
60
61/*---------------------------------------------------------------------------*\
62 Class HashPtrTable Declaration
63\*---------------------------------------------------------------------------*/
64
65template<class T, class Key=word, class Hash=Foam::Hash<Key>>
66class HashPtrTable
67:
68 public HashTable<T*, Key, Hash>
69{
70 // Private Member Functions
71
72 //- Read from Istream using Istream constructor class
73 template<class INew>
74 void readIstream(Istream& is, const INew& inew);
75
76 //- Read from dictionary using Istream constructor class
77 template<class INew>
78 void read(const dictionary& dict, const INew& inew);
79
80
81public:
82
83 //- The template instance used for this table
85
86 //- The template instance used for the parent HashTable
91
92
93 // Constructors
94
95 //- Default construct with default table capacity
96 HashPtrTable() = default;
97
98 //- Construct given initial table capacity
99 inline explicit HashPtrTable(const label size);
100
101 //- Construct from Istream using given Istream constructor class
102 template<class INew>
103 HashPtrTable(Istream& is, const INew& inew);
104
105 //- Construct from Istream using default Istream constructor class
107
108 //- Construct from dictionary with default dictionary constructor class
109 explicit HashPtrTable(const dictionary& dict);
110
111 //- Copy construct, making a copy of each element
112 HashPtrTable(const this_type& rhs);
113
114 //- Move construct
115 HashPtrTable(this_type&& rhs);
116
117
118 //- Destructor
120
121
122 // Member Functions
123
124 // Access
125
126 //- Return const pointer associated with given entry,
127 //- returning a nullptr if the key does not exist in the table.
128 inline const T* get(const Key& key) const;
129
130
131 // Edit
132
133 //- Release ownership of the pointer and replace with a nullptr
134 // Includes a safeguard against the end-iterator.
135 //
136 // \return the entry's old value as an encapsulated pointer.
138
139 //- Release ownership of the pointer and replace with a nullptr
140 //
141 // \return the entry's old value as an encapsulated pointer.
142 autoPtr<T> release(const Key& key);
143
144 //- Remove entry specified by given iterator.
145 // Includes a safeguard against the end-iterator.
146 //
147 // \return the entry's old value as an encapsulated pointer.
149
150 //- Remove entry specified by given key.
151 //
152 // \return the entry's old value as an encapsulated pointer.
153 autoPtr<T> remove(const Key& key);
154
155 //- Erase entry specified by given iterator and delete the
156 //- allocated pointer.
157 // Includes a safeguard against the end-iterator.
158 //
159 // \return True if item was removed
160 bool erase(iterator& iter);
161
162 //- Erase entry specified by given key and delete the
163 //- allocated pointer.
164 //
165 // \return True if item was removed
166 bool erase(const Key& key);
167
168 //- Clear all entries from table and delete any allocated pointers
169 void clear();
170
171
172 // Reading/writing
173
174 //- Invoke write() on each non-null entry
175 void write(Ostream& os) const;
176
177
178 // Member Operators
179
180 //- Copy assignment
181 void operator=(const this_type& rhs);
182
183 //- Move assignment
184 void operator=(this_type&& rhs);
185
186
187 // IOstream Operators
188
189 //- Clear table and read from Istream
190 friend Istream& operator>> <T, Key, Hash>
191 (
192 Istream& is,
194 );
195
196
197 // Override HashTable methods
198
199 //- Emplace insert a new entry, not overwriting existing entries.
200 // \return True if the entry did not previously exist in the table.
201 template<class... Args>
202 inline bool emplace(const Key& key, Args&&... args);
203
204 //- Emplace set an entry, overwriting any existing entries.
205 // \return True, since it always overwrites any entries.
206 template<class... Args>
207 inline bool emplace_set(const Key& key, Args&&... args);
208
209 //- No insert() with raw pointers (potential memory leaks).
210 //- Use insert() with autoPtr or set()
211 inline bool insert(const Key&, T*) = delete;
212
213 //- Insert a new entry, not overwriting existing entries.
214 //
215 // \return True if the entry inserted (not previously in table)
216 inline bool insert(const Key& key, autoPtr<T>& ptr);
217
218 //- Insert a new entry, not overwriting existing entries.
219 //
220 // \return True if the entry inserted (not previously in table)
221 inline bool insert(const Key& key, autoPtr<T>&& ptr);
222
223 //- Insert a new entry, not overwriting existing entries.
224 //
225 // \return True if the entry inserted (not previously in table)
226 inline bool insert(const Key& key, std::unique_ptr<T>&& ptr);
227
228 //- Assign a new entry, overwriting existing entries.
229 inline bool set(const Key& key, T* ptr);
230
231 //- Assign a new entry, overwriting existing entries.
232 inline bool set(const Key& key, autoPtr<T>& ptr);
233
234 //- Assign a new entry, overwriting existing entries.
235 inline bool set(const Key& key, autoPtr<T>&& ptr);
236
237 //- Assign a new entry, overwriting existing entries.
238 inline bool set(const Key& key, std::unique_ptr<T>&& ptr);
239};
240
241
242// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
243
244} // End namespace Foam
245
246// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
247
248#include "HashPtrTableI.H"
249
250// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
251
252#ifdef NoRepository
253 #include "HashPtrTable.C"
254#endif
255
256// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
257
258#endif
259
260// ************************************************************************* //
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Definition: HashPtrTable.H:68
const T * get(const Key &key) const
Definition: HashPtrTableI.H:42
typename parent_type::iterator iterator
Definition: HashPtrTable.H:88
autoPtr< T > remove(iterator &iter)
Remove entry specified by given iterator.
Definition: HashPtrTable.C:104
void operator=(this_type &&rhs)
Move assignment.
bool insert(const Key &, T *)=delete
bool emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
Definition: HashPtrTableI.H:73
HashPtrTable()=default
Default construct with default table capacity.
bool erase(iterator &iter)
Definition: HashPtrTable.C:126
HashPtrTable(const this_type &rhs)
Copy construct, making a copy of each element.
HashPtrTable(this_type &&rhs)
Move construct.
HashPtrTable< T, Key, Hash > this_type
The template instance used for this table.
Definition: HashPtrTable.H:83
bool set(const Key &key, T *ptr)
Assign a new entry, overwriting existing entries.
typename parent_type::const_iterator const_iterator
Definition: HashPtrTable.H:89
void operator=(const this_type &rhs)
Copy assignment.
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
Definition: HashPtrTableI.H:56
void clear()
Clear all entries from table and delete any allocated pointers.
Definition: HashPtrTable.C:152
autoPtr< T > release(iterator &iter)
Release ownership of the pointer and replace with a nullptr.
Definition: HashPtrTable.C:82
HashTable< T *, Key, Hash > parent_type
The template instance used for the parent HashTable.
Definition: HashPtrTable.H:86
~HashPtrTable()
Destructor.
Definition: HashPtrTable.C:73
Forward iterator with const access.
Definition: HashTable.H:794
Forward iterator with non-const access.
Definition: HashTable.H:720
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:52
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
const volScalarField & T
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
Istream & operator>>(Istream &, directionInfo &)
runTime write()
dictionary dict
Foam::argList args(argc, argv)
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:54