STYLE: simpler use of autoPtr, unique_ptr for OSspecific, fileFormats

This commit is contained in:
Mark Olesen
2020-05-13 20:25:17 +02:00
parent 841b65536b
commit 3d0404af18
6 changed files with 34 additions and 36 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2017-2018 OpenCFD Ltd. Copyright (C) 2017-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -45,10 +45,11 @@ SourceFiles
#ifndef fileMonitor_H #ifndef fileMonitor_H
#define fileMonitor_H #define fileMonitor_H
#include <sys/types.h>
#include "Enum.H" #include "Enum.H"
#include "className.H" #include "className.H"
#include "DynamicList.H" #include "DynamicList.H"
#include <memory>
#include <sys/types.h>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -67,7 +68,7 @@ class fileMonitor
{ {
public: public:
// Public data types // Public Data Types
//- Enumeration defining the file state. //- Enumeration defining the file state.
enum fileState enum fileState
@ -81,7 +82,7 @@ public:
private: private:
// Private data // Private Data
//- Whether to use inotify (requires -DFOAM_USE_INOTIFY, see above) //- Whether to use inotify (requires -DFOAM_USE_INOTIFY, see above)
const bool useInotify_; const bool useInotify_;

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2017-2018 OpenCFD Ltd. Copyright (C) 2017-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -45,10 +45,11 @@ SourceFiles
#ifndef fileMonitor_H #ifndef fileMonitor_H
#define fileMonitor_H #define fileMonitor_H
#include <sys/types.h>
#include "Enum.H" #include "Enum.H"
#include "className.H" #include "className.H"
#include "DynamicList.H" #include "DynamicList.H"
#include <memory>
#include <sys/types.h>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -67,7 +68,7 @@ class fileMonitor
{ {
public: public:
// Public data types // Public Data Types
//- Enumeration defining the file state. //- Enumeration defining the file state.
enum fileState enum fileState
@ -81,7 +82,7 @@ public:
private: private:
// Private data // Private Data
//- Whether to use inotify (requires -DFOAM_USE_INOTIFY, see above) //- Whether to use inotify (requires -DFOAM_USE_INOTIFY, see above)
const bool useInotify_; const bool useInotify_;
@ -99,7 +100,7 @@ private:
DynamicList<label> freeWatchFds_; DynamicList<label> freeWatchFds_;
//- Watch mechanism (stat or inotify) //- Watch mechanism (stat or inotify)
mutable autoPtr<fileMonitorWatcher> watcher_; mutable std::unique_ptr<fileMonitorWatcher> watcher_;
// Private Member Functions // Private Member Functions

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2017 OpenCFD Ltd. Copyright (C) 2016-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -91,10 +91,10 @@ int Foam::fileFormats::STLCore::detectBinaryHeader
) )
{ {
bool compressed = false; bool compressed = false;
autoPtr<std::istream> streamPtr std::unique_ptr<std::istream> streamPtr
( {
new std::ifstream(filename, std::ios::binary) new std::ifstream(filename, std::ios::binary)
); };
// If the file is compressed, decompress it before further checking. // If the file is compressed, decompress it before further checking.
if (!streamPtr->good() && isFile(filename + ".gz", false)) if (!streamPtr->good() && isFile(filename + ".gz", false))
@ -102,7 +102,7 @@ int Foam::fileFormats::STLCore::detectBinaryHeader
compressed = true; compressed = true;
streamPtr.reset(new igzstream((filename + ".gz").c_str())); streamPtr.reset(new igzstream((filename + ".gz").c_str()));
} }
std::istream& is = streamPtr(); auto& is = *streamPtr;
if (!is.good()) if (!is.good())
{ {
@ -156,7 +156,7 @@ int Foam::fileFormats::STLCore::detectBinaryHeader
} }
Foam::autoPtr<std::istream> std::unique_ptr<std::istream>
Foam::fileFormats::STLCore::readBinaryHeader Foam::fileFormats::STLCore::readBinaryHeader
( (
const fileName& filename, const fileName& filename,
@ -167,10 +167,10 @@ Foam::fileFormats::STLCore::readBinaryHeader
bool compressed = false; bool compressed = false;
nTrisEstimated = 0; nTrisEstimated = 0;
autoPtr<std::istream> streamPtr std::unique_ptr<std::istream> streamPtr
( {
new std::ifstream(filename, std::ios::binary) new std::ifstream(filename, std::ios::binary)
); };
// If the file is compressed, decompress it before reading. // If the file is compressed, decompress it before reading.
if (!streamPtr->good() && isFile(filename + ".gz", false)) if (!streamPtr->good() && isFile(filename + ".gz", false))
@ -178,12 +178,10 @@ Foam::fileFormats::STLCore::readBinaryHeader
compressed = true; compressed = true;
streamPtr.reset(new igzstream((filename + ".gz").c_str())); streamPtr.reset(new igzstream((filename + ".gz").c_str()));
} }
std::istream& is = streamPtr(); auto& is = *streamPtr;
if (!is.good()) if (!is.good())
{ {
streamPtr.clear();
FatalErrorInFunction FatalErrorInFunction
<< "Cannot read file " << filename << "Cannot read file " << filename
<< " or file " << filename + ".gz" << " or file " << filename + ".gz"
@ -198,8 +196,6 @@ Foam::fileFormats::STLCore::readBinaryHeader
// Check that stream is OK, if not this may be an ASCII file // Check that stream is OK, if not this may be an ASCII file
if (!is.good()) // could check again: startsWithSolid(header) if (!is.good()) // could check again: startsWithSolid(header)
{ {
streamPtr.clear();
FatalErrorInFunction FatalErrorInFunction
<< "problem reading header, perhaps file is not binary " << "problem reading header, perhaps file is not binary "
<< exit(FatalError); << exit(FatalError);
@ -235,8 +231,6 @@ Foam::fileFormats::STLCore::readBinaryHeader
if (bad) if (bad)
{ {
streamPtr.clear();
FatalErrorInFunction FatalErrorInFunction
<< "problem reading number of triangles, perhaps file is not binary" << "problem reading number of triangles, perhaps file is not binary"
<< exit(FatalError); << exit(FatalError);

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2016-2017 OpenCFD Ltd. Copyright (C) 2016-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -39,7 +39,7 @@ SourceFiles
#include "STLpoint.H" #include "STLpoint.H"
#include "STLtriangle.H" #include "STLtriangle.H"
#include "autoPtr.H" #include <memory>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -56,7 +56,7 @@ class STLCore
{ {
public: public:
// Public data types // Public Data Types
//- Enumeration for the format of data in the stream //- Enumeration for the format of data in the stream
enum STLFormat enum STLFormat
@ -88,7 +88,8 @@ protected:
//- Read STL binary file header. //- Read STL binary file header.
// Return the opened file stream and estimated number of triangles. // Return the opened file stream and estimated number of triangles.
// The stream is invalid and number of triangles is 0 on error. // The stream is invalid and number of triangles is 0 on error.
static autoPtr<std::istream> readBinaryHeader static std::unique_ptr<std::istream>
readBinaryHeader
( (
const fileName& filename, const fileName& filename,
label& nTrisEstimated label& nTrisEstimated
@ -100,9 +101,8 @@ protected:
// Constructors // Constructors
//- Construct null //- Default construct
STLCore() = default; STLCore() = default;
}; };

View File

@ -90,17 +90,19 @@ bool Foam::fileFormats::STLReader::readBINARY
format_ = STLFormat::UNKNOWN; format_ = STLFormat::UNKNOWN;
label nTris = 0; label nTris = 0;
autoPtr<istream> streamPtr = readBinaryHeader(filename, nTris); std::unique_ptr<std::istream> streamPtr
{
readBinaryHeader(filename, nTris)
};
if (!streamPtr.valid()) if (!streamPtr)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Error reading file " << filename << "Error reading file " << filename
<< " or file " << filename + ".gz" << " or file " << filename + ".gz"
<< exit(FatalError); << exit(FatalError);
} }
auto& is = *streamPtr;
istream& is = streamPtr();
#ifdef DEBUG_STLBINARY #ifdef DEBUG_STLBINARY
Info<< "# " << nTris << " facets" << endl; Info<< "# " << nTris << " facets" << endl;

View File

@ -211,7 +211,7 @@ Foam::fileName Foam::surfaceWriters::vtkWriter::write()
const meshedSurf& surf = surface(); const meshedSurf& surf = surface();
if (writer_.empty() && (Pstream::master() || !parallel_)) if (!writer_ && (Pstream::master() || !parallel_))
{ {
writer_.reset writer_.reset
( (