Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Development
openfoam
Commits
f3106ec1
Commit
f3106ec1
authored
Feb 17, 2020
by
Mark OLESEN
Browse files
STYLE: use unique_ptr for Fstream resource management
STYLE: change return type of NewOFstream from Ostream to OSstream
parent
3135dcf2
Changes
11
Hide whitespace changes
Inline
Side-by-side
src/OpenFOAM/db/IOstreams/Fstreams/IFstream.C
View file @
f3106ec1
...
...
@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-20
19
OpenCFD Ltd.
Copyright (C) 2017-20
20
OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
...
...
@@ -43,7 +43,7 @@ namespace Foam
Foam
::
Detail
::
IFstreamAllocator
::
IFstreamAllocator
(
const
fileName
&
pathname
)
:
allocatedPtr_
(
nullptr
),
c
ompression_
(
IOstream
::
UNCOMPRESSED
)
detectedC
ompression_
(
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
())
{
c
ompression_
=
IOstream
::
COMPRESSED
;
detectedC
ompression_
=
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
::
c
ompression_
IFstreamAllocator
::
detectedC
ompression_
)
{
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
&
)
{
...
...
src/OpenFOAM/db/IOstreams/Fstreams/IFstream.H
View file @
f3106ec1
...
...
@@ -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
reques
ted compression type
IOstream
::
compressionType
c
ompression_
;
//- The
detec
ted compression type
IOstream
::
compressionType
detectedC
ompression_
;
// Constructors
//- Construct from pathname
IFstreamAllocator
(
const
fileName
&
pathname
);
//- Destructor
~
IFstreamAllocator
();
// Protected Member Functions
//- Delete the stream pointer
void
deallocate
();
};
}
// End namespace Detail
...
...
src/OpenFOAM/db/IOstreams/Fstreams/OFstream.C
View file @
f3106ec1
...
...
@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2017-20
19
OpenCFD Ltd.
Copyright (C) 2017-20
20
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
comp
ression
,
IOstream
::
compressionType
comp
,
const
bool
append
)
:
...
...
@@ -62,15 +63,16 @@ Foam::Detail::OFstreamAllocator::OFstreamAllocator
mode
|=
std
::
ios_base
::
app
;
}
if
(
comp
ression
==
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
{
//
g
et identically named compressed version out of the way
//
G
et 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
));
}
}
...
...
src/OpenFOAM/db/IOstreams/Fstreams/OFstream.H
View file @
f3106ec1
...
...
@@ -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
comp
ression
=
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
...
...
src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.C
View file @
f3106ec1
...
...
@@ -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
<
O
S
stream
>
osPtr
(
NewOFstream
(
...
...
src/OpenFOAM/global/fileOperations/fileOperation/fileOperation.H
View file @
f3106ec1
...
...
@@ -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 O
S
stream that writes a file
virtual
autoPtr
<
O
S
stream
>
NewOFstream
(
const
fileName
&
pathname
,
IOstream
::
streamFormat
format
=
IOstream
::
ASCII
,
...
...
src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.C
View file @
f3106ec1
...
...
@@ -2261,7 +2261,7 @@ bool Foam::fileOperations::masterUncollatedFileOperation::writeObject
// Make sure to pick up any new times
setTime
(
io
.
time
());
autoPtr
<
Ostream
>
osPtr
autoPtr
<
O
S
stream
>
osPtr
(
NewOFstream
(
...
...
@@ -2545,7 +2545,7 @@ Foam::fileOperations::masterUncollatedFileOperation::NewIFstream
}
Foam
::
autoPtr
<
Foam
::
Ostream
>
Foam
::
autoPtr
<
Foam
::
O
S
stream
>
Foam
::
fileOperations
::
masterUncollatedFileOperation
::
NewOFstream
(
const
fileName
&
pathName
,
...
...
@@ -2555,7 +2555,7 @@ Foam::fileOperations::masterUncollatedFileOperation::NewOFstream
const
bool
valid
)
const
{
return
autoPtr
<
Ostream
>
return
autoPtr
<
O
S
stream
>
(
new
masterOFstream
(
...
...
src/OpenFOAM/global/fileOperations/masterUncollatedFileOperation/masterUncollatedFileOperation.H
View file @
f3106ec1
...
...
@@ -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 O
S
stream that writes a file
virtual
autoPtr
<
O
S
stream
>
NewOFstream
(
const
fileName
&
pathname
,
IOstream
::
streamFormat
format
=
IOstream
::
ASCII
,
...
...
src/OpenFOAM/global/fileOperations/uncollatedFileOperation/uncollatedFileOperation.C
View file @
f3106ec1
...
...
@@ -720,7 +720,7 @@ Foam::fileOperations::uncollatedFileOperation::NewIFstream
}
Foam
::
autoPtr
<
Foam
::
Ostream
>
Foam
::
autoPtr
<
Foam
::
O
S
stream
>
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
<
O
S
stream
>
(
new
OFstream
(
pathName
,
fmt
,
ver
,
cmp
));
}
...
...
src/OpenFOAM/global/fileOperations/uncollatedFileOperation/uncollatedFileOperation.H
View file @
f3106ec1
...
...
@@ -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 O
S
stream that writes a file
virtual
autoPtr
<
O
S
stream
>
NewOFstream
(
const
fileName
&
pathname
,
IOstream
::
streamFormat
format
=
IOstream
::
ASCII
,
...
...
src/lagrangian/molecularDynamics/potential/pairPotential/pairPotentialList/pairPotentialList.C
View file @
f3106ec1
...
...
@@ -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
<
O
S
stream
>
ppTabFile
(
fileHandler
().
NewOFstream
(
...
...
@@ -166,7 +166,7 @@ void Foam::pairPotentialList::readPairPotentialDict
if
(
electrostaticPotential_
->
writeTables
())
{
fileHandler
().
mkDir
(
mesh
.
time
().
path
());
autoPtr
<
Ostream
>
ppTabFile
autoPtr
<
O
S
stream
>
ppTabFile
(
fileHandler
().
NewOFstream
(
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment