ENH: support construct/reset refPtr from autoPtr and unique_ptr (#1775)

- makes it easier to use in combination with various 'New' selectors,
  which mostly return an autoPtr.

ENH: add very simple FFT test

- basic sanity test that the library links properly
This commit is contained in:
Mark Olesen 2020-08-11 11:41:13 +02:00
parent e94fdee58d
commit 6a1efe3b5c
10 changed files with 241 additions and 35 deletions

15
applications/test/fft/Allwmake Executable file
View File

@ -0,0 +1,15 @@
#!/bin/sh
cd "${0%/*}" || exit # Run from this directory
. ${WM_PROJECT_DIR:?}/wmake/scripts/AllwmakeParseArguments # (error catching)
. ${WM_PROJECT_DIR:?}/wmake/scripts/have_fftw
#------------------------------------------------------------------------------
if have_fftw
then
wmake $targetType
else
echo "==> skip test (no FFTW)"
fi
#------------------------------------------------------------------------------

View File

@ -0,0 +1,3 @@
Test-fft.C
EXE = $(FOAM_USER_APPBIN)/Test-fft

View File

@ -0,0 +1,5 @@
EXE_INC = \
-I$(LIB_SRC)/randomProcesses/lnInclude
EXE_LIBS = \
-lrandomProcesses

View File

@ -0,0 +1,75 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
Application
Test-fft
Description
Very simple fft tests
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "Time.H"
#include "fft.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
using namespace Foam;
// Simple forward transform
tmp<complexField> forward1D(const tmp<complexField>& input)
{
const label len = input().size();
return fft::forwardTransform(input, List<int>({len})) / len;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
argList::noParallel();
#include "setRootCase.H"
// Simple ones - http://www.sccon.ca/sccon/fft/fft3.htm
{
complexField input(8, Zero);
input[0] = 1;
tmp<complexField> toutput = forward1D(input);
Info<< nl
<< "input = " << input << nl
<< "output = " << toutput << nl;
}
{
complexField input(8, Zero);
input[1] = 1;
tmp<complexField> toutput = forward1D(input);
Info<< nl
<< "input = " << input << nl
<< "output = " << toutput << nl;
}
Info<< nl << "End\n" << endl;
return 0;
}
// ************************************************************************* //

View File

@ -0,0 +1,3 @@
Test-refPtr.C
EXE = $(FOAM_USER_APPBIN)/Test-refPtr

View File

@ -0,0 +1,2 @@
/* EXE_INC = */
/* EXE_LIBS = */

View File

@ -0,0 +1,93 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
Application
Test-refPtr
Description
Tests some basic functionality of refPtr
\*---------------------------------------------------------------------------*/
#include "primitiveFields.H"
#include "Switch.H"
using namespace Foam;
struct myScalarField : public scalarField
{
using scalarField::scalarField;
};
template<class T>
void printInfo(const refPtr<T>& item, const bool verbose = false)
{
Info<< "refPtr valid:" << Switch::name(item.valid())
<< " pointer:" << Switch::name(item.is_pointer())
<< " addr: " << name(item.get())
<< " movable:" << Switch(item.movable());
Info<< nl;
if (verbose && item.valid())
{
Info<< "content: " << item() << nl;
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main()
{
{
Info<< nl << "Construct from reference" << nl;
scalarField f2(10, Foam::sqrt(2.0));
printInfo(refPtr<scalarField>(f2), true);
}
{
Info<< nl << "Construct from New (is_pointer)" << nl;
auto tfld1 = refPtr<scalarField>::New(10, scalar(1));
printInfo(tfld1, true);
Info<< nl << "Construct from autoPtr" << nl;
refPtr<scalarField> tfld2(autoPtr<scalarField>::New(10, scalar(2)));
printInfo(tfld2, true);
Info<< nl << "Construct from unique_ptr" << nl;
std::unique_ptr<scalarField> ptr(new scalarField(10, scalar(3)));
refPtr<scalarField> tfld3(std::move(ptr));
printInfo(tfld3, true);
Info<< nl << "Reset from autoPtr" << nl;
tfld2.reset(autoPtr<scalarField>::New(3, scalar(13)));
printInfo(tfld2, true);
Info<< nl << "Reset from unique_ptr" << nl;
ptr.reset(new scalarField(5, scalar(15)));
tfld3.reset(std::move(ptr));
printInfo(tfld3, true);
}
Info<< "\nEnd" << endl;
}
// ************************************************************************* //

View File

@ -5,23 +5,10 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2019 OpenCFD Ltd.
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
Application
Test-tmp
@ -32,6 +19,7 @@ Description
\*---------------------------------------------------------------------------*/
#include "primitiveFields.H"
#include "Switch.H"
using namespace Foam;
@ -42,18 +30,23 @@ struct myScalarField : public scalarField
template<class T>
void printInfo(const tmp<T>& tmpItem)
void printInfo(const tmp<T>& item, const bool verbose = false)
{
Info<< "tmp valid:" << tmpItem.valid()
<< " isTmp:" << tmpItem.isTmp()
<< " addr: " << name(tmpItem.get());
Info<< "tmp valid:" << Switch::name(item.valid())
<< " pointer:" << Switch::name(item.is_pointer())
<< " addr: " << name(item.get())
<< " movable:" << Switch(item.movable());
if (tmpItem.valid())
if (item.valid())
{
Info<< " refCount:" << tmpItem->count();
Info<< " refCount:" << item->count();
}
Info<< nl;
if (verbose && item.valid())
{
Info<< "content: " << item() << nl;
}
}
@ -75,24 +68,14 @@ int main()
{
auto tfld1 = tmp<scalarField>::New(20, Zero);
printInfo(tfld1);
if (tfld1.valid())
{
Info<< "tmp: " << tfld1() << nl;
}
printInfo(tfld1, true);
// Hold on to the old content for a bit
tmp<scalarField> tfld2 =
tmp<scalarField>::NewFrom<myScalarField>(20, Zero);
printInfo(tfld2);
if (tfld2.valid())
{
Info<< "tmp: " << tfld2() << nl;
}
printInfo(tfld2, true);
tfld2.clear();

View File

@ -42,6 +42,7 @@ See also
#ifndef refPtr_H
#define refPtr_H
#include "autoPtr.H"
#include "tmp.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -119,6 +120,12 @@ public:
//- Construct, taking ownership of the pointer.
inline explicit refPtr(T* p) noexcept;
//- Move construct from autoPtr, transferring ownership.
inline explicit refPtr(autoPtr<T>&& ptr) noexcept;
//- Move construct from unique_ptr, transferring ownership.
inline explicit refPtr(std::unique_ptr<T>&& ptr) noexcept;
//- Construct for a const reference to an object.
inline refPtr(const T& obj) noexcept;
@ -199,6 +206,12 @@ public:
//- Delete managed temporary object and set to new given pointer
inline void reset(T* p = nullptr) noexcept;
//- Clear existing and transfer ownership from autoPtr.
void reset(autoPtr<T>&& other) noexcept { reset(other.release()); }
//- Clear existing and transfer ownership from unique_ptr
void reset(std::unique_ptr<T>&& other) { reset(other.release()); }
//- Clear existing and transfer ownership.
inline void reset(refPtr<T>&& other) noexcept;
@ -233,7 +246,7 @@ public:
//- Transfer ownership of the managed pointer.
// Fatal for a null managed pointer or if the object is const.
inline void operator=(const refPtr<T>& t);
inline void operator=(const refPtr<T>& other);
//- Clear existing and transfer ownership.
inline void operator=(refPtr<T>&& other) noexcept;

View File

@ -80,6 +80,20 @@ inline Foam::refPtr<T>::refPtr(T* p) noexcept
{}
template<class T>
inline Foam::refPtr<T>::refPtr(autoPtr<T>&& rhs) noexcept
:
refPtr<T>(rhs.release())
{}
template<class T>
inline Foam::refPtr<T>::refPtr(std::unique_ptr<T>&& rhs) noexcept
:
refPtr<T>(rhs.release())
{}
template<class T>
inline Foam::refPtr<T>::refPtr(const T& obj) noexcept
: