Loading...
Searching...
No Matches
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-2025 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
34template<class T, class Key, class Hash>
35inline 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
47template<class T, class Key, class Hash>
48inline 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
60template<class T, class Key, class Hash>
61inline T* Foam::HashPtrTable<T, Key, Hash>::get(const Key& key)
62{
63 // Like lookup() with a nullptr + const_cast
64 iterator iter(this->find(key));
65 if (iter.good())
66 {
67 return iter.val();
68 }
69 return nullptr;
70}
71
72
73template<class T, class Key, class Hash>
74template<class... Args>
76(
77 const Key& key,
78 Args&&... args
79)
80{
81 // Use insertion semantics
82 return
83 (
84 !parent_type::contains(key)
85 && this->parent_type::set(key, new T(std::forward<Args>(args)...))
86 );
87}
88
89
90template<class T, class Key, class Hash>
91template<class... Args>
92inline T& Foam::HashPtrTable<T, Key, Hash>::emplaceImpl
93(
94 const bool overwrite,
95 const Key& key,
96 Args&&... args
97)
98{
99 T* ptr = nullptr;
100
101 // Replace existing entry unconditionally (overwrite = true)
102 // or only if nullptr (overwrite = false)
103 iterator iter(this->find(key));
104 if (iter.good())
105 {
106 ptr = iter.val();
107
108 if (overwrite || !ptr)
109 {
110 // Overwrite existing or replace nullptr
111 delete ptr;
112 ptr = new T(std::forward<Args>(args)...);
113 iter.val() = ptr;
114 }
115 }
116 else
117 {
118 ptr = new T(std::forward<Args>(args)...);
119 this->parent_type::set(key, ptr);
120 }
122 return *ptr;
123}
124
125
126template<class T, class Key, class Hash>
127template<class... Args>
129(
130 const Key& key,
131 Args&&... args
132)
133{
134 // overwrite = true
135 return this->emplaceImpl(true, key, std::forward<Args>(args)...);
136}
137
138
139template<class T, class Key, class Hash>
140template<class... Args>
142(
143 const Key& key,
144 Args&&... args
145)
147 // overwrite = false
148 return this->emplaceImpl(false, key, std::forward<Args>(args)...);
149}
150
151
152template<class T, class Key, class Hash>
154(
155 const Key& key,
156 std::unique_ptr<T>&& ptr
157)
158{
159 if (parent_type::insert(key, ptr.get()))
160 {
161 ptr.release(); // Now owned by HashPtrTable
162 return true;
164
165 return false;
166}
167
168
169template<class T, class Key, class Hash>
172 const Key& key,
173 autoPtr<T>&& ptr
174)
175{
176 if (parent_type::insert(key, ptr.get()))
178 ptr.release(); // Now owned by HashPtrTable
179 return true;
181
182 return false;
183}
184
185
186template<class T, class Key, class Hash>
188(
189 const Key& key,
190 T* ptr
191)
192{
193 // Replace existing entry unconditionally
194 iterator iter(this->find(key));
195 if (iter.good())
196 {
197 if (iter.val() != ptr)
198 {
199 delete iter.val();
200 iter.val() = ptr;
201 }
202 return true;
203 }
205 // Or add new entry
206 return this->parent_type::set(key, ptr);
207}
208
209
210template<class T, class Key, class Hash>
212(
213 const Key& key,
214 std::unique_ptr<T>&& ptr
216{
217 return this->set(key, ptr.release());
218}
219
220
221template<class T, class Key, class Hash>
223(
224 const Key& key,
225 autoPtr<T>&& ptr
227{
228 return this->set(key, ptr.release());
229}
230
231
232template<class T, class Key, class Hash>
234(
235 const Key& key,
236 const refPtr<T>& ptr
238{
239 return this->set(key, ptr.ptr()); // release or clone
240}
241
242
243template<class T, class Key, class Hash>
245(
246 const Key& key,
247 const tmp<T>& ptr
248)
249{
250 return this->set(key, ptr.ptr()); // release or clone
251}
252
253
254// ************************************************************************* //
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.
typename parent_type::iterator iterator
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.
T & emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
bool insert(const Key &, T *)=delete
No insert() with raw pointers (potential memory leaks). Use insert() with autoPtr or set().
bool set(const Key &key, T *ptr)
Assign a new entry, overwrites existing.
typename parent_type::const_iterator const_iterator
T & try_emplace(const Key &key, Args &&... args)
Like emplace_set() but will not overwrite an occupied (non-null) location.
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
bool set(const Key &key, const T &obj)
bool contains(const Key &key) const
const_iterator cfind(const Key &key) const
Find and return an const_iterator set at the hashed entry.
Definition HashTableI.H:113
bool insert(const Key &key, const T &obj)
iterator find(const Key &key)
Find and return an iterator set at the hashed entry.
Definition HashTableI.H:86
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A class for managing references or pointers (no reference counting).
Definition refPtr.H:54
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition refPtrI.H:280
A class for managing temporary objects.
Definition tmp.H:75
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition tmpI.H:256
const volScalarField & T
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition ListOps.H:855
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Foam::argList args(argc, argv)