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-------------------------------------------------------------------------------
11License
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#include <utility>
32
33// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
34
35template<class T>
37:
38 UPtrList<T>(list, reuse)
39{
40 if (!reuse)
41 {
42 // This works like an inplace clone method
43 const label len = this->size();
44
45 for (label i=0; i<len; ++i)
46 {
47 this->ptrs_[i] = (list[i]).clone().ptr();
48 }
49 }
50}
51
52
53template<class T>
55:
56 UPtrList<T>(list.size())
57{
58 if (list.size())
59 {
60 label i = 0;
61 for (auto iter = list.cbegin(); iter != list.cend(); ++iter)
62 {
63 this->ptrs_[i++] = (*iter).clone().ptr();
64 }
65 }
66}
67
68
69// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
70
71template<class T>
73{
74 (this->ptrs_).free(); // free old pointers
75}
76
77
78// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
79
80template<class T>
81template<class... Args>
83{
84 const label len = this->size();
85
86 PtrList<T> cloned(len);
87
88 for (label i=0; i<len; ++i)
89 {
90 const T* ptr = this->ptrs_[i];
91
92 if (ptr)
93 {
94 cloned.ptrs_[i] = ptr->clone(std::forward<Args>(args)...).ptr();
95 }
96 }
97
98 return cloned;
99}
100
101
102template<class T>
103void Foam::PtrList<T>::resize(const label newLen)
104{
105 const label oldLen = this->size();
106
107 if (newLen <= 0)
108 {
110 }
111 else if (newLen != oldLen)
113 // Truncation frees old pointers
114 for (label i=newLen; i<oldLen; ++i)
115 {
116 T* ptr = this->ptrs_[i];
117
118 if (ptr)
119 {
120 delete ptr;
121 }
122 }
124 // Any new elements are initialized to nullptr.
125 (this->ptrs_).resize(newLen);
126 }
127}
128
129
130// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
131
132template<class T>
134{
135 if (this == &list)
136 {
137 return; // Self-assignment is a no-op
138 }
139
140 const label oldLen = this->size();
141 const label newLen = list.size();
142
143 // Truncate (frees old pointers) or extend the length
144 resize(newLen);
145
146 if (newLen < oldLen)
147 {
148 // Copy values for existing entries
149 for (label i=0; i<newLen; ++i)
150 {
151 (*this)[i] = list[i];
152 }
153 }
154 else
155 {
156 // Copy values for existing entries
157 for (label i=0; i<oldLen; ++i)
158 {
159 (*this)[i] = list[i];
160 }
161
162 // Clone pointers for new entries
163 for (label i=oldLen; i<newLen; ++i)
164 {
165 this->ptrs_[i] = (list[i]).clone().ptr();
166 }
167 }
168}
169
170
171// ************************************************************************* //
Non-intrusive singly-linked pointer list.
virtual bool resize()
Resize the ODE solver.
Definition: Euler.C:53
Template class for non-intrusive linked PtrLists.
Definition: LPtrList.H:75
const_iterator cbegin() const
Iterator to first item in list with const access.
Definition: LPtrList.H:350
const const_iterator & cend() const
End of list for forward iterators.
Definition: LPtrList.H:387
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: PtrList.H:73
PtrList< T > clone(Args &&... args) const
Make a copy by cloning each of the list elements.
~PtrList()
Destructor.
Definition: PtrList.C:72
constexpr PtrList() noexcept
Default construct.
Definition: PtrListI.H:45
void operator=(const PtrList< T > &list)
Copy assignment.
Definition: PtrList.C:133
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:71
label size() const noexcept
The number of elements in the list.
Definition: UPtrListI.H:106
Detail::PtrListDetail< T > ptrs_
The list of pointers.
Definition: UPtrList.H:77
virtual tmp< fvPatchScalarField > clone() const
Construct and return a clone.
const volScalarField & T
patchWriters resize(patchIds.size())
patchWriters clear()
Foam::argList args(argc, argv)