mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-12-28 03:37:59 +00:00
ENH: refine PtrList emplace method, add emplace for autoPtr/refPtr...
* resize_null() methods for PtrList variants
- for cases where an existing PtrList needs a specific size and
but not retain any existing entries.
Eg,
ptrs.resize_null(100);
vs. ptrs.free(); ptr.resize(100);
or ptr.resize(100); ptrs.free();
* remove stored pointer before emplacing PtrList elements
- may reduce memory peaks
* STYLE: static_cast of (nullptr) instead of reinterpret_cast of (0)
* COMP: implement emplace_set() for PtrDynList
- previously missing, which meant it would have leaked through to the
underlying PtrList definition
* emplace methods for autoPtr, refPtr, tmp
- applies reset() with forwarding arguments.
For example,
tmp<GeoField> tfld = ...;
later...
tfld.emplace(io, mesh);
vs.
tfld.reset(new GeoField(io, mesh));
or
tfld.reset(tmp<GeoField>::New(io, mesh));
The emplace() obviously has reduced typing, but also allows the
existing stored pointer to be deleted *before* creating its
replacement (reduces memory peaks).
This commit is contained in:
@ -47,43 +47,39 @@ class Scalar
|
||||
|
||||
public:
|
||||
|
||||
Scalar()
|
||||
static bool verbose;
|
||||
|
||||
constexpr Scalar() noexcept
|
||||
:
|
||||
data_(0)
|
||||
{}
|
||||
|
||||
Scalar(scalar val)
|
||||
Scalar(scalar val) noexcept
|
||||
:
|
||||
data_(val)
|
||||
{}
|
||||
|
||||
~Scalar()
|
||||
{
|
||||
Info<< "delete Scalar: " << data_ << endl;
|
||||
if (verbose) Info<< "delete Scalar: " << data_ << endl;
|
||||
}
|
||||
|
||||
const scalar& value() const
|
||||
{
|
||||
return data_;
|
||||
}
|
||||
|
||||
scalar& value()
|
||||
{
|
||||
return data_;
|
||||
}
|
||||
scalar value() const noexcept { return data_; }
|
||||
scalar& value() noexcept { return data_; }
|
||||
|
||||
autoPtr<Scalar> clone() const
|
||||
{
|
||||
return autoPtr<Scalar>::New(data_);
|
||||
}
|
||||
|
||||
friend Ostream& operator<<(Ostream& os, const Scalar& val)
|
||||
friend Ostream& operator<<(Ostream& os, const Scalar& item)
|
||||
{
|
||||
os << val.data_;
|
||||
os << item.value();
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
bool Scalar::verbose = true;
|
||||
|
||||
|
||||
// As per
|
||||
@ -268,6 +264,22 @@ Ostream& report
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
#if 1
|
||||
{
|
||||
DLPtrList<Scalar> llist1;
|
||||
Info<< "emplace_front: " << llist1.emplace_front(100) << nl;
|
||||
Info<< "emplace_front: " << llist1.emplace_front(200) << nl;
|
||||
Info<< "emplace_front: " << llist1.emplace_front(300) << nl;
|
||||
Info<< "emplace_back: " << llist1.emplace_back(500) << nl;
|
||||
|
||||
Info<< "DLPtrList: " << llist1 << endl;
|
||||
|
||||
Scalar::verbose = false;
|
||||
llist1.clear();
|
||||
Scalar::verbose = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
{
|
||||
DLPtrList<Scalar> llist1;
|
||||
@ -349,6 +361,16 @@ int main(int argc, char *argv[])
|
||||
list2.emplace(i, (10 + 1.3*i));
|
||||
}
|
||||
|
||||
#if 0
|
||||
list2.release(5);
|
||||
list2.release(10);
|
||||
|
||||
forAll(list2, i)
|
||||
{
|
||||
list2.try_emplace(i, (50 + 1.3*i));
|
||||
}
|
||||
#endif
|
||||
|
||||
PtrList<Scalar> listApp;
|
||||
for (label i = 0; i < 5; ++i)
|
||||
{
|
||||
@ -639,7 +661,7 @@ int main(int argc, char *argv[])
|
||||
dynPlanes.set(6, new plane(vector(2,2,1), vector::one));
|
||||
dynPlanes.set(10, new plane(vector(4,5,6), vector::one));
|
||||
|
||||
Info<< "emplaced :"
|
||||
Info<< "emplaced[12]: "
|
||||
<< dynPlanes.emplace(12, vector(3,2,1), vector::one) << endl;
|
||||
|
||||
dynPlanes.emplace_back(Zero, vector::one);
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
\\ / A nd | www.openfoam.com
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2018-2022 OpenCFD Ltd.
|
||||
Copyright (C) 2018-2023 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -84,6 +84,20 @@ struct DerivedList : public List<T>
|
||||
};
|
||||
|
||||
|
||||
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());
|
||||
|
||||
if (verbose && item)
|
||||
{
|
||||
Info<< " content: " << item();
|
||||
}
|
||||
Info<< nl;
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
// Main program:
|
||||
|
||||
@ -112,6 +126,17 @@ int main(int argc, char *argv[])
|
||||
|
||||
Info<<"move unique to autoPtr: " << *list3 << nl;
|
||||
Info<<"old is " << Switch(bool(list2)) << nl;
|
||||
|
||||
Info<< "before emplace: ";
|
||||
printInfo(list, true);
|
||||
|
||||
list.emplace(4, label(-2));
|
||||
Info<< "after emplace: ";
|
||||
printInfo(list, true);
|
||||
|
||||
list.emplace(2, label(-4));
|
||||
Info<< "after emplace: ";
|
||||
printInfo(list, true);
|
||||
}
|
||||
|
||||
// Confirm that forwarding with move construct actually works as expected
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
\\ / A nd | www.openfoam.com
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2020-2022 OpenCFD Ltd.
|
||||
Copyright (C) 2020-2023 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
|
||||
@ -105,12 +105,21 @@ int main()
|
||||
Info<< nl << "Construct from reference" << nl;
|
||||
|
||||
scalarField f2(10, Foam::sqrt(2.0));
|
||||
printInfo(refPtr<scalarField>(f2), true);
|
||||
refPtr<scalarField> tfld2(f2);
|
||||
printInfo(tfld2, true);
|
||||
|
||||
Info<< nl << "emplaced:"<< nl;
|
||||
tfld2.emplace(25, scalar(1));
|
||||
printInfo(tfld2, true);
|
||||
}
|
||||
|
||||
{
|
||||
Info<< nl << "Construct from New (is_pointer)" << nl;
|
||||
auto tfld1 = refPtr<scalarField>::New(10, scalar(1));
|
||||
auto tfld1 = refPtr<scalarField>::New(10, scalar(-1));
|
||||
printInfo(tfld1, true);
|
||||
|
||||
Info<< nl << "emplaced:"<< nl;
|
||||
tfld1.emplace(15, scalar(1));
|
||||
printInfo(tfld1, true);
|
||||
|
||||
Info<< nl << "Dereferenced: " << *tfld1 << nl;
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
\\ / A nd | www.openfoam.com
|
||||
\\/ M anipulation |
|
||||
-------------------------------------------------------------------------------
|
||||
Copyright (C) 2018-2021 OpenCFD Ltd.
|
||||
Copyright (C) 2018-2023 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
|
||||
@ -78,7 +78,10 @@ int main()
|
||||
}
|
||||
|
||||
{
|
||||
auto tfld1 = tmp<scalarField>::New(20, Zero);
|
||||
auto tfld1 = tmp<scalarField>::New(10, Zero);
|
||||
printInfo(tfld1, true);
|
||||
|
||||
tfld1.emplace(20, Zero);
|
||||
printInfo(tfld1, true);
|
||||
|
||||
// Hold on to the old content for a bit
|
||||
|
||||
Reference in New Issue
Block a user