PtrListDictionary.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2015-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "PtrListDictionary.H"
27 #include "UPtrListDictionary.H"
28 
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
31 template<class T>
33 :
34  DictionaryBase<PtrList<T>, T>(2*size)
35 {
37 }
38 
39 
40 template<class T>
42 :
44 {}
45 
46 
47 template<class T>
49 :
50  DictionaryBase<PtrList<T>, T>(move(dict))
51 {}
52 
53 
54 template<class T>
55 template<class INew>
57 :
58  DictionaryBase<PtrList<T>, T>(is, iNew)
59 {}
60 
61 
62 template<class T>
64 :
65  DictionaryBase<PtrList<T>, T>(is)
66 {}
67 
68 
69 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
70 
71 template<class T>
73 {
74  forAll(*this, i)
75  {
76  if (operator[](i).keyword() == key)
77  {
78  return i;
79  }
80  }
81 
82  return -1;
83 }
84 
85 
86 template<class T>
88 (
89  const wordRe& key
90 ) const
91 {
92  List<label> indices;
93 
94  if (!key.empty())
95  {
96  if (key.isPattern())
97  {
98  indices = findStrings(key, this->toc());
99  }
100  else
101  {
102  indices.setSize(this->size());
103  label nFound = 0;
104  forAll(*this, i)
105  {
106  if (key == operator[](i).keyword())
107  {
108  indices[nFound++] = i;
109  }
110  }
111  indices.setSize(nFound);
112  }
113  }
114 
115  return indices;
116 }
117 
118 
119 template<class T>
121 (
122  const word& key,
123  T* ptr
124 )
125 {
126  if (!DictionaryBase<PtrList<T>, T>::hashedTs_.insert(key, ptr))
127  {
129  << "Cannot insert with key '" << key << "' into hash-table"
130  << abort(FatalError);
131  }
132  return PtrList<T>::append(ptr);
133 }
134 
135 
136 template<class T>
138 (
139  const word& key,
140  const autoPtr<T>& aptr
141 )
142 {
143  return append(key, const_cast<autoPtr<T>&>(aptr).ptr());
144 }
145 
146 
147 template<class T>
149 (
150  const word& key,
151  const tmp<T>& t
152 )
153 {
154  return append(key, const_cast<tmp<T>&>(t).ptr());
155 }
156 
157 
158 template<class T>
160 {
161  append(ptr->keyword(), ptr);
162 }
163 
164 
165 template<class T>
167 {
168  append(aptr->keyword(), aptr);
169 }
170 
171 
172 template<class T>
174 {
175  append(t->keyword(), t);
176 }
177 
178 
179 template<class T>
181 (
182  const label i,
183  const word& key,
184  T* ptr
185 )
186 {
187  if (ptr == nullptr)
188  {
189  // Remove key from hash table if the pointer is null
190  DictionaryBase<PtrList<T>, T>::hashedTs_.erase(key);
191  }
192  else if (!DictionaryBase<PtrList<T>, T>::hashedTs_.set(key, ptr))
193  {
195  << "Cannot set with key '" << key << "' into hash-table"
196  << abort(FatalError);
197  }
198 
199  return PtrList<T>::set(i, ptr);
200 }
201 
202 
203 template<class T>
205 (
206  const label i,
207  const word& key,
208  const autoPtr<T>& aptr
209 )
210 {
211  return set(i, key, const_cast<autoPtr<T>&>(aptr).ptr());
212 }
213 
214 
215 template<class T>
217 (
218  const label i,
219  const word& key,
220  const tmp<T>& t
221 )
222 {
223  return set(i, key, const_cast<tmp<T>&>(t).ptr());
224 }
225 
226 
227 template<class T>
229 (
230  const label i,
231  T* ptr
232 )
233 {
234  return set(i, ptr->keyword(), ptr);
235 }
236 
237 
238 template<class T>
240 (
241  const label i,
242  const autoPtr<T>& aptr
243 )
244 {
245  return set(i, aptr->keyword(), aptr);
246 }
247 
248 
249 template<class T>
251 (
252  const label i,
253  const tmp<T>& t
254 )
255 {
256  return set(i, t->keyword(), t);
257 }
258 
259 
260 template<class T>
261 template<class T2>
263 {
264  UPtrListDictionary<T2> result(this->size());
265 
266  forAll(*this, i)
267  {
268  result.set
269  (
270  i,
271  this->operator()(i)->keyword(),
272  dynamic_cast<T2*>(this->operator()(i))
273  );
274  }
275 
276  return result;
277 }
278 
279 
280 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Base dictionary class templated on both the form of doubly-linked list it uses as well as the type it...
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:50
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
void setSize(const label)
Reset size of List.
Definition: List.C:281
Template dictionary class which manages the storage associated with it.
UPtrListDictionary< T2 > convert()
Convert to list of different pointer type.
List< label > findIndices(const wordRe &key) const
Return the indices for all matches.
void append(const word &key, T *)
Append an element at the end of the list.
autoPtr< T > set(const label, const word &key, T *)
Set element to pointer provided and return old element.
label findIndex(const word &key) const
Return the index of the given the key or -1 if not found.
PtrListDictionary(const label size)
Construct given initial list size.
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: PtrList.H:75
bool set(const label) const
Is element set.
Definition: PtrListI.H:62
void append(T *)
Append an element at the end of the list.
Definition: PtrListI.H:39
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:131
Template dictionary class which manages the storage associated with it.
bool set(const label) const
Is element set.
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A class for managing temporary objects.
Definition: tmp.H:55
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:77
static bool isPattern(const string &)
Test string for regular expression meta characters.
Definition: wordReI.H:34
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
bool findStrings(const wordReListMatcher &matcher, const std::string &str)
Return true if string matches one of the regular expressions.
Definition: stringListOps.H:52
errorManip< error > abort(error &err)
Definition: errorManip.H:131
error FatalError
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
dictionary dict