ENH: wrapped IOField, IOList, IOmapDistributePolyMesh

- Uses a refPtr to reference external content.
  Useful (for example) when writing data without copying.
  Reading into external locations is not implemented
  (no current requirement for that).

    * IOFieldRef -> IOField
    * IOListRef -> IOList
    * IOmapDistributePolyMeshRef -> IOmapDistributePolyMesh

  Eg,

    labelList addressing = ...;

    io.rename("cellProcAddressing");
    IOListRef<label>(io, addressing).write();

  Or,
    primitivePatch patch = ...;
    IOFieldRef<vector>(io, patch.localPoints()).write();
This commit is contained in:
Mark Olesen
2022-05-04 18:39:59 +02:00
parent 036abb8ecb
commit 33693f32b6
10 changed files with 333 additions and 11 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenFOAM Foundation
Copyright (C) 2019-2021 OpenCFD Ltd.
Copyright (C) 2019-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -34,6 +34,7 @@ Description
#include "argList.H"
#include "IOField.H"
#include "IOList.H"
#include "primitiveFields.H"
#include "polyMesh.H"
#include "Time.H"
@ -181,6 +182,7 @@ int main(int argc, char *argv[])
argList::addBoolOption("bool", "Use bool for tests");
argList::addBoolOption("scalar", "Use scalar for tests");
argList::addBoolOption("label", "Use label for tests (default)");
argList::addBoolOption("ref", "Test writing by ref");
#include "addTimeOptions.H"
@ -232,6 +234,36 @@ int main(int argc, char *argv[])
doTests<label>(io, sz);
}
if (args.found("ref"))
{
Info<< nl << "Testing writing referenced external data" << nl << endl;
IOobject ioOutput
(
args.executable(),
"constant",
runTime,
IOobject::NO_READ,
IOobject::NO_WRITE,
false
);
labelList ints(identity(200));
ioOutput.rename(args.executable() + "-labels");
Info<< "write " << ioOutput.objectRelPath() << endl;
{
IOListRef<label>(ioOutput, ints).write();
}
ioOutput.rename(args.executable() + "-points");
Info<< "write " << ioOutput.objectRelPath() << endl;
{
IOFieldRef<vector>(ioOutput, mesh.points()).write();
}
}
Pout<< "\nEnd\n" << endl;

View File

@ -317,7 +317,7 @@ $(IOdictionary)/localIOdictionary.C
$(IOdictionary)/unwatchedIOdictionary.C
$(IOdictionary)/IOdictionary.C
db/IOobjects/IOMap/IOMapName.C
db/IOobjects/IOMap/IOMaps.C
db/IOobjects/decomposedBlockData/decomposedBlockData.C
db/IOobjects/decomposedBlockData/decomposedBlockDataHeader.C
db/IOobjects/GlobalIOField/GlobalIOFields.C

View File

@ -167,12 +167,33 @@ Foam::IOField<Type>::IOField(const IOobject& io, const tmp<Field<Type>>& tfld)
}
template<class Type>
Foam::IOFieldRef<Type>::IOFieldRef
(
const IOobject& io,
const Field<Type>& content
)
:
regIOobject(io),
contentRef_(content) // cref
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
bool Foam::IOField<Type>::writeData(Ostream& os) const
{
return (os << static_cast<const Field<Type>&>(*this)).good();
os << static_cast<const Field<Type>&>(*this);
return os.good();
}
template<class Type>
bool Foam::IOFieldRef<Type>::writeData(Ostream& os) const
{
os << contentRef_.cref();
return os.good();
}

View File

@ -38,8 +38,9 @@ SourceFiles
#ifndef Foam_IOField_H
#define Foam_IOField_H
#include "regIOobject.H"
#include "Field.H"
#include "regIOobject.H"
#include "refPtr.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -100,7 +101,8 @@ public:
// Member Functions
bool writeData(Ostream& os) const;
//- The writeData method for regIOobject write operation
virtual bool writeData(Ostream& os) const;
// Member Operators
@ -113,6 +115,72 @@ public:
};
/*---------------------------------------------------------------------------*\
Class IOFieldRef Declaration
\*---------------------------------------------------------------------------*/
//- A IOField wrapper for writing external data.
template<class Type>
class IOFieldRef
:
public regIOobject
{
// Private Data
//- Reference to the external content
refPtr<Field<Type>> contentRef_;
public:
//- The underlying content type
typedef Field<Type> content_type;
//- Type is identical to IOField
virtual const word& type() const
{
return IOField<Type>::typeName;
}
// Generated Methods
//- No default construct
IOFieldRef() = delete;
//- No copy construct
IOFieldRef(const IOFieldRef&) = delete;
//- No copy assignment
void operator=(const IOFieldRef&) = delete;
// Constructors
//- Construct from IOobject and const data reference
IOFieldRef(const IOobject& io, const Field<Type>& content);
//- Destructor
virtual ~IOFieldRef() = default;
// Member Functions
//- Allow cast to const content
// Fatal if content is not set
operator const Field<Type>&() const
{
return contentRef_.cref();
}
//- The writeData method for regIOobject write operation
// Fatal if content is not set
virtual bool writeData(Ostream& os) const;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2018 OpenCFD Ltd.
Copyright (C) 2016-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -109,12 +109,33 @@ Foam::IOList<T>::IOList(const IOobject& io, List<T>&& content)
}
template<class T>
Foam::IOListRef<T>::IOListRef
(
const IOobject& io,
const List<T>& content
)
:
regIOobject(io),
contentRef_(content)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
bool Foam::IOList<T>::writeData(Ostream& os) const
{
return (os << *this).good();
os << static_cast<const List<T>&>(*this);
return os.good();
}
template<class T>
bool Foam::IOListRef<T>::writeData(Ostream& os) const
{
os << contentRef_.cref();
return os.good();
}

