Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
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
32// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33
34template<class T>
35template<bool CheckSelf>
36void 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 const T* src = list.get(i);
55
56 if (src)
57 {
58 if (this->ptrs_[i])
59 {
60 // Deep copy values into existing destination
61 *(this->ptrs_[i]) = *src;
62 }
63 else
64 {
65 // Clone pointers for new entries
66 this->ptrs_[i] = src->clone().ptr();
67 }
68 }
69 else
70 {
71 // No source pointer, so remove destination (if any) too
72 delete this->ptrs_[i];
73 this->ptrs_[i] = nullptr;
74 }
75 }
76}
77
78
79// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
80
81template<class T>
82Foam::PtrList<T>::PtrList(const SLPtrList<T>& list)
83:
84 UPtrList<T>(list.size())
85{
86 if (list.size())
87 {
88 label i = 0;
89 for (auto iter = list.cbegin(); iter != list.cend(); ++iter)
90 {
91 this->ptrs_[i++] = (*iter).clone().ptr();
92 }
93 }
94}
95
96
97// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
98
99template<class T>
101{
102 (this->ptrs_).free(); // Free old pointers
104
105
106// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
107
108template<class T>
109template<class... Args>
111{
112 const label len = this->size();
113
114 PtrList<T> cloned(len);
115
116 for (label i=0; i<len; ++i)
117 {
118 const T* ptr = this->ptrs_[i];
119
120 if (ptr)
121 {
122 cloned.ptrs_[i] = ptr->clone(std::forward<Args>(args)...).ptr();
123 }
125
126 return cloned;
127}
128
129
130template<class T>
131void Foam::PtrList<T>::resize(const label newLen)
132{
133 const label oldLen = this->size();
134
135 if (newLen <= 0)
136 {
137 clear();
138 }
139 else if (newLen != oldLen)
140 {
141 // Truncation frees old pointers
142 for (label i = newLen; i < oldLen; ++i)
143 {
144 delete this->ptrs_[i];
145 this->ptrs_[i] = nullptr;
146 }
147
148 // Any new elements are initialized to nullptr.
149 (this->ptrs_).resize(newLen);
150 }
152
153
154// ************************************************************************* //
Non-intrusive singly-linked pointer list.
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition PtrList.H:67
PtrList< T > clone(Args &&... args) const
Make a copy by cloning each of the list elements.
void free()
Free memory and nullify all entries. Does not change the list size.
Definition PtrListI.H:106
~PtrList()
Destructor. Frees all pointers.
Definition PtrList.C:93
constexpr PtrList() noexcept
Default construct.
Definition PtrListI.H:29
void resize(const label newLen)
Adjust size of PtrList.
Definition PtrList.C:124
UPtrList(Detail::PtrListDetail< T > &&ptrs) noexcept
Low-level move construct.
Definition UPtrListI.H:48
label size() const noexcept
The number of entries in the list.
Definition UPtrListI.H:106
Detail::PtrListDetail< T > ptrs_
The list of pointers.
Definition UPtrList.H:109
const volScalarField & T
patchWriters resize(patchIds.size())
surface1 clear()
LPtrList< SLListBase, T > SLPtrList
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Foam::argList args(argc, argv)
#define FOAM_UNLIKELY(cond)
Definition stdFoam.H:64