From 3d0404af1844739b35b17cfa99b8f6f36b614803 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Wed, 13 May 2020 20:25:17 +0200 Subject: [PATCH] STYLE: simpler use of autoPtr, unique_ptr for OSspecific, fileFormats --- .../MSwindows/fileMonitor/fileMonitor.H | 9 ++++--- .../POSIX/fileMonitor/fileMonitor.H | 11 ++++---- src/fileFormats/stl/STLCore.C | 26 +++++++------------ src/fileFormats/stl/STLCore.H | 12 ++++----- src/fileFormats/stl/STLReader.C | 10 ++++--- src/surfMesh/writers/vtk/vtkSurfaceWriter.C | 2 +- 6 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/OSspecific/MSwindows/fileMonitor/fileMonitor.H b/src/OSspecific/MSwindows/fileMonitor/fileMonitor.H index c3d90590e16..3b62cd7feab 100644 --- a/src/OSspecific/MSwindows/fileMonitor/fileMonitor.H +++ b/src/OSspecific/MSwindows/fileMonitor/fileMonitor.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2017-2018 OpenCFD Ltd. + Copyright (C) 2017-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -45,10 +45,11 @@ SourceFiles #ifndef fileMonitor_H #define fileMonitor_H -#include <sys/types.h> #include "Enum.H" #include "className.H" #include "DynamicList.H" +#include <memory> +#include <sys/types.h> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -67,7 +68,7 @@ class fileMonitor { public: - // Public data types + // Public Data Types //- Enumeration defining the file state. enum fileState @@ -81,7 +82,7 @@ public: private: - // Private data + // Private Data //- Whether to use inotify (requires -DFOAM_USE_INOTIFY, see above) const bool useInotify_; diff --git a/src/OSspecific/POSIX/fileMonitor/fileMonitor.H b/src/OSspecific/POSIX/fileMonitor/fileMonitor.H index 4472bee15b6..6dbeed8ac5f 100644 --- a/src/OSspecific/POSIX/fileMonitor/fileMonitor.H +++ b/src/OSspecific/POSIX/fileMonitor/fileMonitor.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2017-2018 OpenCFD Ltd. + Copyright (C) 2017-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -45,10 +45,11 @@ SourceFiles #ifndef fileMonitor_H #define fileMonitor_H -#include <sys/types.h> #include "Enum.H" #include "className.H" #include "DynamicList.H" +#include <memory> +#include <sys/types.h> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -67,7 +68,7 @@ class fileMonitor { public: - // Public data types + // Public Data Types //- Enumeration defining the file state. enum fileState @@ -81,7 +82,7 @@ public: private: - // Private data + // Private Data //- Whether to use inotify (requires -DFOAM_USE_INOTIFY, see above) const bool useInotify_; @@ -99,7 +100,7 @@ private: DynamicList<label> freeWatchFds_; //- Watch mechanism (stat or inotify) - mutable autoPtr<fileMonitorWatcher> watcher_; + mutable std::unique_ptr<fileMonitorWatcher> watcher_; // Private Member Functions diff --git a/src/fileFormats/stl/STLCore.C b/src/fileFormats/stl/STLCore.C index bfcfc5d609f..de43611e32f 100644 --- a/src/fileFormats/stl/STLCore.C +++ b/src/fileFormats/stl/STLCore.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2016-2017 OpenCFD Ltd. + Copyright (C) 2016-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -91,10 +91,10 @@ int Foam::fileFormats::STLCore::detectBinaryHeader ) { bool compressed = false; - autoPtr<std::istream> streamPtr - ( + std::unique_ptr<std::istream> streamPtr + { new std::ifstream(filename, std::ios::binary) - ); + }; // If the file is compressed, decompress it before further checking. if (!streamPtr->good() && isFile(filename + ".gz", false)) @@ -102,7 +102,7 @@ int Foam::fileFormats::STLCore::detectBinaryHeader compressed = true; streamPtr.reset(new igzstream((filename + ".gz").c_str())); } - std::istream& is = streamPtr(); + auto& is = *streamPtr; 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 ( const fileName& filename, @@ -167,10 +167,10 @@ Foam::fileFormats::STLCore::readBinaryHeader bool compressed = false; nTrisEstimated = 0; - autoPtr<std::istream> streamPtr - ( + std::unique_ptr<std::istream> streamPtr + { new std::ifstream(filename, std::ios::binary) - ); + }; // If the file is compressed, decompress it before reading. if (!streamPtr->good() && isFile(filename + ".gz", false)) @@ -178,12 +178,10 @@ Foam::fileFormats::STLCore::readBinaryHeader compressed = true; streamPtr.reset(new igzstream((filename + ".gz").c_str())); } - std::istream& is = streamPtr(); + auto& is = *streamPtr; if (!is.good()) { - streamPtr.clear(); - FatalErrorInFunction << "Cannot read file " << filename << " 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 if (!is.good()) // could check again: startsWithSolid(header) { - streamPtr.clear(); - FatalErrorInFunction << "problem reading header, perhaps file is not binary " << exit(FatalError); @@ -235,8 +231,6 @@ Foam::fileFormats::STLCore::readBinaryHeader if (bad) { - streamPtr.clear(); - FatalErrorInFunction << "problem reading number of triangles, perhaps file is not binary" << exit(FatalError); diff --git a/src/fileFormats/stl/STLCore.H b/src/fileFormats/stl/STLCore.H index 4e194b768a3..a7a013686a3 100644 --- a/src/fileFormats/stl/STLCore.H +++ b/src/fileFormats/stl/STLCore.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2016-2017 OpenCFD Ltd. + Copyright (C) 2016-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -39,7 +39,7 @@ SourceFiles #include "STLpoint.H" #include "STLtriangle.H" -#include "autoPtr.H" +#include <memory> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -56,7 +56,7 @@ class STLCore { public: - // Public data types + // Public Data Types //- Enumeration for the format of data in the stream enum STLFormat @@ -88,7 +88,8 @@ protected: //- Read STL binary file header. // Return the opened file stream and estimated number of triangles. // 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, label& nTrisEstimated @@ -100,9 +101,8 @@ protected: // Constructors - //- Construct null + //- Default construct STLCore() = default; - }; diff --git a/src/fileFormats/stl/STLReader.C b/src/fileFormats/stl/STLReader.C index 2d7f75418eb..60cb4e84c1b 100644 --- a/src/fileFormats/stl/STLReader.C +++ b/src/fileFormats/stl/STLReader.C @@ -90,17 +90,19 @@ bool Foam::fileFormats::STLReader::readBINARY format_ = STLFormat::UNKNOWN; label nTris = 0; - autoPtr<istream> streamPtr = readBinaryHeader(filename, nTris); + std::unique_ptr<std::istream> streamPtr + { + readBinaryHeader(filename, nTris) + }; - if (!streamPtr.valid()) + if (!streamPtr) { FatalErrorInFunction << "Error reading file " << filename << " or file " << filename + ".gz" << exit(FatalError); } - - istream& is = streamPtr(); + auto& is = *streamPtr; #ifdef DEBUG_STLBINARY Info<< "# " << nTris << " facets" << endl; diff --git a/src/surfMesh/writers/vtk/vtkSurfaceWriter.C b/src/surfMesh/writers/vtk/vtkSurfaceWriter.C index 0fade0f99f5..df74db79e8a 100644 --- a/src/surfMesh/writers/vtk/vtkSurfaceWriter.C +++ b/src/surfMesh/writers/vtk/vtkSurfaceWriter.C @@ -211,7 +211,7 @@ Foam::fileName Foam::surfaceWriters::vtkWriter::write() const meshedSurf& surf = surface(); - if (writer_.empty() && (Pstream::master() || !parallel_)) + if (!writer_ && (Pstream::master() || !parallel_)) { writer_.reset ( -- GitLab