ENH: regIOobject store() now also registers the object

- previously the store() method just set the ownedByRegistry flag.
  Now ensure that it is indeed registered first.

- support register/store of tmp<> items.
  The tmp parameter is not cleared, but changed from PTR to CREF
  to allow further use.

  The implicit registration allows code simplification using the
  GeometricField::New factory method, for example.

  Old Code
  ========
      volScalarField* ptr = new volScalarField
      (
          IOobject
          (
              fieldName,
              mesh.time().timeName(),
              mesh,
              IOobject::NO_READ,
              IOobject::NO_WRITE,
              true  // Register
          ),
          mesh,
          dimless,
          zeroGradientFvPatchField<scalar>::typeName
      );
      ptr->store();

  New Code
  ========
      auto tptr = volScalarField::New
      (
          fieldName,
          mesh,
          dimless,
          zeroGradientFvPatchField<scalar>::typeName
      );
      regIOobject::store(tptr);

  or even
      regIOobject::store
      (
          volScalarField::New
          (
              fieldName,
              mesh,
              dimless,
              zeroGradientFvPatchField<scalar>::typeName
          )
      );
This commit is contained in:
Mark Olesen 2020-01-09 12:27:45 +01:00 committed by Mattijs Janssens
parent 12bd45673a
commit b0072bfab8
4 changed files with 117 additions and 34 deletions

View File

