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 -------------------------------------------------------------------------------
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 HashPtrTable_H
42 #define HashPtrTable_H
43 
44 #include "HashTable.H"
45 #include <memory>
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 // Forward Declarations
53 
54 template<class T> class autoPtr;
55 template<class T, class Key, class Hash> class HashPtrTable;
56 
57 template<class T, class Key, class Hash>
59 
60 
61 /*---------------------------------------------------------------------------*\
62  Class HashPtrTable Declaration
63 \*---------------------------------------------------------------------------*/
64 
65 template<class T, class Key=word, class Hash=Foam::Hash<Key>>
66 class 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 
81 public:
82 
83  //- The template instance used for this table
85 
86  //- The template instance used for the parent HashTable
88 
89  using iterator = typename parent_type::iterator;
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
106  HashPtrTable(Istream& is);
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
119  ~HashPtrTable();
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.
137  autoPtr<T> release(iterator& iter);
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.
148  autoPtr<T> remove(iterator& iter);
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 // ************************************************************************* //
Foam::HashPtrTable::erase
bool erase(iterator &iter)
Definition: HashPtrTable.C:126
HashTable.H
Foam::HashTable::iterator
Forward iterator with non-const access.
Definition: HashTable.H:693
Foam::HashPtrTable::remove
autoPtr< T > remove(iterator &iter)
Remove entry specified by given iterator.
Definition: HashPtrTable.C:104
HashPtrTable.C
Foam::HashPtrTable::~HashPtrTable
~HashPtrTable()
Destructor.
Definition: HashPtrTable.C:73
HashPtrTableI.H
Foam::HashTable::const_iterator
Forward iterator with const access.
Definition: HashTable.H:754
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::HashPtrTable::release
autoPtr< T > release(iterator &iter)
Release ownership of the pointer and replace with a nullptr.
Definition: HashPtrTable.C:82
Foam::HashPtrTable::this_type
HashPtrTable< T, Key, Hash > this_type
The template instance used for this table.
Definition: HashPtrTable.H:83
Foam::INew
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:51
Foam::Hash
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:53
Foam::HashPtrTable::clear
void clear()
Clear all entries from table and delete any allocated pointers.
Definition: HashPtrTable.C:152
Foam::HashPtrTable::set
bool set(const Key &key, T *ptr)
Assign a new entry, overwriting existing entries.
Definition: HashPtrTableI.H:135
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::HashPtrTable::HashPtrTable
HashPtrTable()=default
Default construct with default table capacity.
Foam::HashPtrTable::insert
bool insert(const Key &, T *)=delete
Foam::HashPtrTable::write
void write(Ostream &os) const
Invoke write() on each non-null entry.
Definition: HashPtrTableIO.C:144
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::HashPtrTable::parent_type
HashTable< T *, Key, Hash > parent_type
The template instance used for the parent HashTable.
Definition: HashPtrTable.H:86
os
OBJstream os(runTime.globalPath()/outputName)
Foam::HashPtrTable::operator=
void operator=(const this_type &rhs)
Copy assignment.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
Foam::HashPtrTable::emplace_set
bool emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
Definition: HashPtrTableI.H:73
Foam::HashPtrTable< exprResult >::const_iterator
typename parent_type::const_iterator const_iterator
Definition: HashPtrTable.H:89
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::HashPtrTable
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Definition: HashPtrTable.H:54
Foam::HashPtrTable::get
const T * get(const Key &key) const
Definition: HashPtrTableI.H:42
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
args
Foam::argList args(argc, argv)
Foam::HashPtrTable< exprResult >::iterator
typename parent_type::iterator iterator
Definition: HashPtrTable.H:88
Foam::HashPtrTable::emplace
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
Definition: HashPtrTableI.H:56