PtrListDetail.C
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-2019 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 "PtrListDetail.H"
29 #include <utility>
30 
31 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
32 
33 template<class T>
35 {
36  label ngood = 0;
37 
38  for (const T* ptr : *this)
39  {
40  if (ptr)
41  {
42  ++ngood;
43  }
44  }
45 
46  return ngood;
47 }
48 
49 
50 template<class T>
52 {
53  label idx = 0;
54 
55  for (const T* ptr : *this)
56  {
57  if (!ptr)
58  {
59  return idx;
60  }
61 
62  ++idx;
63  }
64 
65  return -1;
66 }
67 
68 
69 template<class T>
71 {
72  List<T*>& ptrs = *this;
73  const label len = ptrs.size();
74 
75  for (label i=0; i<len; ++i)
76  {
77  T* ptr = ptrs[i];
78 
79  if (ptr)
80  {
81  delete ptr;
82  }
83 
84  ptrs[i] = nullptr;
85  }
86 }
87 
88 
89 template<class T>
90 template<class... Args>
93 {
94  const List<T*>& ptrs = *this;
95  const label len = ptrs.size();
96 
97  PtrListDetail<T> cloned(len);
98 
99  for (label i=0; i<len; ++i)
100  {
101  const T* ptr = ptrs[i];
102 
103  if (ptr)
104  {
105  cloned[i] = ptr->clone(std::forward<Args>(args)...).ptr();
106  }
107  }
108 
109  return cloned;
110 }
111 
112 
113 // ************************************************************************* //
Foam::Detail::PtrListDetail::findNull
label findNull() const
Locate the first null entry, -1 if there are not any.
Definition: PtrListDetail.C:51
Foam::Detail::PtrListDetail::count
label count() const
Return the count of non-nullptr entries.
Definition: PtrListDetail.C:34
Foam::Detail::PtrListDetail::clone
PtrListDetail< T > clone(Args &&... args) const
Make a copy by cloning each of the list pointers.
Foam::Detail::PtrListDetail::free
void free()
Delete the allocated entries, but retain the list size.
Definition: PtrListDetail.C:70
PtrListDetail.H
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::List< T * >
Foam::Detail::PtrListDetail
A rudimentary list of pointers used for PtrList, UPtrList, etc. This class is considered implementati...
Definition: PtrListDetail.H:61
Foam::List< T * >::clone
autoPtr< List< T * > > clone() const
Clone.
Definition: ListI.H:99
args
Foam::argList args(argc, argv)