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-2025 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 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 template<class T>
35 template<bool CheckSelf>
36 void Foam::PtrList<T>::copyPtrList(const UPtrList<T>& list)
37 {
38  // Check for self-assignment here instead of caller
39  if constexpr (CheckSelf)
40  {
41  if (FOAM_UNLIKELY(this == &list))
42  {
43  return; // Self-assignment is a no-op
44  }
45  }
46 
47  const label len = list.size();
48 
49  // Truncate (frees old pointers) or extend the length
50  PtrList<T>::resize(len);
51 
52  for (label i = 0; i < len; ++i)
53  {
54  if (const T* src = list.get(i); src)
55  {
56  if (this->ptrs_[i])
57  {
58  // Deep copy values into existing destination
59  *(this->ptrs_[i]) = *src;
60  }
61  else
62  {
63  // Clone pointers for new entries
64  this->ptrs_[i] = src->clone().ptr();
65  }
66  }
67  else
68  {
69  // No source pointer, so remove destination (if any) too
70  delete this->ptrs_[i];
71  this->ptrs_[i] = nullptr;
72  }
73  }
74 }
75 
76 
77 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
78 
79 template<class T>
80 Foam::PtrList<T>::PtrList(const SLPtrList<T>& list)
81 :
82  UPtrList<T>(list.size())
83 {
84  if (list.size())
85  {
86  label i = 0;
87  for (auto iter = list.cbegin(); iter != list.cend(); ++iter)
88  {
89  this->ptrs_[i++] = (*iter).clone().ptr();
90  }
91  }
92 }
93 
94 
95 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
96 
97 template<class T>
99 {
100  (this->ptrs_).free(); // Free old pointers
101 }
102 
103 
104 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
105 
106 template<class T>
107 template<class... Args>
109 {
110  const label len = this->size();
111 
112  PtrList<T> cloned(len);
113 
114  for (label i=0; i<len; ++i)
115  {
116  if (const T* ptr = this->ptrs_[i]; ptr)
117  {
118  cloned.ptrs_[i] = ptr->clone(std::forward<Args>(args)...).ptr();
119  }
120  }
121 
122  return cloned;
123 }
124 
125 
126 template<class T>
127 void Foam::PtrList<T>::resize(const label newLen)
128 {
129  if (newLen <= 0)
130  {
131  clear();
132  }
133  else if (const label oldLen = this->size(); newLen != oldLen)
134  {
135  // Truncation frees old pointers
136  for (label i = newLen; i < oldLen; ++i)
137  {
138  delete this->ptrs_[i];
139  this->ptrs_[i] = nullptr;
140  }
141 
142  // Any new elements are initialized to nullptr.
143  (this->ptrs_).resize(newLen);
144  }
145 }
146 
147 
148 // ************************************************************************* //
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:91
constexpr PtrList() noexcept
Default construct.
Definition: PtrListI.H:29
label size() const noexcept
The number of entries in the list.
Definition: UPtrListI.H:106
#define FOAM_UNLIKELY(cond)
Definition: stdFoam.H:65
const volScalarField & T
Non-intrusive singly-linked pointer list.
void resize(const label newLen)
Adjust size of PtrList.
Definition: PtrList.C:120
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: PtrList.H:56
surface1 clear()
Foam::argList args(argc, argv)
Detail::PtrListDetail< T > ptrs_
The list of pointers.
Definition: UPtrList.H:109