ENH: minor improvements, cleanup of token class

- relax casting rules
  * down-cast of labelToken to boolToken
  * up-cast of wordToken to stringToken.
    Can use isStringType() test for word or string types

- simplify constructors, move construct etc.

- expose reset() method as public, which resets to UNDEFINED and
  clears allocated storage etc.

DEFEATURE: remove assign from word or string pointer.

- This was deprecated 2017-11 and now removed.
  For this type of content transfer, move assignment should be used
  instead of stealing pointers.
This commit is contained in:
Mark Olesen 2019-08-20 17:48:31 +02:00
parent 74f1195ff1
commit 9d36d3c08a
7 changed files with 192 additions and 232 deletions

View File

@ -34,7 +34,7 @@ Foam::token Foam::dictionaryTokens::keywordToken(const entry& e)
if (k.empty())
{
return token::undefinedToken;
return token();
}
if (k.isPattern())
{

View File

@ -98,7 +98,7 @@ bool Foam::Istream::peekBack(token& tok)
}
else
{
tok = token::undefinedToken;
tok.reset();
}
return putBack_;

View File

@ -37,7 +37,7 @@ Foam::label Foam::ITstream::parseStream(ISstream& is, tokenList& tokens)
label nTok = 0;
tokens.clear();
tokens.setSize(64, token::undefinedToken);
tokens.resize(64, token());
token tok;
while (!is.read(tok).bad() && tok.good())
@ -45,7 +45,7 @@ Foam::label Foam::ITstream::parseStream(ISstream& is, tokenList& tokens)
tokens.newElmt(nTok++) = std::move(tok);
}
tokens.setSize(nTok);
tokens.resize(nTok);
return nTok;
}
@ -243,7 +243,7 @@ Foam::Istream& Foam::ITstream::read(token& tok)
setEof();
}
tok = token::undefinedToken;
tok.reset();
if (size())
{

View File

@ -36,7 +36,6 @@ namespace Foam
defineRunTimeSelectionTable(tokenCompound, Istream);
}
const char* const Foam::token::typeName = "token";
const Foam::token Foam::token::undefinedToken;

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2004-2011, 2017 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2004-2011, 2017-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
@ -57,12 +57,9 @@ SourceFiles
namespace Foam
{
// Forward declarations
// Forward Declarations
class token;
Istream& operator>>(Istream& is, token& t);
Ostream& operator<<(Ostream& os, const token& t);
Ostream& operator<<(Ostream& os, const token& tok);
/*---------------------------------------------------------------------------*\
@ -81,9 +78,9 @@ public:
UNDEFINED = 0, //!< An undefined token-type
// Fundamental types
BOOL, //!< boolean type
FLAG, //!< stream flag (1-byte bitmask)
PUNCTUATION, //!< single character punctuation
BOOL, //!< boolean type
LABEL, //!< label (integer) type
FLOAT_SCALAR, //!< float (single-precision) type
DOUBLE_SCALAR, //!< double (double-precision) type
@ -106,12 +103,12 @@ public:
{
NO_FLAG = 0, //!< No flags
ASCII = 1, //!< ASCII-mode stream
BINARY = 2, //!< BINARY-mode stream
BINARY = 2 //!< BINARY-mode stream
};
//- Standard punctuation tokens (a character)
enum punctuationToken
enum punctuationToken : char
{
NULL_TOKEN = '\0',
SPACE = ' ',
@ -247,7 +244,7 @@ public:
};
//- Static undefined token
//- An undefined token
static const token undefinedToken;
@ -273,7 +270,7 @@ private:
};
// Private data
// Private Data
//- The data content (as a union).
// For memory alignment this should appear as the first member.
@ -291,23 +288,21 @@ private:
//- Set as UNDEFINED and zero the union content without any checking
inline void setUndefined();
//- Clear any allocated storage (word or string) and set to UNDEFINED
inline void clear();
// Parse error, expected 'expected', found ...
void parseError(const char* expected) const;
public:
// Static data members
// Static Data Members
static const char* const typeName;
//- The type name is "token"
static constexpr const char* const typeName = "token";
// Constructors
//- Construct null
//- Construct null, initialized to an UNDEFINED token.
inline constexpr token() noexcept;
//- Copy construct
@ -317,44 +312,31 @@ public:
inline token(token&& t);
//- Construct punctuation character token
inline explicit token(punctuationToken p);
inline explicit token(punctuationToken p, label lineNumber=0);
//- Construct label token
inline explicit token(const label val);
inline explicit token(const label val, label lineNumber=0);
//- Construct floatScalar token
inline explicit token(const floatScalar val);
//- Construct float token
inline explicit token(const floatScalar val, label lineNumber=0);
//- Construct doubleScalar token
inline explicit token(const doubleScalar val);
//- Construct double token
inline explicit token(const doubleScalar val, label lineNumber=0);
//- Construct word token by copying word contents
inline explicit token(const word& w);
//- Copy construct word token
inline explicit token(const word& w, label lineNumber=0);
//- Construct string token by copying string contents
inline explicit token(const string& str);
//- Copy construct string token
inline explicit token(const string& str, label lineNumber=0);
//- Move construct word token
inline explicit token(word&& w, label lineNumber=0);
//- Construct punctuation character token
inline token(punctuationToken p, const label lineNumber);
//- Construct label token
inline token(const label val, const label lineNumber);
//- Construct floatScalar token
inline token(const floatScalar val, const label lineNumber);
//- Construct doubleScalar token
inline token(const doubleScalar val, const label lineNumber);
//- Construct word token by copying word contents
inline token(const word& w, const label lineNumber);
//- Construct string token by copying string contents
inline token(const string& str, const label lineNumber);
//- Move construct string token
inline explicit token(string&& str, label lineNumber=0);
//- Construct from Istream
token(Istream& is);
explicit token(Istream& is);
//- Destructor
@ -380,7 +362,7 @@ public:
inline static bool isseparator(int c);
// Member functions
// Member Functions
// Status
@ -392,7 +374,7 @@ public:
//- Change the token type, for similar types.
// This can be used to change between string-like variants
// (eg, STRING, VARIABLE, VERBATIMSTRING)
// (eg, STRING, VARIABLE, etc)
// To change types entirely (eg, STRING to DOUBLE_SCALAR),
// use the corresponding assignment operator.
//
@ -444,12 +426,18 @@ public:
//- True if token is WORD
inline bool isWord() const;
//- True if token is STRING, VARIABLE or VERBATIMSTRING
//- True if token is STRING, VARIABLE or VERBATIM string
inline bool isString() const;
//- True if token is VARIABLE
inline bool isVariable() const;
//- True if token is VERBATIM string
inline bool isVerbatim() const;
//- True if token is WORD, STRING, VARIABLE or VERBATIM
inline bool isStringType() const;
//- True if token is COMPOUND
inline bool isCompound() const;
@ -457,7 +445,7 @@ public:
// Access
//- Return boolean token value.
// Report FatalIOError and return false if token is not BOOL
// Report FatalIOError and return false if token is not BOOL or LABEL
inline bool boolToken() const;
//- Return flag bitmask value.
@ -473,20 +461,20 @@ public:
inline label labelToken() const;
//- Return float value.
// Report FatalIOError and return \b 0.0 if token is not FLOAT_SCALAR
// Report FatalIOError and return \b 0 if token is not FLOAT_SCALAR
inline floatScalar floatScalarToken() const;
//- Return double value.
// Report FatalIOError and return \b 0.0 if token is not DOUBLE_SCALAR
// Report FatalIOError and return \b 0 if token is not DOUBLE_SCALAR
inline doubleScalar doubleScalarToken() const;
//- Return float or double value.
// Report FatalIOError and return \b 0.0 if token is not a
// Report FatalIOError and return \b 0 if token is not a
// FLOAT_SCALAR or DOUBLE_SCALAR
inline scalar scalarToken() const;
//- Return label, float or double value.
// Report FatalIOError and return \b 0.0 if token is not a
// Report FatalIOError and return \b 0 if token is not a
// LABEL, FLOAT_SCALAR or DOUBLE_SCALAR
inline scalar number() const;
@ -496,7 +484,7 @@ public:
//- Return const reference to the string contents.
// Report FatalIOError and return \b "" if token is not a
// STRING, VARIABLE or VERBATIMSTRING
// STRING, VARIABLE, VERBATIM or an upcast WORD
inline const string& stringToken() const;
//- Read access for compound token
@ -510,7 +498,10 @@ public:
// Edit
//- Clear token and set to be in an error state.
//- Reset token to UNDEFINED and clear any allocated storage
inline void reset();
//- Clear token and set to be ERROR.
inline void setBad();
//- Swap token contents: type, data, line-number
@ -526,7 +517,7 @@ public:
}
// Member operators
// Member Operators
// Assignment
@ -564,59 +555,64 @@ public:
inline void operator=(compound* compoundPtr);
//- Deprecated(2017-11) transfer word pointer to the token
// \deprecated(2017-11) - use move assign from word
inline void operator=(word* wordPtr);
//- Deprecated(2017-11) transfer string pointer to the token
// \deprecated(2017-11) - use move assign from string
inline void operator=(string* stringPtr);
// Equality
inline bool operator==(const token& t) const;
inline bool operator==(const token& tok) const;
inline bool operator==(const punctuationToken p) const;
inline bool operator==(const label val) const;
inline bool operator==(const floatScalar val) const;
inline bool operator==(const doubleScalar val) const;
inline bool operator==(const word& w) const;
inline bool operator==(const string& str) const;
inline bool operator==(const std::string& s) const;
// Inequality
inline bool operator!=(const token& t) const;
inline bool operator!=(const token& tok) const;
inline bool operator!=(const punctuationToken p) const;
inline bool operator!=(const label val) const;
inline bool operator!=(const floatScalar val) const;
inline bool operator!=(const doubleScalar val) const;
inline bool operator!=(const word& w) const;
inline bool operator!=(const string& str) const;
inline bool operator!=(const std::string& s) const;
// IOstream operators
// IOstream Operators
friend Istream& operator>>(Istream& is, token& t);
friend Ostream& operator<<(Ostream& os, const token& t);
friend Ostream& operator<<(Ostream& os, const token& tok);
friend Ostream& operator<<(Ostream&, const punctuationToken&);
friend ostream& operator<<(ostream&, const punctuationToken&);
friend Ostream& operator<<(Ostream& os, const punctuationToken& pt);
friend ostream& operator<<(ostream& os, const punctuationToken& pt);
friend ostream& operator<<(ostream&, const InfoProxy<token>&);
friend ostream& operator<<(ostream& os, const InfoProxy<token>& ct);
// Housekeeping
//- Deprecated(2017-11) transfer word pointer to the token
// \deprecated(2017-11) - use move assign from word
void operator=(word*) = delete;
//- Deprecated(2017-11) transfer string pointer to the token
// \deprecated(2017-11) - use move assign from string
void operator=(string*) = delete;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Ostream& operator<<(Ostream&, const token::punctuationToken&);
ostream& operator<<(ostream&, const token::punctuationToken&);
Ostream& operator<<(Ostream&, const token::compound&);
// IOstream Operators
ostream& operator<<(ostream&, const InfoProxy<token>&);
Istream& operator>>(Istream& is, token& tok);
Ostream& operator<<(Ostream& os, const token::punctuationToken& pt);
ostream& operator<<(ostream& os, const token::punctuationToken& pt);
Ostream& operator<<(Ostream& os, const token::compound& ct);
ostream& operator<<(ostream& os, const InfoProxy<token>& ip);
template<>
Ostream& operator<<(Ostream& os, const InfoProxy<token>& ip);
// Handling of compound types
#define defineCompoundTypeName(Type, Name) \
defineTemplateTypeNameAndDebugWithName(token::Compound<Type>, #Type, 0);

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2004-2011, 2017-2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2004-2011, 2017-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
@ -89,45 +89,6 @@ inline void Foam::token::setUndefined()
}
inline void Foam::token::clear()
{
switch (type_)
{
case tokenType::WORD:
{
delete data_.wordPtr;
break;
}
case tokenType::STRING:
case tokenType::VARIABLE:
case tokenType::VERBATIMSTRING:
{
delete data_.stringPtr;
break;
}
case tokenType::COMPOUND:
{
if (data_.compoundPtr->unique())
{
delete data_.compoundPtr;
}
else
{
data_.compoundPtr->refCount::operator--();
}
break;
}
default:
break;
}
setUndefined();
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline constexpr Foam::token::token() noexcept
@ -188,42 +149,6 @@ inline Foam::token::token(token&& tok)
}
inline Foam::token::token(punctuationToken p)
:
token(p, 0)
{}
inline Foam::token::token(const word& w)
:
token(w, 0)
{}
inline Foam::token::token(const string& str)
:
token(str, 0)
{}
inline Foam::token::token(const label val)
:
token(val, 0)
{}
inline Foam::token::token(const floatScalar val)
:
token(val, 0)
{}
inline Foam::token::token(const doubleScalar val)
:
token(val, 0)
{}
inline Foam::token::token(punctuationToken p, label lineNumber)
:
data_(),
@ -284,16 +209,75 @@ inline Foam::token::token(const string& str, label lineNumber)
}
inline Foam::token::token(word&& w, label lineNumber)
:
data_(),
type_(tokenType::WORD),
lineNumber_(lineNumber)
{
data_.wordPtr = new word(std::move(w));
}
inline Foam::token::token(string&& str, label lineNumber)
:
data_(),
type_(tokenType::STRING),
lineNumber_(lineNumber)
{
data_.stringPtr = new string(std::move(str));
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
inline Foam::token::~token()
{
clear();
reset();
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline void Foam::token::reset()
{
switch (type_)
{
case tokenType::WORD:
{
delete data_.wordPtr;
break;
}
case tokenType::STRING:
case tokenType::VARIABLE:
case tokenType::VERBATIMSTRING:
{
delete data_.stringPtr;
break;
}
case tokenType::COMPOUND:
{
if (data_.compoundPtr->unique())
{
delete data_.compoundPtr;
}
else
{
data_.compoundPtr->refCount::operator--();
}
break;
}
default:
break;
}
setUndefined();
}
inline void Foam::token::swap(token& tok)
{
std::swap(data_, tok.data_);
@ -401,7 +385,7 @@ inline bool Foam::token::isBool() const
inline bool Foam::token::boolToken() const
{
if (type_ == tokenType::BOOL)
if (type_ == tokenType::BOOL || type_ == tokenType::LABEL)
{
return data_.labelVal;
}
@ -577,12 +561,6 @@ inline const Foam::word& Foam::token::wordToken() const
}
inline bool Foam::token::isVariable() const
{
return (type_ == tokenType::VARIABLE);
}
inline bool Foam::token::isString() const
{
return
@ -594,6 +572,24 @@ inline bool Foam::token::isString() const
}
inline bool Foam::token::isVariable() const
{
return (type_ == tokenType::VARIABLE);
}
inline bool Foam::token::isVerbatim() const
{
return (type_ == tokenType::VERBATIMSTRING);
}
inline bool Foam::token::isStringType() const
{
return (isWord() || isString());
}
inline const Foam::string& Foam::token::stringToken() const
{
if
@ -605,6 +601,11 @@ inline const Foam::string& Foam::token::stringToken() const
{
return *data_.stringPtr;
}
else if (type_ == tokenType::WORD)
{
// Upcast to string
return static_cast<const string&>(*data_.wordPtr);
}
parseError("string");
return string::null;
@ -631,7 +632,7 @@ inline const Foam::token::compound& Foam::token::compoundToken() const
inline void Foam::token::setBad()
{
clear();
reset();
type_ = tokenType::ERROR;
}
@ -640,7 +641,7 @@ inline void Foam::token::setBad()
inline void Foam::token::operator=(const token& tok)
{
clear();
reset();
type_ = tok.type_;
data_ = tok.data_; // bit-wise copy of union content
@ -681,7 +682,7 @@ inline void Foam::token::operator=(const token& tok)
inline void Foam::token::operator=(token&& tok)
{
clear();
reset();
lineNumber_ = 0;
swap(tok);
}
@ -689,7 +690,7 @@ inline void Foam::token::operator=(token&& tok)
inline void Foam::token::operator=(const punctuationToken p)
{
clear();
reset();
type_ = tokenType::PUNCTUATION;
data_.punctuationVal = p;
}
@ -697,7 +698,7 @@ inline void Foam::token::operator=(const punctuationToken p)
inline void Foam::token::operator=(const label val)
{
clear();
reset();
type_ = tokenType::LABEL;
data_.labelVal = val;
}
@ -705,7 +706,7 @@ inline void Foam::token::operator=(const label val)
inline void Foam::token::operator=(const floatScalar val)
{
clear();
reset();
type_ = tokenType::FLOAT_SCALAR;
data_.floatVal = val;
}
@ -713,31 +714,15 @@ inline void Foam::token::operator=(const floatScalar val)
inline void Foam::token::operator=(const doubleScalar val)
{
clear();
reset();
type_ = tokenType::DOUBLE_SCALAR;
data_.doubleVal = val;
}
inline void Foam::token::operator=(word* wordPtr)
{
clear();
type_ = tokenType::WORD;
data_.wordPtr = wordPtr;
}
inline void Foam::token::operator=(string* stringPtr)
{
clear();
type_ = tokenType::STRING;
data_.stringPtr = stringPtr;
}
inline void Foam::token::operator=(const word& w)
{
clear();
reset();
type_ = tokenType::WORD;
data_.wordPtr = new word(w);
}
@ -745,7 +730,7 @@ inline void Foam::token::operator=(const word& w)
inline void Foam::token::operator=(const string& str)
{
clear();
reset();
type_ = tokenType::STRING;
data_.stringPtr = new string(str);
}
@ -753,25 +738,23 @@ inline void Foam::token::operator=(const string& str)
inline void Foam::token::operator=(word&& w)
{
clear();
reset();
type_ = tokenType::WORD;
data_.wordPtr = new word;
data_.wordPtr->swap(w);
data_.wordPtr = new word(std::move(w));
}
inline void Foam::token::operator=(string&& s)
{
clear();
reset();
type_ = tokenType::STRING;
data_.stringPtr = new string;
data_.stringPtr->swap(s);
data_.stringPtr = new string(std::move(s));
}
inline void Foam::token::operator=(Foam::token::compound* compoundPtr)
{
clear();
reset();
type_ = tokenType::COMPOUND;
data_.compoundPtr = compoundPtr;
}
@ -832,22 +815,13 @@ inline bool Foam::token::operator==(const punctuationToken p) const
}
inline bool Foam::token::operator==(const word& w) const
{
return (type_ == tokenType::WORD && wordToken() == w);
}
inline bool Foam::token::operator==(const string& str) const
inline bool Foam::token::operator==(const std::string& s) const
{
return
(
(
type_ == tokenType::STRING
|| type_ == tokenType::VARIABLE
|| type_ == tokenType::VERBATIMSTRING
)
&& stringToken() == str
type_ == tokenType::WORD
? s == *data_.wordPtr
: isString() && s == *data_.stringPtr
);
}
@ -882,9 +856,9 @@ inline bool Foam::token::operator==(const doubleScalar val) const
}
inline bool Foam::token::operator!=(const token& t) const
inline bool Foam::token::operator!=(const token& tok) const
{
return !operator==(t);
return !operator==(tok);
}
@ -912,15 +886,9 @@ inline bool Foam::token::operator!=(const doubleScalar val) const
}
inline bool Foam::token::operator!=(const word& w) const
inline bool Foam::token::operator!=(const std::string& s) const
{
return !operator==(w);
}
inline bool Foam::token::operator!=(const string& str) const
{
return !operator==(str);
return !operator==(s);
}

View File

@ -152,7 +152,7 @@ Foam::word Foam::token::name() const
Foam::Istream& Foam::operator>>(Istream& is, token& tok)
{
tok.clear();
tok.reset();
return is.read(tok);
}
@ -167,10 +167,6 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const token& tok)
<< "Undefined token" << endl;
break;
case token::tokenType::BOOL:
os << tok.data_.labelVal;
break;
case token::tokenType::FLAG:
// Swallow the flag
break;
@ -179,6 +175,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const token& tok)
os << tok.data_.punctuationVal;
break;
case token::tokenType::BOOL:
case token::tokenType::LABEL:
os << tok.data_.labelVal;
break;