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-2018 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 
33 SourceFiles
34  HashPtrTable.C
35  HashPtrTableIO.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef HashPtrTable_H
40 #define HashPtrTable_H
41 
42 #include "HashTable.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 // Forward declarations
50 
51 class Istream;
52 class Ostream;
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  //- Construct null 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
112  HashPtrTable(const this_type& ht);
113 
114  //- Move construct
115  HashPtrTable(this_type&& ht);
116 
117 
118  //- Destructor
119  ~HashPtrTable();
120 
121 
122  // Member Functions
123 
124  // Edit
125 
126  //- Remove entry specified by given iterator.
127  // Includes a safeguard against the end-iterator.
128  //
129  // \return entry as an encapsulated pointer.
130  autoPtr<T> remove(iterator& iter);
131 
132  //- Remove entry specified by given key.
133  //
134  // \return entry as an encapsulated pointer.
135  autoPtr<T> remove(const Key& key);
136 
137  //- Erase entry specified by given iterator and delete the
138  //- allocated pointer.
139  // Includes a safeguard against the end-iterator.
140  //
141  // \return True if item was removed
142  bool erase(iterator& iter);
143 
144  //- Erase entry specified by given key and delete the
145  //- allocated pointer.
146  //
147  // \return True if item was removed
148  bool erase(const Key& key);
149 
150  //- Clear all entries from table and delete any allocated pointers
151  void clear();
152 
153  //- Write
154  void write(Ostream& os) const;
155 
156 
157  // Member Operators
158 
159  //- Copy assignment
160  void operator=(const this_type& rhs);
161 
162  //- Move assignment
163  void operator=(this_type&& rhs);
164 
165 
166  // IOstream Operators
167 
168  friend Istream& operator>> <T, Key, Hash>
169  (
170  Istream& is,
172  );
173 
174 
175  // Housekeeping
176 
177  //- No insert() with raw pointers (potential memory leaks).
178  //- Use insert() with autoPtr or set()
179  inline bool insert(const Key&, T*) = delete;
180 
181  //- Insert a new entry, not overwriting existing entries.
182  //
183  // \return True if the entry inserted (not previously in table)
184  inline bool insert(const Key& key, autoPtr<T>& aptr);
185 
186  //- Insert a new entry, not overwriting existing entries.
187  //
188  // \return True if the entry inserted (not previously in table)
189  inline bool insert(const Key& key, autoPtr<T>&& aptr);
190 
191  //- Assign a new entry, overwriting existing entries.
192  inline bool set(const Key& key, T* ptr);
193 
194  //- Assign a new entry, overwriting existing entries.
195  inline bool set(const Key& key, autoPtr<T>& aptr);
196 
197  //- Assign a new entry, overwriting existing entries.
198  inline bool set(const Key& key, autoPtr<T>&& aptr);
199 };
200 
201 
202 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
203 
204 } // End namespace Foam
205 
206 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
207 
208 #include "HashPtrTableI.H"
209 
210 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
211 
212 #ifdef NoRepository
213  #include "HashPtrTable.C"
214 #endif
215 
216 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
217 
218 #endif
219 
220 // ************************************************************************* //
Foam::HashPtrTable::erase
bool erase(iterator &iter)
Definition: HashPtrTable.C:101
HashTable.H
Foam::HashTable::iterator
Forward iterator with non-const access.
Definition: HashTable.H:676
Foam::HashPtrTable::remove
autoPtr< T > remove(iterator &iter)
Remove entry specified by given iterator.
Definition: HashPtrTable.C:79
HashPtrTable.C
Foam::HashPtrTable::~HashPtrTable
~HashPtrTable()
Destructor.
Definition: HashPtrTable.C:70
HashPtrTableI.H
Foam::HashTable::const_iterator
Forward iterator with const access.
Definition: HashTable.H:737
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:131
Foam::HashPtrTable::set
bool set(const Key &key, T *ptr)
Assign a new entry, overwriting existing entries.
Definition: HashPtrTableI.H:84
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()
Construct null 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< 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>.
Definition: HashPtrTable.H:54
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::HashPtrTable< exprResult >::iterator
typename parent_type::iterator iterator
Definition: HashPtrTable.H:88