ENH: add factory method readContents to IO containers

- useful when regular contents are to be read via an IOobject and
  returned.

  Eg,  dictionary propsDict(IOdictionary::readContents(dictIO));
  vs.  dictionary propsDict(static_cast<dictionary&&>(IOdictionary(dictIO)));

  Commonly these would have simply been constructed directly as the
  IO container:

  eg,  IOdictionary propsDict(dictIO);

  However, that style may not ensure proper move semantics for return
  types.

  Now,
  =====
      labelList decomp(labelIOList::readContents(io));
      ... something
      return decomp;
  =====

  Previously,
  =====
      labelIOList decomp(io);

      // Hope for the best...
      return decomp;

      // Or be explicit and ensure elision occurs...
      return labelList(std::move(static_cast<labelList&>(decomp)));
  =====

  Note:
       labelList list(labelIOList(io));

       looks like a good idea, but generally fails to compile
This commit is contained in:
Mark Olesen 2023-02-28 11:17:59 +01:00
parent 790a5c26f9
commit f75af788c1
20 changed files with 211 additions and 29 deletions

View File

@ -109,7 +109,11 @@ int main(int argc, char *argv[])
{
#include "setConstantRunTimeDictionaryIO.H"
IOdictionary propsDict(dictIO);
#if (OPENFOAM > 2212)
dictionary propsDict(IOdictionary::readContents(dictIO));
#else
dictionary propsDict(static_cast<dictionary&&>(IOdictionary(dictIO)));
#endif
const scalarField xvals(propsDict.lookup("x"));

View File

@ -434,6 +434,13 @@ public:
IOobjectOption::writeOption wOpt
);
//- Copy construct, resetting register option
inline IOobject
(
const IOobject& io,
IOobjectOption::registerOption regOpt
);
//- Clone
autoPtr<IOobject> clone() const

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2022 OpenCFD Ltd.
Copyright (C) 2017-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -145,8 +145,20 @@ inline Foam::IOobject::IOobject
:
IOobject(io)
{
readOpt(rOpt);
writeOpt(wOpt);
IOobjectOption::readOpt(rOpt);
IOobjectOption::writeOpt(wOpt);
}
inline Foam::IOobject::IOobject
(
const IOobject& io,
IOobjectOption::registerOption regOpt
)
:
IOobject(io)
{
IOobjectOption::registerObject(regOpt);
}

View File

