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-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 \*---------------------------------------------------------------------------*/
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  // Mark as not yet read. cf, IOobject::readHeader()
46  headerClassName_.clear();
47 
48  // Everyone check or just master
49  const bool masterOnly
50  (
51  typeGlobal<Type>()
52  && (
55  )
56  );
57 
58  const fileOperation& fp = Foam::fileHandler();
59 
60  // Determine local status
61  bool ok = false;
62  fileName fName;
63 
64  if (!masterOnly || UPstream::master())
65  {
66  fName = typeFilePath<Type>(*this, search);
67  ok = fp.readHeader(*this, fName, Type::typeName);
68  }
69 
70  if (ok && checkType && headerClassName_ != Type::typeName)
71  {
72  ok = false;
73  if (verbose)
74  {
76  << "Unexpected class name \"" << headerClassName_
77  << "\" expected \"" << Type::typeName
78  << "\" when reading " << fName << endl;
79  }
80  }
81 
82  // If masterOnly make sure all processors know about it
83  if (masterOnly)
84  {
86  }
87 
88  return ok;
89 }
90 
91 
92 template<class Type>
94 {
95  if (readOpt() == IOobjectOption::READ_MODIFIED)
96  {
98  << Type::typeName << ' ' << name()
99  << " constructed with READ_MODIFIED but "
100  << Type::typeName << " does not support automatic rereading."
101  << endl;
102  }
103 }
104 
105 
106 // ************************************************************************* //
A class for handling file names.
Definition: fileName.H:71
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler()
An encapsulation of filesystem-related operations.
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
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:326
#define WarningInFunction
Report a warning using Foam::Warning.
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1037
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.