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
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  //- Delete allocated entries and reassign to nullptr.
132  //- Does not affect the list size.
133  void free();
134 
135  //- Make a copy by cloning each of the list pointers.
136  template<class... Args>
137  PtrListDetail<T> clone(Args&&... args) const;
138 
139  //- Reset size of list.
140  // New entries are initialized to nullptr.
141  inline void resize(const label newLen);
142 
143  //- Set the list to the given size and set all entries to nullptr.
144  inline void resize_null(const label newLen);
145 
146  //- Set addressed size to be inconsistent with allocated storage.
147  // Use with care
148  inline void setAddressableSize(const label n) noexcept;
149 
150  //- Write output, optionally silently trimming nullptrs
151  Ostream& write(Ostream& os, const bool trimNull=false) const;
152 
153 
154  // Member Operators
155 
156  //- Copy assignment (shallow copies addresses)
157  inline void operator=(const PtrListDetail<T>& list);
158 
159  //- Move assignment
160  inline void operator=(PtrListDetail<T>&& list);
161 
162  //- Assign all entries to nullptr (without deleting)
163  inline void operator=(std::nullptr_t);
164 
165 
166  // Housekeeping
167 
168  // Just use resize().
169  void setSize(const label) = delete;
170  void setSize(const label, const T&) = delete;
171  void setSize(const label, const T*) = delete;
172 
173  // Too fragile or dangerous
174  void resize_nocopy(const label) = delete;
175 };
176 
177 
178 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
179 
180 } // End namespace Detail
181 } // End namespace Foam
182 
183 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
184 
185 #include "PtrListDetailI.H"
186 
187 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
188 
189 #ifdef NoRepository
190  #include "PtrListDetail.C"
191  #include "PtrListDetailIO.C"
192 #endif
193 
194 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
195 
196 
197 #endif
198 
199 // ************************************************************************* //
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.
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:105
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
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:121
label n
constexpr PtrListDetail() noexcept
Default construct.
void free()
Delete allocated entries and reassign to nullptr. Does not affect the list size.
Definition: PtrListDetail.C:93
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
void resize_null(const label newLen)
Set the list to the given size and set all entries to nullptr.