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(size)
36 {}
37 
38 
39 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
40 
41 template<class T, class Key, class Hash>
42 inline 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 
53 template<class T, class Key, class Hash>
54 template<class... Args>
56 (
57  const Key& key,
58  Args&&... args
59 )
60 {
61  // Use insertion semantics
62  return
63  (
65  && this->parent_type::set(key, new T(std::forward<Args>(args)...))
66  );
67 }
68 
69 
70 template<class T, class Key, class Hash>
71 template<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 
82 template<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 
98 
99 template<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 
116 template<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;
127  }
128 
129  return false;
130 }
131 
132 
133 template<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 
153 template<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 
164 template<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 
175 template<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 // ************************************************************************* //
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::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::HashPtrTable::set
bool set(const Key &key, T *ptr)
Assign a new entry, overwriting existing entries.
Definition: HashPtrTableI.H:135
Foam::autoPtr::release
T * release() noexcept
Release ownership and return the pointer.
Definition: autoPtrI.H:100
Foam::autoPtr::get
T * get() noexcept
Return pointer to managed object without nullptr checking.
Definition: autoPtr.H:152
Foam::HashPtrTable::HashPtrTable
HashPtrTable()=default
Default construct with default table capacity.
Foam::HashPtrTable::insert
bool insert(const Key &, T *)=delete
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:73
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:42
Foam::PtrListOps::get
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
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:56
autoPtr.H