ENH: ensightFile writeInt() method to replace two-parameter write

- more explicit/transparent handling
- avoids compiler warnings about non-virtual methods
This commit is contained in:
Mark Olesen
2024-02-02 12:27:51 +01:00
parent 6dadd3d33e
commit 4ae4f0928d
3 changed files with 53 additions and 41 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) 2016-2023 OpenCFD Ltd. Copyright (C) 2016-2024 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -221,7 +221,7 @@ Foam::Ostream& Foam::ensightFile::write
} }
Foam::Ostream& Foam::ensightFile::write(const int32_t val) void Foam::ensightFile::writeInt(const int32_t val, const int fieldWidth)
{ {
if (format() == IOstreamOption::BINARY) if (format() == IOstreamOption::BINARY)
{ {
@ -233,24 +233,22 @@ Foam::Ostream& Foam::ensightFile::write(const int32_t val)
} }
else else
{ {
stdStream().width(10); stdStream().width(fieldWidth);
stdStream() << val; stdStream() << val;
syncState(); syncState();
} }
return *this;
} }
Foam::Ostream& Foam::ensightFile::write(const int64_t val) void Foam::ensightFile::writeInt(const int64_t val, const int fieldWidth)
{ {
int32_t ivalue(narrowInt32(val)); int32_t work(narrowInt32(val));
return write(ivalue); writeInt(work, fieldWidth);
} }
Foam::Ostream& Foam::ensightFile::write(const float val) void Foam::ensightFile::writeFloat(const float val, const int fieldWidth)
{ {
if (format() == IOstreamOption::BINARY) if (format() == IOstreamOption::BINARY)
{ {
@ -262,40 +260,45 @@ Foam::Ostream& Foam::ensightFile::write(const float val)
} }
else else
{ {
stdStream().width(12); stdStream().width(fieldWidth);
stdStream() << val; stdStream() << val;
syncState(); syncState();
} }
}
void Foam::ensightFile::writeFloat(const double val, const int fieldWidth)
{
float work(narrowFloat(val));
writeFloat(work, fieldWidth);
}
Foam::Ostream& Foam::ensightFile::write(const int32_t val)
{
writeInt(val, 10);
return *this;
}
Foam::Ostream& Foam::ensightFile::write(const int64_t val)
{
writeInt(val, 10);
return *this;
}
Foam::Ostream& Foam::ensightFile::write(const float val)
{
writeFloat(val, 12);
return *this; return *this;
} }
Foam::Ostream& Foam::ensightFile::write(const double val) Foam::Ostream& Foam::ensightFile::write(const double val)
{ {
float fvalue(narrowFloat(val)); writeFloat(val, 12);
return write(fvalue);
}
Foam::Ostream& Foam::ensightFile::write
(
const label value,
const label fieldWidth
)
{
if (format() == IOstreamOption::BINARY)
{
write(value);
}
else
{
stdStream().width(fieldWidth);
stdStream() << value;
syncState();
}
return *this; return *this;
} }
@ -376,7 +379,7 @@ void Foam::ensightFile::beginParticleCoordinates(const label nparticles)
{ {
writeString("particle coordinates"); writeString("particle coordinates");
newline(); newline();
write(nparticles, 8); // unusual width writeInt(nparticles, 8); // Warning: unusual width
newline(); newline();
} }

View File

@ -163,6 +163,18 @@ public:
//- Write string as "%79s" or as binary (max 80 chars) //- Write string as "%79s" or as binary (max 80 chars)
void writeString(const std::string& str); void writeString(const std::string& str);
//- Write integer value with specified width or as binary
void writeInt(const int32_t val, const int fieldWidth);
//- Write (narrowed) integer value with specified width or as binary
void writeInt(const int64_t val, const int fieldWidth);
//- Write floating-point with specified width or as binary
void writeFloat(const float val, const int fieldWidth);
//- Write (narrowed) floating-point with specified width or as binary
void writeFloat(const double val, const int fieldWidth);
//- Write undef value //- Write undef value
void writeUndef(); void writeUndef();
@ -197,15 +209,12 @@ public:
//- Write string, uses writeString() //- Write string, uses writeString()
virtual Ostream& write(const std::string& str) override; virtual Ostream& write(const std::string& str) override;
//- Write integer as "%10d" or as binary //- Write integer value as "%10d" or as binary
virtual Ostream& write(const int32_t val) override; virtual Ostream& write(const int32_t val) override;
//- Write integer as "%10d" or as binary //- Write integer value as "%10d" or as binary (narrowed to int32_t)
virtual Ostream& write(const int64_t val) override; virtual Ostream& write(const int64_t val) override;
//- Write integer with specified width or as binary
Ostream& write(const label value, const label fieldWidth);
//- Write floating-point as "%12.5e" or as binary //- Write floating-point as "%12.5e" or as binary
virtual Ostream& write(const float val) override; virtual Ostream& write(const float val) override;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2016-2022 OpenCFD Ltd. Copyright (C) 2016-2024 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -61,7 +61,7 @@ static inline label writeMeasured_ascii
{ {
for (const floatVector& p : points) for (const floatVector& p : points)
{ {
os.write(++pointId, 8); // 1-index and an unusual width os.writeInt(++pointId, 8); // 1-index and an unusual width
os.write(p.x()); os.write(p.x());
os.write(p.y()); os.write(p.y());
os.write(p.z()); os.write(p.z());