@ -93,7 +93,7 @@ void Foam::IOobject::warnNoRereading() const
{
WarningInFunction
<< Type::typeName << ' ' << name()
<< " constructed with IOobject::MUST_READ_IF_MODIFIED but "
<< " constructed with MUST_READ_IF_MODIFIED but "
<< Type::typeName << " does not support automatic rereading."
<< endl;
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2022 OpenCFD Ltd.
Copyright (C) 2016-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -180,6 +180,23 @@ Foam::IOFieldRef<Type>::IOFieldRef
{}
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
template<class Type>
Foam::Field<Type> Foam::IOField<Type>::readContents(const IOobject& io)
{
IOobject rio(io, IOobjectOption::NO_REGISTER);
if (rio.readOpt() == IOobjectOption::MUST_READ_IF_MODIFIED)
{
rio.readOpt(IOobjectOption::MUST_READ);
}
IOField<Type> reader(rio);
return Field<Type>(std::move(static_cast<Field<Type>&>(reader)));
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018-2022 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -98,6 +98,12 @@ public:
IOField(const IOobject& io, const tmp<Field<Type>>& tfld);
// Factory Methods
//- Read and return contents. The IOobject will not be registered
static Field<Type> readContents(const IOobject& io);
//- Destructor
virtual ~IOField() = default;

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2022 OpenCFD Ltd.
Copyright (C) 2016-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -126,6 +126,23 @@ Foam::IOListRef<T>::IOListRef
{}
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
template<class T>
Foam::List<T> Foam::IOList<T>::readContents(const IOobject& io)
{
IOobject rio(io, IOobjectOption::NO_REGISTER);
if (rio.readOpt() == IOobjectOption::MUST_READ_IF_MODIFIED)
{
rio.readOpt(IOobjectOption::MUST_READ);
}
IOList<T> reader(rio);
return List<T>(std::move(static_cast<List<T>&>(reader)));
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>

View File

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

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018-2022 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -94,6 +94,23 @@ Foam::IOMap<T>::IOMap(const IOobject& io, Map<T>&& content)
}
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
template<class T>
Foam::Map<T> Foam::IOMap<T>::readContents(const IOobject& io)
{
IOobject rio(io, IOobjectOption::NO_REGISTER);
if (rio.readOpt() == IOobjectOption::MUST_READ_IF_MODIFIED)
{
rio.readOpt(IOobjectOption::MUST_READ);
}
IOMap<T> reader(rio);
return Map<T>(std::move(static_cast<Map<T>&>(reader)));
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018-2022 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -89,6 +89,12 @@ public:
IOMap(const IOobject&, Map<T>&& content);
// Factory Methods
//- Read and return contents. The IOobject will not be registered
static Map<T> readContents(const IOobject& io);
//- Destructor
virtual ~IOMap() = default;

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018-2022 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -116,6 +116,23 @@ Foam::IOPtrList<T>::IOPtrList(const IOobject& io, PtrList<T>&& content)
}
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
template<class T>
Foam::PtrList<T> Foam::IOPtrList<T>::readContents(const IOobject& io)
{
IOobject rio(io, IOobjectOption::NO_REGISTER);
if (rio.readOpt() == IOobjectOption::MUST_READ_IF_MODIFIED)
{
rio.readOpt(IOobjectOption::MUST_READ);
}
IOPtrList<T> reader(rio);
return PtrList<T>(std::move(static_cast<PtrList<T>&>(reader)));
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2018 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -35,8 +35,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef IOPtrList_H
#define IOPtrList_H
#ifndef Foam_IOPtrList_H
#define Foam_IOPtrList_H
#include "PtrList.H"
#include "regIOobject.H"
@ -84,6 +84,12 @@ public:
IOPtrList(const IOobject& io, PtrList<T>&& content);
// Factory Methods
//- Read and return contents. The IOobject will not be registered
static PtrList<T> readContents(const IOobject& io);
//- Destructor
virtual ~IOPtrList() = default;

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2021-2022 OpenCFD Ltd.
Copyright (C) 2021-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -91,4 +91,20 @@ Foam::IOdictionary::IOdictionary
}
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
Foam::dictionary Foam::IOdictionary::readContents(const IOobject& io)
{
IOobject rio(io, IOobjectOption::NO_REGISTER);
if (rio.readOpt() == IOobjectOption::MUST_READ_IF_MODIFIED)
{
rio.readOpt(IOobjectOption::MUST_READ);
}
IOdictionary reader(rio);
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 OpenCFD Ltd.
Copyright (C) 2021-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -38,8 +38,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef IOdictionary_H
#define IOdictionary_H
#ifndef Foam_IOdictionary_H
#define Foam_IOdictionary_H
#include "baseIOdictionary.H"
@ -87,6 +87,12 @@ public:
IOdictionary(const IOobject& io, Istream& is);
// Factory Methods
//- Read and return contents. The IOobject will not be registered
static dictionary readContents(const IOobject& io);
//- Destructor
virtual ~IOdictionary() = default;

View File

@ -39,8 +39,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef baseIOdictionary_H
#define baseIOdictionary_H
#ifndef Foam_baseIOdictionary_H
#define Foam_baseIOdictionary_H
#include "dictionary.H"
#include "regIOobject.H"

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2015-2017 OpenFOAM Foundation
Copyright (C) 2021-2022 OpenCFD Ltd.
Copyright (C) 2021-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -88,4 +88,20 @@ Foam::localIOdictionary::localIOdictionary
}
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
Foam::dictionary Foam::localIOdictionary::readContents(const IOobject& io)
{
IOobject rio(io, IOobjectOption::NO_REGISTER);
if (rio.readOpt() == IOobjectOption::MUST_READ_IF_MODIFIED)
{
rio.readOpt(IOobjectOption::MUST_READ);
}
localIOdictionary reader(rio);
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 OpenCFD Ltd.
Copyright (C) 2021-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -36,8 +36,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef localIOdictionary_H
#define localIOdictionary_H
#ifndef Foam_localIOdictionary_H
#define Foam_localIOdictionary_H
#include "baseIOdictionary.H"
@ -84,6 +84,12 @@ public:
localIOdictionary(const IOobject& io, Istream& is);
// Factory Methods
//- Read and return contents. The IOobject will not be registered
static dictionary readContents(const IOobject& io);
//- Destructor
virtual ~localIOdictionary() = default;

View File

@ -37,8 +37,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef unwatchedIOdictionary_H
#define unwatchedIOdictionary_H
#ifndef Foam_unwatchedIOdictionary_H
#define Foam_unwatchedIOdictionary_H
#include "baseIOdictionary.H"

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2022 OpenCFD Ltd.
Copyright (C) 2016-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -188,6 +188,23 @@ Foam::rawIOField<Type>::rawIOField
{}
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
template<class Type>
Foam::Field<Type> Foam::rawIOField<Type>::readContents(const IOobject& io)
{
IOobject rio(io, IOobjectOption::NO_REGISTER);
if (rio.readOpt() == IOobjectOption::MUST_READ_IF_MODIFIED)
{
rio.readOpt(IOobjectOption::MUST_READ);
}
rawIOField<Type> reader(rio);
return Field<Type>(std::move(static_cast<Field<Type>&>(reader)));
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020-2022 OpenCFD Ltd.
Copyright (C) 2020-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -99,6 +99,12 @@ public:
rawIOField(const IOobject& io, const bool readAverage);
// Factory Methods
//- Read and return contents. The IOobject will not be registered
static Field<Type> readContents(const IOobject& io);
//- Destructor
virtual ~rawIOField() = default;