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-2021 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  ptrs[i] = nullptr;
78  }
79 }
80 
81 
82 template<class T>
84 {
85  List<T*>& ptrs = *this;
86  const label len = ptrs.size();
87 
88  for (label i=0; i<len; ++i)
89  {
90  T* ptr = ptrs[i];
91 
92  if (ptr)
93  {
94  delete ptr;
95  }
96 
97  ptrs[i] = nullptr;
98  }
99 }
100 
101 
102 template<class T>
103 template<class... Args>
106 {
107  const List<T*>& ptrs = *this;
108  const label len = ptrs.size();
109 
110  PtrListDetail<T> cloned(len);
111 
112  for (label i=0; i<len; ++i)
113  {
114  const T* ptr = ptrs[i];
115 
116  if (ptr)
117  {
118  cloned[i] = ptr->clone(std::forward<Args>(args)...).ptr();
119  }
120  }
121 
122  return cloned;
123 }
124 
125 
126 // ************************************************************************* //
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::setNull
void setNull()
Assign all pointers to nullptr, without deleting.
Definition: PtrListDetail.C:70
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:83
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:100
args
Foam::argList args(argc, argv)