Commit 37e86352 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: make token constructors explicit (issue #563)

- access tokenType enum values more consistently.
parent c847d343
......@@ -89,11 +89,7 @@ int main(int argc, char *argv[])
break;
}
if
(
fieldName.type() != token::WORD
&& fieldName.wordToken() != "CELL"
)
if (!fieldName.isWord() || fieldName.wordToken() != "CELL")
{
FatalErrorInFunction
<< "Expected first CELL, found "
......@@ -103,7 +99,7 @@ int main(int argc, char *argv[])
label nCols = 0;
smapFile >> fieldName;
while (fieldName.type() == token::WORD)
while (fieldName.isWord())
{
starFieldNames[nCols++] = fieldName.wordToken();
smapFile >> fieldName;
......
......@@ -224,7 +224,7 @@ Foam::Istream& Foam::ISstream::read(token& t)
else
{
t = sPtr;
t.type() = token::VERBATIMSTRING;
t.type() = token::tokenType::VERBATIMSTRING;
}
return *this;
......@@ -266,7 +266,7 @@ Foam::Istream& Foam::ISstream::read(token& t)
else
{
t = sPtr;
t.type() = token::VARIABLE;
t.type() = token::tokenType::VARIABLE;
}
return *this;
}
......
......@@ -31,7 +31,7 @@ License
Foam::Ostream& Foam::OSstream::write(const token& t)
{
if (t.type() == token::VERBATIMSTRING)
if (t.type() == token::tokenType::VERBATIMSTRING)
{
write(char(token::HASH));
write(char(token::BEGIN_BLOCK));
......@@ -39,9 +39,9 @@ Foam::Ostream& Foam::OSstream::write(const token& t)
write(char(token::HASH));
write(char(token::END_BLOCK));
}
else if (t.type() == token::VARIABLE)
else if (t.type() == token::tokenType::VARIABLE)
{
writeQuoted( t.stringToken(), false);
writeQuoted(t.stringToken(), false);
}
return *this;
}
......
......@@ -67,7 +67,7 @@ void Foam::prefixOSstream::print(Ostream& os) const
Foam::Ostream& Foam::prefixOSstream::write(const token& t)
{
if (t.type() == token::VERBATIMSTRING)
if (t.type() == token::tokenType::VERBATIMSTRING)
{
write(char(token::HASH));
write(char(token::BEGIN_BLOCK));
......@@ -75,7 +75,7 @@ Foam::Ostream& Foam::prefixOSstream::write(const token& t)
write(char(token::HASH));
write(char(token::END_BLOCK));
}
else if (t.type() == token::VARIABLE)
else if (t.type() == token::tokenType::VARIABLE)
{
writeQuoted(t.stringToken(), false);
}
......
......@@ -92,7 +92,7 @@ bool Foam::token::compound::isCompound(const word& name)
Foam::token::compound& Foam::token::transferCompoundToken(const Istream& is)
{
if (type_ == COMPOUND)
if (type_ == tokenType::COMPOUND)
{
if (compoundTokenPtr_->empty())
{
......
......@@ -197,7 +197,7 @@ public:
// Write
virtual void write(Ostream&) const = 0;
virtual void write(Ostream& os) const = 0;
// IOstream Operators
......@@ -284,28 +284,47 @@ public:
inline token();
//- Construct as copy
inline token(const token&);
inline token(const token& t);
//- Construct punctuation character token
inline token(punctuationToken, label lineNumber=0);
inline explicit token(punctuationToken p);
//- Construct word token
inline token(const word&, label lineNumber=0);
inline explicit token(const word& w);
//- Construct string token
inline token(const string&, label lineNumber=0);
inline explicit token(const string& str);
//- Construct label token
inline token(const label, label lineNumber=0);
inline explicit token(const label val);
//- Construct floatScalar token
inline token(const floatScalar, label lineNumber=0);
inline explicit token(const floatScalar val);
//- Construct doubleScalar token
inline token(const doubleScalar, label lineNumber=0);
inline explicit token(const doubleScalar val);
//- Construct punctuation character token
inline token(punctuationToken p, const label lineNumber);
//- Construct word token
inline token(const word& w, const label lineNumber);
//- Construct string token
inline token(const string& str, 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 from Istream
token(Istream&);
token(Istream& is);
//- Destructor
......@@ -377,43 +396,43 @@ public:
// Assignment
inline void operator=(const token&);
inline void operator=(const token& t);
inline void operator=(const punctuationToken);
inline void operator=(const punctuationToken p);
inline void operator=(word*);
inline void operator=(const word&);
inline void operator=(word* wPtr);
inline void operator=(const word& w);
inline void operator=(string*);
inline void operator=(const string&);
inline void operator=(string* strPtr);
inline void operator=(const string& str);
inline void operator=(const label);
inline void operator=(const floatScalar);
inline void operator=(const doubleScalar);
inline void operator=(const label val);
inline void operator=(const floatScalar val);
inline void operator=(const doubleScalar val);
inline void operator=(compound*);
inline void operator=(compound* compPtr);
// Equality
inline bool operator==(const token&) const;
inline bool operator==(const punctuationToken) const;
inline bool operator==(const word&) const;
inline bool operator==(const string&) const;
inline bool operator==(const label) const;
inline bool operator==(const floatScalar) const;
inline bool operator==(const doubleScalar) const;
inline bool operator==(const token& t) const;
inline bool operator==(const punctuationToken p) const;
inline bool operator==(const word& w) const;
inline bool operator==(const string& str) const;
inline bool operator==(const label val) const;
inline bool operator==(const floatScalar val) const;
inline bool operator==(const doubleScalar val) const;
// Inequality
inline bool operator!=(const token&) const;
inline bool operator!=(const punctuationToken) const;
inline bool operator!=(const word&) const;
inline bool operator!=(const string&) const;
inline bool operator!=(const label) const;
inline bool operator!=(const floatScalar) const;
inline bool operator!=(const doubleScalar) const;
inline bool operator!=(const token& t) const;
inline bool operator!=(const punctuationToken p) const;
inline bool operator!=(const word& w) const;
inline bool operator!=(const string& str) const;
inline bool operator!=(const label val) const;
inline bool operator!=(const floatScalar val) const;
inline bool operator!=(const doubleScalar val) const;
// IOstream operators
......
......@@ -27,15 +27,20 @@ License
inline void Foam::token::clear()
{
if (type_ == WORD)
if (type_ == tokenType::WORD)
{
delete wordTokenPtr_;
}
else if (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING)
else if
(
type_ == tokenType::STRING
|| type_ == tokenType::VARIABLE
|| type_ == tokenType::VERBATIMSTRING
)
{
delete stringTokenPtr_;
}
else if (type_ == COMPOUND)
else if (type_ == tokenType::COMPOUND)
{
if (compoundTokenPtr_->unique())
{
......@@ -47,7 +52,7 @@ inline void Foam::token::clear()
}
}
type_ = UNDEFINED;
type_ = tokenType::UNDEFINED;
}
......@@ -55,7 +60,7 @@ inline void Foam::token::clear()
inline Foam::token::token()
:
type_(UNDEFINED),
type_(tokenType::UNDEFINED),
lineNumber_(0)
{}
......@@ -67,49 +72,85 @@ inline Foam::token::token(const token& t)
{
switch (type_)
{
case token::UNDEFINED:
case tokenType::UNDEFINED:
break;
case PUNCTUATION:
case tokenType::PUNCTUATION:
punctuationToken_ = t.punctuationToken_;
break;
case WORD:
case tokenType::WORD:
wordTokenPtr_ = new word(*t.wordTokenPtr_);
break;
case STRING:
case VARIABLE:
case VERBATIMSTRING:
case tokenType::STRING:
case tokenType::VARIABLE:
case tokenType::VERBATIMSTRING:
stringTokenPtr_ = new string(*t.stringTokenPtr_);
break;
case LABEL:
case tokenType::LABEL:
labelToken_ = t.labelToken_;
break;
case FLOAT_SCALAR:
case tokenType::FLOAT_SCALAR:
floatScalarToken_ = t.floatScalarToken_;
break;
case DOUBLE_SCALAR:
case tokenType::DOUBLE_SCALAR:
doubleScalarToken_ = t.doubleScalarToken_;
break;
case COMPOUND:
case tokenType::COMPOUND:
compoundTokenPtr_ = t.compoundTokenPtr_;
compoundTokenPtr_->refCount::operator++();
break;
case token::ERROR:
case tokenType::ERROR:
break;
}
}
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)
:
type_(PUNCTUATION),
type_(tokenType::PUNCTUATION),
punctuationToken_(p),
lineNumber_(lineNumber)
{}
......@@ -117,40 +158,40 @@ inline Foam::token::token(punctuationToken p, label lineNumber)
inline Foam::token::token(const word& w, label lineNumber)
:
type_(WORD),
type_(tokenType::WORD),
wordTokenPtr_(new word(w)),
lineNumber_(lineNumber)
{}
inline Foam::token::token(const string& s, label lineNumber)
inline Foam::token::token(const string& str, label lineNumber)
:
type_(STRING),
stringTokenPtr_(new string(s)),
type_(tokenType::STRING),
stringTokenPtr_(new string(str)),
lineNumber_(lineNumber)
{}
inline Foam::token::token(const label l, label lineNumber)
inline Foam::token::token(const label val, label lineNumber)
:
type_(LABEL),
labelToken_(l),
type_(tokenType::LABEL),
labelToken_(val),
lineNumber_(lineNumber)
{}
inline Foam::token::token(const floatScalar s, label lineNumber)
inline Foam::token::token(const floatScalar val, label lineNumber)
:
type_(FLOAT_SCALAR),
floatScalarToken_(s),
type_(tokenType::FLOAT_SCALAR),
floatScalarToken_(val),
lineNumber_(lineNumber)
{}
inline Foam::token::token(const doubleScalar s, label lineNumber)
inline Foam::token::token(const doubleScalar val, label lineNumber)
:
type_(DOUBLE_SCALAR),
doubleScalarToken_(s),
type_(tokenType::DOUBLE_SCALAR),
doubleScalarToken_(val),
lineNumber_(lineNumber)
{}
......@@ -177,27 +218,27 @@ inline Foam::token::tokenType& Foam::token::type()
inline bool Foam::token::good() const
{
return (type_ != ERROR && type_ != UNDEFINED);
return (type_ != tokenType::ERROR && type_ != tokenType::UNDEFINED);
}
inline bool Foam::token::undefined() const
{
return (type_ == UNDEFINED);
return (type_ == tokenType::UNDEFINED);
}
inline bool Foam::token::error() const
{
return (type_ == ERROR);
return (type_ == tokenType::ERROR);
}
inline bool Foam::token::isPunctuation() const
{
return (type_ == PUNCTUATION);
return (type_ == tokenType::PUNCTUATION);
}
inline Foam::token::punctuationToken Foam::token::pToken() const
{
if (type_ == PUNCTUATION)
if (type_ == tokenType::PUNCTUATION)
{
return punctuationToken_;
}
......@@ -210,12 +251,12 @@ inline Foam::token::punctuationToken Foam::token::pToken() const
inline bool Foam::token::isWord() const
{
return (type_ == WORD);
return (type_ == tokenType::WORD);
}
inline const Foam::word& Foam::token::wordToken() const
{
if (type_ == WORD)
if (type_ == tokenType::WORD)
{
return *wordTokenPtr_;
}
......@@ -228,17 +269,27 @@ inline const Foam::word& Foam::token::wordToken() const
inline bool Foam::token::isVariable() const
{
return (type_ == VARIABLE);
return (type_ == tokenType::VARIABLE);
}
inline bool Foam::token::isString() const
{
return (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING);
return
(
type_ == tokenType::STRING
|| type_ == tokenType::VARIABLE
|| type_ == tokenType::VERBATIMSTRING
);
}
inline const Foam::string& Foam::token::stringToken() const
{
if (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING)
if
(
type_ == tokenType::STRING
|| type_ == tokenType::VARIABLE
|| type_ == tokenType::VERBATIMSTRING
)
{
return *stringTokenPtr_;
}
......@@ -251,12 +302,12 @@ inline const Foam::string& Foam::token::stringToken() const
inline bool Foam::token::isLabel() const
{
return (type_ == LABEL);
return (type_ == tokenType::LABEL);
}
inline Foam::label Foam::token::labelToken() const
{
if (type_ == LABEL)
if (type_ == tokenType::LABEL)
{
return labelToken_;
}
......@@ -269,12 +320,12 @@ inline Foam::label Foam::token::labelToken() const
inline bool Foam::token::isFloatScalar() const
{
return (type_ == FLOAT_SCALAR);
return (type_ == tokenType::FLOAT_SCALAR);
}
inline Foam::floatScalar Foam::token::floatScalarToken() const
{
if (type_ == FLOAT_SCALAR)
if (type_ == tokenType::FLOAT_SCALAR)
{
return floatScalarToken_;
}
......@@ -288,12 +339,12 @@ inline Foam::floatScalar Foam::token::floatScalarToken() const
inline bool Foam::token::isDoubleScalar() const
{
return (type_ == DOUBLE_SCALAR);
return (type_ == tokenType::DOUBLE_SCALAR);
}
inline Foam::doubleScalar Foam::token::doubleScalarToken() const
{
if (type_ == DOUBLE_SCALAR)
if (type_ == tokenType::DOUBLE_SCALAR)
{
return doubleScalarToken_;
}
......@@ -307,16 +358,20 @@ inline Foam::doubleScalar Foam::token::doubleScalarToken() const
inline bool Foam::token::isScalar() const
{
return (type_ == FLOAT_SCALAR || type_ == DOUBLE_SCALAR);
return
(
type_ == tokenType::FLOAT_SCALAR
|| type_ == tokenType::DOUBLE_SCALAR
);
}
inline Foam::scalar Foam::token::scalarToken() const
{
if (type_ == FLOAT_SCALAR)
if (type_ == tokenType::FLOAT_SCALAR)
{
return floatScalarToken_;
}
else if (type_ == DOUBLE_SCALAR)
else if (type_ == tokenType::DOUBLE_SCALAR)
{
return doubleScalarToken_;
}
......@@ -329,12 +384,12 @@ inline Foam::scalar Foam::token::scalarToken() const
inline bool Foam::token::isNumber() const
{
return (type_ == LABEL || isScalar());
return (type_ == tokenType::LABEL || isScalar());
}
inline Foam::scalar Foam::token::number() const
{
if (type_ == LABEL)
if (type_ == tokenType::LABEL)
{
return labelToken_;
}
......@@ -351,12 +406,12 @@ inline Foam::scalar Foam::token::number() const
inline bool Foam::token::isCompound() const
{
return (type_ == COMPOUND);
return (type_ == tokenType::COMPOUND);
}
inline const Foam::token::compound& Foam::token::compoundToken() const
{
if (type_ == COMPOUND)
if (type_ == tokenType::COMPOUND)
{
return *compoundTokenPtr_;
}
......@@ -382,7 +437,7 @@ inline Foam::label& Foam::token::lineNumber()
inline void Foam::token::setBad()
{
clear();
type_ = ERROR;
type_ = tokenType::ERROR;
}
......@@ -395,41 +450,41 @@ inline void Foam::token::operator=(const token& t)
switch (type_)
{
case token::UNDEFINED:
case tokenType::UNDEFINED:
break;
case PUNCTUATION:
case tokenType::PUNCTUATION:
punctuationToken_ = t.punctuationToken_;
break;