Skip to content
Snippets Groups Projects
Commit 4daaf6dd authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: support move sematics for get/set putback token

parent 944840f8
No related branches found
No related tags found
1 merge request!619Extend traits to include VectorSpace tests and wrapped access to pTraits static members
......@@ -51,23 +51,53 @@ 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_;
// }
void Foam::Istream::putBackClear()
{
putBackAvail_ = false;
putBackToken_.reset();
}
bool Foam::Istream::peekBack(token& tok)
void Foam::Istream::putBack(const token& tok)
{
if (putBackAvail_)
if (bad())
{
tok = putBackToken_;
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
{
tok.reset();
putBackAvail_ = true;
putBackToken_ = tok;
}
return putBackAvail_;
}
void Foam::Istream::putBack(const token& tok)
void Foam::Istream::putBack(token&& tok)
{
if (bad())
{
......@@ -84,7 +114,7 @@ void Foam::Istream::putBack(const token& tok)
else
{
putBackAvail_ = true;
putBackToken_ = tok;
putBackToken_ = std::move(tok);
}
}
......@@ -100,7 +130,7 @@ bool Foam::Istream::getBack(token& tok)
else if (putBackAvail_)
{
putBackAvail_ = false;
tok = putBackToken_;
tok = std::move(putBackToken_);
return true;
}
......
......@@ -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);
......
......@@ -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)
{
......
......@@ -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)
{
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment