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-2022 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 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
34 template<class T, class Key, class Hash>
36 :
37  parent_type(size)
38 {}
39 
40 
41 template<class T, class Key, class Hash>
43 (
45 )
46 :
47  parent_type(std::move(rhs))
48 {}
49 
50 
51 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
52 
53 template<class T, class Key, class Hash>
54 inline const T* Foam::HashPtrTable<T, Key, Hash>::test(const Key& key) const
55 {
56  // Like lookup() with a nullptr
57  const const_iterator iter(this->cfind(key));
58  if (iter.good())
59  {
60  return iter.val();
61  }
62  return nullptr;
63 }
64 
65 
66 template<class T, class Key, class Hash>
67 inline const T* Foam::HashPtrTable<T, Key, Hash>::get(const Key& key) const
68 {
69  // Like lookup() with a nullptr
70  const const_iterator iter(this->cfind(key));
71  if (iter.good())
72  {
73  return iter.val();
74  }
75  return nullptr;
76 }
77 
78 
79 template<class T, class Key, class Hash>
80 template<class... Args>
82 (
83  const Key& key,
84  Args&&... args
85 )
86 {
87  // Use insertion semantics
88  return
89  (
91  && this->parent_type::set(key, new T(std::forward<Args>(args)...))
92  );
93 }
94 
95 
96 template<class T, class Key, class Hash>
97 template<class... Args>
99 (
100  const Key& key,
101  Args&&... args
102 )
103 {
104  return this->set(key, new T(std::forward<Args>(args)...));
105 }
106 
107 
108 template<class T, class Key, class Hash>
110 (
111  const Key& key,
112  std::unique_ptr<T>&& ptr
113 )
114 {
115  if (parent_type::insert(key, ptr.get()))
116  {
117  ptr.release(); // Now owned by HashPtrTable
118  return true;
119  }
121  return false;
122 }
123 
124 
125 template<class T, class Key, class Hash>
127 (
128  const Key& key,
129  autoPtr<T>&& ptr
130 )
131 {
132  if (parent_type::insert(key, ptr.get()))
133  {
134  ptr.release(); // Now owned by HashPtrTable
135  return true;
136  }
138  return false;
139 }
140 
141 
142 template<class T, class Key, class Hash>
144 (
145  const Key& key,
146  T* ptr
147 )
148 {
149  const T* old = this->get(key);
150 
151  const bool ok = this->parent_type::set(key, ptr);
152 
153  if (ok && old != ptr)
154  {
155  delete const_cast<T*>(old);
156  }
158  return ok;
159 }
160 
161 
162 template<class T, class Key, class Hash>
164 (
165  const Key& key,
166  std::unique_ptr<T>&& ptr
167 )
168 {
169  return this->set(key, ptr.release());
170 }
171 
172 
173 template<class T, class Key, class Hash>
175 (
176  const Key& key,
177  autoPtr<T>&& ptr
178 )
179 {
180  return this->set(key, ptr.release());
181 }
182 
183 
184 template<class T, class Key, class Hash>
186 (
187  const Key& key,
188  const refPtr<T>& ptr
189 )
190 {
191  return this->set(key, ptr.ptr()); // release or clone
192 }
193 
194 
195 template<class T, class Key, class Hash>
197 (
198  const Key& key,
199  const tmp<T>& ptr
200 )
201 {
202  return this->set(key, ptr.ptr()); // release or clone
203 }
204 
205 
206 // ************************************************************************* //
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:75
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:258
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:47
T * get() noexcept
Return pointer without nullptr checking.
Definition: tmp.H:265
bool set(const Key &key, T *ptr)
Assign a new entry, overwrites existing.
A HashTable similar to std::unordered_map.
Definition: HashTable.H:102
HashPtrTable()=default
Default construct with default table capacity.
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:60
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
bool emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
Definition: HashPtrTableI.H:92
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:231
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Foam::argList args(argc, argv)
bool found