ENH: avoid references to temporaries in surfaceWriter

- previously wrapped raw points/faces with a meshedSurfRef on input,
  but now handle the raw -> meshedSurf logic directly within
  surfaceWriter to avoid holding references to temporaries

  Since the updated meshedSurfRef is now modifiable, it can be used
  directly as a redirection mechanism within surfaceWriter.

- add explicit close() in destructor
This commit is contained in:
Mark Olesen
2019-02-22 11:40:58 +01:00
committed by Andrew Heather
parent 0ca95ac0cf
commit e2228aab8a
3 changed files with 54 additions and 11 deletions

View File

@ -135,6 +135,8 @@ Foam::surfaceWriter::New
Foam::surfaceWriter::surfaceWriter() Foam::surfaceWriter::surfaceWriter()
: :
surf_(std::cref<meshedSurf>(emptySurface_)), surf_(std::cref<meshedSurf>(emptySurface_)),
surfComp_(),
useComponents_(false),
upToDate_(false), upToDate_(false),
parallel_(true), parallel_(true),
useTimeDir_(false), useTimeDir_(false),
@ -188,7 +190,9 @@ Foam::surfaceWriter::surfaceWriter
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::surfaceWriter::~surfaceWriter() Foam::surfaceWriter::~surfaceWriter()
{} {
close();
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
@ -305,7 +309,9 @@ void Foam::surfaceWriter::clear()
{ {
close(); close();
expire(); expire();
useComponents_ = false;
surf_ = std::cref<meshedSurf>(emptySurface_); surf_ = std::cref<meshedSurf>(emptySurface_);
surfComp_.clear();
} }
@ -316,7 +322,9 @@ void Foam::surfaceWriter::setSurface
) )
{ {
expire(); expire();
useComponents_ = false;
surf_ = std::cref<meshedSurf>(surf); surf_ = std::cref<meshedSurf>(surf);
surfComp_.clear();
parallel_ = (parallel && Pstream::parRun()); parallel_ = (parallel && Pstream::parRun());
} }
@ -329,7 +337,10 @@ void Foam::surfaceWriter::setSurface
) )
{ {
expire(); expire();
setSurface(meshedSurfRef(points, faces), parallel); useComponents_ = true;
surf_ = std::cref<meshedSurf>(emptySurface_);
surfComp_.reset(points, faces);
parallel_ = (parallel && Pstream::parRun());
} }
@ -372,13 +383,18 @@ bool Foam::surfaceWriter::expire()
bool Foam::surfaceWriter::hasSurface() const bool Foam::surfaceWriter::hasSurface() const
{ {
return (&emptySurface_ != &(surf_.get())); return (useComponents_ || (&emptySurface_ != &(surf_.get())));
} }
bool Foam::surfaceWriter::empty() const bool Foam::surfaceWriter::empty() const
{ {
const bool value = surf_.get().faces().empty(); const bool value =
(
useComponents_
? surfComp_.faces().empty()
: surf_.get().faces().empty()
);
return (parallel_ ? returnReduce(value, andOp<bool>()) : value); return (parallel_ ? returnReduce(value, andOp<bool>()) : value);
} }
@ -386,7 +402,12 @@ bool Foam::surfaceWriter::empty() const
Foam::label Foam::surfaceWriter::size() const Foam::label Foam::surfaceWriter::size() const
{ {
const label value = surf_.get().faces().size(); const bool value =
(
useComponents_
? surfComp_.faces().empty()
: surf_.get().faces().empty()
);
return (parallel_ ? returnReduce(value, sumOp<label>()) : value); return (parallel_ ? returnReduce(value, sumOp<label>()) : value);
} }
@ -410,9 +431,16 @@ bool Foam::surfaceWriter::merge() const
bool changed = false; bool changed = false;
if (parallel_ && Pstream::parRun() && !upToDate_) if (parallel_ && Pstream::parRun() && !upToDate_)
{
if (useComponents_)
{
changed = merged_.merge(surfComp_, mergeDim_);
}
else
{ {
changed = merged_.merge(surf_.get(), mergeDim_); changed = merged_.merge(surf_.get(), mergeDim_);
} }
}
upToDate_ = true; upToDate_ = true;
return changed; return changed;
@ -428,8 +456,15 @@ const Foam::meshedSurf& Foam::surfaceWriter::surface() const
return merged_; return merged_;
} }
if (useComponents_)
{
return surfComp_;
}
else
{
return surf_.get(); return surf_.get();
} }
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -98,17 +98,23 @@ class surfaceWriter
{ {
protected: protected:
// Static data members // Static Data Members
//- Placeholder //- Placeholder
static const meshedSurf::emptySurface emptySurface_; static const meshedSurf::emptySurface emptySurface_;
// Protected data // Protected Data
//- Reference to the surface //- Reference to a surface
std::reference_wrapper<const meshedSurf> surf_; std::reference_wrapper<const meshedSurf> surf_;
//- Reference to raw surface components
meshedSurfRef surfComp_;
//- Use raw surface components instead of surface reference
bool useComponents_;
//- The topology/surface is up-to-date? //- The topology/surface is up-to-date?
mutable bool upToDate_; mutable bool upToDate_;

View File

@ -153,7 +153,9 @@ Foam::surfaceWriters::vtkWriter::vtkWriter
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::surfaceWriters::vtkWriter::~vtkWriter() Foam::surfaceWriters::vtkWriter::~vtkWriter()
{} {
close();
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //