IOField.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) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2018-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::IOField
29 
30 Description
31  A primitive field of type <T> with automated input and output.
32 
33 SourceFiles
34  IOField.C
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Foam_IOField_H
39 #define Foam_IOField_H
40 
41 #include "Field.H"
42 #include "regIOobject.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 /*---------------------------------------------------------------------------*\
50  Class IOField Declaration
51 \*---------------------------------------------------------------------------*/
52 
53 template<class Type>
54 class IOField
55 :
56  public regIOobject,
57  public Field<Type>
58 {
59  // Private Member Functions
60 
61  //- Read with optional 'on-proc' value
62  void readFromStream(const bool readOnProc = true);
63 
64  //- Read if IOobject flags set. Return true if read.
65  bool readContents();
66 
67 public:
68 
69  //- The underlying content type
70  typedef Field<Type> content_type;
71 
72  //- Runtime type information
73  TypeName("Field");
74 
75 
76  // Constructors
77 
78  //- Default copy construct
79  IOField(const IOField&) = default;
80 
81  //- Construct from IOobject
82  explicit IOField(const IOobject& io);
83 
84  //- Construct from IOobject, with local processor conditional reading
85  IOField(const IOobject& io, const bool readOnProc);
86 
87  //- Construct from IOobject and zero size (if not read)
88  IOField(const IOobject& io, Foam::zero);
89 
90  //- Construct from IOobject and field size (if not read)
91  IOField(const IOobject& io, const label len);
92 
93  //- Construct from IOobject and copy of List/Field content
94  IOField(const IOobject& io, const UList<Type>& content);
95 
96  //- Construct by transferring the Field content
97  IOField(const IOobject& io, Field<Type>&& content);
98 
99  //- Construct by copying/moving tmp content
100  IOField(const IOobject& io, const tmp<Field<Type>>& tfld);
101 
102 
103  // Factory Methods
104 
105  //- Read and return contents. The IOobject will not be registered
106  static Field<Type> readContents(const IOobject& io);
107 
108 
109  //- Destructor
110  virtual ~IOField() = default;
111 
112 
113  // Member Functions
114 
115  //- The writeData method for regIOobject write operation
116  virtual bool writeData(Ostream& os) const;
117 
118 
119  // Member Operators
120 
121  //- Copy assignment of entries
122  void operator=(const IOField<Type>& rhs);
123 
124  //- Copy or move assignment of entries
125  using Field<Type>::operator=;
126 };
127 
128 
129 /*---------------------------------------------------------------------------*\
130  Class IOFieldRef Declaration
131 \*---------------------------------------------------------------------------*/
132 
133 //- A IOField wrapper for writing external data.
134 template<class Type>
135 class IOFieldRef
136 :
137  public regIOobject
138 {
139  // Private Data
140 
141  //- Reference to the external content
142  refPtr<Field<Type>> contentRef_;
143 
144 
145 public:
146 
147  //- The underlying content type
148  typedef Field<Type> content_type;
149 
150 
151  //- Type is identical to IOField
152  virtual const word& type() const
153  {
155  }
156 
157 
158  // Generated Methods
159 
160  //- No default construct
161  IOFieldRef() = delete;
162 
163  //- No copy construct
164  IOFieldRef(const IOFieldRef&) = delete;
165 
166  //- No copy assignment
167  void operator=(const IOFieldRef&) = delete;
168 
169 
170  // Constructors
171 
172  //- Construct from IOobject and const data reference
173  IOFieldRef(const IOobject& io, const Field<Type>& content);
174 
175 
176  //- Destructor
177  virtual ~IOFieldRef() = default;
178 
179 
180  // Member Functions
181 
182  //- Allow cast to const content
183  // Fatal if content is not set
184  operator const Field<Type>&() const
185  {
186  return contentRef_.cref();
187  }
188 
189  //- The writeData method for regIOobject write operation
190  // Fatal if content is not set
191  virtual bool writeData(Ostream& os) const;
192 };
193 
194 
195 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
196 
197 } // End namespace Foam
198 
199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
200 
201 #ifdef NoRepository
202  #include "IOField.C"
203 #endif
204 
205 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
206 
207 #endif
208 
209 // ************************************************************************* //
IOFieldRef()=delete
No default construct.
IOField(const IOField &)=default
Default copy construct.
A IOField wrapper for writing external data.
Definition: IOField.H:166
const T & cref() const
Return const reference to the object or to the contents of a (non-null) managed pointer.
Definition: refPtrI.H:216
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
virtual bool writeData(Ostream &os) const
The writeData method for regIOobject write operation.
Definition: IOField.C:195
virtual ~IOFieldRef()=default
Destructor.
Generic templated field type.
Definition: Field.H:62
A class for handling words, derived from Foam::string.
Definition: word.H:63
TypeName("Field")
Runtime type information.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
virtual ~IOField()=default
Destructor.
void operator=(const IOField< Type > &rhs)
Copy assignment of entries.
Definition: IOField.C:213
Field< Type > content_type
The underlying content type.
Definition: IOField.H:71
OBJstream os(runTime.globalPath()/outputName)
Field< Type > content_type
The underlying content type.
Definition: IOField.H:183
virtual const word & type() const
Type is identical to IOField.
Definition: IOField.H:189
void operator=(const IOFieldRef &)=delete
No copy assignment.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:66
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
virtual bool writeData(Ostream &os) const
The writeData method for regIOobject write operation.
Definition: IOField.C:203
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
A primitive field of type <T> with automated input and output.
Namespace for OpenFOAM.