From 4daaf6dd2a44979caa091d9150fa5d8a021464bf Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 9 Aug 2023 15:05:11 +0200 Subject: [PATCH] ENH: support move sematics for get/set putback token --- src/OpenFOAM/db/IOstreams/IOstreams/Istream.C | 54 ++++++++++++++----- src/OpenFOAM/db/IOstreams/IOstreams/Istream.H | 18 +++---- .../db/IOstreams/Pstreams/UIPstreamBase.C | 9 ++-- src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C | 7 +-- src/OpenFOAM/db/IOstreams/token/token.H | 4 ++ 5 files changed, 64 insertions(+), 28 deletions(-) diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Istream.C b/src/OpenFOAM/db/IOstreams/IOstreams/Istream.C index e3e6875ae1..0c6b4cd824 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams/Istream.C +++ b/src/OpenFOAM/db/IOstreams/IOstreams/Istream.C @@ -51,19 +51,27 @@ const Foam::token& Foam::Istream::peekBack() const noexcept return (putBackAvail_ ? putBackToken_ : token::undefinedToken); } +// Return the putback token if available or fetch a new token +// from the stream. +// +// Foam::token& Foam::Istream::peekToken() +// { +// if (!putBackAvail_) +// { +// putBackToken_.reset(); +// token tok; +// this->read(tok); +// putBackToken_ = std::move(tok); +// } +// +// return putBackToken_; +// } -bool Foam::Istream::peekBack(token& tok) + +void Foam::Istream::putBackClear() { - if (putBackAvail_) - { - tok = putBackToken_; - } - else - { - tok.reset(); - } - - return putBackAvail_; + putBackAvail_ = false; + putBackToken_.reset(); } @@ -89,6 +97,28 @@ void Foam::Istream::putBack(const token& tok) } +void Foam::Istream::putBack(token&& tok) +{ + if (bad()) + { + FatalIOErrorInFunction(*this) + << "Attempt to put back onto bad stream" + << exit(FatalIOError); + } + else if (putBackAvail_) + { + FatalIOErrorInFunction(*this) + << "Attempt to put back another token" + << exit(FatalIOError); + } + else + { + putBackAvail_ = true; + putBackToken_ = std::move(tok); + } +} + + bool Foam::Istream::getBack(token& tok) { if (bad()) @@ -100,7 +130,7 @@ bool Foam::Istream::getBack(token& tok) else if (putBackAvail_) { putBackAvail_ = false; - tok = putBackToken_; + tok = std::move(putBackToken_); return true; } diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Istream.H b/src/OpenFOAM/db/IOstreams/IOstreams/Istream.H index 6cd20ba048..1710eab7bf 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams/Istream.H +++ b/src/OpenFOAM/db/IOstreams/IOstreams/Istream.H @@ -77,10 +77,7 @@ protected: // Protected Member Functions //- True if putback token is in use - bool hasPutback() const noexcept - { - return putBackAvail_; - } + bool hasPutback() const noexcept { return putBackAvail_; } public: @@ -124,15 +121,16 @@ public: // if a putback is unavailable. const token& peekBack() const noexcept; - //- Fetch putback token without removing it. - // \return false sets the token to undefined if no put-back - // was available - bool peekBack(token& tok); + //- Drop the putback token + void putBackClear(); - //- Put back a token. Only a single put back is permitted + //- Put back a token (copy). Only a single put back is permitted void putBack(const token& tok); - //- Get the put-back token if there is one. + //- Put back a token (move). Only a single put back is permitted + void putBack(token&& tok); + + //- Retrieve the put-back token if there is one. // \return false and sets token to undefined if no put-back // was available bool getBack(token& tok); diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstreamBase.C b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstreamBase.C index c4d4b8def6..4acde36731 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstreamBase.C +++ b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstreamBase.C @@ -273,6 +273,12 @@ Foam::Istream& Foam::UIPstreamBase::read(token& t) } } + + // Reset token, adjust its line number according to the stream + t.reset(); + t.lineNumber(this->lineNumber()); + + // Read character, return on error // - with additional handling for special stream flags @@ -303,9 +309,6 @@ Foam::Istream& Foam::UIPstreamBase::read(token& t) while (c == token::FLAG); - // Set the line number of this token to the current stream line number - t.lineNumber(this->lineNumber()); - // Analyse input starting with this character. switch (c) { diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C index cfc1deca0f..4385fce1f6 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C +++ b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C @@ -538,6 +538,10 @@ Foam::Istream& Foam::ISstream::read(token& t) return *this; } + // Reset token, adjust its line number according to the stream + t.reset(); + t.lineNumber(this->lineNumber()); + // Assume that the streams supplied are in working order. // Lines are counted by '\n' @@ -546,9 +550,6 @@ Foam::Istream& Foam::ISstream::read(token& t) char c = nextValid(); - // Set the line number of this token to the current stream line number - t.lineNumber(this->lineNumber()); - // Return on error if (!c) { diff --git a/src/OpenFOAM/db/IOstreams/token/token.H b/src/OpenFOAM/db/IOstreams/token/token.H index ec2fa135be..be3cb0194c 100644 --- a/src/OpenFOAM/db/IOstreams/token/token.H +++ b/src/OpenFOAM/db/IOstreams/token/token.H @@ -510,6 +510,9 @@ public: //- Token is COMPOUND inline bool isCompound() const noexcept; + //- True if token is not UNDEFINED or ERROR. Same as good(). + explicit operator bool() const noexcept { return good(); } + // Access @@ -690,6 +693,7 @@ public: void operator=(string*) = delete; }; + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // IOstream Operators