OldTimeField.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) 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::OldTimeField
26 
27 Description
28  Class to add into field types to provide old-time storage and retrieval
29 
30 SourceFiles
31  OldTimeFieldI.H
32  OldTimeField.C
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef OldTimeField_H
37 #define OldTimeField_H
38 
39 #include "Time.H"
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45 
46 // Forward declaration of classes
47 template<class FieldType>
48 class OldTimeField;
49 
50 
51 /*---------------------------------------------------------------------------*\
52  Using OldTimeVoid Declaration
53 \*---------------------------------------------------------------------------*/
54 
55 template<typename T>
56 using OldTimeVoid = void;
57 
58 
59 /*---------------------------------------------------------------------------*\
60  Struct OldTimeBaseFieldType Declaration
61 \*---------------------------------------------------------------------------*/
62 
63 template<class FieldType, typename = void>
65 {
66  nil operator()(const OldTimeField<FieldType>& otf) const
67  {
68  return nil();
69  }
70 };
71 
72 template<class FieldType>
74 <
75  FieldType,
76  OldTimeVoid<typename FieldType::Base::OldTime>
77 >
78 {
79  typedef typename FieldType::Base::OldTime type;
80 
81  const type& operator()(const OldTimeField<FieldType>& otf) const
82  {
83  return static_cast<const type&>(otf.field());
84  }
85 };
86 
87 
88 /*---------------------------------------------------------------------------*\
89  Class OldTimeField Declaration
90 \*---------------------------------------------------------------------------*/
91 
92 template<class FieldType>
93 class OldTimeField
94 {
95 public:
96 
97  // Public Typedefs
98 
99  //- The old-time field type
101 
102 
103 private:
104 
105  // Private Data
106 
107  //- Current time index
108  mutable label timeIndex_;
109 
110  //- The old time field pointer or reference
111  mutable tmp<FieldType> tfield0_;
112 
113 
114  // Private Member Functions
115 
116  //- Get a reference to the field
117  const FieldType& field() const;
118 
119  //- Get a non-const reference to the field
120  FieldType& fieldRef();
121 
122  //- Store the old-time fields. Inner recursion.
123  void storeOldTimesInner() const;
124 
125  //- Set the oldest field pointer to nullObjectPtr. Inner recursion.
126  void nullOldestTimeInner();
127 
128  //- Set the field reference in the base class. Overload for when the
129  // base class is not an old-time field.
130  void setBase(const nil&) const;
131 
132  //- Set the field reference in the base class. Overload for when the
133  // base class is an old-time field.
134  template<class OldTimeBaseField>
135  void setBase(const OldTimeBaseField& otbf) const;
136 
137  //- Set the field reference in the base class
138  void setBase() const;
139 
140 
141 protected:
142 
143  // Protected Member Functions
144 
145  //- Read old-time field from file if it is present
146  bool readOldTimeIfPresent();
147 
148  //- Copy the old times from the given field
149  void copyOldTimes
150  (
151  const IOobject& io,
152  const OldTimeField<FieldType>& otf
153  );
154 
155  //- Copy the old times from the given field
156  void copyOldTimes
157  (
158  const word& newName,
159  const OldTimeField<FieldType>& otf
160  );
161 
162 
163 public:
164 
165  //- Declare friendship with other old-time fields
166  template<class OtherFieldType>
167  friend class OldTimeField;
168 
169  //- Declare friendship with the base old-time field casting struct
170  template<class OtherFieldType, typename>
171  friend struct OldTimeBaseFieldType;
172 
173 
174  // Constructors
175 
176  //- Construct from a time index
178 
179  //- Copy construct
181 
182  //- Move construct
184 
185 
186  //- Destructor
187  ~OldTimeField();
188 
189 
190  // Member Functions
191 
192  //- Return the time index of the field
193  inline label timeIndex() const;
194 
195  //- Return a non-const reference to the time index of the field
196  inline label& timeIndex();
197 
198  //- Return whether or not this is an old-time field
199  bool isOldTime() const;
200 
201  //- Store the old-time fields
202  void storeOldTimes() const;
203 
204  //- Clear old time fields
205  void clearOldTimes();
206 
207  //- Set the oldest field pointer to nullObjectPtr. This removes the
208  // field whilst maintaining a tag distinct from nullptr so that the
209  // field can be reinstated on the next storeOldTimes.
210  void nullOldestTime();
211 
212  //- Return the number of old time fields stored
213  label nOldTimes(const bool includeNull=true) const;
214 
215  //- Return the old time field
216  const FieldType& oldTime() const;
217 
218  //- Return a non-const reference to the old time field
219  FieldType& oldTimeRef();
220 
221  //- Return the n-th old time field
222  const FieldType& oldTime(const label n) const;
223 
224  //- Return a non-const reference to the n-th old time field
225  FieldType& oldTimeRef(const label n);
226 
227 
228  // Member Operators
229 
230  //- Disallow default bitwise assignment
231  void operator=(const OldTimeField<FieldType>&) = delete;
232 };
233 
234 
235 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
236 
237 } // End namespace Foam
238 
239 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
240 
241 #define USING_OLD_TIME_FIELD(FieldType) \
242  using OldTimeField<FieldType>::readOldTimeIfPresent; \
243  using OldTimeField<FieldType>::copyOldTimes; \
244  using OldTimeField<FieldType>::timeIndex; \
245  using OldTimeField<FieldType>::isOldTime; \
246  using OldTimeField<FieldType>::storeOldTimes; \
247  using OldTimeField<FieldType>::clearOldTimes; \
248  using OldTimeField<FieldType>::nullOldestTime; \
249  using OldTimeField<FieldType>::nOldTimes; \
250  using OldTimeField<FieldType>::oldTime; \
251  using OldTimeField<FieldType>::oldTimeRef;
252 
253 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
254 
255 #include "OldTimeFieldI.H"
256 
257 #ifdef NoRepository
258  #include "OldTimeField.C"
259 #endif
260 
261 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
262 
263 #endif
264 
265 // ************************************************************************* //
label n
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
Class to add into field types to provide old-time storage and retrieval.
Definition: OldTimeField.H:93
label timeIndex() const
Return the time index of the field.
Definition: OldTimeFieldI.H:31
bool readOldTimeIfPresent()
Read old-time field from file if it is present.
Definition: OldTimeField.C:125
friend class OldTimeField
Declare friendship with other old-time fields.
Definition: OldTimeField.H:166
const FieldType & oldTime() const
Return the old time field.
Definition: OldTimeField.C:315
label nOldTimes(const bool includeNull=true) const
Return the number of old time fields stored.
Definition: OldTimeField.C:292
void copyOldTimes(const IOobject &io, const OldTimeField< FieldType > &otf)
Copy the old times from the given field.
Definition: OldTimeField.C:161
FieldType & oldTimeRef()
Return a non-const reference to the old time field.
Definition: OldTimeField.C:353
bool isOldTime() const
Return whether or not this is an old-time field.
Definition: OldTimeField.C:239
OldTimeField< FieldType > OldTime
The old-time field type.
Definition: OldTimeField.H:99
void storeOldTimes() const
Store the old-time fields.
Definition: OldTimeField.C:248
~OldTimeField()
Destructor.
Definition: OldTimeField.C:226
void nullOldestTime()
Set the oldest field pointer to nullObjectPtr. This removes the.
Definition: OldTimeField.C:281
void operator=(const OldTimeField< FieldType > &)=delete
Disallow default bitwise assignment.
void clearOldTimes()
Clear old time fields.
Definition: OldTimeField.C:271
A zero-sized class without any storage. Used, for example, in HashSet.
Definition: nil.H:59
A class for handling words, derived from string.
Definition: word.H:62
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
void OldTimeVoid
Definition: OldTimeField.H:55
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
FieldType::Base::OldTime type
Definition: OldTimeField.H:78
nil operator()(const OldTimeField< FieldType > &otf) const
Definition: OldTimeField.H:65