@ -109,6 +109,7 @@ Foam::regIOobject::regIOobject(const regIOobject& rio, bool registerCopy)
{ {
if (rio.registered_) if (rio.registered_)
{ {
// Unregister the original object
const_cast<regIOobject&>(rio).checkOut(); const_cast<regIOobject&>(rio).checkOut();
} }
checkIn(); checkIn();
@ -132,6 +133,9 @@ Foam::regIOobject::regIOobject
{ {
if (registerCopy) if (registerCopy)
{ {
// NOTE: could also unregister the original object
// if (rio.registered_ && newName == rio.name()) ...
checkIn(); checkIn();
} }
} }

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -88,7 +88,7 @@ protected:
private: private:
// Private data // Private Data
//- Is this object registered with the registry //- Is this object registered with the registry
bool registered_; bool registered_;
@ -117,8 +117,8 @@ private:
public: public:
//- Declare friendship with any classes that need access to //- Declare friendship with classes that need access to
// masterOnlyReading //- masterOnlyReading
friend class functionEntries::codeStream; friend class functionEntries::codeStream;
friend class fileOperations::uncollatedFileOperation; friend class fileOperations::uncollatedFileOperation;
friend class fileOperations::masterUncollatedFileOperation; friend class fileOperations::masterUncollatedFileOperation;
@ -164,7 +164,8 @@ public:
// Registration // Registration
//- Add object to registry, if not already registered //- Add object to registry, if not already registered
// \return true if object was newly registered // \return true if object was already registered,
// or was newly registered
bool checkIn(); bool checkIn();
//- Remove all file watches and remove object from registry //- Remove all file watches and remove object from registry
@ -177,30 +178,53 @@ public:
//- Is this object owned by the registry? //- Is this object owned by the registry?
inline bool ownedByRegistry() const; inline bool ownedByRegistry() const;
//- Transfer ownership of this object to its registry //- Register object with its registry
inline void store(); //- and transfer ownership to the registry.
// \return true if now ownedByRegistry
inline bool store();
//- Transfer ownership of the given object pointer to its registry //- Register object pointer with its registry
//- and transfer ownership to the registry.
// \return reference to the object. // \return reference to the object.
template<class Type> template<class Type>
inline static Type& store(Type* p); inline static Type& store(Type* p);
//- Transfer ownership of the given object pointer to its registry //- Register object pointer with its registry
//- and transfer ownership to the registry.
// Clears the autoPtr parameter.
// \return reference to the object. // \return reference to the object.
template<class Type> template<class Type>
inline static Type& store(autoPtr<Type>& aptr); inline static Type& store(autoPtr<Type>& aptr);
//- Transfer ownership of the given object pointer to its registry //- Register object pointer with its registry
//- and transfer ownership to the registry.
// Clears the autoPtr parameter.
// \return reference to the object. // \return reference to the object.
template<class Type> template<class Type>
inline static Type& store(autoPtr<Type>&& aptr); inline static Type& store(autoPtr<Type>&& aptr);
//- Register temporary pointer with its registry
//- and transfer ownership of pointer to the registry.
// After the call, tmp parameter changes from PTR to CREF.
//
// \return reference to the object.
template<class Type>
inline static Type& store(tmp<Type>& tptr);
//- Register temporary pointer with its registry
//- and transfer ownership of pointer to the registry.
// After the call, the tmp parameter content is \em unspecified.
//
// \return reference to the object.
template<class Type>
inline static Type& store(tmp<Type>&& tptr);
//- Release ownership of this object from its registry //- Release ownership of this object from its registry
// \param unregister optionally set as non-registered // \param unregister optionally set as non-registered
inline void release(const bool unregister = false); inline void release(const bool unregister = false);
// Dependency checking // Dependency Checking
//- Event number at last update. //- Event number at last update.
inline label eventNo() const; inline label eventNo() const;
@ -271,8 +295,8 @@ public:
//- Read object //- Read object
virtual bool read(); virtual bool read();
//- Add file watch for fileName on object if not yet watched. Return //- Add file watch for fileName on object if not yet watched.
// index of watch // \return index of watch
virtual label addWatch(const fileName&); virtual label addWatch(const fileName&);
//- Return file-monitoring handles //- Return file-monitoring handles
@ -282,7 +306,7 @@ public:
inline labelList& watchIndices(); inline labelList& watchIndices();
//- Return true if the object's file (or files for objectRegistry) //- Return true if the object's file (or files for objectRegistry)
// have been modified. (modified state is cached by Time) //- have been modified. (modified state is cached by Time)
virtual bool modified() const; virtual bool modified() const;
//- Read object if modified (as set by call to modified) //- Read object if modified (as set by call to modified)
@ -317,9 +341,10 @@ public:
} }
// Member operators // Member Operators
void operator=(const IOobject&); //- Copy assignment
void operator=(const IOobject& io);
}; };

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -34,9 +34,19 @@ inline bool Foam::regIOobject::ownedByRegistry() const
} }
inline void Foam::regIOobject::store() inline bool Foam::regIOobject::store()
{ {
ownedByRegistry_ = true; if (checkIn())
{
ownedByRegistry_ = true;
}
else
{
WarningInFunction
<< "Refuse to store unregistered object: " << this->name() << nl;
}
return ownedByRegistry_;
} }
@ -50,7 +60,15 @@ inline Type& Foam::regIOobject::store(Type* p)
<< abort(FatalError); << abort(FatalError);
} }
p->regIOobject::ownedByRegistry_ = true; const bool ok = p->regIOobject::store();
if (!ok)
{
FatalErrorInFunction
<< "Failed to store pointer: " << p->regIOobject::name()
<< ". Risk of memory leakage\n"
<< abort(FatalError);
}
return *p; return *p;
} }
@ -59,14 +77,57 @@ inline Type& Foam::regIOobject::store(Type* p)
template<class Type> template<class Type>
inline Type& Foam::regIOobject::store(autoPtr<Type>& aptr) inline Type& Foam::regIOobject::store(autoPtr<Type>& aptr)
{ {
return store(aptr.ptr()); // release, pass management to regIOobject // Pass management to objectRegistry
return store(aptr.release());
} }
template<class Type> template<class Type>
inline Type& Foam::regIOobject::store(autoPtr<Type>&& aptr) inline Type& Foam::regIOobject::store(autoPtr<Type>&& aptr)
{ {
return store(aptr.ptr()); // release, pass management to regIOobject // Pass management to objectRegistry
return store(aptr.release());
}
template<class Type>
inline Type& Foam::regIOobject::store(tmp<Type>& tptr)
{
Type* p = nullptr;
if (tptr.isTmp())
{
// Pass management to objectRegistry
p = tptr.ptr();
store(p);
// Adjust tmp<> access to use the stored reference
tptr.cref(*p);
}
else
{
// Taking ownership of a const-ref does not make much sense.
// - Storing the object won't actually do so, it will be removed
// when the original object goes out of scope.
// - Storing a clone may not be what we want.
p = tptr.get();
WarningInFunction
<< "Refuse to store tmp to const-ref: " << p->name()
<< ". Likely indicates a coding error\n";
}
return *p;
}
template<class Type>
inline Type& Foam::regIOobject::store(tmp<Type>&& tptr)
{
// Treat like a normal reference
return store(tptr);
} }

View File

@ -34,24 +34,17 @@ cellZoneID
{ {
if (!volZonePtr) if (!volZonePtr)
{ {
volZonePtr = new volScalarField volZonePtr = &regIOobject::store
( (
IOobject volScalarField::New
( (
fieldName, fieldName,
mesh.time().timeName(),
mesh, mesh,
IOobject::NO_READ, dimless,
IOobject::NO_WRITE, zeroGradientFvPatchField<scalar>::typeName
true // Register )
),
mesh,
dimless,
zeroGradientFvPatchField<scalar>::typeName
); );
volZonePtr->store();
Info<< "Creating " << fieldName Info<< "Creating " << fieldName
<< " field for postProcessing" << nl; << " field for postProcessing" << nl;
} }