ENH: allow modification of tmp state (cached or uncached)

- simplifies handling.
  * enables unprotecting to avoid accidentally cloning.
  * removes the need for dedicated constructor or factory forms.
  * simplfies DimensionedField and GeometricField New factory methods

- update objectRegistry management method (internal use)

  old:  bool cacheTemporaryObject(...)
  new:  bool is_cacheTemporaryObject(...)

  to clarify that it is a query, not a request for caching etc.
This commit is contained in:
Mark Olesen
2023-10-18 09:29:28 +02:00
parent 7cc6d52345
commit 28b6a5b85a
10 changed files with 414 additions and 157 deletions

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2022 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -45,10 +45,31 @@ Description
using namespace Foam;
enum loadTestTypes
{
PLAIN_PTR, AUTO_PTR, REF_PTR, TMP_PTR, CACHE_PTR
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
void printIOobject(const regIOobject* io)
{
if (io)
{
Info<< io->name() << ' ' << "type=" << io->type()
<< " registered=" << io->registered()
<< " owned=" << io->ownedByRegistry() << endl;
}
}
template<class Type>
bool loadField(fvMesh& mesh, const word& fieldName)
bool loadField
(
fvMesh& mesh,
const word& fieldName,
enum loadTestTypes wrapper = loadTestTypes::PLAIN_PTR
)
{
typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
typedef GeometricField<Type, fvsPatchField, surfaceMesh> SurfaceFieldType;
@ -66,20 +87,67 @@ bool loadField(fvMesh& mesh, const word& fieldName)
mesh,
IOobject::MUST_READ,
IOobject::NO_WRITE
// Value of register is fairly irrelevant
);
if (fieldHeader.typeHeaderOk<VolFieldType>(true, true, false))
{
// Store field on mesh database
VolFieldType* ptr = new VolFieldType(fieldHeader, mesh);
mesh.objectRegistry::store(ptr);
switch (wrapper)
{
case loadTestTypes::PLAIN_PTR :
{
auto* ptr = new VolFieldType(fieldHeader, mesh);
printIOobject(ptr);
regIOobject::store(ptr);
break;
}
case loadTestTypes::AUTO_PTR :
{
auto ptr = autoPtr<VolFieldType>::New(fieldHeader, mesh);
printIOobject(ptr.get());
regIOobject::store(ptr);
break;
}
case loadTestTypes::REF_PTR :
{
auto ptr = refPtr<VolFieldType>::New(fieldHeader, mesh);
printIOobject(ptr.get());
regIOobject::store(ptr);
break;
}
case loadTestTypes::TMP_PTR :
{
auto ptr = tmp<VolFieldType>::New(fieldHeader, mesh);
printIOobject(ptr.get());
Info<< "pointer:" << ptr.is_pointer()
<< " movable:" << ptr.movable() << nl;
regIOobject::store(ptr);
break;
}
case loadTestTypes::CACHE_PTR :
{
auto ptr = tmp<VolFieldType>::New(fieldHeader, mesh);
ptr.protect(true);
printIOobject(ptr.get());
Info<< "pointer:" << ptr.is_pointer()
<< " movable:" << ptr.movable() << nl;
regIOobject::store(ptr);
break;
}
}
return true;
}
else if (fieldHeader.typeHeaderOk<SurfaceFieldType>(true, true, false))
{
// Store field on mesh database
SurfaceFieldType* ptr = new SurfaceFieldType(fieldHeader, mesh);
mesh.objectRegistry::store(ptr);
regIOobject::store(ptr);
return true;
}
@ -87,28 +155,38 @@ bool loadField(fvMesh& mesh, const word& fieldName)
}
bool loadField(fvMesh& mesh, const word& fieldName)
bool loadField
(
fvMesh& mesh,
const word& fieldName,
enum loadTestTypes wrapper = loadTestTypes::PLAIN_PTR
)
{
return
(
!mesh.objectRegistry::found(fieldName)
&&
(
loadField<scalar>(mesh, fieldName)
|| loadField<vector>(mesh, fieldName)
|| loadField<sphericalTensor>(mesh, fieldName)
|| loadField<symmTensor>(mesh, fieldName)
|| loadField<tensor>(mesh, fieldName)
loadField<scalar>(mesh, fieldName, wrapper)
|| loadField<vector>(mesh, fieldName, wrapper)
|| loadField<sphericalTensor>(mesh, fieldName, wrapper)
|| loadField<symmTensor>(mesh, fieldName, wrapper)
|| loadField<tensor>(mesh, fieldName, wrapper)
)
);
}
void loadFields(fvMesh& mesh, const IOobjectList& objects)
void loadFields
(
fvMesh& mesh,
const IOobjectList& objects,
enum loadTestTypes wrapper = loadTestTypes::PLAIN_PTR
)
{
for (const word& fieldName : objects.names())
{
loadField(mesh, fieldName);
loadField(mesh, fieldName, wrapper);
}
}
@ -286,8 +364,46 @@ int main(int argc, char *argv[])
// timeSelector::addOptions();
timeSelector::addOptions(true, true);
argList::addBoolOption("plain", "Store from raw pointer (default)");
argList::addBoolOption("autoPtr", "Store from autoPtr");
argList::addBoolOption("refPtr", "Store from refPtr");
argList::addBoolOption("cacheTmp", "Store from tmp (cached)");
argList::addBoolOption("tmp", "Store from tmp (regular)");
argList::addVerboseOption("increase debug value");
#include "setRootCase.H"
const int optVerbose = args.verbose();
enum loadTestTypes loadWrapper = loadTestTypes::PLAIN_PTR;
if (args.found("autoPtr"))
{
Info<< "loading via autoPtr" << nl;
loadWrapper = loadTestTypes::AUTO_PTR;
}
else if (args.found("refPtr"))
{
Info<< "loading via refPtr" << nl;
loadWrapper = loadTestTypes::REF_PTR;
}
else if (args.found("cacheTmp"))
{
Info<< "loading via tmp (cached)" << nl;
loadWrapper = loadTestTypes::CACHE_PTR;
}
else if (args.found("tmp"))
{
Info<< "loading via tmp (regular)" << nl;
loadWrapper = loadTestTypes::TMP_PTR;
}
else
{
Info<< "loading via plain ptr" << nl;
loadWrapper = loadTestTypes::PLAIN_PTR;
}
// wordRes matcher;
// if (args.readListIfPresent<wordRe>("filter", matcher))
// {
@ -299,6 +415,12 @@ int main(int argc, char *argv[])
instantList timeDirs = timeSelector::select0(runTime, args);
if (optVerbose)
{
objectRegistry::debug = optVerbose;
regIOobject::debug = optVerbose;
}
forAll(timeDirs, timeI)
{
runTime.setTime(timeDirs[timeI], timeI);
@ -309,7 +431,7 @@ int main(int argc, char *argv[])
IOobjectList objects(mesh, runTime.timeName());
// Read volFields
loadFields(mesh, objects);
loadFields(mesh, objects, loadWrapper);
printRegistry(Info, mesh);

View File

@ -633,7 +633,13 @@ public:
//FUTURE void addTemporaryObject(const word& name) const;
//- True if given name is in the cacheTemporaryObjects set
bool cacheTemporaryObject(const word& name) const;
bool is_cacheTemporaryObject(const word& name) const;
//- True if name of object is in the cacheTemporaryObjects set
bool is_cacheTemporaryObject(const regIOobject* io) const;
//- True if name of object is in the cacheTemporaryObjects set
bool is_cacheTemporaryObject(const regIOobject& io) const;
//- Cache the given object. Moves content and stores
template<class Type>
@ -643,7 +649,7 @@ public:
void resetCacheTemporaryObject(const regIOobject* io) const;
//- Reset the cache state of the given object
// in the cacheTemporaryObjects set
//- in the cacheTemporaryObjects set
void resetCacheTemporaryObject(const regIOobject& io) const;
//- Check that all objects specified in the cacheTemporaryObjects

View File

@ -65,6 +65,12 @@ void Foam::objectRegistry::readCacheTemporaryObjects() const
{
cacheTemporaryObjects_.emplace(objName, false, false);
}
if (objectRegistry::debug)
{
Info<< "objectRegistry::cacheTemporaryObjects : "
<< flatOutput(objectNames) << endl;
}
}
}
@ -90,12 +96,24 @@ void Foam::objectRegistry::deleteCachedObject(regIOobject* io) const
// }
bool Foam::objectRegistry::cacheTemporaryObject
bool Foam::objectRegistry::is_cacheTemporaryObject
(
const word& name
) const
{
return cacheTemporaryObjects_.found(name);
return cacheTemporaryObjects_.contains(name);
}
bool Foam::objectRegistry::is_cacheTemporaryObject(const regIOobject* io) const
{
return io && cacheTemporaryObjects_.contains(io->name());
}
bool Foam::objectRegistry::is_cacheTemporaryObject(const regIOobject& io) const
{
return cacheTemporaryObjects_.contains(io.name());
}

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.
@ -56,7 +56,7 @@ Foam::regIOobject::regIOobject(const IOobject& io, const bool isTimeObject)
metaDataPtr_(nullptr),
isPtr_(nullptr)
{
if (registerObject())
if (IOobject::registerObject())
{
// Register (check-in) with objectRegistry if requested
checkIn();
@ -137,7 +137,7 @@ Foam::regIOobject::regIOobject
metaDataPtr_(rio.metaDataPtr_.clone()),
isPtr_(nullptr)
{
if (registerObject())
if (IOobject::registerObject())
{
checkIn();
}
@ -194,7 +194,7 @@ bool Foam::regIOobject::checkIn()
{
// multiple checkin of same object is disallowed - this would mess up
// any mapping
registered_ = db().checkIn(*this);
registered_ = db().checkIn(this);
// check-in on defaultRegion is allowed to fail, since subsetted meshes
// are created with the same name as their originating mesh
@ -205,17 +205,18 @@ bool Foam::regIOobject::checkIn()
// for ease of finding where attempted duplicate check-in
// originated
FatalErrorInFunction
<< "failed to register object " << objectPath()
<< " the name already exists in the objectRegistry" << endl
<< "Contents:" << db().sortedToc()
<< "Failed to register: " << name() << ' '
<< objectRelPath()
<< " : the name already exists in the registry" << nl
<< "Contents:" << db().sortedToc() << endl
<< abort(FatalError);
}
else
{
WarningInFunction
<< "failed to register object " << objectPath()
<< " the name already exists in the objectRegistry"
<< endl;
<< "Failed to register: " << name() << ' '
<< objectRelPath()
<< " : the name already exists in the registry" << endl;
}
}
}
@ -474,7 +475,7 @@ void Foam::regIOobject::rename(const word& newName)
IOobject::rename(newName);
if (registerObject())
if (IOobject::registerObject())
{
// Re-register object with objectRegistry
checkIn();
@ -519,7 +520,7 @@ void Foam::regIOobject::operator=(const IOobject& io)
IOobject::operator=(io);
if (registerObject())
if (IOobject::registerObject())
{
// Re-register object with objectRegistry
checkIn();

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2018-2022 OpenCFD Ltd.
Copyright (C) 2018-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -145,6 +145,8 @@ inline Type& Foam::regIOobject::store(tmp<Type>& ptr)
if (ptr.is_pointer())
{
// Acquire ownership, pass management to objectRegistry
ptr.protect(false); // Storing (ie, not cached/protected)
p = ptr.ptr();
store(p);

View File

@ -37,11 +37,8 @@ Foam::DimensionedField<Type, GeoMesh>::New
const Field<Type>& iField
)
{
const bool caching = mesh.thisDb().cacheTemporaryObject(name);
return tmp<DimensionedField<Type, GeoMesh>>::NewImmovable
auto ptr = tmp<DimensionedField<Type, GeoMesh>>::New
(
caching, // (true: immovable, false: movable)
IOobject
(
name,
@ -49,12 +46,22 @@ Foam::DimensionedField<Type, GeoMesh>::New
mesh.thisDb(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
caching // (true: REGISTER, false: NO_REGISTER)
IOobjectOption::NO_REGISTER
),
mesh,
dims,
iField
);
if
(
ptr->db().is_cacheTemporaryObject(ptr.get())
)
{
ptr.protect(true);
ptr->checkIn();
}
return ptr;
}
@ -68,11 +75,8 @@ Foam::DimensionedField<Type, GeoMesh>::New
Field<Type>&& iField
)
{
const bool caching = mesh.thisDb().cacheTemporaryObject(name);
return tmp<DimensionedField<Type, GeoMesh>>::NewImmovable
auto ptr = tmp<DimensionedField<Type, GeoMesh>>::New
(
caching, // (true: immovable, false: movable)
IOobject
(
name,
@ -80,12 +84,22 @@ Foam::DimensionedField<Type, GeoMesh>::New
mesh.thisDb(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
caching // (true: REGISTER, false: NO_REGISTER)
IOobjectOption::NO_REGISTER
),
mesh,
dims,
std::move(iField)
);
if
(
ptr->db().is_cacheTemporaryObject(ptr.get())
)
{
ptr.protect(true);
ptr->checkIn();
}
return ptr;
}
@ -98,11 +112,8 @@ Foam::DimensionedField<Type, GeoMesh>::New
const dimensionSet& dims
)
{
const bool caching = mesh.thisDb().cacheTemporaryObject(name);
return tmp<DimensionedField<Type, GeoMesh>>::NewImmovable
auto ptr = tmp<DimensionedField<Type, GeoMesh>>::New
(
caching, // (true: immovable, false: movable)
IOobject
(
name,
@ -110,12 +121,22 @@ Foam::DimensionedField<Type, GeoMesh>::New
mesh.thisDb(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
caching // (true: REGISTER, false: NO_REGISTER)
IOobjectOption::NO_REGISTER
),
mesh,
dims,
false // checkIOFlags off
);
if
(
ptr->db().is_cacheTemporaryObject(ptr.get())
)
{
ptr.protect(true);
ptr->checkIn();
}
return ptr;
}
@ -129,11 +150,8 @@ Foam::DimensionedField<Type, GeoMesh>::New
const dimensionSet& dims
)
{
const bool caching = mesh.thisDb().cacheTemporaryObject(name);
return tmp<DimensionedField<Type, GeoMesh>>::NewImmovable
auto ptr = tmp<DimensionedField<Type, GeoMesh>>::New
(
caching, // (true: immovable, false: movable)
IOobject
(
name,
@ -141,13 +159,23 @@ Foam::DimensionedField<Type, GeoMesh>::New
mesh.thisDb(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
caching // (true: REGISTER, false: NO_REGISTER)
IOobjectOption::NO_REGISTER
),
mesh,
value,
dims,
false // checkIOFlags off
);
if
(
ptr->db().is_cacheTemporaryObject(ptr.get())
)
{
ptr.protect(true);
ptr->checkIn();
}
return ptr;
}
@ -178,11 +206,8 @@ Foam::DimensionedField<Type, GeoMesh>::New
const tmp<DimensionedField<Type, GeoMesh>>& tfld
)
{
const bool caching = tfld().db().cacheTemporaryObject(name);
return tmp<DimensionedField<Type, GeoMesh>>::NewImmovable
auto ptr = tmp<DimensionedField<Type, GeoMesh>>::New
(
caching, // (true: immovable, false: movable)
IOobject
(
name,
@ -191,10 +216,20 @@ Foam::DimensionedField<Type, GeoMesh>::New
tfld().db(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
caching // (true: REGISTER, false: NO_REGISTER)
IOobjectOption::NO_REGISTER
),
tfld
);
if
(
ptr->db().is_cacheTemporaryObject(ptr.get())
)
{
ptr.protect(true);
ptr->checkIn();
}
return ptr;
}
@ -208,11 +243,8 @@ Foam::DimensionedField<Type, GeoMesh>::New
const dimensionSet& dims
)
{
const bool caching = fld.db().cacheTemporaryObject(name);
return tmp<DimensionedField<Type, GeoMesh>>::NewImmovable
auto ptr = tmp<DimensionedField<Type, GeoMesh>>::New
(
caching, // (true: immovable, false: movable)
IOobject
(
name,
@ -220,11 +252,21 @@ Foam::DimensionedField<Type, GeoMesh>::New
fld.db(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
caching // (true: REGISTER, false: NO_REGISTER)
IOobjectOption::NO_REGISTER
),
fld.mesh(),
dims
);
if
(
ptr->db().is_cacheTemporaryObject(ptr.get())
)
{
ptr.protect(true);
ptr->checkIn();
}
return ptr;
}
@ -238,11 +280,8 @@ Foam::DimensionedField<Type, GeoMesh>::New
const dimensioned<Type>& dt
)
{
const bool caching = fld.db().cacheTemporaryObject(name);
return tmp<DimensionedField<Type, GeoMesh>>::NewImmovable
auto ptr = tmp<DimensionedField<Type, GeoMesh>>::New
(
caching, // (true: immovable, false: movable)
IOobject
(
name,
@ -250,12 +289,22 @@ Foam::DimensionedField<Type, GeoMesh>::New
fld.db(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
caching // (true: REGISTER, false: NO_REGISTER)
IOobjectOption::NO_REGISTER
),
fld.mesh(),
dt.value(),
dt.dimensions()
);
if
(
ptr->db().is_cacheTemporaryObject(ptr.get())
)
{
ptr.protect(true);
ptr->checkIn();
}
return ptr;
}

View File

@ -38,11 +38,8 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
const word& patchFieldType
)
{
const bool caching = mesh.thisDb().cacheTemporaryObject(name);
return tmp<GeometricField<Type, PatchField, GeoMesh>>::NewImmovable
auto ptr = tmp<GeometricField<Type, PatchField, GeoMesh>>::New
(
caching, // (true: immovable, false: movable)
IOobject
(
name,
@ -50,12 +47,22 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
mesh.thisDb(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
caching // (true: REGISTER, false: NO_REGISTER)
IOobjectOption::NO_REGISTER
),
mesh,
dims,
patchFieldType
);
if
(
ptr->db().is_cacheTemporaryObject(ptr.get())
)
{
ptr.protect(true);
ptr->checkIn();
}
return ptr;
}
@ -70,11 +77,8 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
const word& patchFieldType
)
{
const bool caching = mesh.thisDb().cacheTemporaryObject(name);
return tmp<GeometricField<Type, PatchField, GeoMesh>>::NewImmovable
auto ptr = tmp<GeometricField<Type, PatchField, GeoMesh>>::New
(
caching, // (true: immovable, false: movable)
IOobject
(
name,
@ -82,13 +86,23 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
mesh.thisDb(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
caching // (true: REGISTER, false: NO_REGISTER)
IOobjectOption::NO_REGISTER
),
mesh,
dims,
iField,
patchFieldType
);
if
(
ptr->db().is_cacheTemporaryObject(ptr.get())
)
{
ptr.protect(true);
ptr->checkIn();
}
return ptr;
}
@ -103,11 +117,8 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
const word& patchFieldType
)
{
const bool caching = mesh.thisDb().cacheTemporaryObject(name);
return tmp<GeometricField<Type, PatchField, GeoMesh>>::NewImmovable
auto ptr = tmp<GeometricField<Type, PatchField, GeoMesh>>::New
(
caching, // (true: immovable, false: movable)
IOobject
(
name,
@ -115,13 +126,23 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
mesh.thisDb(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
caching // (true: REGISTER, false: NO_REGISTER)
IOobjectOption::NO_REGISTER
),
mesh,
dims,
std::move(iField),
patchFieldType
);
if
(
ptr->db().is_cacheTemporaryObject(ptr.get())
)
{
ptr.protect(true);
ptr->checkIn();
}
return ptr;
}
@ -136,11 +157,8 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
const word& patchFieldType
)
{
const bool caching = mesh.thisDb().cacheTemporaryObject(name);
return tmp<GeometricField<Type, PatchField, GeoMesh>>::NewImmovable
auto ptr = tmp<GeometricField<Type, PatchField, GeoMesh>>::New
(
caching, // (true: immovable, false: movable)
IOobject
(
name,
@ -148,13 +166,23 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
mesh.thisDb(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
caching // (true: REGISTER, false: NO_REGISTER)
IOobjectOption::NO_REGISTER
),
mesh,
value,
dims,
patchFieldType
);
if
(
ptr->db().is_cacheTemporaryObject(ptr.get())
)
{
ptr.protect(true);
ptr->checkIn();
}
return ptr;
}
@ -170,11 +198,8 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
const wordList& actualPatchTypes
)
{
const bool caching = mesh.thisDb().cacheTemporaryObject(name);
return tmp<GeometricField<Type, PatchField, GeoMesh>>::NewImmovable
auto ptr = tmp<GeometricField<Type, PatchField, GeoMesh>>::New
(
caching, // (true: immovable, false: movable)
IOobject
(
name,
@ -182,7 +207,7 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
mesh.thisDb(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
caching // (true: REGISTER, false: NO_REGISTER)
IOobjectOption::NO_REGISTER
),
mesh,
value,
@ -190,6 +215,16 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
patchFieldTypes,
actualPatchTypes
);
if
(
ptr->db().is_cacheTemporaryObject(ptr.get())
)
{
ptr.protect(true);
ptr->checkIn();
}
return ptr;
}
@ -246,11 +281,8 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
const word& patchFieldType
)
{
const bool caching = tgf().db().cacheTemporaryObject(name);
return tmp<GeometricField<Type, PatchField, GeoMesh>>::NewImmovable
auto ptr = tmp<GeometricField<Type, PatchField, GeoMesh>>::New
(
caching, // (true: immovable, false: movable)
IOobject
(
name,
@ -259,11 +291,21 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
tgf().db(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
caching // (true: REGISTER, false: NO_REGISTER)
IOobjectOption::NO_REGISTER
),
tgf,
patchFieldType
);
if
(
ptr->db().is_cacheTemporaryObject(ptr.get())
)
{
ptr.protect(true);
ptr->checkIn();
}
return ptr;
}
@ -275,11 +317,8 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
const tmp<GeometricField<Type, PatchField, GeoMesh>>& tgf
)
{
const bool caching = tgf().db().cacheTemporaryObject(name);
return tmp<GeometricField<Type, PatchField, GeoMesh>>::NewImmovable
auto ptr = tmp<GeometricField<Type, PatchField, GeoMesh>>::New
(
caching, // (true: immovable, false: movable)
IOobject
(
name,
@ -288,10 +327,20 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
tgf().db(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
caching // (true: REGISTER, false: NO_REGISTER)
IOobjectOption::NO_REGISTER
),
tgf
);
if
(
ptr->db().is_cacheTemporaryObject(ptr.get())
)
{
ptr.protect(true);
ptr->checkIn();
}
return ptr;
}
@ -305,11 +354,8 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
const wordList& actualPatchTypes
)
{
const bool caching = tgf().db().cacheTemporaryObject(name);
return tmp<GeometricField<Type, PatchField, GeoMesh>>::NewImmovable
auto ptr = tmp<GeometricField<Type, PatchField, GeoMesh>>::New
(
caching, // (true: immovable, false: movable)
IOobject
(
name,
@ -318,12 +364,22 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
tgf().db(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
caching // (true: REGISTER, false: NO_REGISTER)
IOobjectOption::NO_REGISTER
),
tgf,
patchFieldTypes,
actualPatchTypes
);
if
(
ptr->db().is_cacheTemporaryObject(ptr.get())
)
{
ptr.protect(true);
ptr->checkIn();
}
return ptr;
}
@ -338,11 +394,8 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
const word& patchFieldType
)
{
const bool caching = fld.db().cacheTemporaryObject(name);
return tmp<GeometricField<Type, PatchField, GeoMesh>>::NewImmovable
auto ptr = tmp<GeometricField<Type, PatchField, GeoMesh>>::New
(
caching, // (true: immovable, false: movable)
IOobject
(
name,
@ -350,12 +403,22 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
fld.db(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
caching // (true: REGISTER, false: NO_REGISTER)
IOobjectOption::NO_REGISTER
),
fld.mesh(),
dims,
patchFieldType
);
if
(
ptr->db().is_cacheTemporaryObject(ptr.get())
)
{
ptr.protect(true);
ptr->checkIn();
}
return ptr;
}
@ -370,11 +433,8 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
const word& patchFieldType
)
{
const bool caching = fld.db().cacheTemporaryObject(name);
return tmp<GeometricField<Type, PatchField, GeoMesh>>::NewImmovable
auto ptr = tmp<GeometricField<Type, PatchField, GeoMesh>>::New
(
caching, // (true: immovable, false: movable)
IOobject
(
name,
@ -382,13 +442,23 @@ Foam::GeometricField<Type, PatchField, GeoMesh>::New
fld.db(),
IOobjectOption::NO_READ,
IOobjectOption::NO_WRITE,
caching // (true: REGISTER, false: NO_REGISTER)
IOobjectOption::NO_REGISTER
),
fld.mesh(),
dt.value(),
dt.dimensions(),
patchFieldType
);
if
(
ptr->db().is_cacheTemporaryObject(ptr.get())
)
{
ptr.protect(true);
ptr->checkIn();
}
return ptr;
}

View File

@ -120,10 +120,6 @@ public:
//- Construct, taking ownership of the pointer.
inline explicit tmp(T* p);
//- Construct, taking ownership of the pointer,
//- with specified protection against moving.
inline tmp(bool immovable, T* p);
//- Implicit construct for a const reference to an object.
inline constexpr tmp(const T& obj) noexcept;
@ -173,19 +169,6 @@ public:
return tmp<T>(new T(std::forward<Args>(args)...));
}
//- Construct movable/immovable tmp with forwarding arguments
// \param immovable create as non-movable pointer
// \param args list of arguments with which an instance of T
// will be constructed.
//
// \note Similar to std::make_shared, but the overload for
// array types is not disabled.
template<class... Args>
static tmp<T> NewImmovable(bool immovable, Args&&... args)
{
return tmp<T>(immovable, new T(std::forward<Args>(args)...));
}
//- Construct tmp from derived type with forwarding arguments
// \param args list of arguments with which an instance of U
// will be constructed.
@ -256,16 +239,16 @@ public:
//- delete object and set pointer to nullptr
inline void clear() const noexcept;
//- Use specified protection against moving for the managed pointer.
//- No-op for references.
inline void protect(bool on) noexcept;
//- Clear existing and transfer ownership.
inline void reset(tmp<T>&& other) noexcept;
//- Delete managed temporary object and set to new given pointer.
inline void reset(T* p = nullptr) noexcept;
//- Delete managed temporary object and set to new given pointer,
//- with specified protection against moving.
inline void reset(bool immovable, T* p) noexcept;
//- Avoid inadvertent casting (to object or pointer)
void reset(const autoPtr<T>&) = delete;

View File

@ -90,15 +90,6 @@ inline Foam::tmp<T>::tmp(T* p)
}
template<class T>
inline Foam::tmp<T>::tmp(bool immovable, T* p)
:
tmp<T>(p)
{
if (ptr_ && immovable) type_ = CACHE_PTR;
}
template<class T>
inline constexpr Foam::tmp<T>::tmp(const T& obj) noexcept
:
@ -319,6 +310,28 @@ inline void Foam::tmp<T>::clear() const noexcept
}
template<class T>
inline void Foam::tmp<T>::protect(bool on) noexcept
{
if (on)
{
// ON: from PTR -> CACHE_PTR, but not nullptr
if (ptr_ && type_ == PTR)
{
type_ = CACHE_PTR;
}
}
else
{
// OFF: from CACHE_PTR -> PTR
if (type_ == CACHE_PTR)
{
type_ = PTR;
}
}
}
template<class T>
inline void Foam::tmp<T>::reset(T* p) noexcept
{
@ -328,14 +341,6 @@ inline void Foam::tmp<T>::reset(T* p) noexcept
}
template<class T>
inline void Foam::tmp<T>::reset(bool immovable, T* p) noexcept
{
reset(p);
if (ptr_ && immovable) type_ = CACHE_PTR;
}
template<class T>
inline void Foam::tmp<T>::reset(tmp<T>&& other) noexcept
{

View File

@ -222,13 +222,14 @@ bool Foam::functionObjects::writeObjects::write()
// TBD:
// If the object is a temporary field expression wrap with tmp<...>
// if (obj.db().cacheTemporaryObject(objName))
// if (obj.db().is_cacheTemporaryObject(obj))
// {
// obj.IOobject::rename("tmp<" + objName + ">");
// const word oldName(obj.name());
// obj.IOobject::rename("tmp<" + oldName + ">");
//
// Log << " writing object " << obj.name() << endl;
// obj.write();
// obj.IOobject::rename(objName);
// obj.IOobject::rename(oldName);
// }
// else
{