Commit e2228aab authored by Mark Olesen's avatar Mark Olesen Committed by Andrew Heather
Browse files

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
parent 0ca95ac0
...@@ -135,6 +135,8 @@ Foam::surfaceWriter::New ...@@ -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 ...@@ -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() ...@@ -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 ...@@ -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 ...@@ -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() ...@@ -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 ...@@ -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);
} }
...@@ -411,7 +432,14 @@ bool Foam::surfaceWriter::merge() const ...@@ -411,7 +432,14 @@ bool Foam::surfaceWriter::merge() const
if (parallel_ && Pstream::parRun() && !upToDate_) if (parallel_ && Pstream::parRun() && !upToDate_)
{ {
changed = merged_.merge(surf_.get(), mergeDim_); if (useComponents_)
{
changed = merged_.merge(surfComp_, mergeDim_);
}
else
{
changed = merged_.merge(surf_.get(), mergeDim_);
}
} }
upToDate_ = true; upToDate_ = true;
...@@ -428,7 +456,14 @@ const Foam::meshedSurf& Foam::surfaceWriter::surface() const ...@@ -428,7 +456,14 @@ const Foam::meshedSurf& Foam::surfaceWriter::surface() const
return merged_; return merged_;
} }
return surf_.get(); if (useComponents_)
{
return surfComp_;
}
else
{
return surf_.get();
}
} }
......
...@@ -98,17 +98,23 @@ class surfaceWriter ...@@ -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_;
......
...@@ -153,7 +153,9 @@ Foam::surfaceWriters::vtkWriter::vtkWriter ...@@ -153,7 +153,9 @@ Foam::surfaceWriters::vtkWriter::vtkWriter
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::surfaceWriters::vtkWriter::~vtkWriter() Foam::surfaceWriters::vtkWriter::~vtkWriter()
{} {
close();
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment