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