From e56e195ab544184568c9bacd4cf7587e7cadab0b Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 30 Jul 2020 09:45:55 +0200 Subject: [PATCH] ENH: support objectRegistry store of refPtr (as per tmp) --- src/OpenFOAM/db/regIOobject/regIOobject.H | 64 ++++++++++--------- src/OpenFOAM/db/regIOobject/regIOobjectI.H | 73 +++++++++++++++++----- 2 files changed, 92 insertions(+), 45 deletions(-) diff --git a/src/OpenFOAM/db/regIOobject/regIOobject.H b/src/OpenFOAM/db/regIOobject/regIOobject.H index 6ac4db7c28..d1ed827872 100644 --- a/src/OpenFOAM/db/regIOobject/regIOobject.H +++ b/src/OpenFOAM/db/regIOobject/regIOobject.H @@ -42,6 +42,8 @@ SourceFiles #define regIOobject_H #include "IOobject.H" +#include "refPtr.H" +#include "tmp.H" #include "typeInfo.H" #include "stdFoam.H" #include "OSspecific.H" @@ -52,6 +54,8 @@ SourceFiles namespace Foam { +// Forward Declarations + namespace functionEntries { class codeStream; @@ -59,7 +63,6 @@ namespace functionEntries namespace fileOperations { class uncollatedFileOperation; - class masterUncollatedFileOperation; } /*---------------------------------------------------------------------------*\ @@ -118,11 +121,9 @@ private: public: - //- Declare friendship with classes that need access to - //- masterOnlyReading + //- Friendship with classes needing access to masterOnlyReading friend class functionEntries::codeStream; friend class fileOperations::uncollatedFileOperation; - friend class fileOperations::masterUncollatedFileOperation; // Static data @@ -184,41 +185,46 @@ public: // \return true if now ownedByRegistry inline bool store(); - //- Register object pointer with its registry - //- and transfer ownership to the registry. - // \return reference to the object. + //- Transfer pointer ownership to its registry. + // \return reference to the stored object template inline static Type& store(Type* p); - //- Register object pointer with its registry - //- and transfer ownership to the registry. - // Clears the autoPtr parameter. - // \return reference to the object. + //- Transfer pointer ownership to its registry. + // Resets (clears) the parameter. + // \return reference to the stored object template - inline static Type& store(autoPtr& aptr); + inline static Type& store(autoPtr& ptr); - //- Register object pointer with its registry - //- and transfer ownership to the registry. - // Clears the autoPtr parameter. - // \return reference to the object. + //- Transfer pointer ownership to its registry. + // Resets (clears) the parameter. + // \return reference to the stored object + template + inline static Type& store(autoPtr&& ptr); + + //- Transfer pointer ownership to its registry. + // Changes parameter from PTR to CREF (do not rely on this). + // \return reference to the stored object + template + inline static Type& store(refPtr& ptr); + + //- Transfer pointer ownership to its registry. + // Changes parameter from PTR to CREF (do not rely on this). + // \return reference to the stored object template - inline static Type& store(autoPtr&& aptr); + inline static Type& store(refPtr&& ptr); - //- 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. + //- Transfer pointer ownership to its registry. + // Changes parameter from PTR to CREF (do not rely on this). + // \return reference to the stored object template - inline static Type& store(tmp& tptr); + inline static Type& store(tmp& ptr); - //- 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. + //- Transfer pointer ownership to its registry. + // Changes parameter from PTR to CREF (do not rely on this). + // \return reference to the stored object template - inline static Type& store(tmp&& tptr); + inline static Type& store(tmp&& ptr); //- Release ownership of this object from its registry // \param unregister optionally set as non-registered diff --git a/src/OpenFOAM/db/regIOobject/regIOobjectI.H b/src/OpenFOAM/db/regIOobject/regIOobjectI.H index 07623d9368..aa490b75ce 100644 --- a/src/OpenFOAM/db/regIOobject/regIOobjectI.H +++ b/src/OpenFOAM/db/regIOobject/regIOobjectI.H @@ -75,47 +75,47 @@ inline Type& Foam::regIOobject::store(Type* p) template -inline Type& Foam::regIOobject::store(autoPtr& aptr) +inline Type& Foam::regIOobject::store(autoPtr& ptr) { // Pass management to objectRegistry - return store(aptr.release()); + return store(ptr.release()); } template -inline Type& Foam::regIOobject::store(autoPtr&& aptr) +inline Type& Foam::regIOobject::store(autoPtr&& ptr) { // Pass management to objectRegistry - return store(aptr.release()); + return store(ptr.release()); } template -inline Type& Foam::regIOobject::store(tmp& tptr) +inline Type& Foam::regIOobject::store(refPtr& ptr) { Type* p = nullptr; - if (tptr.isTmp()) + if (ptr.is_pointer()) { - // Pass management to objectRegistry - p = tptr.ptr(); + // Acquire ownership, pass management to objectRegistry + p = ptr.ptr(); store(p); - // Adjust tmp<> access to use the stored reference - tptr.cref(*p); + // Change parameter to access the stored reference + ptr.cref(*p); } else { - // Taking ownership of a const-ref does not make much sense. + // Taking ownership of reference 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(); + p = ptr.get(); WarningInFunction - << "Refuse to store tmp to const-ref: " << p->name() + << "Refuse to store reference: " << p->name() << ". Likely indicates a coding error\n"; } @@ -124,10 +124,51 @@ inline Type& Foam::regIOobject::store(tmp& tptr) template -inline Type& Foam::regIOobject::store(tmp&& tptr) +inline Type& Foam::regIOobject::store(refPtr&& ptr) { - // Treat like a normal reference - return store(tptr); + // Forward as named reference, which also does a move + return store(ptr); +} + + +template +inline Type& Foam::regIOobject::store(tmp& ptr) +{ + Type* p = nullptr; + + if (ptr.is_pointer()) + { + // Acquire ownership, pass management to objectRegistry + p = ptr.ptr(); + + store(p); + + // Change parameter to access the stored reference + ptr.cref(*p); + } + else + { + // Taking ownership of reference 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 = ptr.get(); + + WarningInFunction + << "Refuse to store reference: " << p->name() + << ". Likely indicates a coding error\n"; + } + + return *p; +} + + +template +inline Type& Foam::regIOobject::store(tmp&& ptr) +{ + // Forward as named reference, which also does a move + return store(ptr); } -- GitLab