DEFEATURE: remove OStringStream::count() method (issue #3281)

- as seen in #3201, using count() based on the ostringstream tellp
  is not reliable since it is not updated with reset or copying.

STYLE: minor changes to string/char/span streams

- update docs to only mention string_view
- use auto return, without extra trailing 'decltype'

ENH: add IOstream size check helper methods

  * checkNativeSizes() : test only
  * fatalCheckNativeSizes() : an assert with FatalIOError
This commit is contained in:
Mark Olesen
2025-08-04 15:08:28 +02:00
parent fcc8e62e47
commit 6a72b0e607
14 changed files with 71 additions and 58 deletions

View File

@ -89,12 +89,6 @@ Ostream& printView(Ostream& os, std::string_view s)
}
Ostream& printView(Ostream& os, stdFoam::span<char> s)
{
return printView(os, s.begin(), s.end());
}
Ostream& printView(Ostream& os, const UList<char>& list)
{
return printView(os, list.begin(), list.end());

View File

@ -89,12 +89,6 @@ Ostream& printView(Ostream& os, std::string_view s)
}
Ostream& printView(Ostream& os, stdFoam::span<char> s)
{
return printView(os, s.begin(), s.end());
}
Ostream& printView(Ostream& os, const UList<char>& list)
{
return printView(os, list.begin(), list.end());

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2023 OpenCFD Ltd.
Copyright (C) 2017-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -65,7 +65,6 @@ int main(int argc, char *argv[])
OCountStream cnt;
OCharStream cstr;
OStringStream sstr;
ocountstream plain;
generateOutput(cstr);
@ -77,7 +76,6 @@ int main(int argc, char *argv[])
Info<< "counter state: " << (cnt.stdStream().rdstate()) << nl
<< "via char-stream: " << label(cstr.view().size()) << " chars" << nl
<< "via string-stream: " << label(sstr.count()) << " chars" << nl
<< "via ocountstream: " << plain.count() << " chars" << endl;
fileName outputName;

View File

@ -91,12 +91,6 @@ Ostream& printView(Ostream& os, std::string_view s)
}
Ostream& printView(Ostream& os, stdFoam::span<char> s)
{
return printView(os, s.begin(), s.end());
}
Ostream& printView(Ostream& os, const UList<char>& list)
{
return printView(os, list.begin(), list.end());

View File

@ -71,6 +71,26 @@ bool Foam::IOstream::fatalCheck(const char* operation) const
}
bool Foam::IOstream::fatalCheckNativeSizes(const char* operation) const
{
const bool ok = this->checkNativeSizes();
if (!ok)
{
FatalIOErrorInFunction(*this)
<< "Error in stream: " << relativeName()
<< " for operation " << operation << nl
<< "Expecting (label=" << (8*sizeof(label))
<< ";scalar=" << (8*sizeof(scalar))
<< ") found (label=" << (8*this->labelByteSize())
<< ";scalar=" << (8*this->scalarByteSize()) << ')' << nl
<< exit(FatalIOError);
}
return ok;
}
void Foam::IOstream::print(Ostream& os) const
{
os << "IOstream: " << "Version " << version() << ", format "

View File

@ -297,7 +297,7 @@ public:
}
//- Check if the label byte-size associated with the stream
//- Test if the label byte-size associated with the stream
//- is the same as the given type
template<class T = label>
std::enable_if_t<std::is_integral_v<T>, bool>
@ -306,7 +306,7 @@ public:
return sizeofLabel_ == sizeof(T);
}
//- Check if the scalar byte-size associated with the stream
//- Test if the scalar byte-size associated with the stream
//- is the same as the given type
template<class T = scalar>
std::enable_if_t<std::is_floating_point_v<T>, bool>
@ -315,6 +315,22 @@ public:
return sizeofScalar_ == sizeof(T);
}
//- Test if the label/scalar byte-size associated with the stream
//- are the native label/scalar sizes
bool checkNativeSizes() const noexcept
{
return
(
sizeofLabel_ == sizeof(label)
&& sizeofScalar_ == sizeof(scalar)
);
}
//- Assert that the label/scalar byte-size associated with the stream
//- are the native label/scalar sizes.
// Generate a FatalIOError for any mismatch.
bool fatalCheckNativeSizes(const char* operation) const;
// Stream State Functions

View File

@ -215,9 +215,6 @@ public:
// Member Functions
//- The number of bytes outputted
std::streamsize count() { return stream_.tellp(); }
//- Get the string.
//- As Foam::string instead of std::string (may change in future)
Foam::string str() const { return Foam::string(stream_.str()); }

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2024 OpenCFD Ltd.
Copyright (C) 2017-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -141,8 +141,8 @@ public:
);
}
//- A string_view (c++17) or span view (older c++) of buffer contents
auto view() const -> decltype(buffer_type::view())
//- A string_view of buffer contents
auto view() const
{
return buffer_type::view();
}
@ -290,15 +290,15 @@ public:
//- Span of the input characters (is modifiable!)
UList<char> list() const { return stream_.list(); }
//- A string_view (c++17) or span view (older c++) of buffer contents
auto view() const -> decltype(stream_.view())
//- A string_view of buffer contents
auto view() const
{
return stream_.view();
}
//- For IStringStream compatibility, return the buffer as string copy.
// Use sparingly - it creates a full copy!!
auto str() const -> decltype(stream_.str())
auto str() const
{
return stream_.str();
}

View File

@ -169,8 +169,8 @@ public:
);
}
//- A string_view (c++17) or span view (older c++) of buffer contents
auto view() const -> decltype(buffer_type::view())
//- A string_view of buffer contents
auto view() const
{
return buffer_type::view();
}
@ -351,15 +351,15 @@ public:
//- Span of the current input characters (is modifiable!)
UList<char> list() const { return stream_.list(); }
//- A string_view (c++17) or span view (older c++) of buffer contents
auto view() const -> decltype(stream_.view())
//- A string_view of buffer contents
auto view() const
{
return stream_.view();
}
//- For IStringStream compatibility, return the buffer as string copy.
// Use sparingly - it creates a full copy!!
auto str() const -> decltype(stream_.str())
auto str() const
{
return stream_.str();
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2024 OpenCFD Ltd.
Copyright (C) 2017-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -147,8 +147,8 @@ public:
);
}
//- A string_view (c++17) or span view (older c++) of buffer contents
auto view() const -> decltype(buffer_type::view())
//- A string_view of buffer contents
auto view() const
{
return buffer_type::view();
}
@ -273,15 +273,15 @@ public:
//- Span of the current output characters (is modifiable!)
UList<char> list() const { return stream_.list(); }
//- A string_view (c++17) or span view (older c++) of buffer contents
auto view() const -> decltype(stream_.view())
//- A string_view of buffer contents
auto view() const
{
return stream_.view();
}
//- For OStringStream compatibility, return the buffer as string copy.
// Use sparingly - it creates a full copy!!
auto str() const -> decltype(stream_.str())
auto str() const
{
return stream_.str();
}

View File

@ -162,8 +162,8 @@ public:
);
}
//- A string_view (c++17) or span view (older c++) of buffer contents
auto view() const -> decltype(buffer_type::view())
//- A string_view of buffer contents
auto view() const
{
return buffer_type::view();
}
@ -305,15 +305,15 @@ public:
//- Span of the current output characters (is modifiable!)
UList<char> list() const { return stream_.list(); }
//- A string_view (c++17) or span view (older c++) of buffer contents
auto view() const -> decltype(stream_.view())
//- A string_view of buffer contents
auto view() const
{
return stream_.view();
}
//- For OStringStream compatibility, return buffer as string copy.
// Use sparingly - it creates a full copy!!
auto str() const -> decltype(stream_.str())
auto str() const
{
return stream_.str();
}

View File

@ -64,9 +64,9 @@ Foam::Istream& Foam::operator>>
{
is >> rhs.normal_;
}
else if (!is.checkLabelSize<>() || !is.checkScalarSize<>())
else if (!is.checkScalarSize<>())
{
// Non-native label or scalar size
// Non-native scalar size
is.beginRawRead();
readRawScalar(is, rhs.normal_.data(), vector::nComponents);

View File

@ -5,7 +5,7 @@
\ / A nd | www.openfoam.com
\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2024 OpenCFD Ltd.
Copyright (C) 2024-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -53,7 +53,7 @@ bool Foam::substitutionModels::dictionaryValue::processDict
{
const string& lookup = entries_[key];
OStringStream oss;
OCharStream oss;
if (lookup.empty())
{
// Add complete dictionary
@ -216,4 +216,4 @@ Foam::wordList Foam::substitutionModels::dictionaryValue::keys() const
}
// ************************************************************************* //
// ************************************************************************* //

View File

@ -5,7 +5,7 @@
\ / A nd | www.openfoam.com
\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2024 OpenCFD Ltd.
Copyright (C) 2024-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -124,15 +124,15 @@ bool Foam::substitutionModels::fileRegEx::apply
Info<< "Cached " << lines.size() << " lines" << endl;
OStringStream oss;
regExp re(entries_[key].c_str());
OCharStream oss;
const regExp re(entries_[key].c_str());
for (const string& data : lines)
{
regExp::results_type match;
if (re.match(data, match))
{
oss.reset();
oss.rewind();
for (size_t i = 1; i < match.size(); ++i)
{
@ -160,4 +160,4 @@ Foam::wordList Foam::substitutionModels::fileRegEx::keys() const
}
// ************************************************************************* //
// ************************************************************************* //