Commit f3106ec1 authored by Mark Olesen's avatar Mark Olesen
Browse files

STYLE: use unique_ptr for Fstream resource management

STYLE: change return type of NewOFstream from Ostream to OSstream
parent 3135dcf2
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2019 OpenCFD Ltd. Copyright (C) 2017-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
...@@ -43,7 +43,7 @@ namespace Foam ...@@ -43,7 +43,7 @@ namespace Foam
Foam::Detail::IFstreamAllocator::IFstreamAllocator(const fileName& pathname) Foam::Detail::IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
: :
allocatedPtr_(nullptr), allocatedPtr_(nullptr),
compression_(IOstream::UNCOMPRESSED) detectedCompression_(IOstream::UNCOMPRESSED)
{ {
if (pathname.empty()) if (pathname.empty())
{ {
...@@ -55,7 +55,7 @@ Foam::Detail::IFstreamAllocator::IFstreamAllocator(const fileName& pathname) ...@@ -55,7 +55,7 @@ Foam::Detail::IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
const std::ios_base::openmode mode(std::ios_base::in|std::ios_base::binary); const std::ios_base::openmode mode(std::ios_base::in|std::ios_base::binary);
allocatedPtr_ = new std::ifstream(pathname, mode); allocatedPtr_.reset(new std::ifstream(pathname, mode));
// If the file is compressed, decompress it before reading. // If the file is compressed, decompress it before reading.
if (!allocatedPtr_->good() && isFile(pathname + ".gz", false)) if (!allocatedPtr_->good() && isFile(pathname + ".gz", false))
...@@ -65,37 +65,16 @@ Foam::Detail::IFstreamAllocator::IFstreamAllocator(const fileName& pathname) ...@@ -65,37 +65,16 @@ Foam::Detail::IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
InfoInFunction << "Decompressing " << pathname + ".gz" << endl; InfoInFunction << "Decompressing " << pathname + ".gz" << endl;
} }
delete allocatedPtr_; allocatedPtr_.reset(new igzstream((pathname + ".gz").c_str(), mode));
allocatedPtr_ = new igzstream((pathname + ".gz").c_str(), mode);
if (allocatedPtr_->good()) if (allocatedPtr_->good())
{ {
compression_ = IOstream::COMPRESSED; detectedCompression_ = IOstream::COMPRESSED;
} }
} }
} }
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::Detail::IFstreamAllocator::~IFstreamAllocator()
{
deallocate();
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::Detail::IFstreamAllocator::deallocate()
{
if (allocatedPtr_)
{
delete allocatedPtr_;
allocatedPtr_ = nullptr;
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::IFstream::IFstream Foam::IFstream::IFstream
...@@ -112,7 +91,7 @@ Foam::IFstream::IFstream ...@@ -112,7 +91,7 @@ Foam::IFstream::IFstream
pathname, pathname,
format, format,
version, version,
IFstreamAllocator::compression_ IFstreamAllocator::detectedCompression_
) )
{ {
setClosed(); setClosed();
...@@ -173,7 +152,7 @@ void Foam::IFstream::rewind() ...@@ -173,7 +152,7 @@ void Foam::IFstream::rewind()
try try
{ {
gzPtr = dynamic_cast<igzstream*>(allocatedPtr_); gzPtr = dynamic_cast<igzstream*>(allocatedPtr_.get());
} }
catch (const std::bad_cast&) catch (const std::bad_cast&)
{ {
......
...@@ -41,8 +41,8 @@ SourceFiles ...@@ -41,8 +41,8 @@ SourceFiles
#include "ISstream.H" #include "ISstream.H"
#include "fileName.H" #include "fileName.H"
#include "className.H" #include "className.H"
#include <fstream> #include <fstream>
#include <memory>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
...@@ -64,26 +64,16 @@ protected: ...@@ -64,26 +64,16 @@ protected:
// Member Data // Member Data
//- The allocated stream pointer (ifstream or igzstream). //- The allocated stream pointer (ifstream or igzstream).
std::istream* allocatedPtr_; std::unique_ptr<std::istream> allocatedPtr_;
//- The requested compression type //- The detected compression type
IOstream::compressionType compression_; IOstream::compressionType detectedCompression_;
// Constructors // Constructors
//- Construct from pathname //- Construct from pathname
IFstreamAllocator(const fileName& pathname); IFstreamAllocator(const fileName& pathname);
//- Destructor
~IFstreamAllocator();
// Protected Member Functions
//- Delete the stream pointer
void deallocate();
}; };
} // End namespace Detail } // End namespace Detail
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2017-2019 OpenCFD Ltd. Copyright (C) 2017-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
...@@ -37,12 +37,13 @@ namespace Foam ...@@ -37,12 +37,13 @@ namespace Foam
defineTypeNameAndDebug(OFstream, 0); defineTypeNameAndDebug(OFstream, 0);
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::Detail::OFstreamAllocator::OFstreamAllocator Foam::Detail::OFstreamAllocator::OFstreamAllocator
( (
const fileName& pathname, const fileName& pathname,
IOstream::compressionType compression, IOstream::compressionType comp,
const bool append const bool append
) )
: :
...@@ -62,15 +63,16 @@ Foam::Detail::OFstreamAllocator::OFstreamAllocator ...@@ -62,15 +63,16 @@ Foam::Detail::OFstreamAllocator::OFstreamAllocator
mode |= std::ios_base::app; mode |= std::ios_base::app;
} }
if (compression == IOstream::COMPRESSED) if (comp == IOstream::COMPRESSED)
{ {
// Get identically named uncompressed version out of the way // Get identically named uncompressed version out of the way
fileName gzPathName(pathname + ".gz");
fileName::Type pathType = Foam::type(pathname, false); fileName::Type pathType = Foam::type(pathname, false);
if (pathType == fileName::FILE || pathType == fileName::LINK) if (pathType == fileName::FILE || pathType == fileName::LINK)
{ {
rm(pathname); rm(pathname);
} }
fileName gzPathName(pathname + ".gz");
if (!append && Foam::type(gzPathName) == fileName::LINK) if (!append && Foam::type(gzPathName) == fileName::LINK)
{ {
...@@ -79,17 +81,19 @@ Foam::Detail::OFstreamAllocator::OFstreamAllocator ...@@ -79,17 +81,19 @@ Foam::Detail::OFstreamAllocator::OFstreamAllocator
rm(gzPathName); rm(gzPathName);
} }
allocatedPtr_ = new ogzstream(gzPathName.c_str(), mode); allocatedPtr_.reset(new ogzstream(gzPathName.c_str(), mode));
} }
else else
{ {
// get identically named compressed version out of the way // Get identically named compressed version out of the way
fileName gzPathName(pathname + ".gz"); fileName gzPathName(pathname + ".gz");
fileName::Type gzType = Foam::type(gzPathName, false); fileName::Type gzType = Foam::type(gzPathName, false);
if (gzType == fileName::FILE || gzType == fileName::LINK) if (gzType == fileName::FILE || gzType == fileName::LINK)
{ {
rm(gzPathName); rm(gzPathName);
} }
if (!append && Foam::type(pathname, false) == fileName::LINK) if (!append && Foam::type(pathname, false) == fileName::LINK)
{ {
// Disallow writing into softlink to avoid any problems with // Disallow writing into softlink to avoid any problems with
...@@ -97,23 +101,7 @@ Foam::Detail::OFstreamAllocator::OFstreamAllocator ...@@ -97,23 +101,7 @@ Foam::Detail::OFstreamAllocator::OFstreamAllocator
rm(pathname); rm(pathname);
} }
allocatedPtr_ = new std::ofstream(pathname, mode); allocatedPtr_.reset(new std::ofstream(pathname, mode));
}
}
Foam::Detail::OFstreamAllocator::~OFstreamAllocator()
{
deallocate();
}
void Foam::Detail::OFstreamAllocator::deallocate()
{
if (allocatedPtr_)
{
delete allocatedPtr_;
allocatedPtr_ = nullptr;
} }
} }
......
...@@ -41,8 +41,8 @@ SourceFiles ...@@ -41,8 +41,8 @@ SourceFiles
#include "OSstream.H" #include "OSstream.H"
#include "fileName.H" #include "fileName.H"
#include "className.H" #include "className.H"
#include <fstream> #include <fstream>
#include <memory>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
...@@ -64,7 +64,7 @@ protected: ...@@ -64,7 +64,7 @@ protected:
// Member Data // Member Data
//- The allocated stream pointer (ofstream or ogzstream). //- The allocated stream pointer (ofstream or ogzstream).
std::ostream* allocatedPtr_; std::unique_ptr<std::ostream> allocatedPtr_;
// Constructors // Constructors
...@@ -73,19 +73,9 @@ protected: ...@@ -73,19 +73,9 @@ protected:
OFstreamAllocator OFstreamAllocator
( (
const fileName& pathname, const fileName& pathname,
IOstream::compressionType compression=IOstream::UNCOMPRESSED, IOstream::compressionType comp = IOstream::UNCOMPRESSED,
const bool append = false const bool append = false
); );
//- Destructor
~OFstreamAllocator();
// Protected Member Functions
//- Delete the stream pointer
void deallocate();
}; };
} // End namespace Detail } // End namespace Detail
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2017-2018 OpenFOAM Foundation Copyright (C) 2017-2018 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
...@@ -476,7 +476,7 @@ bool Foam::fileOperation::writeObject ...@@ -476,7 +476,7 @@ bool Foam::fileOperation::writeObject
mkDir(pathName.path()); mkDir(pathName.path());
autoPtr<Ostream> osPtr autoPtr<OSstream> osPtr
( (
NewOFstream NewOFstream
( (
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2017 OpenFOAM Foundation Copyright (C) 2017 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
...@@ -425,8 +426,8 @@ public: ...@@ -425,8 +426,8 @@ public:
//- Generate an ISstream that reads a file //- Generate an ISstream that reads a file
virtual autoPtr<ISstream> NewIFstream(const fileName&) const = 0; virtual autoPtr<ISstream> NewIFstream(const fileName&) const = 0;
//- Generate an Ostream that writes a file //- Generate an OSstream that writes a file
virtual autoPtr<Ostream> NewOFstream virtual autoPtr<OSstream> NewOFstream
( (
const fileName& pathname, const fileName& pathname,
IOstream::streamFormat format=IOstream::ASCII, IOstream::streamFormat format=IOstream::ASCII,
......
...@@ -2261,7 +2261,7 @@ bool Foam::fileOperations::masterUncollatedFileOperation::writeObject ...@@ -2261,7 +2261,7 @@ bool Foam::fileOperations::masterUncollatedFileOperation::writeObject
// Make sure to pick up any new times // Make sure to pick up any new times
setTime(io.time()); setTime(io.time());
autoPtr<Ostream> osPtr autoPtr<OSstream> osPtr
( (
NewOFstream NewOFstream
( (
...@@ -2545,7 +2545,7 @@ Foam::fileOperations::masterUncollatedFileOperation::NewIFstream ...@@ -2545,7 +2545,7 @@ Foam::fileOperations::masterUncollatedFileOperation::NewIFstream
} }
Foam::autoPtr<Foam::Ostream> Foam::autoPtr<Foam::OSstream>
Foam::fileOperations::masterUncollatedFileOperation::NewOFstream Foam::fileOperations::masterUncollatedFileOperation::NewOFstream
( (
const fileName& pathName, const fileName& pathName,
...@@ -2555,7 +2555,7 @@ Foam::fileOperations::masterUncollatedFileOperation::NewOFstream ...@@ -2555,7 +2555,7 @@ Foam::fileOperations::masterUncollatedFileOperation::NewOFstream
const bool valid const bool valid
) const ) const
{ {
return autoPtr<Ostream> return autoPtr<OSstream>
( (
new masterOFstream new masterOFstream
( (
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2017 OpenFOAM Foundation Copyright (C) 2017 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
...@@ -706,8 +706,8 @@ public: ...@@ -706,8 +706,8 @@ public:
//- Generate an ISstream that reads a file //- Generate an ISstream that reads a file
virtual autoPtr<ISstream> NewIFstream(const fileName&) const; virtual autoPtr<ISstream> NewIFstream(const fileName&) const;
//- Generate an Ostream that writes a file //- Generate an OSstream that writes a file
virtual autoPtr<Ostream> NewOFstream virtual autoPtr<OSstream> NewOFstream
( (
const fileName& pathname, const fileName& pathname,
IOstream::streamFormat format=IOstream::ASCII, IOstream::streamFormat format=IOstream::ASCII,
......
...@@ -720,7 +720,7 @@ Foam::fileOperations::uncollatedFileOperation::NewIFstream ...@@ -720,7 +720,7 @@ Foam::fileOperations::uncollatedFileOperation::NewIFstream
} }
Foam::autoPtr<Foam::Ostream> Foam::autoPtr<Foam::OSstream>
Foam::fileOperations::uncollatedFileOperation::NewOFstream Foam::fileOperations::uncollatedFileOperation::NewOFstream
( (
const fileName& pathName, const fileName& pathName,
...@@ -730,7 +730,7 @@ Foam::fileOperations::uncollatedFileOperation::NewOFstream ...@@ -730,7 +730,7 @@ Foam::fileOperations::uncollatedFileOperation::NewOFstream
const bool valid const bool valid
) const ) const
{ {
return autoPtr<Ostream>(new OFstream(pathName, fmt, ver, cmp)); return autoPtr<OSstream>(new OFstream(pathName, fmt, ver, cmp));
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2017 OpenFOAM Foundation Copyright (C) 2017 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
...@@ -277,8 +278,8 @@ public: ...@@ -277,8 +278,8 @@ public:
//- Generate an ISstream that reads a file //- Generate an ISstream that reads a file
virtual autoPtr<ISstream> NewIFstream(const fileName&) const; virtual autoPtr<ISstream> NewIFstream(const fileName&) const;
//- Generate an Ostream that writes a file //- Generate an OSstream that writes a file
virtual autoPtr<Ostream> NewOFstream virtual autoPtr<OSstream> NewOFstream
( (
const fileName& pathname, const fileName& pathname,
IOstream::streamFormat format=IOstream::ASCII, IOstream::streamFormat format=IOstream::ASCII,
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd. Copyright (C) 2019-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
...@@ -120,7 +120,7 @@ void Foam::pairPotentialList::readPairPotentialDict ...@@ -120,7 +120,7 @@ void Foam::pairPotentialList::readPairPotentialDict
if ((*this)[pairPotentialIndex(a, b)].writeTables()) if ((*this)[pairPotentialIndex(a, b)].writeTables())
{ {
fileHandler().mkDir(mesh.time().path()); fileHandler().mkDir(mesh.time().path());
autoPtr<Ostream> ppTabFile autoPtr<OSstream> ppTabFile
( (
fileHandler().NewOFstream fileHandler().NewOFstream
( (
...@@ -166,7 +166,7 @@ void Foam::pairPotentialList::readPairPotentialDict ...@@ -166,7 +166,7 @@ void Foam::pairPotentialList::readPairPotentialDict
if (electrostaticPotential_->writeTables()) if (electrostaticPotential_->writeTables())
{ {
fileHandler().mkDir(mesh.time().path()); fileHandler().mkDir(mesh.time().path());
autoPtr<Ostream> ppTabFile autoPtr<OSstream> ppTabFile
( (
fileHandler().NewOFstream fileHandler().NewOFstream
( (
......
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