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-2020 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=string::hash>
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  inline HashPtrTable();
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  //- Remove entry specified by given iterator.
134  // Includes a safeguard against the end-iterator.
135  //
136  // \return entry as an encapsulated pointer.
137  autoPtr<T> remove(iterator& iter);
138 
139  //- Remove entry specified by given key.
140  //
141  // \return entry as an encapsulated pointer.
142  autoPtr<T> remove(const Key& key);
143 
144  //- Erase entry specified by given iterator and delete the
145  //- allocated pointer.
146  // Includes a safeguard against the end-iterator.
147  //
148  // \return True if item was removed
149  bool erase(iterator& iter);
150 
151  //- Erase entry specified by given key and delete the
152  //- allocated pointer.
153  //
154  // \return True if item was removed
155  bool erase(const Key& key);
156 
157  //- Clear all entries from table and delete any allocated pointers
158  void clear();
159 
160  //- Write
161  void write(Ostream& os) const;
162 
163 
164  // Member Operators
165 
166  //- Copy assignment
167  void operator=(const this_type& rhs);
168 
169  //- Move assignment
170  void operator=(this_type&& rhs);
171 
172 
173  // IOstream Operators
174 
175  friend Istream& operator>> <T, Key, Hash>
176  (
177  Istream& is,
179  );
180 
181 
182  // Override HashTable methods
183 
184  //- Emplace insert a new entry, not overwriting existing entries.
185  // \return True if the entry did not previously exist in the table.
186  template<class... Args>
187  inline bool emplace(const Key& key, Args&&... args);
188 
189  //- Emplace set an entry, overwriting any existing entries.
190  // \return True, since it always overwrites any entries.
191  template<class... Args>
192  inline bool emplace_set(const Key& key, Args&&... args);
193 
194  //- No insert() with raw pointers (potential memory leaks).
195  //- Use insert() with autoPtr or set()
196  inline bool insert(const Key&, T*) = delete;
197 
198  //- Insert a new entry, not overwriting existing entries.
199  //
200  // \return True if the entry inserted (not previously in table)
201  inline bool insert(const Key& key, autoPtr<T>& ptr);
202 
203  //- Insert a new entry, not overwriting existing entries.
204  //
205  // \return True if the entry inserted (not previously in table)
206  inline bool insert(const Key& key, autoPtr<T>&& ptr);
207 
208  //- Insert a new entry, not overwriting existing entries.
209  //
210  // \return True if the entry inserted (not previously in table)
211  inline bool insert(const Key& key, std::unique_ptr<T>&& ptr);
212 
213  //- Assign a new entry, overwriting existing entries.
214  inline bool set(const Key& key, T* ptr);
215 
216  //- Assign a new entry, overwriting existing entries.
217  inline bool set(const Key& key, autoPtr<T>& ptr);
218 
219  //- Assign a new entry, overwriting existing entries.
220  inline bool set(const Key& key, autoPtr<T>&& ptr);
221 
222  //- Assign a new entry, overwriting existing entries.
223  inline bool set(const Key& key, std::unique_ptr<T>&& ptr);
224 };
225 
226 
227 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
228 
229 } // End namespace Foam
230 
231 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
232 
233 #include "HashPtrTableI.H"
234 
235 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
236 
237 #ifdef NoRepository
238  #include "HashPtrTable.C"
239 #endif
240 
241 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
242 
243 #endif
244 
245 // ************************************************************************* //
Foam::HashPtrTable::erase
bool erase(iterator &iter)
Definition: HashPtrTable.C:103
HashTable.H
Foam::HashTable::iterator
Forward iterator with non-const access.
Definition: HashTable.H:681
Foam::HashPtrTable::remove
autoPtr< T > remove(iterator &iter)
Remove entry specified by given iterator.
Definition: HashPtrTable.C:81
HashPtrTable.C
Foam::HashPtrTable::~HashPtrTable
~HashPtrTable()
Destructor.
Definition: HashPtrTable.C:72
HashPtrTableI.H
Foam::HashTable::const_iterator
Forward iterator with const access.
Definition: HashTable.H:742
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
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:55
Foam::HashPtrTable::clear
void clear()
Clear all entries from table and delete any allocated pointers.
Definition: HashPtrTable.C:129
Foam::HashPtrTable::set
bool set(const Key &key, T *ptr)
Assign a new entry, overwriting existing entries.
Definition: HashPtrTableI.H:142
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::insert
bool insert(const Key &, T *)=delete
Foam::HashPtrTable::write
void write(Ostream &os) const
Write.
Definition: HashPtrTableIO.C:155
Foam::HashPtrTable::HashPtrTable
HashPtrTable()
Default construct with default table capacity.
Definition: HashPtrTableI.H:33
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:121
Foam::HashPtrTable::parent_type
HashTable< T *, Key, Hash > parent_type
The template instance used for the parent HashTable.
Definition: HashPtrTable.H:86
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:80
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:49
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:63