Commit e3e0d7c8 authored by Mark Olesen's avatar Mark Olesen Committed by Andrew Heather
Browse files

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).
parent de673f3b
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| 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:
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)
{
f1 = f2 + f3 + f2 + f3;
......@@ -60,30 +76,33 @@ int main()
{
auto tfld1 = tmp<scalarField>::New(20, Zero);
Info<< "tmp refCount = " << tfld1->count() << nl;
printInfo(tfld1);
if (tfld1.valid())
{
Info<<"tmp: " << tfld1() << nl;
}
Info<<"tmp addr: " << long(tfld1.get()) << nl;
// Hold on to the old content for a bit
tmp<scalarField> tfld2 =
tmp<scalarField>::NewFrom<myScalarField>(20, Zero);
Info<< "tmp refCount = " << tfld2->count() << nl;
printInfo(tfld2);
if (tfld2.valid())
{
Info<<"tmp: " << tfld2() << nl;
}
Info<<"tmp addr: " << long(tfld2.get()) << nl;
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;
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -217,6 +217,9 @@ public:
//- Delete managed temporary object and set to new given pointer
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.
inline void swap(tmp<T>& other) noexcept;
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| 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>
inline void Foam::tmp<T>::swap(tmp<T>& other) noexcept
{
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2016 OpenFOAM Foundation
......@@ -198,6 +198,9 @@ public:
//- Delete managed temporary object and set to new given pointer
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.
inline void swap(tmpNrc<T>& other) noexcept;
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| 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>
inline void Foam::tmpNrc<T>::swap(tmpNrc<T>& other) noexcept
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment