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 // * * * * * * * * * * * * * * * * 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  (
90  !parent_type::contains(key)
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  T* ptr = new T(std::forward<Args>(args)...);
105  (void)this->set(key, ptr);
106  return *ptr;
107 }
108 
109 
110 template<class T, class Key, class Hash>
112 (
113  const Key& key,
114  std::unique_ptr<T>&& ptr
115 )
116 {
117  if (parent_type::insert(key, ptr.get()))
118  {
119  ptr.release(); // Now owned by HashPtrTable
120  return true;
121  }
123  return false;
124 }
125 
126 
127 template<class T, class Key, class Hash>
129 (
130  const Key& key,
131  autoPtr<T>&& ptr
132 )
133 {
134  if (parent_type::insert(key, ptr.get()))
135  {
136  ptr.release(); // Now owned by HashPtrTable
137  return true;
138  }
140  return false;
141 }
142 
143 
144 template<class T, class Key, class Hash>
146 (
147  const Key& key,
148  T* ptr
149 )
150 {
151  const T* old = this->get(key);
152 
153  const bool ok = this->parent_type::set(key, ptr);
154 
155  if (ok && old != ptr)
156  {
157  delete const_cast<T*>(old);
158  }
160  return ok;
161 }
162 
163 
164 template<class T, class Key, class Hash>
166 (
167  const Key& key,
168  std::unique_ptr<T>&& ptr
169 )
170 {
171  return this->set(key, ptr.release());
172 }
173 
174 
175 template<class T, class Key, class Hash>
177 (
178  const Key& key,
179  autoPtr<T>&& ptr
180 )
181 {
182  return this->set(key, ptr.release());
183 }
184 
185 
186 template<class T, class Key, class Hash>
188 (
189  const Key& key,
190  const refPtr<T>& ptr
191 )
192 {
193  return this->set(key, ptr.ptr()); // release or clone
194 }
195 
196 
197 template<class T, class Key, class Hash>
199 (
200  const Key& key,
201  const tmp<T>& ptr
202 )
203 {
204  return this->set(key, ptr.ptr()); // release or clone
205 }
206 
207 
208 // ************************************************************************* //
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: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:47
T & emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
Definition: HashPtrTableI.H:92
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
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:265
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Foam::argList args(argc, argv)