Commit 0ff02713 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: stream adjustments

- make stream constructors explicit

- remove "using std::ifstream", "using std::iofstream" statements
  for a cleaner namespace.

* copy/move assignments for ITstream

* IStringStream: default construct and construct from std::string
  instead of Foam::string

- reduce some overhead in masterOFstream

- simplify Pstream handling of string variants (#1525)
parent 2270f649
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2019 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -94,7 +94,7 @@ SourceFiles
namespace Foam
{
// Forward declarations
// Forward Declarations
class Time;
class objectRegistry;
......@@ -106,7 +106,7 @@ class IOobject
{
public:
// Public data types
// Public Data Types
//- Enumeration defining the valid states of an IOobject
enum objectState : char
......@@ -198,7 +198,7 @@ protected:
public:
//- Runtime type information
//- Declare type-name, virtual type (with debug switch)
TypeName("IOobject");
......@@ -208,7 +208,7 @@ public:
static fileCheckTypes fileModificationChecking;
// Static Member Functions
// Static Functions
//- Split path into instance, local, name components
//
......@@ -267,6 +267,15 @@ public:
);
// Generated Methods
//- Copy construct
IOobject(const IOobject&) = default;
//- Destructor
virtual ~IOobject() = default;
// Constructors
//- Construct from name, instance, registry, io options
......@@ -309,14 +318,14 @@ public:
bool globalObject = false
);
//- Construct as copy resetting registry
//- Copy construct, resetting registry
IOobject
(
const IOobject& io,
const objectRegistry& registry
);
//- Construct as copy resetting name
//- Copy construct, resetting name
IOobject
(
const IOobject& io,
......@@ -336,10 +345,6 @@ public:
}
//- Destructor
virtual ~IOobject() = default;
// Member Functions
// General access
......
......@@ -34,12 +34,12 @@ License
namespace Foam
{
defineTypeNameAndDebug(baseIOdictionary, 0);
defineTypeNameAndDebug(baseIOdictionary, 0);
bool baseIOdictionary::writeDictionaries
(
debug::infoSwitch("writeDictionaries", 0)
);
bool baseIOdictionary::writeDictionaries
(
debug::infoSwitch("writeDictionaries", 0)
);
}
......@@ -77,12 +77,6 @@ Foam::baseIOdictionary::baseIOdictionary
}
// * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
Foam::baseIOdictionary::~baseIOdictionary()
{}
// * * * * * * * * * * * * * * * Members Functions * * * * * * * * * * * * * //
const Foam::word& Foam::baseIOdictionary::name() const
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -59,15 +60,28 @@ class baseIOdictionary
public regIOobject,
public dictionary
{
// Private data
// Private Data
static bool writeDictionaries;
public:
//- Declare type-name, virtual type (with debug switch)
TypeName("dictionary");
// Generated Methods
//- Copy construct
baseIOdictionary(const baseIOdictionary&) = default;
//- Move construct
baseIOdictionary(baseIOdictionary&&) = default;
//- Destructor
virtual ~baseIOdictionary() = default;
// Constructors
//- Construct given an IOobject
......@@ -80,35 +94,33 @@ public:
baseIOdictionary(const IOobject&, Istream&);
//- Destructor
virtual ~baseIOdictionary();
// Member functions
// Member Functions
//- Return complete path + object name if the file exists
// either in the case/processor or case otherwise null
//- either in the case/processor or case otherwise null
virtual fileName filePath() const = 0;
//- Name function is needed to disambiguate those inherited
// from regIOobject and dictionary
//- from regIOobject and dictionary
const word& name() const;
//- ReadData function required for regIOobject read operation
//- The readData function required by regIOobject read operation
virtual bool readData(Istream&);
//- WriteData function required for regIOobject write operation
//- The writeData function required by regIOobject write operation
virtual bool writeData(Ostream&) const;
//- Is object global
virtual bool global() const = 0;
// Member operators
// Member Operators
//- Assignment of other baseIOdictionary's entries to this
// baseIOdictionary
//- Copy assignment of dictionary entries (leave regIOobject untouched)
void operator=(const baseIOdictionary&);
//- Copy assignment of dictionary entries
using dictionary::operator=;
};
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2017-2018 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -43,7 +43,6 @@ SourceFiles
#include "className.H"
#include <fstream>
using std::ifstream;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -85,7 +84,6 @@ protected:
//- Delete the stream pointer
void deallocate();
};
} // End namespace Detail
......@@ -102,14 +100,14 @@ class IFstream
{
public:
// Declare name of the class and its debug switch
//- Declare type-name (with debug switch)
ClassName("IFstream");
// Constructors
//- Construct from pathname
IFstream
explicit IFstream
(
const fileName& pathname,
streamFormat format=ASCII,
......@@ -143,7 +141,7 @@ public:
// Print
//- Print description of IOstream to Ostream
//- Print stream description to Ostream
virtual void print(Ostream& os) const;
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2017-2018 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -43,7 +43,6 @@ SourceFiles
#include "className.H"
#include <fstream>
using std::ofstream;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -87,7 +86,6 @@ protected:
//- Delete the stream pointer
void deallocate();
};
} // End namespace Detail
......@@ -104,14 +102,14 @@ class OFstream
{
public:
// Declare name of the class and its debug switch
//- Declare type-name (with debug switch)
ClassName("OFstream");
// Constructors
//- Construct from pathname
OFstream
explicit OFstream
(
const fileName& pathname,
streamFormat format=ASCII,
......@@ -125,7 +123,7 @@ public:
~OFstream() = default;
// Member functions
// Member Functions
// Access
......@@ -144,7 +142,7 @@ public:
// Print
//- Print description of IOstream to Ostream
//- Print stream description to Ostream
void print(Ostream& os) const;
};
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -31,15 +32,23 @@ License
#include "PstreamBuffers.H"
#include "masterUncollatedFileOperation.H"
#include "boolList.H"
#include <algorithm>
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::masterOFstream::checkWrite
(
const fileName& fName,
const string& str
const char* str,
std::streamsize len
)
{
if (!len)
{
// Can probably skip all of this if there is nothing to write
return;
}
mkDir(fName.path());
OFstream os
......@@ -53,43 +62,35 @@ void Foam::masterOFstream::checkWrite
if (!os.good())
{
FatalIOErrorInFunction(os)
<< "Could not open file " << fName
<< "Could not open file " << fName << nl
<< exit(FatalIOError);
}
os.writeQuoted(str, false);
// Use writeRaw() instead of writeQuoted(string,false) to output
// characters directly.
os.writeRaw(str, len);
if (!os.good())
{
FatalIOErrorInFunction(os)
<< "Failed writing to " << fName
<< "Failed writing to " << fName << nl
<< exit(FatalIOError);
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::masterOFstream::masterOFstream
void Foam::masterOFstream::checkWrite
(
const fileName& pathName,
streamFormat format,
versionNumber version,
compressionType compression,
const bool append,
const bool valid
const fileName& fName,
const std::string& s
)
:
OStringStream(format, version),
pathName_(pathName),
compression_(compression),
append_(append),
valid_(valid)
{}
{
checkWrite(fName, &s[0], s.length());
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::masterOFstream::~masterOFstream()
void Foam::masterOFstream::commit()
{
if (Pstream::parRun())
{
......@@ -109,10 +110,13 @@ Foam::masterOFstream::~masterOFstream()
{
if (Pstream::master() && valid_)
{
checkWrite(pathName_, str());
checkWrite(pathName_, this->str());
}
this->reset();
return;
}
boolList valid(Pstream::nProcs());
valid[Pstream::myProcNo()] = valid_;
Pstream::gatherList(valid);
......@@ -126,7 +130,9 @@ Foam::masterOFstream::~masterOFstream()
{
UOPstream os(Pstream::masterNo(), pBufs);
string s(this->str());
os.write(&s[0], s.size());
this->reset();
os.write(&s[0], s.length());
}
labelList recvSizes;
......@@ -134,36 +140,70 @@ Foam::masterOFstream::~masterOFstream()
if (Pstream::master())
{
// Write my own data
// Write master data
if (valid[Pstream::masterNo()])
{
if (valid[Pstream::myProcNo()])
{
checkWrite(filePaths[Pstream::myProcNo()], str());
}
checkWrite(filePaths[Pstream::masterNo()], this->str());
}
this->reset();
// Find the max slave size
recvSizes[Pstream::masterNo()] = 0;
List<char> buf
(
*std::max_element(recvSizes.cbegin(), recvSizes.cend())
);
for (label proci = 1; proci < Pstream::nProcs(); proci++)
for (label proci = 1; proci < Pstream::nProcs(); ++proci)
{
UIPstream is(proci, pBufs);
List<char> buf(recvSizes[proci]);
is.read(buf.begin(), buf.size());
const std::streamsize count(recvSizes[proci]);
is.read(buf.data(), count);
if (valid[proci])
{
checkWrite
(
filePaths[proci],
string(buf.begin(), buf.size())
);
checkWrite(filePaths[proci], buf.cdata(), count);
}
}
}
}
else
{
checkWrite(pathName_, str());
checkWrite(pathName_, this->str());
this->reset();
}
// This method is only called once (internally)
// so no need to clear/flush old buffered data
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::masterOFstream::masterOFstream
(
const fileName& pathName,
streamFormat format,
versionNumber version,
compressionType compression,
const bool append,
const bool valid
)
:
OStringStream(format, version),
pathName_(pathName),
compression_(compression),
append_(append),
valid_(valid)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::masterOFstream::~masterOFstream()
{
commit();
}
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -52,7 +53,7 @@ class masterOFstream
:
public OStringStream
{
// Private data
// Private Data
const fileName pathName_;
......@@ -66,8 +67,19 @@ class masterOFstream
// Private Member Functions
//- Open file with checking
void checkWrite(const fileName& fName, const string& str);
//- Open file with checking and write append contents
void checkWrite
(
const fileName& fName,
const char* str,
std::streamsize len
);
//- Open file with checking and write append contents
void checkWrite(const fileName& fName, const std::string& s);
//- Commit buffered information, including parallel gather as required
void commit();
public:
......@@ -75,7 +87,7 @@ public:
// Constructors
//- Construct and set stream status
masterOFstream
explicit masterOFstream
(
const fileName& pathname,
streamFormat format=ASCII,
......@@ -86,7 +98,7 @@ public:
);
//- Destructor
//- Destructor - commits buffered information to file
~masterOFstream();
};
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2018-2019 OpenCFD Ltd.
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -147,6 +147,15 @@ protected:
public:
// Generated Methods
//- Copy construct
IOstream(const IOstream&) = default;
//- Destructor
virtual ~IOstream() = default;
// Constructors
//- Construct with specified stream option
......@@ -174,10 +183,6 @@ public:
{}
//- Destructor
virtual ~IOstream() = default;
// Member Functions
// Access
......@@ -383,7 +388,7 @@ public:
// Print
//- Print description of IOstream to Ostream
//- Print stream description to Ostream
virtual void print(Ostream& os) const;
//- Print information about the stream state bits
......
......@@ -74,9 +74,18 @@ class Istream
public:
// Generated Methods
//- Copy construct
Istream(const Istream&) = default;
//- Destructor
virtual ~Istream() = default;
// Constructors
//- Construct and set stream status
//- Default construct, set stream status