CompactIOField.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) 2020-2025 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::CompactIOField
29 
30 Description
31  A Field of objects of type <T> with automated input and output using
32  a compact storage. Behaves like IOField except when binary output in
33  case it writes a CompactListList.
34 
35  Useful for fields of small subfields e.g. in lagrangian
36 
37 SourceFiles
38  CompactIOField.C
39 
40 \*---------------------------------------------------------------------------*/
41 
42 #ifndef Foam_CompactIOField_H
43 #define Foam_CompactIOField_H
44 
45 #include "IOField.H"
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 // Forward Declarations
53 template<class T> class CompactIOField;
54 
55 template<class T> Istream& operator>>(Istream&, CompactIOField<T>&);
56 template<class T> Ostream& operator<<(Ostream&, const CompactIOField<T>&);
57 
58 /*---------------------------------------------------------------------------*\
59  Class CompactIOField Declaration
60 \*---------------------------------------------------------------------------*/
61 
62 template<class T>
63 class CompactIOField
64 :
65  public regIOobject,
66  public Field<T>
67 {
68  // Private Member Functions
69 
70  //- Read if IOobject flags set and 'on-proc' is true.
71  //- Reads according to the header type.
72  // Return true if read (only accurate when readOnProc == true).
73  bool readIOcontents(bool readOnProc = true);
74 
75  //- Read the content size.
76  // Return the size if read, -1 otherwise
77  label readIOsize(bool readOnProc = true);
78 
79  //- Has too many elements in it?
80  bool overflows() const;
81 
82 public:
83 
84  //- The underlying content type
85  typedef Field<T> content_type;
86 
87  //- Runtime type information
88  TypeName("FieldField");
89 
90 
91  // Constructors
92 
93  //- Default copy construct
94  CompactIOField(const CompactIOField&) = default;
95 
96  //- Construct from IOobject. Will be zero size if not read.
97  explicit CompactIOField(const IOobject& io);
98 
99  //- Construct from IOobject, with local processor conditional reading
100  CompactIOField(const IOobject& io, const bool readOnProc);
101 
102  //- Construct from IOobject and zero size (if not read)
104 
105  //- Construct from IOobject and size (if not read)
106  CompactIOField(const IOobject& io, const label len);
107 
108  //- Construct from IOobject and a List/Field content
109  CompactIOField(const IOobject& io, const UList<T>& content);
110 
111  //- Construct by transferring the Field contents
112  CompactIOField(const IOobject& io, Field<T>&& content);
113 
114 
115  // Factory Methods
116 
117  //- Read and return content size, -1 if not read.
118  // The IOobject is never registered
119  static label readContentsSize(const IOobject& io);
120 
121  //- Read and return contents. The IOobject is never registered
122  static Field<T> readContents(const IOobject& io);
123 
124 
125  //- Destructor
126  virtual ~CompactIOField() = default;
127 
128 
129  // Member Functions
130 
131  //- Read as offsets/packed-values and transcribe into *this
133 
134  //- Write as offsets/packed-values
135  Ostream& writeCompact(Ostream& os) const;
136 
137  //- Write using stream options. Checks for overflow in binary
138  virtual bool writeObject
139  (
140  IOstreamOption streamOpt,
141  const bool writeOnProc
142  ) const;
143 
144  //- Write as plain or compact content (depends on stream format)
145  virtual bool writeData(Ostream& os) const;
146 
147 
148  // Member Operators
149 
150  //- Copy or move assignment of entries
151  using Field<T>::operator=;
152 
153  //- Copy assignment of entries
154  void operator=(const CompactIOField<T>& rhs)
155  {
157  }
158 
159  //- Move assignment of entries
161  {
162  Field<T>::operator=(std::move(static_cast<Field<T>&>(rhs)));
163  }
164 };
165 
166 
167 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
168 
169 } // End namespace Foam
170 
171 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
172 
173 #ifdef NoRepository
174  #include "CompactIOField.C"
175 #endif
176 
177 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
178 
179 #endif
180 
181 // ************************************************************************* //
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write using stream options. Checks for overflow in binary.
CompactIOField(const CompactIOField &)=default
Default copy construct.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
virtual bool writeData(Ostream &os) const
Write as plain or compact content (depends on stream format)
Istream & readCompact(Istream &is)
Read as offsets/packed-values and transcribe into *this.
A simple container for options an IOstream can normally have.
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition: Field.H:69
Istream & operator>>(Istream &, directionInfo &)
const auto & io
static Field< T > readContents(const IOobject &io)
Read and return contents. The IOobject is never registered.
Ostream & writeCompact(Ostream &os) const
Write as offsets/packed-values.
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
void operator=(const Field< Type > &)
Copy assignment.
Definition: Field.C:781
void operator=(const CompactIOField< T > &rhs)
Copy assignment of entries.
A Field of objects of type <T> with automated input and output using a compact storage. Behaves like IOField except when binary output in case it writes a CompactListList.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:68
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:57
static label readContentsSize(const IOobject &io)
Read and return content size, -1 if not read.
Field< T > content_type
The underlying content type.
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:188
virtual ~CompactIOField()=default
Destructor.
TypeName("FieldField")
Runtime type information.
Namespace for OpenFOAM.