ENH: extend readContents IO factory methods

- support wantedType for localIOdictionary::readContents()

- add readContents() for GlobalIOList, GlobalIOField
This commit is contained in:
Mark Olesen 2024-05-22 11:09:02 +02:00
parent bbde236be5
commit b81fe70830
12 changed files with 132 additions and 25 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2015 OpenFOAM Foundation
Copyright (C) 2016-2022 OpenCFD Ltd.
Copyright (C) 2016-2024 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -122,6 +122,26 @@ Foam::GlobalIOField<Type>::GlobalIOField
}
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
template<class Type>
Foam::Field<Type> Foam::GlobalIOField<Type>::readContents(const IOobject& io)
{
IOobject rio(io, IOobjectOption::NO_REGISTER);
if (rio.readOpt() == IOobjectOption::READ_MODIFIED)
{
rio.readOpt(IOobjectOption::MUST_READ);
}
// The object is global
rio.globalObject(true);
GlobalIOField<Type> reader(rio);
return Field<Type>(std::move(static_cast<Field<Type>&>(reader)));
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
@ -135,7 +155,8 @@ bool Foam::GlobalIOField<Type>::readData(Istream& is)
template<class Type>
bool Foam::GlobalIOField<Type>::writeData(Ostream& os) const
{
return (os << static_cast<const Field<Type>&>(*this)).good();
os << static_cast<const Field<Type>&>(*this);
return os.good();
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2015-2017 OpenFOAM Foundation
Copyright (C) 2016-2018 OpenCFD Ltd.
Copyright (C) 2016-2024 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -85,13 +85,19 @@ public:
GlobalIOField(const IOobject& io, const tmp<Field<Type>>& tfld);
// Factory Methods
//- Read and return contents. The IOobject is never registered
static Field<Type> readContents(const IOobject& io);
//- Destructor
virtual ~GlobalIOField() = default;
// Member Functions
//- Is object global
//- The object is global
virtual bool global() const
{
return true;

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2015 OpenFOAM Foundation
Copyright (C) 2016-2022 OpenCFD Ltd.
Copyright (C) 2016-2024 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -106,6 +106,26 @@ Foam::GlobalIOList<Type>::GlobalIOList
}
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
template<class Type>
Foam::List<Type> Foam::GlobalIOList<Type>::readContents(const IOobject& io)
{
IOobject rio(io, IOobjectOption::NO_REGISTER);
if (rio.readOpt() == IOobjectOption::READ_MODIFIED)
{
rio.readOpt(IOobjectOption::MUST_READ);
}
// The object is global
rio.globalObject(true);
GlobalIOList<Type> reader(rio);
return List<Type>(std::move(static_cast<List<Type>&>(reader)));
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
@ -119,7 +139,8 @@ bool Foam::GlobalIOList<Type>::readData(Istream& is)
template<class Type>
bool Foam::GlobalIOList<Type>::writeData(Ostream& os) const
{
return (os << *this).good();
os << static_cast<const List<Type>&>(*this);
return os.good();
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2015-2017 OpenFOAM Foundation
Copyright (C) 2018-2023 OpenCFD Ltd.
Copyright (C) 2018-2024 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -85,6 +85,12 @@ public:
GlobalIOList(const IOobject& io, List<Type>&& content);
// Factory Methods
//- Read and return contents. The IOobject is never registered
static List<Type> readContents(const IOobject& io);
//- Destructor
virtual ~GlobalIOList() = default;

View File

@ -105,6 +105,9 @@ Foam::Map<T> Foam::IOMap<T>::readContents(const IOobject& io)
rio.readOpt(IOobjectOption::MUST_READ);
}
// The object is global
rio.globalObject(true);
IOMap<T> reader(rio);
return Map<T>(std::move(static_cast<Map<T>&>(reader)));

View File

@ -101,9 +101,7 @@ public:
// Member Functions
bool writeData(Ostream& os) const;
//- Is object global
//- This object is global
virtual bool global() const
{
return true;
@ -116,6 +114,9 @@ public:
return globalFilePath(type());
}
//- The writeData method for regIOobject write operation
bool writeData(Ostream& os) const;
// Member Operators

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2021-2023 OpenCFD Ltd.
Copyright (C) 2021-2024 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -94,6 +94,16 @@ Foam::IOdictionary::IOdictionary
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
Foam::dictionary Foam::IOdictionary::readContents(const IOobject& io)
{
return readContents(io, typeName);
}
Foam::dictionary Foam::IOdictionary::readContents
(
const IOobject& io,
const word& wantedType
)
{
IOobject rio(io, IOobjectOption::NO_REGISTER);
if (rio.readOpt() == IOobjectOption::READ_MODIFIED)
@ -101,7 +111,14 @@ Foam::dictionary Foam::IOdictionary::readContents(const IOobject& io)
rio.readOpt(IOobjectOption::MUST_READ);
}
IOdictionary reader(rio);
// The object is global
rio.globalObject(true);
IOdictionary reader
(
rio,
(wantedType.empty() ? typeName : wantedType)
);
return dictionary(std::move(static_cast<dictionary&>(reader)));
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2021-2023 OpenCFD Ltd.
Copyright (C) 2021-2024 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -89,9 +89,18 @@ public:
// Factory Methods
//- Read and return contents. The IOobject will not be registered
//- Read and return contents, testing for "dictionary" type.
//- The IOobject will not be registered
static dictionary readContents(const IOobject& io);
//- Read and return contents, testing for expected type.
//- The IOobject will not be registered
static dictionary readContents
(
const IOobject& io,
const word& wantedType
);
//- Destructor
virtual ~IOdictionary() = default;
@ -99,14 +108,14 @@ public:
// Member Functions
//- Is object global
//- The object is global
virtual bool global() const
{
return true;
}
//- Return complete path + object name if the file exists
// either in the case/processor or case otherwise null
//- either in the case/processor or case otherwise null
virtual fileName filePath() const
{
return globalFilePath(type());

View File

@ -92,7 +92,7 @@ bool Foam::baseIOdictionary::readData(Istream& is)
{
is >> *this;
if (writeDictionaries && Pstream::master() && !is.bad())
if (writeDictionaries && UPstream::master() && !is.bad())
{
Sout<< nl
<< "--- baseIOdictionary " << name()

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2015-2017 OpenFOAM Foundation
Copyright (C) 2021-2023 OpenCFD Ltd.
Copyright (C) 2021-2024 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -91,6 +91,16 @@ Foam::localIOdictionary::localIOdictionary
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
Foam::dictionary Foam::localIOdictionary::readContents(const IOobject& io)
{
return readContents(io, typeName);
}
Foam::dictionary Foam::localIOdictionary::readContents
(
const IOobject& io,
const word& wantedType
)
{
IOobject rio(io, IOobjectOption::NO_REGISTER);
if (rio.readOpt() == IOobjectOption::READ_MODIFIED)
@ -98,7 +108,11 @@ Foam::dictionary Foam::localIOdictionary::readContents(const IOobject& io)
rio.readOpt(IOobjectOption::MUST_READ);
}
localIOdictionary reader(rio);
localIOdictionary reader
(
rio,
(wantedType.empty() ? typeName : wantedType)
);
return dictionary(std::move(static_cast<dictionary&>(reader)));
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2015-2017 OpenFOAM Foundation
Copyright (C) 2021-2023 OpenCFD Ltd.
Copyright (C) 2021-2024 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -86,9 +86,18 @@ public:
// Factory Methods
//- Read and return contents. The IOobject will not be registered
//- Read and return contents, testing for "dictionary" type.
//- The IOobject will not be registered
static dictionary readContents(const IOobject& io);
//- Read and return contents, testing for expected type.
//- The IOobject will not be registered
static dictionary readContents
(
const IOobject& io,
const word& wantedType
);
//- Destructor
virtual ~localIOdictionary() = default;
@ -96,14 +105,14 @@ public:
// Member Functions
//- Is object global
//- The object is not global
virtual bool global() const
{
return false;
}
//- Return complete path + object name if the file exists
// in the case otherwise null
//- in the case otherwise null
virtual fileName filePath() const
{
// Use default (local only) search strategy

View File

@ -101,14 +101,14 @@ public:
// Member Functions
//- Is object global
//- The object is global
virtual bool global() const
{
return true;
}
//- Return complete path + object name if the file exists
// either in the case/processor or case otherwise null
//- either in the case/processor or case otherwise null
virtual fileName filePath() const
{
return globalFilePath(type());