ENH: add possibility to change const reference in tmp.

- previously it was only possible to reset a pointer, but not to
  change a const-reference directly (needed a swap() to do this).
This commit is contained in:
Mark Olesen
2019-02-11 18:23:06 +01:00
committed by Andrew Heather
parent de673f3bcf
commit e3e0d7c8b9
5 changed files with 57 additions and 14 deletions

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011 OpenFOAM Foundation | Copyright (C) 2011 OpenFOAM Foundation
@ -41,14 +41,30 @@ struct myScalarField : public scalarField
}; };
template<class T>
void printInfo(const tmp<T>& tmpItem)
{
Info<< "tmp valid:" << tmpItem.valid()
<< " isTmp:" << tmpItem.isTmp()
<< " addr: " << long(tmpItem.get());
if (tmpItem.valid())
{
Info<< " refCount:" << tmpItem->count();
}
Info<< nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program: // Main program:
int main() int main()
{ {
{
scalarField f1(1000000, 1.0), f2(1000000, 2.0), f3(1000000, 3.0); scalarField f1(1000000, 1.0), f2(1000000, 2.0), f3(1000000, 3.0);
{
for (int iter=0; iter < 50; ++iter) for (int iter=0; iter < 50; ++iter)
{ {
f1 = f2 + f3 + f2 + f3; f1 = f2 + f3 + f2 + f3;
@ -60,30 +76,33 @@ int main()
{ {
auto tfld1 = tmp<scalarField>::New(20, Zero); auto tfld1 = tmp<scalarField>::New(20, Zero);
Info<< "tmp refCount = " << tfld1->count() << nl; printInfo(tfld1);
if (tfld1.valid()) if (tfld1.valid())
{ {
Info<<"tmp: " << tfld1() << nl; Info<<"tmp: " << tfld1() << nl;
} }
Info<<"tmp addr: " << long(tfld1.get()) << nl;
// Hold on to the old content for a bit // Hold on to the old content for a bit
tmp<scalarField> tfld2 = tmp<scalarField> tfld2 =
tmp<scalarField>::NewFrom<myScalarField>(20, Zero); tmp<scalarField>::NewFrom<myScalarField>(20, Zero);
Info<< "tmp refCount = " << tfld2->count() << nl; printInfo(tfld2);
if (tfld2.valid()) if (tfld2.valid())
{ {
Info<<"tmp: " << tfld2() << nl; Info<<"tmp: " << tfld2() << nl;
} }
Info<<"tmp addr: " << long(tfld2.get()) << nl;
tfld2.clear(); tfld2.clear();
Info<<"after clear: " << long(tfld2.get()) << nl; Info<<"After clear : ";
printInfo(tfld2);
tfld2.cref(f1);
Info<<"Reset const-ref : ";
printInfo(tfld2);
} }
Info<< "\nEnd" << endl; Info<< "\nEnd" << endl;

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation | Copyright (C) 2011-2016 OpenFOAM Foundation
@ -217,6 +217,9 @@ public:
//- Delete managed temporary object and set to new given pointer //- Delete managed temporary object and set to new given pointer
inline void reset(T* p) noexcept; inline void reset(T* p) noexcept;
//- Delete managed temporary object and set to const reference
inline void cref(const T& obj) noexcept;
//- Swaps the managed object with other tmp. //- Swaps the managed object with other tmp.
inline void swap(tmp<T>& other) noexcept; inline void swap(tmp<T>& other) noexcept;

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2011-2017 OpenFOAM Foundation | Copyright (C) 2011-2017 OpenFOAM Foundation
@ -356,6 +356,15 @@ inline void Foam::tmp<T>::reset(T* p) noexcept
} }
template<class T>
inline void Foam::tmp<T>::cref(const T& obj) noexcept
{
clear();
ptr_ = const_cast<T*>(&obj);
type_ = CREF;
}
template<class T> template<class T>
inline void Foam::tmp<T>::swap(tmp<T>& other) noexcept inline void Foam::tmp<T>::swap(tmp<T>& other) noexcept
{ {

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2016 OpenFOAM Foundation | Copyright (C) 2016 OpenFOAM Foundation
@ -198,6 +198,9 @@ public:
//- Delete managed temporary object and set to new given pointer //- Delete managed temporary object and set to new given pointer
inline void reset(T* p) noexcept; inline void reset(T* p) noexcept;
//- Delete managed temporary object and set to const reference
inline void cref(const T& obj) noexcept;
//- Swaps the managed object with other tmpNrc. //- Swaps the managed object with other tmpNrc.
inline void swap(tmpNrc<T>& other) noexcept; inline void swap(tmpNrc<T>& other) noexcept;

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
| Copyright (C) 2016-2017 OpenFOAM Foundation | Copyright (C) 2016-2017 OpenFOAM Foundation
@ -306,6 +306,15 @@ inline void Foam::tmpNrc<T>::reset(T* p) noexcept
} }
template<class T>
inline void Foam::tmpNrc<T>::cref(const T& obj) noexcept
{
clear();
ptr_ = const_cast<T*>(&obj);
type_ = CREF;
}
template<class T> template<class T>
inline void Foam::tmpNrc<T>::swap(tmpNrc<T>& other) noexcept inline void Foam::tmpNrc<T>::swap(tmpNrc<T>& other) noexcept
{ {