ENH: support move sematics for get/set putback token

This commit is contained in:
Mark Olesen 2023-08-09 15:05:11 +02:00
parent 944840f8d6
commit 4daaf6dd2a
5 changed files with 64 additions and 28 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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