IOobjectTemplates.C
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) 2015-2017 OpenFOAM Foundation
9  Copyright (C) 2016-2022 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 \*---------------------------------------------------------------------------*/
28 
29 #include "IOobject.H"
30 #include "fileOperation.H"
31 #include "Istream.H"
32 #include "IOstreams.H"
33 #include "Pstream.H"
34 
35 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
36 
37 template<class Type>
39 (
40  const bool checkType,
41  const bool search,
42  const bool verbose
43 )
44 {
45  // Everyone check or just master
46  const bool masterOnly
47  (
48  typeGlobal<Type>()
49  && (
52  )
53  );
54 
55  const fileOperation& fp = Foam::fileHandler();
56 
57  // Determine local status
58  bool ok = false;
59  fileName fName;
60 
61  if (!masterOnly || Pstream::master())
62  {
63  fName = typeFilePath<Type>(*this, search);
64  ok = fp.readHeader(*this, fName, Type::typeName);
65  }
66 
67  if (ok && checkType && headerClassName_ != Type::typeName)
68  {
69  ok = false;
70  if (verbose)
71  {
73  << "Unexpected class name \"" << headerClassName_
74  << "\" expected \"" << Type::typeName
75  << "\" when reading " << fName << endl;
76  }
77  }
78 
79  // If masterOnly make sure all processors know about it
80  if (masterOnly)
81  {
83  }
84 
85  return ok;
86 }
87 
88 
89 template<class Type>
91 {
92  if (readOpt() == IOobject::MUST_READ_IF_MODIFIED)
93  {
95  << Type::typeName << ' ' << name()
96  << " constructed with IOobject::MUST_READ_IF_MODIFIED but "
97  << Type::typeName << " does not support automatic rereading."
98  << endl;
99  }
100 }
101 
102 
103 // ************************************************************************* //
A class for handling file names.
Definition: fileName.H:71
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
autoPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler.
An encapsulation of filesystem-related operations.
Definition: fileOperation.H:63
static void broadcast(Type &value, const label comm=UPstream::worldComm)
Broadcast content (contiguous or non-contiguous) to all processes in communicator.
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
Reading required, file watched for runTime modification.
bool typeHeaderOk(const bool checkType=true, const bool search=true, const bool verbose=true)
Read header (uses typeFilePath to find file) and check its info.
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:313
#define WarningInFunction
Report a warning using Foam::Warning.
static bool master(const label communicator=worldComm)
Am I the master rank.
Definition: UPstream.H:672
fileName search(const word &file, const fileName &directory)
Recursively search the given directory for the file.
Definition: fileName.C:640
void warnNoRereading() const
Helper: warn that type does not support re-reading.
virtual bool readHeader(IOobject &, const fileName &, const word &typeName) const =0
Read object header from supplied file.