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-2020 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
30// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31
32template<class T, class Key, class Hash>
34:
35 parent_type(size)
36{}
37
38
39// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
40
41template<class T, class Key, class Hash>
42inline const T* Foam::HashPtrTable<T, Key, Hash>::get(const Key& key) const
43{
44 const const_iterator iter(this->cfind(key));
45 if (iter.good())
46 {
47 return iter.val();
48 }
49 return nullptr;
50}
51
52
53template<class T, class Key, class Hash>
54template<class... Args>
56(
57 const Key& key,
58 Args&&... args
59)
60{
61 // Use insertion semantics
62 return
63 (
64 !parent_type::found(key)
65 && this->parent_type::set(key, new T(std::forward<Args>(args)...))
66 );
67}
68
69
70template<class T, class Key, class Hash>
71template<class... Args>
73(
74 const Key& key,
75 Args&&... args
76)
77{
78 return this->set(key, new T(std::forward<Args>(args)...));
79}
80
81
82template<class T, class Key, class Hash>
84(
85 const Key& key,
86 autoPtr<T>& ptr
87)
88{
89 if (parent_type::insert(key, ptr.get()))
90 {
91 ptr.release(); // Now owned by HashPtrTable
92 return true;
93 }
94
95 return false;
96}
97
99template<class T, class Key, class Hash>
101(
102 const Key& key,
103 autoPtr<T>&& ptr
104)
105{
106 if (parent_type::insert(key, ptr.get()))
107 {
108 ptr.release(); // Now owned by HashPtrTable
109 return true;
110 }
111
112 return false;
113}
114
115
116template<class T, class Key, class Hash>
118(
119 const Key& key,
120 std::unique_ptr<T>&& ptr
121)
122{
123 if (parent_type::insert(key, ptr.get()))
124 {
125 ptr.release(); // Now owned by HashPtrTable
126 return true;
128
129 return false;
130}
131
132
133template<class T, class Key, class Hash>
135(
136 const Key& key,
137 T* ptr
138)
139{
140 const T* old = this->get(key);
141
142 const bool ok = this->parent_type::set(key, ptr);
143
144 if (ok && old != ptr)
145 {
146 delete const_cast<T*>(old);
147 }
148
149 return ok;
150}
151
152
153template<class T, class Key, class Hash>
155(
156 const Key& key,
157 autoPtr<T>& ptr
158)
159{
160 return this->set(key, ptr.release());
161}
162
163
164template<class T, class Key, class Hash>
166(
167 const Key& key,
168 autoPtr<T>&& ptr
169)
170{
171 return this->set(key, ptr.release());
172}
173
174
175template<class T, class Key, class Hash>
177(
178 const Key& key,
179 std::unique_ptr<T>&& ptr
180)
181{
182 return this->set(key, ptr.release());
183}
184
185
186// ************************************************************************* //
bool insert(const Key &, T *)=delete
bool emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
Definition: HashPtrTableI.H:73
HashPtrTable()=default
Default construct with default table capacity.
typename parent_type::const_iterator const_iterator
Definition: HashPtrTable.H:89
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
Definition: HashPtrTableI.H:56
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
unsigned int get() const
Get value as unsigned, no range-checking.
Definition: PackedListI.H:302
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
T * get() noexcept
Return pointer to managed object without nullptr checking.
Definition: autoPtr.H:152
T * release() noexcept
Release ownership and return the pointer.
Definition: autoPtrI.H:100
bool set() const
Are all the vector set.
Definition: triadI.H:76
const volScalarField & T
Foam::argList args(argc, argv)