From 6a1efe3b5cd566f22e6712436239d3634474fa35 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 11 Aug 2020 11:41:13 +0200 Subject: [PATCH] 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 --- applications/test/fft/Allwmake | 15 +++++ applications/test/fft/Make/files | 3 + applications/test/fft/Make/options | 5 ++ applications/test/fft/Test-fft.C | 75 +++++++++++++++++++++ applications/test/refPtr/Make/files | 3 + applications/test/refPtr/Make/options | 2 + applications/test/refPtr/Test-refPtr.C | 93 ++++++++++++++++++++++++++ applications/test/tmp/Test-tmp.C | 51 +++++--------- src/OpenFOAM/memory/refPtr/refPtr.H | 15 ++++- src/OpenFOAM/memory/refPtr/refPtrI.H | 14 ++++ 10 files changed, 241 insertions(+), 35 deletions(-) create mode 100755 applications/test/fft/Allwmake create mode 100644 applications/test/fft/Make/files create mode 100644 applications/test/fft/Make/options create mode 100644 applications/test/fft/Test-fft.C create mode 100644 applications/test/refPtr/Make/files create mode 100644 applications/test/refPtr/Make/options create mode 100644 applications/test/refPtr/Test-refPtr.C diff --git a/applications/test/fft/Allwmake b/applications/test/fft/Allwmake new file mode 100755 index 0000000000..49eb86c685 --- /dev/null +++ b/applications/test/fft/Allwmake @@ -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 + +#------------------------------------------------------------------------------ diff --git a/applications/test/fft/Make/files b/applications/test/fft/Make/files new file mode 100644 index 0000000000..4ba0d4c86f --- /dev/null +++ b/applications/test/fft/Make/files @@ -0,0 +1,3 @@ +Test-fft.C + +EXE = $(FOAM_USER_APPBIN)/Test-fft diff --git a/applications/test/fft/Make/options b/applications/test/fft/Make/options new file mode 100644 index 0000000000..024ac5b486 --- /dev/null +++ b/applications/test/fft/Make/options @@ -0,0 +1,5 @@ +EXE_INC = \ + -I$(LIB_SRC)/randomProcesses/lnInclude + +EXE_LIBS = \ + -lrandomProcesses diff --git a/applications/test/fft/Test-fft.C b/applications/test/fft/Test-fft.C new file mode 100644 index 0000000000..0f5a43ef8a --- /dev/null +++ b/applications/test/fft/Test-fft.C @@ -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 forward1D(const tmp& input) +{ + const label len = input().size(); + + return fft::forwardTransform(input, List({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 toutput = forward1D(input); + + Info<< nl + << "input = " << input << nl + << "output = " << toutput << nl; + } + + { + complexField input(8, Zero); + input[1] = 1; + + tmp toutput = forward1D(input); + + Info<< nl + << "input = " << input << nl + << "output = " << toutput << nl; + } + + Info<< nl << "End\n" << endl; + + return 0; +} + + +// ************************************************************************* // diff --git a/applications/test/refPtr/Make/files b/applications/test/refPtr/Make/files new file mode 100644 index 0000000000..1d4637bee8 --- /dev/null +++ b/applications/test/refPtr/Make/files @@ -0,0 +1,3 @@ +Test-refPtr.C + +EXE = $(FOAM_USER_APPBIN)/Test-refPtr diff --git a/applications/test/refPtr/Make/options b/applications/test/refPtr/Make/options new file mode 100644 index 0000000000..18e6fe47af --- /dev/null +++ b/applications/test/refPtr/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = */ +/* EXE_LIBS = */ diff --git a/applications/test/refPtr/Test-refPtr.C b/applications/test/refPtr/Test-refPtr.C new file mode 100644 index 0000000000..e60f676e76 --- /dev/null +++ b/applications/test/refPtr/Test-refPtr.C @@ -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 +void printInfo(const refPtr& 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(f2), true); + } + + { + Info<< nl << "Construct from New (is_pointer)" << nl; + auto tfld1 = refPtr::New(10, scalar(1)); + printInfo(tfld1, true); + + + Info<< nl << "Construct from autoPtr" << nl; + refPtr tfld2(autoPtr::New(10, scalar(2))); + printInfo(tfld2, true); + + + Info<< nl << "Construct from unique_ptr" << nl; + std::unique_ptr ptr(new scalarField(10, scalar(3))); + refPtr tfld3(std::move(ptr)); + printInfo(tfld3, true); + + + Info<< nl << "Reset from autoPtr" << nl; + tfld2.reset(autoPtr::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; +} + + +// ************************************************************************* // diff --git a/applications/test/tmp/Test-tmp.C b/applications/test/tmp/Test-tmp.C index 88b081630a..da0486af23 100644 --- a/applications/test/tmp/Test-tmp.C +++ b/applications/test/tmp/Test-tmp.C @@ -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 . + 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 -void printInfo(const tmp& tmpItem) +void printInfo(const tmp& 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::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 tfld2 = tmp::NewFrom(20, Zero); - printInfo(tfld2); - if (tfld2.valid()) - { - Info<< "tmp: " << tfld2() << nl; - } + printInfo(tfld2, true); tfld2.clear(); diff --git a/src/OpenFOAM/memory/refPtr/refPtr.H b/src/OpenFOAM/memory/refPtr/refPtr.H index 905ba67f6c..700b72e746 100644 --- a/src/OpenFOAM/memory/refPtr/refPtr.H +++ b/src/OpenFOAM/memory/refPtr/refPtr.H @@ -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&& ptr) noexcept; + + //- Move construct from unique_ptr, transferring ownership. + inline explicit refPtr(std::unique_ptr&& 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&& other) noexcept { reset(other.release()); } + + //- Clear existing and transfer ownership from unique_ptr + void reset(std::unique_ptr&& other) { reset(other.release()); } + //- Clear existing and transfer ownership. inline void reset(refPtr&& 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); + inline void operator=(const refPtr& other); //- Clear existing and transfer ownership. inline void operator=(refPtr&& other) noexcept; diff --git a/src/OpenFOAM/memory/refPtr/refPtrI.H b/src/OpenFOAM/memory/refPtr/refPtrI.H index c20a13a276..e9e74da3b5 100644 --- a/src/OpenFOAM/memory/refPtr/refPtrI.H +++ b/src/OpenFOAM/memory/refPtr/refPtrI.H @@ -80,6 +80,20 @@ inline Foam::refPtr::refPtr(T* p) noexcept {} +template +inline Foam::refPtr::refPtr(autoPtr&& rhs) noexcept +: + refPtr(rhs.release()) +{} + + +template +inline Foam::refPtr::refPtr(std::unique_ptr&& rhs) noexcept +: + refPtr(rhs.release()) +{} + + template inline Foam::refPtr::refPtr(const T& obj) noexcept :