IOobjectTemplates.C
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) 2015-2022 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 \*---------------------------------------------------------------------------*/
25 
26 #include "IOobject.H"
27 #include "fileOperation.H"
28 #include "Istream.H"
29 #include "IOstreams.H"
30 #include "Pstream.H"
31 
32 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33 
34 template<class Type>
35 bool Foam::IOobject::typeHeaderOk(const bool checkType)
36 {
37  bool ok = true;
38 
39  // Everyone check or just master
40  const bool masterOnly =
42  && (
45  );
46 
47  const fileOperation& fp = Foam::fileHandler();
48 
49  // Determine local status
50  if (!masterOnly || Pstream::master())
51  {
52  const fileName fName
53  (
54  filePath(Type::typeName, typeGlobalFile<Type>::global)
55  );
56 
57  ok = fp.readHeader(*this, fName, Type::typeName);
58  if (ok && checkType && headerClassName_ != Type::typeName)
59  {
61  << "unexpected class name " << headerClassName_
62  << " expected " << Type::typeName
63  << " when reading " << fName << endl;
64 
65  ok = false;
66  }
67  }
68 
69  // If masterOnly make sure all processors know about it
70  if (masterOnly)
71  {
72  Pstream::scatter(ok);
73  }
74 
75  return ok;
76 }
77 
78 
79 template<class Type>
81 {
82  if (readOpt() == IOobject::MUST_READ_IF_MODIFIED)
83  {
85  << Type::typeName << ' ' << name()
86  << " constructed with IOobject::MUST_READ_IF_MODIFIED"
87  " but " << Type::typeName
88  << " does not support automatic rereading."
89  << endl;
90  }
91 }
92 
93 
94 template<class Type>
96 {
97  return typeHeaderOk<Type>(true);
98 }
99 
100 
101 // ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
bool typeHeaderOk(const bool checkType)
Read header using typeGlobalFile to find file.
fileName filePath(const word &typeName, const bool global) const
Return complete path + object name if the file exists.
Definition: IOobject.C:407
@ MUST_READ_IF_MODIFIED
Definition: IOobject.H:119
void warnNoRereading() const
Helper: warn that type does not support re-reading.
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:226
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
A class for handling file names.
Definition: fileName.H:82
bool headerOk()
Read header (uses typeGlobalFile to find file) and check.
#define WarningInFunction
Report a warning using Foam::Warning.
const fileOperation & fileHandler()
Get current file handler.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
Trait for obtaining global write status.
Definition: IOobject.H:511
Trait for obtaining global status.
Definition: IOobject.H:504