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 -------------------------------------------------------------------------------
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 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class T, class Key, class Hash>
34 :
35  parent_type()
36 {}
37 
38 
39 template<class T, class Key, class Hash>
41 :
42  parent_type(size)
43 {}
44 
45 
46 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
47 
48 template<class T, class Key, class Hash>
49 inline const T* Foam::HashPtrTable<T, Key, Hash>::get(const Key& key) const
50 {
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::found(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 Key& key,
82  Args&&... args
83 )
84 {
85  return this->set(key, new T(std::forward<Args>(args)...));
86 }
87 
88 
89 template<class T, class Key, class Hash>
91 (
92  const Key& key,
93  autoPtr<T>& ptr
94 )
95 {
96  if (parent_type::insert(key, ptr.get()))
97  {
98  ptr.release(); // Now owned by HashPtrTable
99  return true;
100  }
101 
102  return false;
103 }
104 
105 
106 template<class T, class Key, class Hash>
108 (
109  const Key& key,
110  autoPtr<T>&& ptr
111 )
112 {
113  if (parent_type::insert(key, ptr.get()))
114  {
115  ptr.release(); // Now owned by HashPtrTable
116  return true;
117  }
118 
119  return false;
120 }
121 
122 
123 template<class T, class Key, class Hash>
125 (
126  const Key& key,
127  std::unique_ptr<T>&& ptr
128 )
129 {
130  if (parent_type::insert(key, ptr.get()))
131  {
132  ptr.release(); // Now owned by HashPtrTable
133  return true;
134  }
135 
136  return false;
137 }
138 
139 
140 template<class T, class Key, class Hash>
142 (
143  const Key& key,
144  T* ptr
145 )
146 {
147  const T* old = this->get(key);
148 
149  const bool ok = this->parent_type::set(key, ptr);
150 
151  if (ok && old != ptr)
152  {
153  delete const_cast<T*>(old);
154  }
155 
156  return ok;
157 }
158 
159 
160 template<class T, class Key, class Hash>
162 (
163  const Key& key,
164  autoPtr<T>& ptr
165 )
166 {
167  return this->set(key, ptr.release());
168 }
169 
170 
171 template<class T, class Key, class Hash>
173 (
174  const Key& key,
175  autoPtr<T>&& ptr
176 )
177 {
178  return this->set(key, ptr.release());
179 }
180 
181 
182 template<class T, class Key, class Hash>
184 (
185  const Key& key,
186  std::unique_ptr<T>&& ptr
187 )
188 {
189  return this->set(key, ptr.release());
190 }
191 
192 
193 // ************************************************************************* //
insert
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
Foam::HashPtrTable::set
bool set(const Key &key, T *ptr)
Assign a new entry, overwriting existing entries.
Definition: HashPtrTableI.H:142
Foam::autoPtr::release
T * release() noexcept
Return pointer to the managed object and release ownership.
Definition: autoPtrI.H:100
Foam::autoPtr::get
T * get() noexcept
Return pointer to managed object without nullptr checking.
Definition: autoPtr.H:158
Foam::HashPtrTable::insert
bool insert(const Key &, T *)=delete
Foam::HashPtrTable::HashPtrTable
HashPtrTable()
Default construct with default table capacity.
Definition: HashPtrTableI.H:33
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
Foam::HashPtrTable::emplace_set
bool emplace_set(const Key &key, Args &&... args)
Emplace set an entry, overwriting any existing entries.
Definition: HashPtrTableI.H:80
found
bool found
Definition: TABSMDCalcMethod2.H:32
Foam::HashPtrTable< exprResult >::const_iterator
typename parent_type::const_iterator const_iterator
Definition: HashPtrTable.H:89
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::HashPtrTable::get
const T * get(const Key &key) const
Definition: HashPtrTableI.H:49
args
Foam::argList args(argc, argv)
Foam::HashPtrTable::emplace
bool emplace(const Key &key, Args &&... args)
Emplace insert a new entry, not overwriting existing entries.
Definition: HashPtrTableI.H:63
autoPtr.H