ENH: add global (serial) path accessor to IOobject (#3007)

- new methods added to IOobject to ease mixed (serial vs parallel)
  file locations. Some redirect to Time, others are defined for
  IOobject only.

    | "normal" (serial/parallel) | "global" (serial locations) |
    | ---------------------------|-----------------------------|
    | caseName()                 | globalCaseName()            |
    | path()                     | globalPath()          *new* |
    | path(...)                  | globalPath(...)       *new* |
    | objectPath()               | globalObjectPath()    *new* |
This commit is contained in:
Mark Olesen 2023-10-26 15:08:20 +02:00
parent 269be2f4ea
commit 41b6794e2d
3 changed files with 82 additions and 31 deletions

View File

@ -141,6 +141,31 @@ namespace Foam
//! \endcond
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
// A file is 'outside' of the case if it has been specified using an
// absolute path.
//
// Like 'fileName::isAbsolute' but with even fewer checks since the
// swapping of '\\' with '/' will have already occurred.
static inline bool file_isOutsideCase(const std::string& str)
{
return !str.empty() &&
(
// Starts with '/'
(str[0] == '/')
#ifdef _WIN32
// Filesytem root - eg, d:/path
|| (
(str.length() > 2 && str[1] == ':')
&& (str[2] == '/')
)
#endif
);
}
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
bool Foam::IOobject::fileNameComponents
@ -441,34 +466,28 @@ const Foam::Time& Foam::IOobject::time() const noexcept
}
const Foam::fileName& Foam::IOobject::rootPath() const
const Foam::fileName& Foam::IOobject::rootPath() const noexcept
{
return time().rootPath();
}
const Foam::fileName& Foam::IOobject::caseName() const
const Foam::fileName& Foam::IOobject::caseName() const noexcept
{
return time().caseName();
}
const Foam::fileName& Foam::IOobject::globalCaseName() const noexcept
{
return time().globalCaseName();
}
Foam::fileName Foam::IOobject::path() const
{
// A file is 'outside' of the case if it has been specified using an
// absolute path
const auto first = instance().find('/');
if
(
first == 0
#ifdef _WIN32
|| (first == 2 && instance()[1] == ':') // Eg, d:/path
#endif
)
if (file_isOutsideCase(instance()))
{
// Absolute path (starts with '/' or 'd:/')
return instance();
}
@ -476,6 +495,17 @@ Foam::fileName Foam::IOobject::path() const
}
Foam::fileName Foam::IOobject::globalPath() const
{
if (file_isOutsideCase(instance()))
{
return instance();
}
return rootPath()/globalCaseName()/instance()/db_.dbDir()/local();
}
Foam::fileName Foam::IOobject::path
(
const word& instance,
@ -487,22 +517,21 @@ Foam::fileName Foam::IOobject::path
}
Foam::fileName Foam::IOobject::globalPath
(
const word& instance,
const fileName& local
) const
{
// Note: can only be called with relative instance since is word type
return rootPath()/globalCaseName()/instance/db_.dbDir()/local;
}
Foam::fileName Foam::IOobject::objectRelPath() const
{
// A file is 'outside' of the case if it has been specified using an
// absolute path
const auto first = instance().find('/');
if
(
first == 0
#ifdef _WIN32
|| (first == 2 && instance()[1] == ':') // Eg, d:/path
#endif
)
if (file_isOutsideCase(instance()))
{
// Absolute path (starts with '/' or 'd:/')
return instance()/name();
}

View File

@ -541,10 +541,13 @@ public:
inline word member() const;
//- Return the Time::rootPath()
const fileName& rootPath() const;
const fileName& rootPath() const noexcept;
//- Return the Time::caseName()
const fileName& caseName() const;
const fileName& caseName() const noexcept;
//- Return the Time::globalCaseName()
const fileName& globalCaseName() const noexcept;
//- Read access to instance path component
inline const fileName& instance() const noexcept;
@ -555,9 +558,12 @@ public:
//- Read access to local path component
inline const fileName& local() const noexcept;
//- The complete path
//- The complete path for the object (with instance, local,...).
fileName path() const;
//- The complete global path for the object (with instance, local,...)
fileName globalPath() const;
//- The complete path with alternative instance and local
fileName path
(
@ -565,9 +571,19 @@ public:
const fileName& local = fileName::null
) const;
//- The complete global path with alternative instance and local
fileName globalPath
(
const word& instance,
const fileName& local = fileName::null
) const;
//- The complete path + object name
inline fileName objectPath() const;
//- The complete global path + object name
inline fileName globalObjectPath() const;
//- The object path relative to the root
fileName objectRelPath() const;

View File

@ -294,6 +294,12 @@ inline Foam::fileName Foam::IOobject::objectPath() const
}
inline Foam::fileName Foam::IOobject::globalObjectPath() const
{
return globalPath()/name();
}
// Error Handling
inline bool Foam::IOobject::good() const noexcept