HashPtrTableI.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) 2018-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "autoPtr.H"
29 #include "refPtr.H"
30 #include "tmp.H"
31 
32 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33 
34 template<class T, class Key, class Hash>
35 inline const T* Foam::HashPtrTable<T, Key, Hash>::test(const Key& key) const
36 {
37  // Like lookup() with a nullptr
38  const const_iterator iter(this->cfind(key));
39  if (iter.good())
40  {
41  return iter.val();
42  }
43  return nullptr;
44 }
45 
46 
47 template<class T, class Key, class Hash>
48 inline const T* Foam::HashPtrTable<T, Key, Hash>::get(const Key& key) const
49 {
50  // Like lookup() with a nullptr
51  const const_iterator iter(this->cfind(key));
52  if (iter.good())
53  {
54  return iter.val();
55  }
56  return nullptr;
57 }
58 
59 
60 template<class T, class Key, class Hash>
61 template<class... Args>
63 (
64  const Key& key,
65  Args&&... args
66 )
67 {
68  // Use insertion semantics
69  return
70  (
71  !parent_type::contains(key)
72  && this->parent_type::set(key, new T(std::forward<Args>(args)...))
73  );
74 }
75 
76 
77 template<class T, class Key, class Hash>
78 template<class... Args>
80 (
81  const bool overwrite,
82  const Key& key,
83  Args&&... args
84 )
85 {
86  T* ptr = nullptr;
87 
88  // Replace existing entry unconditionally (overwrite = true)
89  // or only if nullptr (overwrite = false)
90  iterator iter(this->find(key));
91  if (iter.good())
92  {
93  ptr = iter.val();
94 
95  if (overwrite || !ptr)
96  {
97  // Overwrite existing or replace nullptr
98  delete ptr;
99  ptr = new T(std::forward<Args>(args)...);
100  iter.val() = ptr;
101  }
102  }
103  else
104  {
105  ptr = new T(std::forward<Args>(args)...);
106  this->parent_type::set(key, ptr);
107  }
108 
109  return *ptr;
110 }
111 
112 
113 template<class T, class Key, class Hash>
114 template<class... Args>
116 (
117  const Key& key,
118  Args&&... args
119 )
120 {
121  // overwrite = true
122  return this->emplaceImpl(true, key, std::forward<Args>(args)...);
123 }
124 
125 
126 template<class T, class Key, class Hash>
127 template<class... Args>
129 (
130  const Key& key,
131  Args&&... args
132 )
133 {
134  // overwrite = false
135  return this->emplaceImpl(false, key, std::forward<Args>(args)...);
136 }
137 
138 
139 template<class T, class Key, class Hash>
141 (
142  const Key& key,
143  std::unique_ptr<T>&& ptr
144 )
145 {
146  if (parent_type::insert(key, ptr.get()))
147  {
148  ptr.release(); // Now owned by HashPtrTable
149  return true;
150  }
152  return false;
153 }
154 
155 
156 template<class T, class Key, class Hash>
158 (
159  const Key& key,
160  autoPtr<T>&& ptr
161 )
162 {
163  if (parent_type::insert(key, ptr.get()))
164  {
165  ptr.release(); // Now owned by HashPtrTable
166  return true;
167  }
169  return false;
170 }
171 
172 
173 template<class T, class Key, class Hash>
175 (
176  const Key& key,
177  T* ptr
178 )
179 {
180  // Replace existing entry unconditionally
181  iterator iter(this->find(key));
182  if (iter.good())
183  {
184  if (iter.val() != ptr)
185  {
186  delete iter.val();
187  iter.val() = ptr;
188  }
189  return true;
190  }
191 
192  // Or add new entry
193  return this->parent_type::set(key, ptr);
194 }
195 
196 
197 template<class T, class Key, class Hash>
199 (
200  const Key& key,
201  std::unique_ptr<T>&& ptr
202 )
203 {
204  return this->set(key, ptr.release());
205 }
206 
207 
208 template<class T, class Key, class Hash>
210 (
211  const Key& key,
212  autoPtr<T>&& ptr
213 )
214 {
215  return this->set(key, ptr.release());
216 }
217 
218 
219 template<class T, class Key, class Hash>
221 (
222  const Key& key,
223  const refPtr<T>& ptr
224 )
225 {
226  return this->set(key, ptr.ptr()); // release or clone
227 }
228 
229 
230 template<class T, class Key, class Hash>
232 (
233  const Key& key,
234  const tmp<T>& ptr
235 )
236 {
237  return this->set(key, ptr.ptr()); // release or clone
238 }
239 
240 
241 // ************************************************************************* //
T & try_emplace(const Key &key, Args &&... args)
Like emplace_set() but will not overwrite an occupied (non-null) location.
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition: ListOps.H:795
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
Definition: HashPtrTableI.H:56
void set(List< bool > &bools, const labelUList &locations)
Set the listed locations (assign &#39;true&#39;).
Definition: BitOps.C:30
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers...
Definition: HashPtrTable.H:51
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: refPtrI.H:280
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
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:28
T & emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
bool set(const Key &key, T *ptr)
Assign a new entry, overwrites existing.
typename parent_type::const_iterator const_iterator
Definition: HashPtrTable.H:104
const volScalarField & T
const T * get(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:41
bool insert(const Key &, T *)=delete
No insert() with raw pointers (potential memory leaks). Use insert() with autoPtr or set() ...
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
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: tmpI.H:256
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Foam::argList args(argc, argv)