ENH: improve autoPtr/refPtr/tmp consistency (#2571)

- disallow inadvertant casting and hidden copy constructions etc
This commit is contained in:
Mark Olesen
2022-09-05 13:39:01 +02:00
parent 052d8b13e3
commit c031f7d00a
11 changed files with 541 additions and 389 deletions

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020-2021 OpenCFD Ltd.
Copyright (C) 2020-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@ -19,6 +19,9 @@ Description
\*---------------------------------------------------------------------------*/
#include "primitiveFields.H"
#include "autoPtr.H"
#include "refPtr.H"
#include "tmp.H"
#include "Switch.H"
using namespace Foam;
@ -29,6 +32,36 @@ struct myScalarField : public scalarField
};
template<class T>
void constructInfo()
{
Info<< " move-constructible:"
<< std::is_move_constructible<T>::value
<< " move-assignable:"
<< std::is_move_assignable<T>::value
<< " nothrow:"
<< std::is_nothrow_move_assignable<T>::value
<< " trivially:"
<< std::is_trivially_move_assignable<T>::value
<< nl;
}
template<class T>
void printInfo(const autoPtr<T>& item, const bool verbose = false)
{
Info<< "autoPtr good:" << Switch::name(item.good())
<< " addr: " << Foam::name(item.get());
constructInfo<autoPtr<T>>();
if (verbose && item)
{
Info<< "content: " << item() << nl;
}
}
template<class T>
void printInfo(const refPtr<T>& item, const bool verbose = false)
{
@ -37,15 +70,24 @@ void printInfo(const refPtr<T>& item, const bool verbose = false)
<< " addr: " << Foam::name(item.get())
<< " movable:" << Switch(item.movable());
Info<< " move-constructible:"
<< std::is_move_constructible<refPtr<T>>::value
<< " move-assignable:"
<< std::is_move_assignable<refPtr<T>>::value
<< " nothrow:"
<< std::is_nothrow_move_assignable<refPtr<T>>::value
<< " trivially:"
<< std::is_trivially_move_assignable<refPtr<T>>::value
<< nl;
constructInfo<refPtr<T>>();
if (verbose && item)
{
Info<< "content: " << item() << nl;
}
}
template<class T>
void printInfo(const tmp<T>& item, const bool verbose = false)
{
Info<< "tmp good:" << Switch::name(item.good())
<< " pointer:" << Switch::name(item.is_pointer())
<< " addr: " << Foam::name(item.get())
<< " movable:" << Switch(item.movable());
constructInfo<tmp<T>>();
if (verbose && item)
{
@ -101,6 +143,62 @@ int main()
printInfo(tfld3, true);
}
{
refPtr<scalarField> tfld1;
auto aptr = autoPtr<scalarField>::New(2, scalar(2));
tmp<scalarField> tfld2;
printInfo(tfld2, true);
tfld2 = new scalarField(10, Zero);
/*
tfld2 = aptr.get();
// tfld1.reset(aptr);
// tfld1 = std::move(aptr);
// tfld1 = aptr;
Info<< nl << "From autoPtr" << nl;
printInfo(aptr, true);
//& Info<< nl << "Construct from autoPtr" << nl;
//& // refPtr<scalarField> tfld2(autoPtr<scalarField>::New(10, scalar(2)));
//& printInfo(tfld2, true);
*/
}
{
auto aptr1 = autoPtr<labelField>::New(2, Zero);
//auto aptr1 = autoPtr<scalarField>::New(2, scalar(2));
auto aptr2 = autoPtr<scalarField>::New(2, scalar(2));
refPtr<scalarField> tfld2(std::move(aptr2));
// aptr2 = std::move(aptr1);
}
{
auto tptr1 = tmp<labelField>::New(2, Zero);
auto aptr1 = autoPtr<labelField>::New(2, Zero);
auto tfld2 = refPtr<labelField>::New(2, Zero);
// Deleted: refPtr<labelField> tfld1(aptr1);
refPtr<labelField> tfld1;
// refPtr<labelField> tfld1(std::move(tptr1));
// refPtr<labelField> tfld1(tptr1);
tfld1 = std::move(aptr1);
// tfld1.reset(aptr1);
// tfld1.reset(tfld2);
// tfld1 = std::move(tptr1);
// Does not compile: tfld1.ref(tptr1);
// Deleted: tfld1.cref(tptr1);
// Deleted: tfld1.ref(aptr1);
}
Info<< "\nEnd" << endl;
return 0;