STYLE: IOobject/regIOobject - noexcept methods, isolate local functions

- local writeHeaderEntry helper was not marked as file-scope static.

- use do/while to simplify handling of padding spaces

ENH: IOobject - copy construct, resetting name and local component

- when copying with a new local component, this is simpler than
  constructing from all of the components, which was previously the
  only possibility for setting a new local component.
This commit is contained in:
Mark Olesen
2022-05-03 10:33:31 +02:00
parent 0e01e530a8
commit cb6f908798
11 changed files with 143 additions and 117 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2021 OpenCFD Ltd. Copyright (C) 2016-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -312,8 +312,8 @@ Foam::IOobject::IOobject
const word& name, const word& name,
const fileName& instance, const fileName& instance,
const objectRegistry& registry, const objectRegistry& registry,
readOption ro, readOption rOpt,
writeOption wo, writeOption wOpt,
bool registerObject, bool registerObject,
bool globalObject bool globalObject
) )
@ -323,11 +323,11 @@ Foam::IOobject::IOobject
note_(), note_(),
instance_(instance), instance_(instance),
local_(), local_(),
rOpt_(ro), rOpt_(rOpt),
wOpt_(wo), wOpt_(wOpt),
registerObject_(registerObject), registerObject_(registerObject),
globalObject_(globalObject), globalObject_(globalObject),
objState_(GOOD), objState_(objectState::GOOD),
sizeofLabel_(static_cast<unsigned char>(sizeof(label))), sizeofLabel_(static_cast<unsigned char>(sizeof(label))),
sizeofScalar_(static_cast<unsigned char>(sizeof(scalar))), sizeofScalar_(static_cast<unsigned char>(sizeof(scalar))),
@ -349,8 +349,8 @@ Foam::IOobject::IOobject
const fileName& instance, const fileName& instance,
const fileName& local, const fileName& local,
const objectRegistry& registry, const objectRegistry& registry,
readOption ro, readOption rOpt,
writeOption wo, writeOption wOpt,
bool registerObject, bool registerObject,
bool globalObject bool globalObject
) )
@ -360,11 +360,11 @@ Foam::IOobject::IOobject
note_(), note_(),
instance_(instance), instance_(instance),
local_(local), local_(local),
rOpt_(ro), rOpt_(rOpt),
wOpt_(wo), wOpt_(wOpt),
registerObject_(registerObject), registerObject_(registerObject),
globalObject_(globalObject), globalObject_(globalObject),
objState_(GOOD), objState_(objectState::GOOD),
sizeofLabel_(static_cast<unsigned char>(sizeof(label))), sizeofLabel_(static_cast<unsigned char>(sizeof(label))),
sizeofScalar_(static_cast<unsigned char>(sizeof(scalar))), sizeofScalar_(static_cast<unsigned char>(sizeof(scalar))),
@ -384,8 +384,8 @@ Foam::IOobject::IOobject
( (
const fileName& path, const fileName& path,
const objectRegistry& registry, const objectRegistry& registry,
readOption ro, readOption rOpt,
writeOption wo, writeOption wOpt,
bool registerObject, bool registerObject,
bool globalObject bool globalObject
) )
@ -395,11 +395,11 @@ Foam::IOobject::IOobject
note_(), note_(),
instance_(), instance_(),
local_(), local_(),
rOpt_(ro), rOpt_(rOpt),
wOpt_(wo), wOpt_(wOpt),
registerObject_(registerObject), registerObject_(registerObject),
globalObject_(globalObject), globalObject_(globalObject),
objState_(GOOD), objState_(objectState::GOOD),
sizeofLabel_(static_cast<unsigned char>(sizeof(label))), sizeofLabel_(static_cast<unsigned char>(sizeof(label))),
sizeofScalar_(static_cast<unsigned char>(sizeof(scalar))), sizeofScalar_(static_cast<unsigned char>(sizeof(scalar))),
@ -471,14 +471,27 @@ Foam::IOobject::IOobject
Foam::IOobject::IOobject Foam::IOobject::IOobject
( (
const IOobject& io, const IOobject& io,
readOption ro, const word& name,
writeOption wo const fileName& local
)
:
IOobject(io, name)
{
local_ = local;
}
Foam::IOobject::IOobject
(
const IOobject& io,
readOption rOpt,
writeOption wOpt
) )
: :
IOobject(io) IOobject(io)
{ {
rOpt_ = ro; rOpt_ = rOpt;
wOpt_ = wo; wOpt_ = wOpt;
} }
@ -589,7 +602,7 @@ Foam::fileName Foam::IOobject::globalFilePath
void Foam::IOobject::setBad(const string& s) void Foam::IOobject::setBad(const string& s)
{ {
if (objState_ != GOOD) if (objState_ != objectState::GOOD)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Recurrent failure for object " << s << "Recurrent failure for object " << s
@ -602,7 +615,7 @@ void Foam::IOobject::setBad(const string& s)
<< "Broken object " << s << info() << endl; << "Broken object " << s << info() << endl;
} }
objState_ = BAD; objState_ = objectState::BAD;
} }

