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-2021 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 from UList. Size set to UList size.
109  // Also constructs from DynamicField with different sizing parameters.
110  explicit inline DynamicField(const UList<T>&);
111 
112  //- Move constructor transferring the list contents
113  explicit inline DynamicField(List<T>&&);
114 
115  //- Construct by 1 to 1 mapping from the given field
116  inline DynamicField
117  (
118  const UList<T>& mapF,
119  const labelList& mapAddressing
120  );
121 
122  //- Construct by interpolative mapping from the given field
123  inline DynamicField
124  (
125  const UList<T>& mapF,
126  const labelListList& mapAddressing,
127  const scalarListList& weights
128  );
129 
130  //- Copy constructor
132 
133  //- Move constructor
135 
136  //- Construct from Istream. Size set to size of list read.
137  explicit DynamicField(Istream&);
138 
139  //- Clone
141 
142 
143  // Member Functions
144 
145  // Access
146 
147  //- Size of the underlying storage.
148  inline label capacity() const;
149 
150  // Edit
151 
152  //- Alter the size of the underlying storage.
153  // The addressed size will be truncated if needed to fit, but will
154  // remain otherwise untouched.
155  // Use this or reserve() in combination with append().
156  inline void setCapacity(const label);
157 
158  //- Alter the addressed list size.
159  // New space will be allocated if required.
160  // Use this to resize the list prior to using the operator[] for
161  // setting values (as per List usage).
162  inline void setSize(const label);
163 
164  //- Alter the addressed list size and fill new space with a
165  // constant.
166  inline void setSize(const label, const T&);
167 
168  //- Alter the addressed list size.
169  // New space will be allocated if required.
170  // Use this to resize the list prior to using the operator[] for
171  // setting values (as per List usage).
172  inline void resize(const label);
173 
174  //- Alter the addressed list size and fill new space with a
175  // constant.
176  inline void resize(const label, const T&);
177 
178  //- Reserve allocation space for at least this size.
179  // Never shrinks the allocated size, use setCapacity() for that.
180  inline void reserve(const label);
181 
182  //- Clear the addressed list, i.e. set the size to zero.
183  // Allocated size does not change
184  inline void clear();
185 
186  //- Clear the list and delete storage.
187  inline void clearStorage();
188 
189  //- Shrink the allocated space to the number of elements used.
190  // Returns a reference to the DynamicField.
192 
193 
194  // Member Operators
195 
196  //- Append an element at the end of the list
198  (
199  const T&
200  );
201 
202  //- Append a List at the end of this list
204  (
205  const UList<T>&
206  );
207 
208  //- Remove and return the top element
209  inline T remove();
210 
211  //- Return non-const access to an element, resizing list if
212  // necessary
213  inline T& operator()(const label);
214 
215  //- Assignment of all addressed entries to the given value
216  inline void operator=(const T&);
217 
218  //- Assignment operator
219  inline void operator=
220  (
222  );
223 
224  //- Move assignment operator
225  inline void operator=
226  (
228  );
229 
230  //- Assignment to UList
231  inline void operator=(const UList<T>&);
232 
233  //- Move assignment to List
234  inline void operator=(List<T>&&);
235 
236 
237  // IOstream Operators
238 
239  // Write DynamicField to Ostream.
240  friend Ostream& operator<< <T, SizeInc, SizeMult, SizeDiv>
241  (
242  Ostream&,
244  );
245 
246  //- Read from Istream, discarding contents of existing DynamicField.
247  friend Istream& operator>> <T, SizeInc, SizeMult, SizeDiv>
248  (
249  Istream&,
251  );
252 };
253 
254 
255 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
256 
257 } // End namespace Foam
258 
259 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
260 
261 #include "DynamicFieldI.H"
262 
263 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
264 
265 #ifdef NoRepository
266  #include "DynamicField.C"
267 #endif
268 
269 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
270 
271 #endif
272 
273 // ************************************************************************* //
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:40
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:82
tmp< Field< T > > T() const
Return the field transpose (only defined for second rank tensors)
Definition: Field.C:515
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
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 class for managing temporary objects.
Definition: tmp.H:55
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