Commit c9cb4ce3 authored by Mark Olesen's avatar Mark Olesen Committed by Andrew Heather
Browse files

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.
parent b5342c16
......@@ -34,7 +34,7 @@ Foam::token Foam::dictionaryTokens::keywordToken(const entry& e)
if (k.empty())
{
return token::undefinedToken;
return token();
}
if (k.isPattern())
{
......
......@@ -98,7 +98,7 @@ bool Foam::Istream::peekBack(token& tok)
}
else
{
tok = token::undefinedToken;
tok.reset();
}
return putBack_;
......
......@@ -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())
{
......
......@@ -36,7 +36,6 @@ namespace Foam
defineRunTimeSelectionTable(tokenCompound, Istream);
}
const char* const Foam::token::typeName = "token";
const Foam::token Foam::token::undefinedToken;
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\ / A nd | Copyright (C) 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);
//- Construct floatScalar token
inline explicit token(const floatScalar val);
//- Construct doubleScalar token
inline explicit token(const doubleScalar val);
//- Construct word token by copying word contents
inline explicit token(const word& w);
//- Construct string token by copying string contents
inline explicit token(const string& str);
inline explicit token(const label val, label lineNumber=0);
//- Construct float token
inline explicit token(const floatScalar val, 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 double token
inline explicit token(const doubleScalar val, label lineNumber=0);
//- Construct floatScalar token
inline token(const floatScalar val, const label lineNumber);
//- Copy construct word token
inline explicit token(const word& w, label lineNumber=0);
//- Construct doubleScalar token
inline token(const doubleScalar val, const label lineNumber);
//- Copy construct string token
inline explicit token(const string& str, label lineNumber=0);
//- Construct word token by copying word contents
inline token(const word& w, const label lineNumber);
//- Move construct word token
inline explicit token(word&& w, label lineNumber=0);
//- 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;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// IOstream Operators
Ostream& operator<<(Ostream&, const token::punctuationToken&);
ostream& operator<<(ostream&, const token::punctuationToken&);
Ostream& operator<<(Ostream&, const token::compound&);
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&, const InfoProxy<token>&);
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);
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 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);
}