View File

@ -270,7 +270,7 @@ public:
return bannerEnabled_; return bannerEnabled_;
} }
//- Enable/disable an output file banner //- Enable/disable output file banner
// \return the previous value // \return the previous value
static bool bannerEnabled(bool on) noexcept static bool bannerEnabled(bool on) noexcept
{ {
@ -360,8 +360,8 @@ public:
const word& name, const word& name,
const fileName& instance, const fileName& instance,
const objectRegistry& registry, const objectRegistry& registry,
readOption r=NO_READ, readOption rOpt = NO_READ,
writeOption w=NO_WRITE, writeOption wOpt = NO_WRITE,
bool registerObject = true, bool registerObject = true,
bool globalObject = false bool globalObject = false
); );
@ -373,8 +373,8 @@ public:
const fileName& instance, const fileName& instance,
const fileName& local, const fileName& local,
const objectRegistry& registry, const objectRegistry& registry,
readOption r=NO_READ, readOption rOpt = NO_READ,
writeOption w=NO_WRITE, writeOption wOpt = NO_WRITE,
bool registerObject = true, bool registerObject = true,
bool globalObject = false bool globalObject = false
); );
@ -389,33 +389,24 @@ public:
( (
const fileName& path, const fileName& path,
const objectRegistry& registry, const objectRegistry& registry,
readOption r=NO_READ, readOption rOpt = NO_READ,
writeOption w=NO_WRITE, writeOption wOpt = NO_WRITE,
bool registerObject = true, bool registerObject = true,
bool globalObject = false bool globalObject = false
); );
//- Copy construct, resetting registry //- Copy construct, resetting registry
IOobject IOobject(const IOobject& io, const objectRegistry& registry);
(
const IOobject& io,
const objectRegistry& registry
);
//- Copy construct, resetting name //- Copy construct, resetting name
IOobject IOobject(const IOobject& io, const word& name);
(
const IOobject& io, //- Copy construct, resetting name and local component
const word& name IOobject(const IOobject& io, const word& name, const fileName& local);
);
//- Copy construct, resetting read/write options
IOobject(const IOobject& io, readOption rOpt, writeOption wOpt);
//- Copy construct, resetting io options
IOobject
(
const IOobject& io,
readOption,
writeOption
);
//- Clone //- Clone
autoPtr<IOobject> clone() const autoPtr<IOobject> clone() const
@ -440,7 +431,7 @@ public:
//- Return Time associated with the objectRegistry //- Return Time associated with the objectRegistry
const Time& time() const; const Time& time() const;
//- Return name //- Return the object name
inline const word& name() const noexcept; inline const word& name() const noexcept;
//- Return name of the class name read from header //- Return name of the class name read from header
@ -452,10 +443,10 @@ public:
//- Return the optional note //- Return the optional note
inline const string& note() const noexcept; inline const string& note() const noexcept;
//- Return non-constant access to the optional note //- Modifiable access to the optional note
inline string& note() noexcept; inline string& note() noexcept;
//- Rename //- Rename the object
virtual void rename(const word& newName) virtual void rename(const word& newName)
{ {
name_ = newName; name_ = newName;
@ -522,14 +513,19 @@ public:
//- Return member (name without the extension) //- Return member (name without the extension)
inline word member() const; inline word member() const;
//- Return the Time::rootPath()
const fileName& rootPath() const; const fileName& rootPath() const;
//- Return the Time::caseName()
const fileName& caseName() const; const fileName& caseName() const;
//- Read access to instance path component
inline const fileName& instance() const noexcept; inline const fileName& instance() const noexcept;
//- Write access to instance path component
inline fileName& instance() noexcept; inline fileName& instance() noexcept;
//- Read access to local path component
inline const fileName& local() const noexcept; inline const fileName& local() const noexcept;
//- The complete path //- The complete path
@ -633,8 +629,10 @@ public:
// Error Handling // Error Handling
//- Did last readHeader() succeed?
inline bool good() const noexcept; inline bool good() const noexcept;
//- Did last readHeader() fail?
inline bool bad() const noexcept; inline bool bad() const noexcept;
@ -650,6 +648,7 @@ public:
// Member Operators // Member Operators
//- Copy assignment, copies all values (except the registry)
void operator=(const IOobject& io); void operator=(const IOobject& io);

View File

@ -227,13 +227,13 @@ inline Foam::fileName Foam::IOobject::objectPath() const
inline bool Foam::IOobject::good() const noexcept inline bool Foam::IOobject::good() const noexcept
{ {
return objState_ == GOOD; return objState_ == objectState::GOOD;
} }
inline bool Foam::IOobject::bad() const noexcept inline bool Foam::IOobject::bad() const noexcept
{ {
return objState_ == BAD; return objState_ == objectState::BAD;
} }

View File

@ -128,16 +128,17 @@ bool Foam::IOobject::readHeader(dictionary& headerDict, Istream& is)
} }
// Check stream is still OK // Check stream is still OK
objState_ = (is.good() ? GOOD : BAD); objState_ = (is.good() ? objectState::GOOD : objectState::BAD);
if (IOobject::debug) if (IOobject::debug)
{ {
Info<< " .... read - state: " Info<< " .... read - state: "
<< (objState_ == GOOD ? "good" : "bad") << endl; << (objState_ == objectState::GOOD ? "good" : "bad")
<< endl;
} }
if (objState_ == BAD) if (objState_ == objectState::BAD)
{ {
if if
( (

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2021 OpenCFD Ltd. Copyright (C) 2016-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -36,24 +36,27 @@ License
namespace Foam namespace Foam
{ {
inline void writeSpaces(Ostream& os, label nSpaces) // Like Ostream::writeEntry, but with fewer spaces
template<class T>
static inline void writeHeaderEntry
(
Ostream& os,
const word& key,
const T& value
)
{ {
if (nSpaces < 1) os.indent();
{ os.write(key);
nSpaces = 1;
} label padding = (12 - label(key.size()));
while (nSpaces--)
// Write padding spaces (always at least one)
do
{ {
os.write(char(token::SPACE)); os.write(char(token::SPACE));
} }
} while (--padding > 0);
// Similar to writeEntry, but with fewer spaces
template<class T>
inline void writeHeaderEntry(Ostream& os, const word& key, const T& value)
{
os << indent << key;
writeSpaces(os, 12 - label(key.size()));
os << value << char(token::END_STATEMENT) << nl; os << value << char(token::END_STATEMENT) << nl;
} }

View File

@ -107,8 +107,8 @@ Foam::IOobjectList::IOobjectList
const objectRegistry& db, const objectRegistry& db,
const fileName& instance, const fileName& instance,
const fileName& local, const fileName& local,
IOobject::readOption r, IOobject::readOption rOpt,
IOobject::writeOption w, IOobject::writeOption wOpt,
bool registerObject bool registerObject
) )
: :
@ -131,8 +131,8 @@ Foam::IOobjectList::IOobjectList
newInstance, newInstance,
local, local,
db, db,
r, rOpt,
w, wOpt,
registerObject registerObject
); );

View File

@ -164,8 +164,8 @@ public:
const objectRegistry& db, const objectRegistry& db,
const fileName& instance, const fileName& instance,
const fileName& local = "", const fileName& local = "",
IOobject::readOption r = IOobject::MUST_READ, IOobject::readOption rOpt = IOobject::MUST_READ,
IOobject::writeOption w = IOobject::NO_WRITE, IOobject::writeOption wOpt = IOobject::NO_WRITE,
bool registerObject = true bool registerObject = true
); );

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2021 OpenCFD Ltd. Copyright (C) 2021-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -36,24 +36,27 @@ License
namespace Foam namespace Foam
{ {
inline void writeSpaces(Ostream& os, label nSpaces) // Like Ostream::writeEntry, but with fewer spaces
template<class T>
static inline void writeHeaderEntry
(
Ostream& os,
const word& key,
const T& value
)
{ {
if (nSpaces < 1) os.indent();
{ os.write(key);
nSpaces = 1;
} label padding = (12 - label(key.size()));
while (nSpaces--)
// Write padding spaces (always at least one)
do
{ {
os.write(char(token::SPACE)); os.write(char(token::SPACE));
} }
} while (--padding > 0);
// Similar to writeEntry, but with fewer spaces
template<class T>
inline void writeHeaderEntry(Ostream& os, const word& key, const T& value)
{
os << indent << key;
writeSpaces(os, 12 - label(key.size()));
os << value << char(token::END_STATEMENT) << nl; os << value << char(token::END_STATEMENT) << nl;
} }

View File

@ -65,24 +65,20 @@ Foam::Ostream& Foam::Ostream::writeKeyword(const keyType& kw)
return *this; return *this;
} }
label nSpaces = entryIndentation_ - label(kw.size()); label padding = (entryIndentation_ - label(kw.size()));
// Account for quotes surrounding pattern // Account for quotes surrounding pattern
if (kw.isPattern()) if (kw.isPattern())
{ {
nSpaces -= 2; padding -= 2;
} }
// Could also increment by indentSize_ ... // Write padding spaces (always at least one)
if (nSpaces < 1) do
{
nSpaces = 1;
}
while (nSpaces--)
{ {
write(char(token::SPACE)); write(char(token::SPACE));
} }
while (--padding > 0);
return *this; return *this;
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018-2021 OpenCFD Ltd. Copyright (C) 2018-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -38,8 +38,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#ifndef regIOobject_H #ifndef Foam_regIOobject_H
#define regIOobject_H #define Foam_regIOobject_H
#include "IOobject.H" #include "IOobject.H"
#include "refPtr.H" #include "refPtr.H"
@ -174,8 +174,13 @@ public:
//- Add file watch on object (if registered and READ_IF_MODIFIED) //- Add file watch on object (if registered and READ_IF_MODIFIED)
virtual void addWatch(); virtual void addWatch();
//- Query the registered state (ie, has been checked in).
//- This is not necessarily the same as registerObject(),
//- which is just a stated preference.
inline bool registered() const noexcept;
//- Is this object owned by the registry? //- Is this object owned by the registry?
inline bool ownedByRegistry() const; inline bool ownedByRegistry() const noexcept;
//- Register object with its registry //- Register object with its registry
//- and transfer ownership to the registry. //- and transfer ownership to the registry.
@ -223,18 +228,18 @@ public:
template<class Type> template<class Type>
inline static Type& store(tmp<Type>&& ptr); inline static Type& store(tmp<Type>&& ptr);
//- Release ownership of this object from its registry //- Set object as \b not ownedByRegistry
// \param unregister optionally set as non-registered // \param unregister optionally set as non-registered too
inline void release(const bool unregister = false); inline void release(const bool unregister = false) noexcept;
// Dependency Checking // Dependency Checking
//- Event number at last update. //- Event number at last update.
inline label eventNo() const; inline label eventNo() const noexcept;
//- Event number at last update. //- Event number at last update.
inline label& eventNo(); inline label& eventNo() noexcept;
//- Return true if up-to-date with respect to given object //- Return true if up-to-date with respect to given object
bool upToDate(const regIOobject&) const; bool upToDate(const regIOobject&) const;
@ -318,11 +323,11 @@ public:
// \return index of watch // \return index of watch
virtual label addWatch(const fileName&); virtual label addWatch(const fileName&);
//- Return file-monitoring handles //- Read access to file-monitoring handles
inline const labelList& watchIndices() const; inline const labelList& watchIndices() const noexcept;
//- Return file-monitoring handles //- Write access to file-monitoring handles
inline labelList& watchIndices(); inline labelList& watchIndices() noexcept;
//- Return true if the object's file (or files for objectRegistry) //- Return true if the object's file (or files for objectRegistry)
//- have been modified. (modified state is cached by Time) //- have been modified. (modified state is cached by Time)

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2018-2021 OpenCFD Ltd. Copyright (C) 2018-2022 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -28,7 +28,13 @@ License
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline bool Foam::regIOobject::ownedByRegistry() const inline bool Foam::regIOobject::registered() const noexcept
{
return registered_;
}
inline bool Foam::regIOobject::ownedByRegistry() const noexcept
{ {
return ownedByRegistry_; return ownedByRegistry_;
} }
@ -172,7 +178,7 @@ inline Type& Foam::regIOobject::store(tmp<Type>&& ptr)
} }
inline void Foam::regIOobject::release(const bool unregister) inline void Foam::regIOobject::release(const bool unregister) noexcept
{ {
ownedByRegistry_ = false; ownedByRegistry_ = false;
if (unregister) if (unregister)
@ -182,24 +188,24 @@ inline void Foam::regIOobject::release(const bool unregister)
} }
inline Foam::label Foam::regIOobject::eventNo() const inline Foam::label Foam::regIOobject::eventNo() const noexcept
{ {
return eventNo_; return eventNo_;
} }
inline Foam::label& Foam::regIOobject::eventNo() inline Foam::label& Foam::regIOobject::eventNo() noexcept
{ {
return eventNo_; return eventNo_;
} }
inline const Foam::labelList& Foam::regIOobject::watchIndices() const inline const Foam::labelList& Foam::regIOobject::watchIndices() const noexcept
{ {
return watchIndices_; return watchIndices_;
} }
inline Foam::labelList& Foam::regIOobject::watchIndices() inline Foam::labelList& Foam::regIOobject::watchIndices() noexcept
{ {
return watchIndices_; return watchIndices_;
} }