View File

@ -40,6 +40,7 @@ SourceFiles
#include "List.H"
#include "regIOobject.H"
#include "refPtr.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -94,7 +95,8 @@ public:
// Member Functions
bool writeData(Ostream& os) const;
//- The writeData method for regIOobject write operation
virtual bool writeData(Ostream& os) const;
// Member Operators
@ -107,6 +109,72 @@ public:
};
/*---------------------------------------------------------------------------*\
Class IOListRef Declaration
\*---------------------------------------------------------------------------*/
//- A IOList wrapper for writing external data.
template<class T>
class IOListRef
:
public regIOobject
{
// Private Data
//- Reference to the external content
refPtr<List<T>> contentRef_;
public:
//- The underlying content type
typedef List<T> content_type;
//- Type is identical to IOList
virtual const word& type() const
{
return IOList<T>::typeName;
}
// Generated Methods
//- No default construct
IOListRef() = delete;
//- No copy construct
IOListRef(const IOListRef&) = delete;
//- No copy assignment
void operator=(const IOListRef&) = delete;
// Constructors
//- Construct from IOobject and const data reference
IOListRef(const IOobject& io, const List<T>& content);
//- Destructor
virtual ~IOListRef() = default;
// Member Functions
//- Allow cast to const content
// Fatal if content is not set
operator const List<T>&() const
{
return contentRef_.cref();
}
//- The writeData method for regIOobject write operation
// Fatal if content is not set
virtual bool writeData(Ostream& os) const;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam

View File

@ -106,7 +106,8 @@ Foam::IOMap<T>::IOMap(const IOobject& io, Map<T>&& content)
template<class T>
bool Foam::IOMap<T>::writeData(Ostream& os) const
{
return (os << *this).good();
os << *this;
return os.good();
}

View File

@ -106,17 +106,59 @@ Foam::IOmapDistributePolyMesh::IOmapDistributePolyMesh
}
Foam::IOmapDistributePolyMeshRef::IOmapDistributePolyMeshRef
(
const IOobject& io,
const mapDistributePolyMesh& map
)
:
regIOobject(io),
contentRef_(map) // cref
{}
// Not sure if we need this yet...
//
/// Foam::IOmapDistributePolyMeshRef::IOmapDistributePolyMeshRef
/// (
/// const IOobject& io,
/// mapDistributePolyMesh& map
/// )
/// :
/// regIOobject(io),
/// contentRef_()
/// {
/// contentRef_.ref(map); // writable reference
/// }
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::IOmapDistributePolyMesh::readData(Istream& is)
{
return (is >> *this).good();
is >> *this;
return is.good();
}
bool Foam::IOmapDistributePolyMesh::writeData(Ostream& os) const
{
return (os << *this).good();
os << *this;
return os.good();
}
bool Foam::IOmapDistributePolyMeshRef::readData(Istream& is)
{
is >> contentRef_.ref();
return is.good();
}
bool Foam::IOmapDistributePolyMeshRef::writeData(Ostream& os) const
{
os << contentRef_.cref();
return os.good();
}

View File

@ -42,6 +42,7 @@ SourceFiles
#include "mapDistributePolyMesh.H"
#include "regIOobject.H"
#include "refPtr.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -102,6 +103,74 @@ public:
};
/*---------------------------------------------------------------------------*\
Class IOmapDistributePolyMeshRef Declaration
\*---------------------------------------------------------------------------*/
//- A IOmapDistributePolyMesh wrapper for using referenced external data.
class IOmapDistributePolyMeshRef
:
public regIOobject
{
// Private Data
//- Reference to the external content
refPtr<mapDistributePolyMesh> contentRef_;
public:
//- Type is identical to IOmapDistributePolyMesh
virtual const word& type() const
{
return IOmapDistributePolyMesh::typeName;
}
// Generated Methods
//- No default construct
IOmapDistributePolyMeshRef() = delete;
//- No copy construct
IOmapDistributePolyMeshRef(const IOmapDistributePolyMeshRef&) = delete;
//- No copy assignment
void operator=(const IOmapDistributePolyMeshRef&) = delete;
// Constructors
//- Construct from IOobject and const data reference
IOmapDistributePolyMeshRef
(
const IOobject& io,
const mapDistributePolyMesh& map
);
//- Destructor
virtual ~IOmapDistributePolyMeshRef() = default;
// Member Functions
//- Allow cast to const content
// Fatal if content is not set
operator const mapDistributePolyMesh&() const
{
return contentRef_.cref();
}
//- The readData method for regIOobject read operation
// Fatal if content is constant (or not set)
virtual bool readData(Istream& is);
//- The writeData method for regIOobject write operation
// Fatal if content is not set
virtual bool writeData(Ostream& os) const;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam