PtrListDetail.H
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) 2018-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::Detail::PtrListDetail
28 
29 Description
30  A rudimentary list of pointers used for PtrList, UPtrList, etc.
31  This class is considered implementation detail and should not normally
32  be used other than by OpenFOAM container classes.
33 
34  It stores a list of pointers, but makes leaves memory management
35  to the caller or sub-class.
36  The free() method can be used explicitly as required.
37 
38 SourceFiles
39  PtrListDetail.C
40  PtrListDetailI.H
41  PtrListDetailIO.C
42 
43 \*---------------------------------------------------------------------------*/
44 
45 #ifndef Foam_PtrListDetail_H
46 #define Foam_PtrListDetail_H
47 
48 #include "List.H"
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 namespace Detail
55 {
56 
57 /*---------------------------------------------------------------------------*\
58  Class Detail::PtrListDetail Declaration
59 \*---------------------------------------------------------------------------*/
60 
61 template<class T>
62 class PtrListDetail
63 :
64  public List<T*>
65 {
66 public:
67 
68  // Constructors
69 
70  //- Default construct
71  inline constexpr PtrListDetail() noexcept;
72 
73  //- Construct with specified size, each element initialized to nullptr
74  inline explicit PtrListDetail(const label len);
75 
76  //- Copy a list of pointers.
77  // The caller is responsible for memory management.
78  inline explicit PtrListDetail(const UList<T*>& list);
79 
80  //- Copy construct (shallow copies addresses)
81  inline PtrListDetail(const PtrListDetail<T>& list);
82 
83  //- Move construct
84  inline PtrListDetail(PtrListDetail<T>&& list);
85 
86  //- Copy or move (reuse) construct as specified
87  inline PtrListDetail(PtrListDetail<T>& list, bool reuse);
88 
89 
90  // Member Functions
91 
92  //- Return const pointer to element or nullptr for out-of-range access.
93  inline const T* get(const label i) const;
94 
95  //- Return pointer to element or nullptr for out-of-range access.
96  inline T* get(const label i);
97 
98  //- Return the number of non-null entries
99  label count() const noexcept;
100 
101  //- FatalError if any null exists in the list
102  inline void checkNonNull() const;
103 
104  //- Locate the first entry that is non-null.
105  // \return the location or -1 if there are no bits set.
106  //
107  // \note Method name as per bitSet
108  label find_first() const;
109 
110  //- Locate the first entry that is null, -1 if there are none (or empty list)
111  // \return the location or -1 if the list is empty or all entries
112  // are non-null.
113  //
114  // \note Method name as per bitSet, symmetry with find_first()
115  label find_first_not() const;
116 
117  //- Locate the next non-null entry, starting one beyond the specified
118  //- position
119  // \return the location or -1 if remaining entries are null.
120  //
121  // \note Method name as per bitSet
122  label find_next(label pos) const;
123 
124  //- Locate the next null entry, starting one beyond the specified
125  //- position
126  // \return the location or -1 if remaining entries are non-null.
127  //
128  // \note Method name as per bitSet
129  label find_next_not(label pos) const;
130 
131  //- Reassign all pointers to nullptr, without deleting.
132  //- Does not affect the list size.
133  void setNull();
134 
135  //- Delete allocated entries and reassign to nullptr.
136  //- Does not affect the list size.
137  void free();
138 
139  //- Make a copy by cloning each of the list pointers.
140  template<class... Args>
141  PtrListDetail<T> clone(Args&&... args) const;
142 
143  //- Reset size of list.
144  // New entries are initialized to nullptr.
145  inline void resize(const label newLen);
146 
147  //- Set addressed size to be inconsistent with allocated storage.
148  // Use with care
149  inline void setAddressableSize(const label n) noexcept;
150 
151  //- Write output, optionally silently trimming nullptrs
152  Ostream& write(Ostream& os, const bool trimNull=false) const;
153 
154 
155  // Member Operators
156 
157  //- Copy assignment (shallow copies addresses)
158  inline void operator=(const PtrListDetail<T>& list);
159 
160  //- Move assignment
161  inline void operator=(PtrListDetail<T>&& list);
162 
163 
164  // Housekeeping
165 
166  // Just use resize().
167  void setSize(const label) = delete;
168  void setSize(const label, const T&) = delete;
169  void setSize(const label, const T*) = delete;
170 
171  // Too fragile or dangerous
172  void resize_nocopy(const label) = delete;
173 };
174 
175 
176 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
177 
178 } // End namespace Detail
179 } // End namespace Foam
180 
181 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
182 
183 #include "PtrListDetailI.H"
184 
185 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
186 
187 #ifdef NoRepository
188  #include "PtrListDetail.C"
189  #include "PtrListDetailIO.C"
190 #endif
191 
192 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
193 
194 
195 #endif
196 
197 // ************************************************************************* //
void resize_nocopy(const label)=delete
Ostream & write(Ostream &os, const bool trimNull=false) const
Write output, optionally silently trimming nullptrs.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
label find_first_not() const
Locate the first entry that is null, -1 if there are none (or empty list)
Definition: PtrListDetail.C:50
A rudimentary list of pointers used for PtrList, UPtrList, etc. This class is considered implementati...
Definition: PtrListDetail.H:57
void setSize(const label)=delete
void checkNonNull() const
FatalError if any null exists in the list.
void setNull()
Reassign all pointers to nullptr, without deleting. Does not affect the list size.
Definition: PtrListDetail.C:93
dimensionedScalar pos(const dimensionedScalar &ds)
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:99
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
const direction noexcept
Definition: Scalar.H:258
void operator=(const PtrListDetail< T > &list)
Copy assignment (shallow copies addresses)
OBJstream os(runTime.globalPath()/outputName)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void setAddressableSize(const label n) noexcept
Set addressed size to be inconsistent with allocated storage.
label find_next_not(label pos) const
Locate the next null entry, starting one beyond the specified position.
Definition: PtrListDetail.C:75
void resize(const label newLen)
Reset size of list.
label count() const noexcept
Return the number of non-null entries.
Definition: PtrListDetail.C:26
label find_first() const
Locate the first entry that is non-null.
Definition: PtrListDetail.C:43
autoPtr< List< T * > > clone() const
Clone.
Definition: ListI.H:93
label n
constexpr PtrListDetail() noexcept
Default construct.
void free()
Delete allocated entries and reassign to nullptr. Does not affect the list size.
Foam::argList args(argc, argv)
Namespace for OpenFOAM.
label find_next(label pos) const
Locate the next non-null entry, starting one beyond the specified position.
Definition: PtrListDetail.C:57