PtrList.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2018-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "PtrList.H"
30 #include "SLPtrList.H"
31 
32 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
33 
34 template<class T>
35 Foam::PtrList<T>::PtrList(PtrList<T>& list, bool reuse)
36 :
37  UPtrList<T>(list, reuse)
38 {
39  if (!reuse)
40  {
41  // This works like an inplace clone method
42  const label len = this->size();
43 
44  for (label i=0; i<len; ++i)
45  {
46  this->ptrs_[i] = (list[i]).clone().ptr();
47  }
48  }
49 }
50 
51 
52 template<class T>
53 Foam::PtrList<T>::PtrList(const SLPtrList<T>& list)
54 :
55  UPtrList<T>(list.size())
56 {
57  if (list.size())
58  {
59  label i = 0;
60  for (auto iter = list.cbegin(); iter != list.cend(); ++iter)
61  {
62  this->ptrs_[i++] = (*iter).clone().ptr();
63  }
64  }
65 }
66 
67 
68 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
69 
70 template<class T>
72 {
73  (this->ptrs_).free(); // Free old pointers
74 }
75 
76 
77 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
78 
79 template<class T>
80 template<class... Args>
82 {
83  const label len = this->size();
84 
85  PtrList<T> cloned(len);
86 
87  for (label i=0; i<len; ++i)
88  {
89  const T* ptr = this->ptrs_[i];
90 
91  if (ptr)
92  {
93  cloned.ptrs_[i] = ptr->clone(std::forward<Args>(args)...).ptr();
94  }
95  }
96 
97  return cloned;
98 }
99 
100 
101 template<class T>
102 void Foam::PtrList<T>::resize(const label newLen)
103 {
104  const label oldLen = this->size();
105 
106  if (newLen <= 0)
107  {
108  clear();
109  }
110  else if (newLen != oldLen)
111  {
112  // Truncation frees old pointers
113  for (label i = newLen; i < oldLen; ++i)
114  {
115  delete this->ptrs_[i];
116  this->ptrs_[i] = nullptr;
117  }
118 
119  // Any new elements are initialized to nullptr.
120  (this->ptrs_).resize(newLen);
121  }
122 }
123 
124 
125 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
126 
127 template<class T>
128 void Foam::PtrList<T>::operator=(const PtrList<T>& list)
129 {
130  if (this == &list)
131  {
132  return; // Self-assignment is a no-op
133  }
134 
135  const label oldLen = this->size();
136  const label newLen = list.size();
137 
138  // Truncate (frees old pointers) or extend the length
139  resize(newLen);
140 
141  if (newLen < oldLen)
142  {
143  // Copy values for existing entries
144  for (label i=0; i<newLen; ++i)
145  {
146  (*this)[i] = list[i];
147  }
148  }
149  else
150  {
151  // Copy values for existing entries
152  for (label i=0; i<oldLen; ++i)
153  {
154  (*this)[i] = list[i];
155  }
156 
157  // Clone pointers for new entries
158  for (label i=oldLen; i<newLen; ++i)
159  {
160  this->ptrs_[i] = (list[i]).clone().ptr();
161  }
162  }
163 }
164 
165 
166 // ************************************************************************* //
patchWriters resize(patchIds.size())
PtrList< T > clone(Args &&... args) const
Make a copy by cloning each of the list elements.
~PtrList()
Destructor. Frees all pointers.
Definition: PtrList.C:64
constexpr PtrList() noexcept
Default construct.
Definition: PtrListI.H:29
patchWriters clear()
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:106
void operator=(const PtrList< T > &list)
Copy assignment.
Definition: PtrList.C:121
const volScalarField & T
Non-intrusive singly-linked pointer list.
void resize(const label newLen)
Adjust size of PtrList.
Definition: PtrList.C:95
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
Foam::argList args(argc, argv)
Detail::PtrListDetail< T > ptrs_
The list of pointers.
Definition: UPtrList.H:109