PtrListI.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 | Copyright (C) 2011-2015 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 "autoPtr.H"
27 #include "tmp.H"
28 
29 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
30 
31 template<class T>
33 {
34  return ptrs_.size();
35 }
36 
37 
38 template<class T>
39 inline bool Foam::PtrList<T>::empty() const
40 {
41  return ptrs_.empty();
42 }
43 
44 
45 template<class T>
47 {
48  return this->operator[](0);
49 }
50 
51 
52 template<class T>
53 inline const T& Foam::PtrList<T>::first() const
54 {
55  return this->operator[](0);
56 }
57 
58 
59 template<class T>
61 {
62  return this->operator[](this->size()-1);
63 }
64 
65 
66 template<class T>
67 inline const T& Foam::PtrList<T>::last() const
68 {
69  return this->operator[](this->size()-1);
70 }
71 
72 
73 template<class T>
74 inline void Foam::PtrList<T>::resize(const label newSize)
75 {
76  this->setSize(newSize);
77 }
78 
79 
80 template<class T>
81 inline void Foam::PtrList<T>::append(T* ptr)
82 {
83  label sz = size();
84  this->setSize(sz+1);
85  ptrs_[sz] = ptr;
86 }
87 
88 
89 template<class T>
90 inline void Foam::PtrList<T>::append(const autoPtr<T>& aptr)
91 {
92  return append(const_cast<autoPtr<T>&>(aptr).ptr());
93 }
94 
95 
96 template<class T>
97 inline void Foam::PtrList<T>::append
98 (
99  const tmp<T>& t
100 )
101 {
102  return append(const_cast<tmp<T>&>(t).ptr());
103 }
104 
105 
106 template<class T>
107 inline bool Foam::PtrList<T>::set(const label i) const
108 {
109  return ptrs_[i] != NULL;
110 }
111 
112 
113 template<class T>
114 inline Foam::autoPtr<T> Foam::PtrList<T>::set(const label i, T* ptr)
115 {
116  autoPtr<T> old(ptrs_[i]);
117  ptrs_[i] = ptr;
118  return old;
119 }
120 
121 
122 template<class T>
124 (
125  const label i,
126  const autoPtr<T>& aptr
127 )
128 {
129  return set(i, const_cast<autoPtr<T>&>(aptr).ptr());
130 }
131 
132 
133 template<class T>
135 (
136  const label i,
137  const tmp<T>& t
138 )
139 {
140  return set(i, const_cast<tmp<T>&>(t).ptr());
141 }
142 
143 
144 template<class T>
146 {
147  return xferMove(*this);
148 }
149 
150 
151 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
152 
153 template<class T>
154 inline const T& Foam::PtrList<T>::operator[](const label i) const
155 {
156  if (!ptrs_[i])
157  {
159  << "hanging pointer of type " << typeid(T).name()
160  << " at index " << i
161  << " (size " << size()
162  << "), cannot dereference"
163  << abort(FatalError);
164  }
165 
166  return *(ptrs_[i]);
167 }
168 
169 
170 template<class T>
171 inline T& Foam::PtrList<T>::operator[](const label i)
172 {
173  if (!ptrs_[i])
174  {
176  << "hanging pointer of type " << typeid(T).name()
177  << " at index " << i
178  << " (size " << size()
179  << "), cannot dereference"
180  << abort(FatalError);
181  }
182 
183  return *(ptrs_[i]);
184 }
185 
186 
187 template<class T>
188 inline const T* Foam::PtrList<T>::operator()(const label i) const
189 {
190  return ptrs_[i];
191 }
192 
193 
194 // * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
195 
196 template<class T>
198 :
199  ptr_(ptr)
200 {}
201 
202 
203 template<class T>
204 inline bool Foam::PtrList<T>::iterator::operator==(const iterator& iter) const
205 {
206  return ptr_ == iter.ptr_;
207 }
208 
209 
210 template<class T>
211 inline bool Foam::PtrList<T>::iterator::operator!=(const iterator& iter) const
212 {
213  return ptr_ != iter.ptr_;
214 }
215 
216 
217 template<class T>
219 {
220  return **ptr_;
221 }
222 
223 
224 template<class T>
226 {
227  return operator*();
228 }
229 
230 
231 template<class T>
232 inline typename Foam::PtrList<T>::iterator
234 {
235  ++ptr_;
236  return *this;
237 }
238 
239 
240 template<class T>
241 inline typename Foam::PtrList<T>::iterator
243 {
244  iterator tmp = *this;
245  ++ptr_;
246  return tmp;
247 }
248 
249 
250 template<class T>
251 inline typename Foam::PtrList<T>::iterator
253 {
254  --ptr_;
255  return *this;
256 }
257 
258 
259 template<class T>
260 inline typename Foam::PtrList<T>::iterator
262 {
263  iterator tmp = *this;
264  --ptr_;
265  return tmp;
266 }
267 
268 
269 template<class T>
270 inline typename Foam::PtrList<T>::iterator
272 {
273  ptr_ += n;
274  return *this;
275 }
276 
277 
278 template<class T>
279 inline typename Foam::PtrList<T>::iterator
280 Foam::operator+(const typename PtrList<T>::iterator& iter, label n)
281 {
282  typename PtrList<T>::iterator tmp = iter;
283  return tmp += n;
284 }
285 
286 
287 template<class T>
288 inline typename Foam::PtrList<T>::iterator
289 Foam::operator+(label n, const typename PtrList<T>::iterator& iter)
290 {
291  typename PtrList<T>::iterator tmp = iter;
292  return tmp += n;
293 }
294 
295 
296 template<class T>
297 inline typename Foam::PtrList<T>::iterator
299 {
300  ptr_ -= n;
301  return *this;
302 }
303 
304 
305 template<class T>
306 inline typename Foam::PtrList<T>::iterator
307 Foam::operator-(const typename PtrList<T>::iterator& iter, label n)
308 {
309  typename PtrList<T>::iterator tmp = iter;
310  return tmp -= n;
311 }
312 
313 
314 template<class T>
315 inline Foam::label Foam::operator-
316 (
317  const typename PtrList<T>::iterator& iter1,
318  const typename PtrList<T>::iterator& iter2
319 )
320 {
321  return (iter1.ptr_ - iter2.ptr_)/sizeof(T*);
322 }
323 
324 
325 template<class T>
327 {
328  return *(*this + n);
329 }
330 
331 
332 template<class T>
333 inline bool Foam::PtrList<T>::iterator::operator<(const iterator& iter) const
334 {
335  return ptr_ < iter.ptr_;
336 }
337 
338 
339 template<class T>
340 inline bool Foam::PtrList<T>::iterator::operator>(const iterator& iter) const
341 {
342  return ptr_ > iter.ptr_;
343 }
344 
345 
346 template<class T>
347 inline bool Foam::PtrList<T>::iterator::operator<=(const iterator& iter) const
348 {
349  return ptr_ <= iter.ptr_;
350 }
351 
352 
353 template<class T>
354 inline bool Foam::PtrList<T>::iterator::operator>=(const iterator& iter) const
355 {
356  return ptr_ >= iter.ptr_;
357 }
358 
359 
360 template<class T>
362 {
363  return ptrs_.begin();
364 }
365 
366 
367 template<class T>
369 {
370  return ptrs_.end();
371 }
372 
373 
374 // * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * //
375 
376 template<class T>
377 inline Foam::PtrList<T>::const_iterator::const_iterator(const T* const* ptr)
378 :
379  ptr_(ptr)
380 {}
381 
382 
383 template<class T>
384 inline Foam::PtrList<T>::const_iterator::const_iterator(const iterator& iter)
385 :
386  ptr_(iter.ptr_)
387 {}
388 
389 
390 template<class T>
392 (
393  const const_iterator& iter
394 ) const
395 {
396  return ptr_ == iter.ptr_;
397 }
398 
399 
400 template<class T>
402 (
403  const const_iterator& iter
404 ) const
405 {
406  return ptr_ != iter.ptr_;
407 }
408 
409 
410 template<class T>
412 {
413  return **ptr_;
414 }
415 
416 
417 template<class T>
419 {
420  return operator*();
421 }
422 
423 
424 template<class T>
425 inline typename Foam::PtrList<T>::const_iterator
427 {
428  ++ptr_;
429  return *this;
430 }
431 
432 
433 template<class T>
434 inline typename Foam::PtrList<T>::const_iterator
436 {
437  const_iterator tmp = *this;
438  ++ptr_;
439  return tmp;
440 }
441 
442 
443 template<class T>
444 inline typename Foam::PtrList<T>::const_iterator
446 {
447  --ptr_;
448  return *this;
449 }
450 
451 
452 template<class T>
453 inline typename Foam::PtrList<T>::const_iterator
455 {
456  const_iterator tmp = *this;
457  --ptr_;
458  return tmp;
459 }
460 
461 
462 template<class T>
463 inline typename Foam::PtrList<T>::const_iterator
465 {
466  ptr_ += n;
467  return *this;
468 }
469 
470 
471 template<class T>
472 inline typename Foam::PtrList<T>::const_iterator
473 Foam::operator+(const typename PtrList<T>::const_iterator& iter, label n)
474 {
475  typename PtrList<T>::const_iterator tmp = iter;
476  return tmp += n;
477 }
478 
479 
480 template<class T>
481 inline typename Foam::PtrList<T>::const_iterator
482 Foam::operator+(label n, const typename PtrList<T>::const_iterator& iter)
483 {
484  typename PtrList<T>::const_iterator tmp = iter;
485  return tmp += n;
486 }
487 
488 
489 template<class T>
490 inline typename Foam::PtrList<T>::const_iterator
492 {
493  ptr_ -= n;
494  return *this;
495 }
496 
497 
498 template<class T>
499 inline typename Foam::PtrList<T>::const_iterator
500 Foam::operator-(const typename PtrList<T>::const_iterator& iter, label n)
501 {
502  typename PtrList<T>::const_iterator tmp = iter;
503  return tmp -= n;
504 }
505 
506 
507 template<class T>
508 inline Foam::label Foam::operator-
509 (
510  const typename PtrList<T>::const_iterator& iter1,
511  const typename PtrList<T>::const_iterator& iter2
512 )
513 {
514  return (iter1.ptr_ - iter2.ptr_)/sizeof(T*);
515 }
516 
517 
518 template<class T>
520 {
521  return *(*this + n);
522 }
523 
524 
525 template<class T>
527 (
528  const const_iterator& iter
529 ) const
530 {
531  return ptr_ < iter.ptr_;
532 }
533 
534 
535 template<class T>
537 (
538  const const_iterator& iter
539 ) const
540 {
541  return ptr_ > iter.ptr_;
542 }
543 
544 
545 template<class T>
547 (
548  const const_iterator& iter
549 ) const
550 {
551  return ptr_ <= iter.ptr_;
552 }
553 
554 
555 template<class T>
557 (
558  const const_iterator& iter
559 ) const
560 {
561  return ptr_ >= iter.ptr_;
562 }
563 
564 
565 template<class T>
566 inline typename Foam::PtrList<T>::const_iterator
568 {
569  return ptrs_.begin();
570 }
571 
572 
573 template<class T>
574 inline typename Foam::PtrList<T>::const_iterator
576 {
577  return ptrs_.end();
578 }
579 
580 
581 template<class T>
582 inline typename Foam::PtrList<T>::const_iterator
584 {
585  return ptrs_.begin();
586 }
587 
588 
589 template<class T>
590 inline typename Foam::PtrList<T>::const_iterator
592 {
593  return ptrs_.end();
594 }
595 
596 
597 // ************************************************************************* //
setSize
points setSize(newPointi)
Foam::PtrList::const_iterator::operator+=
const_iterator operator+=(label)
Definition: PtrListI.H:464
Foam::PtrList::last
T & last()
Return reference to the last element of the list.
Definition: PtrListI.H:60
Foam::PtrList::iterator::operator<
bool operator<(const iterator &) const
Definition: PtrListI.H:333
Foam::PtrList::iterator::operator++
iterator operator++()
Definition: PtrListI.H:233
Foam::PtrList::append
void append(T *)
Append an element at the end of the list.
Foam::PtrList::iterator::operator*
Tref operator*()
Definition: PtrListI.H:218
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:118
Foam::PtrList::begin
iterator begin()
Return an iterator to begin traversing the PtrList.
Definition: PtrListI.H:361
Foam::PtrList::xfer
Xfer< PtrList< T > > xfer()
Transfer contents to the Xfer container.
Definition: PtrListI.H:145
Foam::PtrList::const_iterator::const_iterator
const_iterator(const T *const *)
Construct for a given PtrList entry.
Foam::PtrList::iterator::iterator
iterator(T **)
Construct for a given PtrList entry.
Definition: PtrListI.H:197
Foam::PtrList::iterator::operator[]
T & operator[](label)
Definition: PtrListI.H:326
Foam::PtrList::const_iterator::operator--
const_iterator operator--()
Definition: PtrListI.H:445
Foam::PtrList::const_iterator::operator()
Tref operator()()
Definition: PtrListI.H:418
Foam::Xfer
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
Foam::PtrList::iterator
An STL-conforming iterator.
Definition: PtrList.H:281
Foam::PtrList::operator()
const T * operator()(const label) const
Return element const pointer.
Definition: PtrListI.H:188
Foam::PtrList::end
iterator end()
Return an iterator to end traversing the PtrList.
Definition: PtrListI.H:368
Foam::PtrList::iterator::operator<=
bool operator<=(const iterator &) const
Definition: PtrListI.H:347
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::PtrList::const_iterator
An STL-conforming const_iterator.
Definition: PtrList.H:342
Foam::operator-
tmp< fvMatrix< Type > > operator-(const fvMatrix< Type > &)
Foam::PtrList::set
bool set(const label) const
Is element set.
Foam::label
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
Foam::PtrList::empty
bool empty() const
Return true if the PtrList is empty (ie, size() is zero).
Definition: PtrListI.H:39
Foam::PtrList::iterator::ptr_
T ** ptr_
Definition: PtrList.H:283
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
Foam::PtrList::cend
const_iterator cend() const
Return an const_iterator to end traversing the PtrList.
Definition: PtrListI.H:591
Foam::PtrList::operator[]
const T & operator[](const label) const
Return element const reference.
Foam::PtrList::iterator::operator!=
bool operator!=(const iterator &) const
Definition: PtrListI.H:211
Foam::PtrList
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:61
Foam::PtrList::ptrs_
List< T * > ptrs_
Definition: PtrList.H:130
Foam::PtrList::iterator::operator>
bool operator>(const iterator &) const
Definition: PtrListI.H:340
Foam::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::PtrList::iterator::operator-=
iterator operator-=(label)
Definition: PtrListI.H:298
Foam::PtrList::const_iterator::operator-=
const_iterator operator-=(label)
Definition: PtrListI.H:491
Foam::operator*
tmp< fvMatrix< Type > > operator*(const DimensionedField< scalar, volMesh > &, const fvMatrix< Type > &)
Foam::PtrList::cbegin
const_iterator cbegin() const
Return an const_iterator to begin traversing the PtrList.
Definition: PtrListI.H:583
Foam::PtrList::iterator::operator+=
iterator operator+=(label)
Definition: PtrListI.H:271
Foam::PtrList::resize
void resize(const label)
Alias for setSize(const label)
Definition: PtrListI.H:74
Foam::autoPtr
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
tmp.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::PtrList::first
T & first()
Return reference to the first element of the list.
Definition: PtrListI.H:46
T
const volScalarField & T
Definition: createFields.H:25
Foam::xferMove
Xfer< T > xferMove(T &)
Construct by transferring the contents of the arg.
Foam::PtrList::iterator::operator>=
bool operator>=(const iterator &) const
Definition: PtrListI.H:354
Foam::PtrList::iterator::operator()
Tref operator()()
Definition: PtrListI.H:225
Foam::PtrList::const_iterator::operator[]
const T & operator[](label)
Definition: PtrListI.H:519
Foam::PtrList::const_iterator::ptr_
const T *const * ptr_
Definition: PtrList.H:344
Foam::PtrList::const_iterator::operator*
Tref operator*()
Definition: PtrListI.H:411
Foam::PtrList::const_iterator::operator++
const_iterator operator++()
Definition: PtrListI.H:426
Foam::PtrList::size
label size() const
Return the number of elements in the PtrList.
Definition: PtrListI.H:32
Foam::PtrList::iterator::operator==
bool operator==(const iterator &) const
Definition: PtrListI.H:204
Foam::PtrList::iterator::operator--
iterator operator--()
Definition: PtrListI.H:252
Foam::operator+
tmp< fvMatrix< Type > > operator+(const fvMatrix< Type > &, const fvMatrix< Type > &)
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
autoPtr.H