DynamicField.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::DynamicField
26 
27 Description
28  Dynamically sized Field.
29 
30 SourceFiles
31  DynamicFieldI.H
32  DynamicField.C
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef DynamicField_H
37 #define DynamicField_H
38 
39 #include "Field.H"
40 #include "DynamicFieldFwd.H"
41 #include <type_traits>
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 // Forward declaration of friend functions and operators
49 
50 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
51 Ostream& operator<<
52 (
53  Ostream&,
55 );
56 
57 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
58 Istream& operator>>
59 (
60  Istream&,
62 );
63 
64 
65 /*---------------------------------------------------------------------------*\
66  Class DynamicField Declaration
67 \*---------------------------------------------------------------------------*/
68 
69 template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
70 class DynamicField
71 :
72  public Field<T>
73 {
74  static_assert
75  (
76  (SizeInc || SizeMult) && SizeDiv,
77  "Avoid invalid sizing parameters"
78  );
79 
80  // Private Data
81 
82  //- The capacity (allocated size) of the underlying field.
83  label capacity_;
84 
85 
86 public:
87 
88  // Static Member Functions
89 
90  //- Return a null field
91  inline static const DynamicField<T, SizeInc, SizeMult, SizeDiv>& null()
92  {
93  return *reinterpret_cast
94  <
96  >(0);
97  }
98 
99 
100  // Constructors
101 
102  //- Construct null
103  inline DynamicField();
104 
105  //- Construct given size.
106  explicit inline DynamicField(const label);
107 
108  //- Construct given size and initial value
109  inline DynamicField(const label, const T&);
110 
111  //- Construct given size and initialised to zero
112  inline DynamicField(const label, const zero);
113 
114  //- Construct from UList. Size set to UList size.
115  // Also constructs from DynamicField with different sizing parameters.
116  explicit inline DynamicField(const UList<T>&);
117 
118  //- Move constructor transferring the list contents
119  explicit inline DynamicField(List<T>&&);
120 
121  //- Construct by 1 to 1 mapping from the given field
122  inline DynamicField
123  (
124  const UList<T>& mapF,
125  const labelList& mapAddressing
126  );
127 
128  //- Construct by interpolative mapping from the given field
129  inline DynamicField
130  (
131  const UList<T>& mapF,
132  const labelListList& mapAddressing,
133  const scalarListList& weights
134  );
135 
136  //- Copy constructor
138 
139  //- Copy constructor or reuse as specified
140  inline DynamicField
141  (
143  const bool
144  );
145 
146  //- Move constructor
148 
149  //- Construct from Istream. Size set to size of list read.
150  explicit DynamicField(Istream&);
151 
152  //- Construct from a dictionary entry
153  DynamicField(const word& keyword, const dictionary&, const label size);
154 
155  //- Construct from a dictionary entry with unit conversion
157  (
158  const word& keyword,
159  const unitConversion&,
160  const dictionary&,
161  const label size
162  );
163 
164  //- Clone
166 
167 
168  // Member Functions
169 
170  // Access
171 
172  //- Size of the underlying storage.
173  inline label capacity() const;
174 
175  // Edit
176 
177  //- Alter the size of the underlying storage.
178  // The addressed size will be truncated if needed to fit, but will
179  // remain otherwise untouched.
180  // Use this or reserve() in combination with append().
181  inline void setCapacity(const label);
182 
183  //- Alter the addressed list size.
184  // New space will be allocated if required.
185  // Use this to resize the list prior to using the operator[] for
186  // setting values (as per List usage).
187  inline void setSize(const label);
188 
189  //- Alter the addressed list size and fill new space with a
190  // constant.
191  inline void setSize(const label, const T&);
192 
193  //- Alter the addressed list size.
194  // New space will be allocated if required.
195  // Use this to resize the list prior to using the operator[] for
196  // setting values (as per List usage).
197  inline void resize(const label);
198 
199  //- Alter the addressed list size and fill new space with a
200  // constant.
201  inline void resize(const label, const T&);
202 
203  //- Reserve allocation space for at least this size.
204  // Never shrinks the allocated size, use setCapacity() for that.
205  inline void reserve(const label);
206 
207  //- Clear the addressed list, i.e. set the size to zero.
208  // Allocated size does not change
209  inline void clear();
210 
211  //- Clear the list and delete storage.
212  inline void clearStorage();
213 
214  //- Shrink the allocated space to the number of elements used.
215  // Returns a reference to the DynamicField.
217 
218 
219  // Member Operators
220 
221  //- Append an element at the end of the list
223  (
224  const T&
225  );
226 
227  //- Append a List at the end of this list
229  (
230  const UList<T>&
231  );
232 
233  //- Remove and return the top element
234  inline T remove();
235 
236  //- Return non-const access to an element, resizing list if
237  // necessary
238  inline T& operator()(const label);
239 
240  //- Assignment of all addressed entries to the given value
241  inline void operator=(const T&);
242 
243  //- Assignment operator
244  inline void operator=
245  (
247  );
248 
249  //- Move assignment operator
250  inline void operator=
251  (
253  );
254 
255  //- Assignment to UList
256  inline void operator=(const UList<T>&);
257 
258  //- Move assignment to List
259  inline void operator=(List<T>&&);
260 
261 
262  // IOstream Operators
263 
264  // Write DynamicField to Ostream.
265  friend Ostream& operator<< <T, SizeInc, SizeMult, SizeDiv>
266  (
267  Ostream&,
269  );
270 
271  //- Read from Istream, discarding contents of existing DynamicField.
272  friend Istream& operator>> <T, SizeInc, SizeMult, SizeDiv>
273  (
274  Istream&,
276  );
277 };
278 
279 
280 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
281 
282 } // End namespace Foam
283 
284 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
285 
286 #include "DynamicFieldI.H"
287 
288 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
289 
290 #ifdef NoRepository
291  #include "DynamicField.C"
292 #endif
293 
294 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
295 
296 #endif
297 
298 // ************************************************************************* //
Dynamically sized Field.
Definition: DynamicField.H:72
T remove()
Remove and return the top element.
DynamicField< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
void setCapacity(const label)
Alter the size of the underlying storage.
void resize(const label)
Alter the addressed list size.
tmp< DynamicField< T, SizeInc, SizeMult, SizeDiv > > clone() const
Clone.
Definition: DynamicField.C:67
label capacity() const
Size of the underlying storage.
DynamicField()
Construct null.
Definition: DynamicFieldI.H:29
DynamicField< T, SizeInc, SizeMult, SizeDiv > & shrink()
Shrink the allocated space to the number of elements used.
void operator=(const T &)
Assignment of all addressed entries to the given value.
void reserve(const label)
Reserve allocation space for at least this size.
void clearStorage()
Clear the list and delete storage.
T & operator()(const label)
Return non-const access to an element, resizing list if.
void clear()
Clear the addressed list, i.e. set the size to zero.
void setSize(const label)
Alter the addressed list size.
Pre-declare SubField and related Field type.
Definition: Field.H:83
tmp< Field< T > > T() const
Return the field transpose (only defined for second rank tensors)
Definition: Field.C:539
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
label size() const
Return the number of elements in the UList.
Definition: ListI.H:171
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:74
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
A class for managing temporary objects.
Definition: tmp.H:55
Unit conversion structure. Contains the associated dimensions and the multiplier with which to conver...
A class for handling words, derived from string.
Definition: word.H:62
A class representing the concept of 0 used to avoid unnecessary manipulations for objects that are kn...
Definition: zero.H:50
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59