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  T* ptr = this->ptrs_[i];
116 
117  if (ptr)
118  {
119  delete ptr;
120  }
121  }
122 
123  // Any new elements are initialized to nullptr.
124  (this->ptrs_).resize(newLen);
125  }
126 }
127 
128 
129 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
130 
131 template<class T>
132 void Foam::PtrList<T>::operator=(const PtrList<T>& list)
133 {
134  if (this == &list)
135  {
136  return; // Self-assignment is a no-op
137  }
138 
139  const label oldLen = this->size();
140  const label newLen = list.size();
141 
142  // Truncate (frees old pointers) or extend the length
143  resize(newLen);
144 
145  if (newLen < oldLen)
146  {
147  // Copy values for existing entries
148  for (label i=0; i<newLen; ++i)
149  {
150  (*this)[i] = list[i];
151  }
152  }
153  else
154  {
155  // Copy values for existing entries
156  for (label i=0; i<oldLen; ++i)
157  {
158  (*this)[i] = list[i];
159  }
160 
161  // Clone pointers for new entries
162  for (label i=oldLen; i<newLen; ++i)
163  {
164  this->ptrs_[i] = (list[i]).clone().ptr();
165  }
166  }
167 }
168 
169 
170 // ************************************************************************* //
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:38
patchWriters clear()
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:100
void operator=(const PtrList< T > &list)
Copy assignment.
Definition: PtrList.C:125
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:75