ENH: add writeContents() static methods for IOList etc.

- encapsulates IOListRef etc into a simpler syntax

ENH: use UList instead of refPtr for IOListRef (avoids List cast etc)
This commit is contained in:
Mark Olesen 2025-04-09 14:23:49 +02:00
parent 6c20df2808
commit a7e8a43f4a
18 changed files with 110 additions and 82 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2017 OpenFOAM Foundation Copyright (C) 2017 OpenFOAM Foundation
Copyright (C) 2019-2022 OpenCFD Ltd. Copyright (C) 2019-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -269,7 +269,7 @@ int main(int argc, char *argv[])
ioOutput.rename(args.executable() + "-labels"); ioOutput.rename(args.executable() + "-labels");
Info<< "write " << ioOutput.objectRelPath() << endl; Info<< "write " << ioOutput.objectRelPath() << endl;
{ {
IOListRef<label>(ioOutput, ints).write(); IOList<label>::writeContents(ioOutput, ints);
} }
ioOutput.rename(args.executable() + "-points"); ioOutput.rename(args.executable() + "-points");

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2023 OpenCFD Ltd. Copyright (C) 2017-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -700,19 +700,11 @@ Foam::label Foam::checkTopology
<< mesh.time().timeName()/"cellToRegion" << mesh.time().timeName()/"cellToRegion"
<< endl; << endl;
IOListRef<label> IOList<label>::writeContents
( (
IOobject IOobject("cellToRegion", mesh.time().timeName(), mesh),
(
"cellToRegion",
mesh.time().timeName(),
mesh,
IOobject::NO_READ,
IOobject::NO_WRITE,
IOobject::NO_REGISTER
),
rs rs
).write(); );
// Points in multiple regions // Points in multiple regions

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2024 OpenCFD Ltd. Copyright (C) 2016-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -1913,13 +1913,13 @@ int main(int argc, char *argv[])
); );
meshMapIO.resetHeader("cellMap"); meshMapIO.resetHeader("cellMap");
IOListRef<label>(meshMapIO, map().cellMap()).write(); IOList<label>::writeContents(meshMapIO, map().cellMap());
meshMapIO.resetHeader("faceMap"); meshMapIO.resetHeader("faceMap");
IOListRef<label>(meshMapIO, map().faceMap()).write(); IOList<label>::writeContents(meshMapIO, map().faceMap());
meshMapIO.resetHeader("pointMap"); meshMapIO.resetHeader("pointMap");
IOListRef<label>(meshMapIO, map().pointMap()).write(); IOList<label>::writeContents(meshMapIO, map().pointMap());
} }
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019-2024 OpenCFD Ltd. Copyright (C) 2019-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -846,12 +846,9 @@ bool Foam::domainDecomposition::writeDecomposition(const bool decomposeSets)
"boundaryProcAddressing", "boundaryProcAddressing",
procMesh.facesInstance(), procMesh.facesInstance(),
polyMesh::meshSubDir/pointMesh::meshSubDir, polyMesh::meshSubDir/pointMesh::meshSubDir,
procPointMesh.thisDb(), procPointMesh.thisDb()
IOobject::NO_READ,
IOobject::NO_WRITE,
IOobject::NO_REGISTER
); );
IOListRef<label>(ioAddr, boundaryProcAddressing).write(); IOList<label>::writeContents(ioAddr, boundaryProcAddressing);
} }
} }
@ -1013,15 +1010,15 @@ bool Foam::domainDecomposition::writeDecomposition(const bool decomposeSets)
// pointProcAddressing // pointProcAddressing
ioAddr.rename("pointProcAddressing"); ioAddr.rename("pointProcAddressing");
IOListRef<label>(ioAddr, procPointAddressing_[proci]).write(); IOList<label>::writeContents(ioAddr, procPointAddressing_[proci]);
// faceProcAddressing // faceProcAddressing
ioAddr.rename("faceProcAddressing"); ioAddr.rename("faceProcAddressing");
IOListRef<label>(ioAddr, procFaceAddressing_[proci]).write(); IOList<label>::writeContents(ioAddr, procFaceAddressing_[proci]);
// cellProcAddressing // cellProcAddressing
ioAddr.rename("cellProcAddressing"); ioAddr.rename("cellProcAddressing");
IOListRef<label>(ioAddr, procCellAddressing_[proci]).write(); IOList<label>::writeContents(ioAddr, procCellAddressing_[proci]);
// Write patch map for backwards compatibility. // Write patch map for backwards compatibility.
// (= identity map for original patches, -1 for processor patches) // (= identity map for original patches, -1 for processor patches)
@ -1031,7 +1028,7 @@ bool Foam::domainDecomposition::writeDecomposition(const bool decomposeSets)
// boundaryProcAddressing // boundaryProcAddressing
ioAddr.rename("boundaryProcAddressing"); ioAddr.rename("boundaryProcAddressing");
IOListRef<label>(ioAddr, procBoundaryAddr).write(); IOList<label>::writeContents(ioAddr, procBoundaryAddr);
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2024 OpenCFD Ltd. Copyright (C) 2016-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -502,7 +502,7 @@ void writeMaps
Info<< " pointProcAddressing" << endl; Info<< " pointProcAddressing" << endl;
ioAddr.rename("pointProcAddressing"); ioAddr.rename("pointProcAddressing");
IOListRef<label>(ioAddr, pointProcAddressing).write(); IOList<label>::writeContents(ioAddr, pointProcAddressing);
// From processor face to reconstructed mesh face // From processor face to reconstructed mesh face
Info<< " faceProcAddressing" << endl; Info<< " faceProcAddressing" << endl;
@ -551,13 +551,13 @@ void writeMaps
// From processor cell to reconstructed mesh cell // From processor cell to reconstructed mesh cell
Info<< " cellProcAddressing" << endl; Info<< " cellProcAddressing" << endl;
ioAddr.rename("cellProcAddressing"); ioAddr.rename("cellProcAddressing");
IOListRef<label>(ioAddr, cellProcAddressing).write(); IOList<label>::writeContents(ioAddr, cellProcAddressing);
// From processor patch to reconstructed mesh patch // From processor patch to reconstructed mesh patch
Info<< " boundaryProcAddressing" << endl; Info<< " boundaryProcAddressing" << endl;
ioAddr.rename("boundaryProcAddressing"); ioAddr.rename("boundaryProcAddressing");
IOListRef<label>(ioAddr, boundProcAddressing).write(); IOList<label>::writeContents(ioAddr, boundProcAddressing);
Info<< endl; Info<< endl;
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2015-2024 OpenCFD Ltd. Copyright (C) 2015-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -461,19 +461,12 @@ void writeDecomposition
{ {
// Write the decomposition as labelList for use with 'manual' // Write the decomposition as labelList for use with 'manual'
// decomposition method. // decomposition method.
IOListRef<label> IOList<label>::writeContents
( (
IOobject // NB: mesh read from facesInstance
( IOobject("cellDecomposition", mesh.facesInstance(), mesh),
"cellDecomposition",
mesh.facesInstance(), // mesh read from facesInstance
mesh,
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
),
decomp decomp
).write(); );
InfoOrPout InfoOrPout
<< "Writing wanted cell distribution to volScalarField " << name << "Writing wanted cell distribution to volScalarField " << name
@ -991,7 +984,8 @@ autoPtr<mapDistributePolyMesh> redistributeAndWrite
polyMesh::meshSubDir, polyMesh::meshSubDir,
mesh.thisDb(), mesh.thisDb(),
IOobjectOption::NO_READ, IOobjectOption::NO_READ,
IOobjectOption::AUTO_WRITE IOobjectOption::AUTO_WRITE,
IOobjectOption::REGISTER
), ),
distMap() distMap()
); );
@ -3029,20 +3023,17 @@ int main(int argc, char *argv[])
{ {
auto oldHandler = fileOperation::fileHandler(writeHandler); auto oldHandler = fileOperation::fileHandler(writeHandler);
IOmapDistributePolyMeshRef IOmapDistributePolyMesh::writeContents
( (
IOobject IOobject
( (
"procAddressing", "procAddressing",
areaProcMeshPtr->facesInstance(), areaProcMeshPtr->facesInstance(),
faMesh::meshSubDir, faMesh::meshSubDir,
areaProcMeshPtr->thisDb(), areaProcMeshPtr->thisDb()
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
IOobjectOption::NO_REGISTER
), ),
faDistMap faDistMap
).write(); );
areaProcMeshPtr->write(); areaProcMeshPtr->write();

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2024 OpenCFD Ltd. Copyright (C) 2016-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -118,7 +118,7 @@ template<class T>
Foam::IOListRef<T>::IOListRef Foam::IOListRef<T>::IOListRef
( (
const IOobject& io, const IOobject& io,
const List<T>& content const UList<T>& content
) )
: :
regIOobject(io), regIOobject(io),
@ -143,6 +143,23 @@ Foam::List<T> Foam::IOList<T>::readContents(const IOobject& io)
} }
template<class T>
void Foam::IOList<T>::writeContents
(
const IOobject& io,
const UList<T>& content
)
{
IOListRef<T> writer
(
IOobject(io, IOobjectOption::NO_REGISTER),
content
);
writer.write();
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T> template<class T>
@ -156,7 +173,7 @@ bool Foam::IOList<T>::writeData(Ostream& os) const
template<class T> template<class T>
bool Foam::IOListRef<T>::writeData(Ostream& os) const bool Foam::IOListRef<T>::writeData(Ostream& os) const
{ {
os << contentRef_.cref(); os << contentRef_;
return os.good(); return os.good();
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018-2024 OpenCFD Ltd. Copyright (C) 2018-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -96,6 +96,10 @@ public:
//- Read and return contents. The IOobject is never registered //- Read and return contents. The IOobject is never registered
static List<T> readContents(const IOobject& io); static List<T> readContents(const IOobject& io);
//- Write contents. The IOobject is never registered.
// Uses IOListRef internally.
static void writeContents(const IOobject& io, const UList<T>& content);
//- Destructor //- Destructor
virtual ~IOList() = default; virtual ~IOList() = default;
@ -121,7 +125,7 @@ public:
Class IOListRef Declaration Class IOListRef Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
//- A IOList wrapper for writing external data. //- A IOList wrapper for writing external List data
template<class T> template<class T>
class IOListRef class IOListRef
: :
@ -130,7 +134,7 @@ class IOListRef
// Private Data // Private Data
//- Reference to the external content //- Reference to the external content
refPtr<List<T>> contentRef_; UList<T> contentRef_;
public: public:
@ -161,7 +165,7 @@ public:
// Constructors // Constructors
//- Construct from IOobject and const data reference //- Construct from IOobject and const data reference
IOListRef(const IOobject& io, const List<T>& content); IOListRef(const IOobject& io, const UList<T>& content);
//- Destructor //- Destructor
@ -171,10 +175,9 @@ public:
// Member Functions // Member Functions
//- Allow cast to const content //- Allow cast to const content
// Fatal if content is not set operator const UList<T>&() const
operator const List<T>&() const
{ {
return contentRef_.cref(); return contentRef_;
} }
//- The writeData method for regIOobject write operation //- The writeData method for regIOobject write operation

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2015 OpenFOAM Foundation Copyright (C) 2015 OpenFOAM Foundation
Copyright (C) 2022-2024 OpenCFD Ltd. Copyright (C) 2022-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -124,6 +124,23 @@ Foam::IOmapDistributePolyMeshRef::IOmapDistributePolyMeshRef
/// contentRef_.ref(map); // writable reference /// contentRef_.ref(map); // writable reference
/// } /// }
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
void Foam::IOmapDistributePolyMesh::writeContents
(
const IOobject& io,
const mapDistributePolyMesh& map
)
{
IOmapDistributePolyMeshRef writer
(
IOobject(io, IOobjectOption::NO_REGISTER),
map
);
writer.write();
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2015 OpenFOAM Foundation Copyright (C) 2015 OpenFOAM Foundation
Copyright (C) 2022-2024 OpenCFD Ltd. Copyright (C) 2022-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -88,6 +88,17 @@ public:
); );
// Factory Methods
//- Write contents. The IOobject is never registered.
// Uses IOmapDistributePolyMeshRef internally.
static void writeContents
(
const IOobject& io,
const mapDistributePolyMesh& map
);
//- Destructor //- Destructor
virtual ~IOmapDistributePolyMesh() = default; virtual ~IOmapDistributePolyMesh() = default;

View File

@ -393,7 +393,7 @@ private:
( (
const objectRegistry& registry, const objectRegistry& registry,
const word& propertyName, const word& propertyName,
const labelList& list, const labelUList& list,
IOstreamOption streamOpt IOstreamOption streamOpt
) const; ) const;

View File

@ -145,7 +145,7 @@ void Foam::ccm::reader::writeMeshLabelList
( (
const objectRegistry& registry, const objectRegistry& registry,
const word& propertyName, const word& propertyName,
const labelList& list, const labelUList& list,
IOstreamOption streamOpt IOstreamOption streamOpt
) const ) const
{ {

View File

@ -192,7 +192,7 @@ private:
( (
const objectRegistry& registry, const objectRegistry& registry,
const word& propertyName, const word& propertyName,
const labelList& list, const labelUList& list,
IOstreamOption streamOpt IOstreamOption streamOpt
) const; ) const;

View File

@ -108,7 +108,7 @@ void Foam::meshReader::writeMeshLabelList
( (
const objectRegistry& registry, const objectRegistry& registry,
const word& propertyName, const word& propertyName,
const labelList& list, const labelUList& list,
IOstreamOption streamOpt IOstreamOption streamOpt
) const ) const
{ {

View File

@ -101,8 +101,8 @@ Foam::hexRef8Data::hexRef8Data
( (
const IOobject& io, const IOobject& io,
const hexRef8Data& data, const hexRef8Data& data,
const labelList& cellMap, const labelUList& cellMap,
const labelList& pointMap const labelUList& pointMap
) )
{ {
if (data.cellLevelPtr_) if (data.cellLevelPtr_)

View File

@ -93,8 +93,8 @@ public:
( (
const IOobject& io, const IOobject& io,
const hexRef8Data&, const hexRef8Data&,
const labelList& cellMap, const labelUList& cellMap,
const labelList& pointMap const labelUList& pointMap
); );
//- Construct from multiple hexRef8Data //- Construct from multiple hexRef8Data

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2016-2017 Wikki Ltd Copyright (C) 2016-2017 Wikki Ltd
Copyright (C) 2018-2024 OpenCFD Ltd. Copyright (C) 2018-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -1318,19 +1318,19 @@ bool Foam::faMeshDecomposition::writeDecomposition()
// pointProcAddressing // pointProcAddressing
ioAddr.rename("pointProcAddressing"); ioAddr.rename("pointProcAddressing");
IOListRef<label>(ioAddr, procPatchPointAddressing_[procI]).write(); IOList<label>::writeContents(ioAddr, procPatchPointAddressing_[procI]);
// edgeProcAddressing // edgeProcAddressing
ioAddr.rename("edgeProcAddressing"); ioAddr.rename("edgeProcAddressing");
IOListRef<label>(ioAddr, procEdgeAddressing_[procI]).write(); IOList<label>::writeContents(ioAddr, procEdgeAddressing_[procI]);
// faceProcAddressing // faceProcAddressing
ioAddr.rename("faceProcAddressing"); ioAddr.rename("faceProcAddressing");
IOListRef<label>(ioAddr, procFaceAddressing_[procI]).write(); IOList<label>::writeContents(ioAddr, procFaceAddressing_[procI]);
// boundaryProcAddressing // boundaryProcAddressing
ioAddr.rename("boundaryProcAddressing"); ioAddr.rename("boundaryProcAddressing");
IOListRef<label>(ioAddr, procBoundaryAddressing_[procI]).write(); IOList<label>::writeContents(ioAddr, procBoundaryAddressing_[procI]);
} }

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2021-2024 OpenCFD Ltd. Copyright (C) 2021-2025 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -693,19 +693,19 @@ void Foam::faMeshReconstructor::writeAddressing
// boundaryProcAddressing // boundaryProcAddressing
ioAddr.rename("boundaryProcAddressing"); ioAddr.rename("boundaryProcAddressing");
IOListRef<label>(ioAddr, faBoundaryProcAddr).write(); IOList<label>::writeContents(ioAddr, faBoundaryProcAddr);
// faceProcAddressing // faceProcAddressing
ioAddr.rename("faceProcAddressing"); ioAddr.rename("faceProcAddressing");
IOListRef<label>(ioAddr, faFaceProcAddr).write(); IOList<label>::writeContents(ioAddr, faFaceProcAddr);
// pointProcAddressing // pointProcAddressing
ioAddr.rename("pointProcAddressing"); ioAddr.rename("pointProcAddressing");
IOListRef<label>(ioAddr, faPointProcAddr).write(); IOList<label>::writeContents(ioAddr, faPointProcAddr);
// edgeProcAddressing // edgeProcAddressing
ioAddr.rename("edgeProcAddressing"); ioAddr.rename("edgeProcAddressing");
IOListRef<label>(ioAddr, faEdgeProcAddr).write(); IOList<label>::writeContents(ioAddr, faEdgeProcAddr);
} }
@ -760,7 +760,7 @@ void Foam::faMeshReconstructor::writeMesh
IOobject io(fullMesh.boundary()); IOobject io(fullMesh.boundary());
io.rename("faceLabels"); io.rename("faceLabels");
IOListRef<label>(io, singlePatchFaceLabels).write(); IOList<label>::writeContents(io, singlePatchFaceLabels);
fullMesh.boundary().write(); fullMesh.boundary().write();