diff --git a/applications/test/token/Make/files b/applications/test/token/Make/files
new file mode 100644
index 0000000000..27b95da13e
--- /dev/null
+++ b/applications/test/token/Make/files
@@ -0,0 +1,3 @@
+Test-token.C
+
+EXE = $(FOAM_USER_APPBIN)/Test-token
diff --git a/applications/test/token/Make/options b/applications/test/token/Make/options
new file mode 100644
index 0000000000..1f502ad153
--- /dev/null
+++ b/applications/test/token/Make/options
@@ -0,0 +1 @@
+/* EXE_INC = */
diff --git a/applications/test/token/Test-token.C b/applications/test/token/Test-token.C
new file mode 100644
index 0000000000..b8918c54b7
--- /dev/null
+++ b/applications/test/token/Test-token.C
@@ -0,0 +1,75 @@
+/*---------------------------------------------------------------------------*\
+ ========= |
+ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
+ \\ / O peration |
+ \\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
+ \\/ M anipulation |
+-------------------------------------------------------------------------------
+License
+ This file is part of OpenFOAM.
+
+ OpenFOAM is free software: you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with OpenFOAM. If not, see .
+
+Description
+ Test token construct assign etc.
+\*---------------------------------------------------------------------------*/
+
+#include "argList.H"
+#include "IOobject.H"
+#include "IOstreams.H"
+#include "IFstream.H"
+#include "StringStream.H"
+#include "cpuTime.H"
+#include "DynamicList.H"
+
+using namespace Foam;
+
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+// Main program:
+
+int main(int argc, char *argv[])
+{
+ argList::noBanner();
+ argList::noParallel();
+
+ argList args(argc, argv, false, true);
+
+ token tok1;
+ Info<< "construct null: " << tok1.info() << endl;
+
+ tok1 = double(3.14159);
+ Info<< "assign double: " << tok1.info() << endl;
+
+ token tok2(tok1);
+ Info<< "copy construct: " << tok2.info() << endl;
+
+ tok1 = word("this-word");
+ Info<< "assign word: " << tok1.info() << endl;
+
+ token tok3(tok1);
+ Info<< "copy construct: " << tok3.info() << endl;
+ Info<< "orig: " << tok1.info() << endl;
+
+ token tok4(std::move(tok1));
+ Info<< "move construct: " << tok4.info() << endl;
+ Info<< "orig: " << tok1.info() << endl;
+
+ tok3 = tok4;
+ Info<< "assign token: " << tok3.info() << endl;
+ Info<< "orig: " << tok4.info() << endl;
+
+ return 0;
+}
+
+// ************************************************************************* //
diff --git a/applications/test/tokenize/Test-tokenize.C b/applications/test/tokenize/Test-tokenize.C
index bd2f84a482..f944036d66 100644
--- a/applications/test/tokenize/Test-tokenize.C
+++ b/applications/test/tokenize/Test-tokenize.C
@@ -33,6 +33,7 @@ Description
#include "IFstream.H"
#include "StringStream.H"
#include "cpuTime.H"
+#include "DynamicList.H"
using namespace Foam;
@@ -69,6 +70,8 @@ int main(int argc, char *argv[])
IStringStream is(rawArg);
+ DynamicList tokens;
+
while (is.good())
{
token tok(is);
@@ -83,12 +86,23 @@ int main(int argc, char *argv[])
<< " lookahead: '" << char(lookahead) << "'"
<< endl;
}
+
+ if (tok.good())
+ {
+ tokens.append(std::move(tok));
+ if (verbose)
+ {
+ Info<< "after append: " << tok.info() << endl;
+ }
+ }
}
if (verbose)
{
Info<< nl;
IOobject::writeDivider(Info);
+
+ Info<< "tokenList:" << tokens << endl;
}
}
}
diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Istream.C b/src/OpenFOAM/db/IOstreams/IOstreams/Istream.C
index 25ee28e005..941b78cfd6 100644
--- a/src/OpenFOAM/db/IOstreams/IOstreams/Istream.C
+++ b/src/OpenFOAM/db/IOstreams/IOstreams/Istream.C
@@ -27,7 +27,7 @@ License
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
-void Foam::Istream::putBack(const token& t)
+void Foam::Istream::putBack(const token& tok)
{
if (bad())
{
@@ -43,13 +43,13 @@ void Foam::Istream::putBack(const token& t)
}
else
{
- putBackToken_ = t;
+ putBackToken_ = tok;
putBack_ = true;
}
}
-bool Foam::Istream::getBack(token& t)
+bool Foam::Istream::getBack(token& tok)
{
if (bad())
{
@@ -59,7 +59,7 @@ bool Foam::Istream::getBack(token& t)
}
else if (putBack_)
{
- t = putBackToken_;
+ tok = putBackToken_;
putBack_ = false;
return true;
}
@@ -68,15 +68,15 @@ bool Foam::Istream::getBack(token& t)
}
-bool Foam::Istream::peekBack(token& t)
+bool Foam::Istream::peekBack(token& tok)
{
if (putBack_)
{
- t = putBackToken_;
+ tok = putBackToken_;
}
else
{
- t = token::undefinedToken;
+ tok = token::undefinedToken;
}
return putBack_;
diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Istream.H b/src/OpenFOAM/db/IOstreams/IOstreams/Istream.H
index 24df823bdd..e55211309d 100644
--- a/src/OpenFOAM/db/IOstreams/IOstreams/Istream.H
+++ b/src/OpenFOAM/db/IOstreams/IOstreams/Istream.H
@@ -96,16 +96,16 @@ public:
//- Put back token
// Only a single put back is permitted
- void putBack(const token&);
+ void putBack(const token& tok);
//- Get the put back token if there is one and return true.
// Return false if no put back token is available.
- bool getBack(token&);
+ bool getBack(token& tok);
//- Peek at the put back token without removing it.
// Returns false if no put back token is available and set the
// token to undefined.
- bool peekBack(token&);
+ bool peekBack(token& tok);
//- Return next token from stream
virtual Istream& read(token&) = 0;
@@ -116,7 +116,7 @@ public:
//- Read a word
virtual Istream& read(word&) = 0;
- // Read a string (including enclosing double-quotes)
+ //- Read a string (including enclosing double-quotes)
virtual Istream& read(string&) = 0;
//- Read a label
diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C
index 0e40098fa5..1ed965b758 100644
--- a/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C
+++ b/src/OpenFOAM/db/IOstreams/Pstreams/UIPstream.C
@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
- \\/ M anipulation |
+ \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@@ -164,22 +164,20 @@ Foam::Istream& Foam::UIPstream::read(token& t)
// Word
case token::tokenType::WORD :
{
- word* pval = new word;
- if (read(*pval))
+ word val;
+ if (read(val))
{
- if (token::compound::isCompound(*pval))
+ if (token::compound::isCompound(val))
{
- t = token::compound::New(*pval, *this).ptr();
- delete pval;
+ t = token::compound::New(val, *this).ptr();
}
else
{
- t = pval;
+ t = std::move(val);
}
}
else
{
- delete pval;
t.setBad();
}
return *this;
@@ -190,26 +188,25 @@ Foam::Istream& Foam::UIPstream::read(token& t)
{
// Recurse to read actual string
read(t);
- t.type() = token::tokenType::VERBATIMSTRING;
+ t.setType(token::tokenType::VERBATIMSTRING);
return *this;
}
case token::tokenType::VARIABLE :
{
// Recurse to read actual string
read(t);
- t.type() = token::tokenType::VARIABLE;
+ t.setType(token::tokenType::VARIABLE);
return *this;
}
case token::tokenType::STRING :
{
- string* pval = new string;
- if (read(*pval))
+ string val;
+ if (read(val))
{
- t = pval;
+ t = std::move(val);
}
else
{
- delete pval;
t.setBad();
}
return *this;
diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C
index 010eef975b..cc9a0e5a65 100644
--- a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C
+++ b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C
@@ -116,21 +116,18 @@ char Foam::ISstream::nextValid()
void Foam::ISstream::readWordToken(token& t)
{
- word* wPtr = new word;
-
- if (read(*wPtr).bad())
+ word val;
+ if (read(val).bad())
{
- delete wPtr;
t.setBad();
}
- else if (token::compound::isCompound(*wPtr))
+ else if (token::compound::isCompound(val))
{
- t = token::compound::New(*wPtr, *this).ptr();
- delete wPtr;
+ t = token::compound::New(val, *this).ptr();
}
else
{
- t = wPtr; // Token takes ownership
+ t = std::move(val); // Move contents to token
}
}
@@ -192,16 +189,15 @@ Foam::Istream& Foam::ISstream::read(token& t)
case token::BEGIN_STRING :
{
putback(c);
- string* sPtr = new string;
- if (read(*sPtr).bad())
+ string val;
+ if (read(val).bad())
{
- delete sPtr;
t.setBad();
}
else
{
- t = sPtr; // Token takes ownership
+ t = std::move(val); // Move contents to token
}
return *this;
@@ -219,17 +215,16 @@ Foam::Istream& Foam::ISstream::read(token& t)
else if (nextC == token::BEGIN_BLOCK)
{
// Verbatim string: #{ ... #}
- string* sPtr = new string;
- if (readVerbatim(*sPtr).bad())
+ string val;
+ if (readVerbatim(val).bad())
{
- delete sPtr;
t.setBad();
}
else
{
- t = sPtr; // Token takes ownership
- t.type() = token::tokenType::VERBATIMSTRING;
+ t = std::move(val); // Move contents to token
+ t.setType(token::tokenType::VERBATIMSTRING);
}
}
else
@@ -259,17 +254,15 @@ Foam::Istream& Foam::ISstream::read(token& t)
putback(nextC);
putback(c);
- string* sPtr = new string;
-
- if (readVariable(*sPtr).bad())
+ string val;
+ if (readVariable(val).bad())
{
- delete sPtr;
t.setBad();
}
else
{
- t = sPtr; // Token takes ownership
- t.type() = token::tokenType::VARIABLE;
+ t = std::move(val); // Move contents to token
+ t.setType(token::tokenType::VARIABLE);
}
}
else
diff --git a/src/OpenFOAM/db/IOstreams/token/token.C b/src/OpenFOAM/db/IOstreams/token/token.C
index 17019bda99..fe1f76df5c 100644
--- a/src/OpenFOAM/db/IOstreams/token/token.C
+++ b/src/OpenFOAM/db/IOstreams/token/token.C
@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
- \\/ M anipulation |
+ \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@@ -29,14 +29,14 @@ License
namespace Foam
{
- const char* const token::typeName = "token";
- token token::undefinedToken;
-
typedef token::compound tokenCompound;
defineTypeNameAndDebug(tokenCompound, 0);
defineRunTimeSelectionTable(tokenCompound, Istream);
}
+const char* const Foam::token::typeName = "token";
+const Foam::token Foam::token::undefinedToken;
+
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
@@ -94,7 +94,7 @@ Foam::token::compound& Foam::token::transferCompoundToken(const Istream& is)
{
if (type_ == tokenType::COMPOUND)
{
- if (compoundTokenPtr_->empty())
+ if (data_.compoundPtr->empty())
{
FatalIOErrorInFunction(is)
<< "compound has already been transfered from token\n "
@@ -102,16 +102,14 @@ Foam::token::compound& Foam::token::transferCompoundToken(const Istream& is)
}
else
{
- compoundTokenPtr_->empty() = true;
+ data_.compoundPtr->empty() = true;
}
- return *compoundTokenPtr_;
- }
- else
- {
- parseError("compound");
- return *compoundTokenPtr_;
+ return *data_.compoundPtr;
}
+
+ parseError("compound");
+ return *data_.compoundPtr;
}
diff --git a/src/OpenFOAM/db/IOstreams/token/token.H b/src/OpenFOAM/db/IOstreams/token/token.H
index 1de16e5e13..90897917c3 100644
--- a/src/OpenFOAM/db/IOstreams/token/token.H
+++ b/src/OpenFOAM/db/IOstreams/token/token.H
@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
- \\/ M anipulation |
+ \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@@ -25,7 +25,7 @@ Class
Foam::token
Description
- A token holds items read from Istream.
+ A token holds an item read from Istream.
SourceFiles
tokenI.H
@@ -64,34 +64,40 @@ Ostream& operator<<(Ostream& os, const token& t);
/*---------------------------------------------------------------------------*\
- Class token Declaration
+ Class token Declaration
\*---------------------------------------------------------------------------*/
class token
{
-
public:
- //- Enumeration defining the types of token
+ //- Enumeration defining the types of token.
+ // Since these values are also used to tag content in Pstream,
+ // the maximum number of types is limited to 30.
enum tokenType
{
- UNDEFINED,
+ UNDEFINED, //!< An undefined token-type
- PUNCTUATION,
- WORD,
- VARIABLE,
- STRING,
- VERBATIMSTRING,
- LABEL,
- FLOAT_SCALAR,
- DOUBLE_SCALAR,
- COMPOUND,
+ // Fundamental types
+ PUNCTUATION, //!< single character punctuation
+ LABEL, //!< label (integer) type
+ FLOAT_SCALAR, //!< float (single-precision) type
+ DOUBLE_SCALAR, //!< double (double-precision) type
- ERROR
+ // Pointer types
+ WORD, //!< Contents represent a Foam::word
+ STRING, //!< Contents represent a Foam::string
+ VARIABLE, //!< Contents are a Foam::string representing a
+ //!< dictionary \c $variable expansion
+ VERBATIMSTRING, //!< Contents are a Foam::string representing verbatim
+ //!< content
+ COMPOUND, //!< Compound type such as List\