INT: token/stream support for reading/writing int32 and int64

integration-by: Mark Olesen <Mark.Olesen@keysight.com>
This commit is contained in:
Will Bainbridge
2024-04-14 21:03:01 +01:00
committed by Andrew Heather
parent 11166821f1
commit aa96119de2
31 changed files with 1158 additions and 293 deletions

View File

@ -1,3 +1,3 @@
Test-compoundToken1.C
Test-compoundToken1.cxx
EXE = $(FOAM_USER_APPBIN)/Test-compoundToken1

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2023 OpenCFD Ltd.
Copyright (C) 2023-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -158,7 +158,8 @@ void populateCompound(token::compound& ct, const dictionary& dict)
}
break;
case token::tokenType::LABEL :
case token::tokenType::INTEGER_32 :
case token::tokenType::INTEGER_64 :
{
fillComponents(label, cmpts, 123);
}

View File

@ -43,6 +43,29 @@ Description
using namespace Foam;
const char yesno[2] = { 'n', 'y' };
void printIntegralTest(const token& tok)
{
Info<< "Test: " << tok.info() << nl
<< " is int32 = " << yesno[tok.is_int32()]
<< ", int64 = " << yesno[tok.is_int64()]
<< ", uint32 = " << yesno[tok.is_uint32()]
<< ", uint64 = " << yesno[tok.is_uint64()]
<< nl;
}
void printFloatTest(const token& tok)
{
Info<< "Test: " << tok.info() << nl
<< " is float = " << yesno[tok.isFloat()]
<< ", double = " << yesno[tok.isDouble()]
<< ", number = " << yesno[tok.isNumber()]
<< nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
@ -56,6 +79,23 @@ int main(int argc, char *argv[])
token tok1;
Info<< "default construct: " << tok1.info() << endl;
tok1 = label(100);
Info<< "assign label: " << tok1.info() << endl;
tok1 = int64_t(100);
Info<< "assign int64: " << tok1.info() << endl;
tok1 = int32_t(100);
Info<< "assign int32: " << tok1.info() << endl;
{
tok1.int64Token(int64_t(INT32_MIN)-1);
Info<< "set int64Token: " << tok1.info() << endl;
printIntegralTest(tok1);
printFloatTest(tok1);
}
tok1 = double(3.14159);
Info<< "assign double: " << tok1.info() << endl;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2011-2016,2024 OpenFOAM Foundation
Copyright (C) 2017-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -150,8 +150,17 @@ public:
//- Read a string (including enclosing double-quotes)
virtual Istream& read(string&) = 0;
//- Read a label
virtual Istream& read(label&) = 0;
//- Read int32_t
virtual Istream& read(int32_t&) = 0;
//- Read int64_t
virtual Istream& read(int64_t&) = 0;
//- Read uint32_t
virtual Istream& read(uint32_t&) = 0;
//- Read uint64_t
virtual Istream& read(uint64_t&) = 0;
//- Read a float
virtual Istream& read(float&) = 0;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2011-2016,2024 OpenFOAM Foundation
Copyright (C) 2016-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -154,6 +154,12 @@ public:
//- Write int64_t
virtual Ostream& write(const int64_t val) = 0;
//- Write uint32_t
virtual Ostream& write(const uint32_t val) = 0;
//- Write uint64_t
virtual Ostream& write(const uint64_t val) = 0;
//- Write float
virtual Ostream& write(const float val) = 0;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2013 OpenFOAM Foundation
Copyright (C) 2011-2013,2024 OpenFOAM Foundation
Copyright (C) 2017-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -184,8 +184,17 @@ public:
// Read a string
virtual Istream& read(string& str) override;
//- Read a label
virtual Istream& read(label& val) override;
//- Read int32_t
virtual Istream& read(int32_t& val) override;
//- Read int64_t
virtual Istream& read(int64_t& val) override;
//- Read uint32_t
virtual Istream& read(uint32_t& val) override;
//- Read uint64_t
virtual Istream& read(uint64_t& val) override;
//- Read a float
virtual Istream& read(float& val) override;

View File

@ -5,8 +5,8 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2017-2023 OpenCFD Ltd.
Copyright (C) 2011-2015,2024 OpenFOAM Foundation
Copyright (C) 2017-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -306,9 +306,7 @@ Foam::Istream& Foam::UIPstreamBase::read(token& t)
if (c == token::FLAG)
{
char flagVal;
if (read(flagVal))
if (char flagVal; read(flagVal))
{
processFlags(*this, flagVal);
}
@ -349,8 +347,7 @@ Foam::Istream& Foam::UIPstreamBase::read(token& t)
case token::tokenType::WORD :
case token::tokenType::DIRECTIVE :
{
word val;
if (readString(val))
if (word val; readString(val))
{
if
(
@ -376,8 +373,7 @@ Foam::Istream& Foam::UIPstreamBase::read(token& t)
case token::tokenType::VERBATIM :
case token::tokenType::CHAR_DATA :
{
string val;
if (readString(val))
if (string val; readString(val))
{
t = std::move(val);
t.setType(token::tokenType(c));
@ -389,13 +385,54 @@ Foam::Istream& Foam::UIPstreamBase::read(token& t)
return *this;
}
// Label
case token::tokenType::LABEL :
// (signed) int32
case token::tokenType::INTEGER_32 :
{
label val;
if (read(val))
if (int32_t val; read(val))
{
t = val;
t.int32Token(val);
}
else
{
t.setBad();
}
return *this;
}
// (signed) int64
case token::tokenType::INTEGER_64 :
{
if (int64_t val; read(val))
{
t.int64Token(val);
}
else
{
t.setBad();
}
return *this;
}
// (unsigned) int32
case token::tokenType::UNSIGNED_INTEGER_32 :
{
if (uint32_t val; read(val))
{
t.uint32Token(val);
}
else
{
t.setBad();
}
return *this;
}
// (unsigned) int64
case token::tokenType::UNSIGNED_INTEGER_64 :
{
if (uint64_t val; read(val))
{
t.uint64Token(val);
}
else
{
@ -407,8 +444,7 @@ Foam::Istream& Foam::UIPstreamBase::read(token& t)
// Float
case token::tokenType::FLOAT :
{
float val;
if (read(val))
if (float val; read(val))
{
t = val;
}
@ -422,8 +458,7 @@ Foam::Istream& Foam::UIPstreamBase::read(token& t)
// Double
case token::tokenType::DOUBLE :
{
double val;
if (read(val))
if (double val; read(val))
{
t = val;
}
@ -473,7 +508,28 @@ Foam::Istream& Foam::UIPstreamBase::read(string& str)
}
Foam::Istream& Foam::UIPstreamBase::read(label& val)
Foam::Istream& Foam::UIPstreamBase::read(int32_t& val)
{
readFromBuffer(val);
return *this;
}
Foam::Istream& Foam::UIPstreamBase::read(int64_t& val)
{
readFromBuffer(val);
return *this;
}
Foam::Istream& Foam::UIPstreamBase::read(uint32_t& val)
{
readFromBuffer(val);
return *this;
}
Foam::Istream& Foam::UIPstreamBase::read(uint64_t& val)
{
readFromBuffer(val);
return *this;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2014 OpenFOAM Foundation
Copyright (C) 2011-2014,2024 OpenFOAM Foundation
Copyright (C) 2017-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -192,12 +192,18 @@ public:
//- Write string
virtual Ostream& write(const std::string& str) override;
//- Write int32_t as a label
//- Write int32_t
virtual Ostream& write(const int32_t val) override;
//- Write int64_t as a label
//- Write int64_t
virtual Ostream& write(const int64_t val) override;
//- Write uint32_t
virtual Ostream& write(const uint32_t val) override;
//- Write uint64_t
virtual Ostream& write(const uint64_t val) override;
//- Write float
virtual Ostream& write(const float val) override;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2011-2017,2024 OpenFOAM Foundation
Copyright (C) 2016-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -321,7 +321,7 @@ Foam::Ostream& Foam::UOPstreamBase::write(const std::string& str)
Foam::Ostream& Foam::UOPstreamBase::write(const int32_t val)
{
putChar(token::tokenType::LABEL);
putChar(token::tokenType::INTEGER_32);
writeToBuffer(val);
return *this;
}
@ -329,7 +329,23 @@ Foam::Ostream& Foam::UOPstreamBase::write(const int32_t val)
Foam::Ostream& Foam::UOPstreamBase::write(const int64_t val)
{
putChar(token::tokenType::LABEL);
putChar(token::tokenType::INTEGER_64);
writeToBuffer(val);
return *this;
}
Foam::Ostream& Foam::UOPstreamBase::write(const uint32_t val)
{
putChar(token::tokenType::UNSIGNED_INTEGER_32);
writeToBuffer(val);
return *this;
}
Foam::Ostream& Foam::UOPstreamBase::write(const uint64_t val)
{
putChar(token::tokenType::UNSIGNED_INTEGER_64);
writeToBuffer(val);
return *this;
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2023 OpenCFD Ltd.
Copyright (C) 2017-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -714,7 +714,7 @@ Foam::Istream& Foam::ISstream::read(token& t)
case '0' : case '1' : case '2' : case '3' : case '4' :
case '5' : case '6' : case '7' : case '8' : case '9' :
{
label labelVal = (c != '.'); // used as bool here
bool isIntegral = (c != '.'); // possible integral value?
unsigned nChar = 0;
buf[nChar++] = c;
@ -725,7 +725,7 @@ Foam::Istream& Foam::ISstream::read(token& t)
(
is_.get(c)
&& (
isdigit(c)
std::isdigit(c)
|| c == '+'
|| c == '-'
|| c == '.'
@ -734,10 +734,10 @@ Foam::Istream& Foam::ISstream::read(token& t)
)
)
{
if (labelVal)
{
labelVal = isdigit(c);
}
// Silently skip "'" digit separators in numeric literals?
// Still possible as integral?
isIntegral = isIntegral && std::isdigit(c);
buf[nChar++] = c;
if (nChar == bufLen)
@ -766,23 +766,91 @@ Foam::Istream& Foam::ISstream::read(token& t)
{
is_.putback(c);
if (nChar == 1 && buf[0] == '-')
if (nChar == 1)
{
// A single '-' is punctuation
t = token::punctuationToken(token::MINUS);
}
else if (labelVal && Foam::read(buf, labelVal))
{
t = labelVal;
}
else
{
scalar scalarVal;
if (readScalar(buf, scalarVal))
// Special single char handling
switch (buf[0])
{
// A scalar or too big to fit as a label
t = scalarVal;
case '-' : // A single '-' is punctuation
{
t.pToken(token::MINUS);
break;
}
case '.' : // A single '.' is currently bad
{
t.setBad();
break;
}
default :
{
if (isIntegral)
{
// Single digit : conversion is trivial
t.int32Token(buf[0] - '0');
}
else
{
// At the moment nothing to handle here
t.setBad();
}
break;
}
}
return *this;
}
if (isIntegral)
{
// Parse as an integral?
// - read with largest resolution and narrow when possible.
// - retain (signed|unsigned) int32/int64 ...
if (int64_t val; Foam::readInt64(buf, val))
{
// Use smaller representations when possible
if (val >= INT32_MIN && val <= INT32_MAX)
{
t.int32Token(static_cast<int32_t>(val));
}
else if (val >= 0 && val <= int64_t(UINT32_MAX))
{
t.uint32Token(static_cast<uint32_t>(val));
}
else
{
t.int64Token(val);
}
}
else if (uint64_t val; Foam::readUint64(buf, val))
{
// Use smaller representations when possible
if (val <= UINT32_MAX)
{
t.uint32Token(static_cast<uint32_t>(val));
}
else
{
t.uint64Token(val);
}
}
else
{
// Fallthrough to scalar parsing
isIntegral = false;
}
}
// Floating point format or a series of digits that are too
// big to fit an integral representation
//
// - read as 'scalar' (float|double) since this is what the
// rest of the code expects to handle anyhow
if (!isIntegral)
{
if (scalar val; Foam::readScalar(buf, val))
{
t = val;
}
else
{
@ -1007,7 +1075,31 @@ Foam::Istream& Foam::ISstream::read(string& str)
}
Foam::Istream& Foam::ISstream::read(label& val)
Foam::Istream& Foam::ISstream::read(int32_t& val)
{
is_ >> val;
syncState();
return *this;
}
Foam::Istream& Foam::ISstream::read(int64_t& val)
{
is_ >> val;
syncState();
return *this;
}
Foam::Istream& Foam::ISstream::read(uint32_t& val)
{
is_ >> val;
syncState();
return *this;
}
Foam::Istream& Foam::ISstream::read(uint64_t& val)
{
is_ >> val;
syncState();

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2012 OpenFOAM Foundation
Copyright (C) 2011-2012,2024 OpenFOAM Foundation
Copyright (C) 2017-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -226,8 +226,17 @@ public:
// and an embedded newline character.
virtual Istream& read(string& str) override;
//- Read a label
virtual Istream& read(label& val) override;
//- Read int32_t
virtual Istream& read(int32_t& val) override;
//- Read int64_t
virtual Istream& read(int64_t& val) override;
//- Read uint32_t
virtual Istream& read(uint32_t& val) override;
//- Read uint64_t
virtual Istream& read(uint64_t& val) override;
//- Read a float
virtual Istream& read(float& val) override;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2011-2016,2024 OpenFOAM Foundation
Copyright (C) 2017-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -218,6 +218,22 @@ Foam::Ostream& Foam::OSstream::write(const int64_t val)
}
Foam::Ostream& Foam::OSstream::write(const uint32_t val)
{
os_ << val;
syncState();
return *this;
}
Foam::Ostream& Foam::OSstream::write(const uint64_t val)
{
os_ << val;
syncState();
return *this;
}
Foam::Ostream& Foam::OSstream::write(const float val)
{
os_ << val;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2014 OpenFOAM Foundation
Copyright (C) 2011-2014,2024 OpenFOAM Foundation
Copyright (C) 2017-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -198,6 +198,12 @@ public:
//- Write int64_t
virtual Ostream& write(const int64_t val) override;
//- Write uint32_t
virtual Ostream& write(const uint32_t val) override;
//- Write uint64_t
virtual Ostream& write(const uint64_t val) override;
//- Write float
virtual Ostream& write(const float val) override;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2014 OpenFOAM Foundation
Copyright (C) 2011-2014,2024 OpenFOAM Foundation
Copyright (C) 2020-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -140,6 +140,20 @@ Foam::Ostream& Foam::prefixOSstream::write(const int64_t val)
}
Foam::Ostream& Foam::prefixOSstream::write(const uint32_t val)
{
checkWritePrefix();
return OSstream::write(val);
}
Foam::Ostream& Foam::prefixOSstream::write(const uint64_t val)
{
checkWritePrefix();
return OSstream::write(val);
}
Foam::Ostream& Foam::prefixOSstream::write(const float val)
{
checkWritePrefix();

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2014 OpenFOAM Foundation
Copyright (C) 2011-2014,2024 OpenFOAM Foundation
Copyright (C) 2020-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -130,6 +130,12 @@ public:
//- Write int64_t
virtual Ostream& write(const int64_t val) override;
//- Write uint32_t
virtual Ostream& write(const uint32_t val) override;
//- Write uint64_t
virtual Ostream& write(const uint64_t val) override;
//- Write float
virtual Ostream& write(const float val) override;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2011-2015,2024 OpenFOAM Foundation
Copyright (C) 2017-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -643,7 +643,28 @@ Foam::Istream& Foam::ITstream::read(string&)
}
Foam::Istream& Foam::ITstream::read(label&)
Foam::Istream& Foam::ITstream::read(int32_t&)
{
NotImplemented;
return *this;
}
Foam::Istream& Foam::ITstream::read(int64_t&)
{
NotImplemented;
return *this;
}
Foam::Istream& Foam::ITstream::read(uint32_t&)
{
NotImplemented;
return *this;
}
Foam::Istream& Foam::ITstream::read(uint64_t&)
{
NotImplemented;
return *this;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2011-2016,2024 OpenFOAM Foundation
Copyright (C) 2017-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -509,8 +509,17 @@ public:
//- triggers not implemented error
virtual Istream& read(string&) override;
//- Read a label : triggers not implemented error
virtual Istream& read(label&) override;
//- Read int32_t : triggers not implemented error
virtual Istream& read(int32_t&) override;
//- Read int64_t : triggers not implemented error
virtual Istream& read(int64_t&) override;
//- Read uint32_t : triggers not implemented error
virtual Istream& read(uint32_t&) override;
//- Read uint64_t : triggers not implemented error
virtual Istream& read(uint64_t&) override;
//- Read a float : triggers not implemented error
virtual Istream& read(float&) override;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019-2023 OpenCFD Ltd.
Copyright (C) 2019-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -65,7 +65,7 @@ Foam::Ostream& Foam::OTstream::writeQuoted
if (quoted)
{
// tokenType::STRING
tokens().emplace_back() = string(str, len);
tokens().emplace_back() = Foam::string(str, len);
}
else if (len > 0)
{
@ -120,7 +120,7 @@ Foam::Ostream& Foam::OTstream::write(const std::string& str)
Foam::Ostream& Foam::OTstream::write(const int32_t val)
{
tokens().push_back(token(label(val))); // tokenType::LABEL
tokens().emplace_back().int32Token(val);
return *this;
}
@ -128,24 +128,36 @@ Foam::Ostream& Foam::OTstream::write(const int32_t val)
Foam::Ostream& Foam::OTstream::write(const int64_t val)
{
tokens().push_back(token(label(val))); // tokenType::LABEL
tokens().emplace_back().int64Token(val);
return *this;
}
Foam::Ostream& Foam::OTstream::write(const uint32_t val)
{
tokens().emplace_back().uint32Token(val);
return *this;
}
Foam::Ostream& Foam::OTstream::write(const uint64_t val)
{
tokens().emplace_back().uint64Token(val);
return *this;
}
Foam::Ostream& Foam::OTstream::write(const float val)
{
tokens().push_back(token(val)); // tokenType::FLOAT
tokens().emplace_back().floatToken(val);
return *this;
}
Foam::Ostream& Foam::OTstream::write(const double val)
{
tokens().push_back(token(val)); // tokenType::DOUBLE
tokens().emplace_back().doubleToken(val);
return *this;
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019-2024 OpenCFD Ltd.
Copyright (C) 2019-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -137,12 +137,18 @@ public:
//- Write string
virtual Ostream& write(const std::string& str) override;
//- Write int32_t as a label
//- Write int32_t
virtual Ostream& write(const int32_t val) override;
//- Write int64_t as a label
//- Write int64_t
virtual Ostream& write(const int64_t val) override;
//- Write uint32_t
virtual Ostream& write(const uint32_t val) override;
//- Write uint64_t
virtual Ostream& write(const uint64_t val) override;
//- Write float
virtual Ostream& write(const float val) override;

View File

@ -128,8 +128,29 @@ public:
return *this;
}
//- Read a label
virtual Istream& read(label&) override
//- Read int32_t
virtual Istream& read(int32_t&) override
{
NotImplemented;
return *this;
}
//- Read int64_t
virtual Istream& read(int64_t&) override
{
NotImplemented;
return *this;
}
//- Read uint32_t
virtual Istream& read(uint32_t&) override
{
NotImplemented;
return *this;
}
//- Read uint64_t
virtual Istream& read(uint64_t&) override
{
NotImplemented;
return *this;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2011-2016,2024 OpenFOAM Foundation
Copyright (C) 2017-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -30,6 +30,10 @@ Class
Description
A token holds an item read from Istream.
Note
Use boolean() static method for creating a \b bool token
since \c bool and \c int are otherwise indistinguishable
SourceFiles
tokenI.H
token.C
@ -84,7 +88,10 @@ public:
FLAG, //!< stream flag (1-byte bitmask)
PUNCTUATION, //!< single character punctuation
BOOL, //!< boolean type
LABEL, //!< label (integer) type
INTEGER_32, //!< int32 type
INTEGER_64, //!< int64 type
UNSIGNED_INTEGER_32, //!< uint32 type
UNSIGNED_INTEGER_64, //!< uint64 type
FLOAT, //!< float (single-precision) type
DOUBLE, //!< double (double-precision) type
@ -107,7 +114,13 @@ public:
// Aliases
FLOAT_SCALAR = FLOAT, //!< compatibility name for FLOAT
DOUBLE_SCALAR = DOUBLE, //!< compatibility name for DOUBLE
VERBATIMSTRING = VERBATIM //!< compatibility name for VERBATIM
VERBATIMSTRING = VERBATIM, //!< compatibility name for VERBATIM
#if (WM_LABEL_SIZE == 64)
LABEL = INTEGER_64, //!< compatibility name
#else
LABEL = INTEGER_32, //!< compatibility name
#endif
};
@ -463,10 +476,11 @@ private:
// Fundamental values. Largest first for any {} initialization.
int64_t int64Val;
int32_t int32Val;
uint64_t uint64Val;
uint32_t uint32Val;
int flagVal; // bitmask - stored as int, not enum
punctuationToken punctuationVal;
label labelVal;
float floatVal;
double doubleVal;
@ -495,12 +509,23 @@ private:
//- Set as UNDEFINED and zero the union content without any checking
inline void setUndefined() noexcept;
//- True if token type corresponds to WORD or WORD-variant.
//- Token type corresponds to (int32, uint32, ...)
inline static bool is_integerToken(tokenType tokType) noexcept;
//- Token type corresponds to WORD or WORD-variant.
inline static bool is_wordToken(tokenType tokType) noexcept;
//- True if token type corresponds to STRING or STRING-variant
//- Token type corresponds to STRING or STRING-variant
inline static bool is_stringToken(tokenType tokType) noexcept;
//- Return integral type or emit parseError
template<class Type>
inline Type getIntegral(const char* expected) const;
//- Return integral/floating-point type or emit parseError
template<class Type>
inline Type getArithmetic(const char* expected) const;
// Parse error, expected 'expected', found ...
void parseError(const char* expected) const;
@ -527,16 +552,23 @@ public:
//- Construct punctuation character token
inline explicit token(punctuationToken p, label lineNum=0) noexcept;
//- Construct label token
//- \note Use boolean() static method for creating a \b bool token
//- since \c bool and \c int are otherwise indistinguishable
inline explicit token(const label val, label lineNum=0) noexcept;
//- Construct 32-bit integer token
inline explicit token(int32_t val, label lineNum=0) noexcept;
//- Construct 64-bit integer token
inline explicit token(int64_t val, label lineNum=0) noexcept;
//- Construct unsigned 32-bit integer token
inline explicit token(uint32_t val, label lineNum=0) noexcept;
//- Construct unsigned 64-bit integer token
inline explicit token(uint64_t val, label lineNum=0) noexcept;
//- Construct float token
inline explicit token(const float val, label lineNum=0) noexcept;
inline explicit token(float val, label lineNum=0) noexcept;
//- Construct double token
inline explicit token(const double val, label lineNum=0) noexcept;
inline explicit token(double val, label lineNum=0) noexcept;
//- Copy construct word token
inline explicit token(const word& w, label lineNum=0);
@ -640,15 +672,33 @@ public:
inline bool isPunctuation() const noexcept;
//- True if token is PUNCTUATION and equal to parameter
inline bool isPunctuation(const punctuationToken p) const noexcept;
inline bool isPunctuation(punctuationToken p) const noexcept;
//- Token is PUNCTUATION and isseparator
inline bool isSeparator() const noexcept;
//- Token is LABEL
//- Token is (int32 | int64 | uint32 | uint64)
inline bool isIntType() const noexcept;
//- Token is INTEGER_32 or is convertible to one
inline bool is_int32() const noexcept;
//- Token is INTEGER_64 or is convertible to one
inline bool is_int64() const noexcept;
//- Token is UNSIGNED_INTEGER_32 or is convertible to one
inline bool is_uint32() const noexcept;
//- Token is UNSIGNED_INTEGER_64 or is convertible to one
inline bool is_uint64() const noexcept;
//- Integral token is convertible to Foam::label
inline bool isLabel() const noexcept;
//- True if token is LABEL and equal to parameter
//- Integral token is convertible to Foam::uLabel
inline bool isULabel() const noexcept;
//- True if token is integer type and equal to parameter
inline bool isLabel(const label value) const noexcept;
//- Token is FLOAT
@ -660,7 +710,7 @@ public:
//- Token is FLOAT or DOUBLE
inline bool isScalar() const noexcept;
//- Token is LABEL, FLOAT or DOUBLE
//- Token is (signed/unsigned) integer type, FLOAT or DOUBLE
inline bool isNumber() const noexcept;
//- Token is word-variant (WORD, DIRECTIVE)
@ -729,23 +779,55 @@ public:
inline punctuationToken pToken() const;
//- Assign to a punctuation token
inline void pToken(const punctuationToken p);
inline void pToken(punctuationToken p);
//- Return label value.
// Report FatalIOError and return \b 0 if token is not LABEL
//- Return int32 value, convert from other integer type or Error
inline int32_t int32Token() const;
//- Assign a int32 value token
inline void int32Token(int32_t val);
//- Return int64 value, convert from other integer type or Error
inline int64_t int64Token() const;
//- Assign a int64 value token
inline void int64Token(int64_t val);
//- Return int32 value, convert from other integer type or Error
inline uint32_t uint32Token() const;
//- Assign a uint32 value token
inline void uint32Token(uint32_t val);
//- Return int64 value, convert from other integer type or Error
inline uint64_t uint64Token() const;
//- Assign a uint64 value token
inline void uint64Token(uint64_t val);
//- Return integer type as label value or Error
inline label labelToken() const;
//- Assign to a label token
//- Return integer type as uLabel value or Error
inline uLabel uLabelToken() const;
//- Assign to a label (int32 or int64) token
inline void labelToken(const label val);
//- Return float value.
// Report FatalIOError and return \b 0 if token is not FLOAT
inline float floatToken() const;
//- Assign to a float token
inline void floatToken(const float val);
//- Return double value.
// Report FatalIOError and return \b 0 if token is not DOUBLE
inline double doubleToken() const;
//- Assign to a double token
inline void doubleToken(const double val);
//- Return float or double value.
// Report FatalIOError and return \b 0 if token is not a
// FLOAT or DOUBLE
@ -753,7 +835,7 @@ public:
//- Return label, float or double value.
// Report FatalIOError and return \b 0 if token is not a
// LABEL, FLOAT or DOUBLE
// an integer type, FLOAT or DOUBLE
inline scalar number() const;
//- Return const reference to the word contents.
@ -841,14 +923,23 @@ public:
//- Copy assign from punctuation
inline void operator=(const punctuationToken p);
//- Copy assign from label
inline void operator=(const label val);
//- Copy assign from int32_t
inline void operator=(int32_t val);
//- Copy assign from int64_t
inline void operator=(int64_t val);
//- Copy assign from uint32_t
inline void operator=(uint32_t val);
//- Copy assign from uint64_t
inline void operator=(uint64_t val);
//- Copy assign from float
inline void operator=(const float val);
inline void operator=(float val);
//- Copy assign from double
inline void operator=(const double val);
inline void operator=(double val);
//- Copy assign from word content
inline void operator=(const word& w);
@ -873,20 +964,29 @@ public:
inline bool operator==(const token& tok) const;
inline bool operator==(const punctuationToken p) const noexcept;
inline bool operator==(const label val) const noexcept;
inline bool operator==(const int32_t val) const noexcept;
inline bool operator==(const int64_t val) const noexcept;
inline bool operator==(const uint32_t val) const noexcept;
inline bool operator==(const uint64_t val) const noexcept;
inline bool operator==(const float val) const noexcept;
inline bool operator==(const double val) const noexcept;
inline bool operator==(const std::string& s) const;
inline bool operator==(const std::string&) const;
// Inequality
inline bool operator!=(const token& tok) const;
inline bool operator!=(const punctuationToken p) const noexcept;
inline bool operator!=(const label val) const noexcept;
inline bool operator!=(const float val) const noexcept;
inline bool operator!=(const double val) const noexcept;
inline bool operator!=(const std::string& s) const;
bool operator!=(const token& t) const { return !operator==(t); }
bool operator!=(punctuationToken p) const noexcept
{
return !operator==(p);
}
bool operator!=(int32_t b) const noexcept { return !operator==(b); }
bool operator!=(int64_t b) const noexcept { return !operator==(b); }
bool operator!=(uint32_t b) const noexcept { return !operator==(b); }
bool operator!=(uint64_t b) const noexcept { return !operator==(b); }
bool operator!=(float b) const noexcept { return !operator==(b); }
bool operator!=(double b) const noexcept { return !operator==(b); }
bool operator!=(const std::string& s) const { return !operator==(s); }
// IOstream Operators
@ -907,11 +1007,11 @@ public:
//- Token is FLOAT
// \deprecated(2020-01) - isFloat()
bool isFloatScalar() const { return isFloat(); };
bool isFloatScalar() const noexcept { return isFloat(); };
//- Token is DOUBLE
// \deprecated(2020-01) - isDouble()
bool isDoubleScalar() const { return isDouble(); }
bool isDoubleScalar() const noexcept { return isDouble(); }
//- Return float value.
// \deprecated(2020-01) - floatToken()

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2011-2016,2024 OpenFOAM Foundation
Copyright (C) 2017-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -34,7 +34,7 @@ inline Foam::token Foam::token::boolean(bool on) noexcept
{
token tok;
tok.type_ = tokenType::BOOL;
tok.data_.labelVal = on;
tok.data_.flagVal = on;
return tok;
}
@ -50,6 +50,18 @@ inline Foam::token Foam::token::flag(int bitmask) noexcept
}
inline bool Foam::token::is_integerToken(tokenType tokType) noexcept
{
return
(
tokType == tokenType::INTEGER_32
|| tokType == tokenType::INTEGER_64
|| tokType == tokenType::UNSIGNED_INTEGER_32
|| tokType == tokenType::UNSIGNED_INTEGER_64
);
}
inline bool Foam::token::is_wordToken(tokenType tokType) noexcept
{
return
@ -115,6 +127,41 @@ inline void Foam::token::setUndefined() noexcept
}
// inline bool Foam::token:is_pointer() const noexcept
// {
// return (isWord() || isString() || isCompound());
// }
template<class Type>
inline Type Foam::token::getIntegral(const char* expected) const
{
switch (type_)
{
case tokenType::INTEGER_32 : return data_.int32Val;
case tokenType::INTEGER_64 : return data_.int64Val;
case tokenType::UNSIGNED_INTEGER_32 : return data_.uint32Val;
case tokenType::UNSIGNED_INTEGER_64 : return data_.uint64Val;
default: parseError(expected); return 0;
}
}
template<class Type>
inline Type Foam::token::getArithmetic(const char* expected) const
{
switch (type_)
{
case tokenType::INTEGER_32 : return data_.int32Val;
case tokenType::INTEGER_64 : return data_.int64Val;
case tokenType::UNSIGNED_INTEGER_32 : return data_.uint32Val;
case tokenType::UNSIGNED_INTEGER_64 : return data_.uint64Val;
case tokenType::FLOAT : return data_.floatVal;
case tokenType::DOUBLE : return data_.doubleVal;
default: parseError(expected); return 0;
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline constexpr Foam::token::token() noexcept
@ -190,17 +237,47 @@ inline Foam::token::token(punctuationToken p, label lineNum) noexcept
}
inline Foam::token::token(const label val, label lineNum) noexcept
inline Foam::token::token(int32_t val, label lineNum) noexcept
:
data_(),
type_(tokenType::LABEL),
type_(tokenType::INTEGER_32),
line_(lineNum)
{
data_.labelVal = val;
data_.int32Val = val;
}
inline Foam::token::token(const float val, label lineNum) noexcept
inline Foam::token::token(int64_t val, label lineNum) noexcept
:
data_(),
type_(tokenType::INTEGER_64),
line_(lineNum)
{
data_.int64Val = val;
}
inline Foam::token::token(uint32_t val, label lineNum) noexcept
:
data_(),
type_(tokenType::UNSIGNED_INTEGER_32),
line_(lineNum)
{
data_.uint32Val = val;
}
inline Foam::token::token(uint64_t val, label lineNum) noexcept
:
data_(),
type_(tokenType::UNSIGNED_INTEGER_64),
line_(lineNum)
{
data_.uint64Val = val;
}
inline Foam::token::token(float val, label lineNum) noexcept
:
data_(),
type_(tokenType::FLOAT),
@ -210,7 +287,7 @@ inline Foam::token::token(const float val, label lineNum) noexcept
}
inline Foam::token::token(const double val, label lineNum) noexcept
inline Foam::token::token(double val, label lineNum) noexcept
:
data_(),
type_(tokenType::DOUBLE),
@ -425,13 +502,13 @@ inline bool Foam::token::setType(token::tokenType tokType) noexcept
switch (tokType)
{
case tokenType::FLAG:
case tokenType::BOOL:
case tokenType::LABEL:
{
switch (type_)
{
case tokenType::FLAG:
case tokenType::BOOL:
case tokenType::LABEL:
type_ = tokType;
return true;
break;
@ -537,9 +614,13 @@ inline bool Foam::token::isBool() const noexcept
inline bool Foam::token::boolToken() const
{
if (type_ == tokenType::BOOL || type_ == tokenType::LABEL)
if (type_ == tokenType::BOOL)
{
return data_.labelVal;
return data_.flagVal;
}
else if (type_ == tokenType::INTEGER_32)
{
return data_.int32Val;
}
parseError("bool");
@ -551,7 +632,7 @@ inline void Foam::token::boolToken(bool on)
{
reset();
type_ = tokenType::BOOL;
data_.labelVal = on;
data_.flagVal = on;
}
@ -579,7 +660,7 @@ inline bool Foam::token::isPunctuation() const noexcept
}
inline bool Foam::token::isPunctuation(const punctuationToken p) const noexcept
inline bool Foam::token::isPunctuation(punctuationToken p) const noexcept
{
return
(
@ -606,12 +687,12 @@ inline Foam::token::punctuationToken Foam::token::pToken() const
return data_.punctuationVal;
}
parseError("punctuation character");
parseError("punctuation");
return punctuationToken::NULL_TOKEN;
}
inline void Foam::token::pToken(const punctuationToken p)
inline void Foam::token::pToken(punctuationToken p)
{
reset();
type_ = tokenType::PUNCTUATION;
@ -619,57 +700,224 @@ inline void Foam::token::pToken(const punctuationToken p)
}
inline bool Foam::token::isLabel() const noexcept
inline bool Foam::token::isIntType() const noexcept
{
return
return is_integerToken(type_);
}
// inline bool Foam::token::isSignedIntType() const noexcept
// {
// return
// (
// (type_ == tokenType::INTEGER_32)
// || (type_ == tokenType::INTEGER_64)
// );
// }
//
//
// inline bool Foam::token::isUnsignedIntType() const noexcept
// {
// return
// (
// (type_ == tokenType::UNSIGNED_INTEGER_32)
// || (type_ == tokenType::UNSIGNED_INTEGER_64)
// );
// }
inline bool Foam::token::is_int32() const noexcept
{
return (type_ == tokenType::INTEGER_32) ||
(
type_ == tokenType::LABEL
// FUTURE?
// || type_ == tokenType::INT32
// || type_ == tokenType::INT64
(type_ == tokenType::INTEGER_64)
? (data_.int64Val >= INT32_MIN && data_.int64Val <= INT32_MAX)
: (type_ == tokenType::UNSIGNED_INTEGER_32)
? (data_.uint32Val <= INT32_MAX)
:
(
type_ == tokenType::UNSIGNED_INTEGER_64
&& data_.uint64Val <= INT32_MAX
)
);
}
inline bool Foam::token::isLabel(const label value) const noexcept
inline int32_t Foam::token::int32Token() const
{
// FUTURE?
// return
// (
// type_ == tokenType::LABEL
// ? value == data_.labelVal
// : type_ == tokenType::INT32
// ? value == data_.int32Val
// : type_ == tokenType::INT64
// ? value == data_.int64Val
// : false
// );
return getIntegral<int32_t>("int32");
}
inline void Foam::token::int32Token(int32_t val)
{
reset();
type_ = tokenType::INTEGER_32;
data_.int32Val = val;
}
inline bool Foam::token::is_int64() const noexcept
{
return (type_ == tokenType::INTEGER_64) ||
(
(type_ == tokenType::INTEGER_32)
|| (type_ == tokenType::UNSIGNED_INTEGER_32)
|| (
type_ == tokenType::UNSIGNED_INTEGER_64
&& data_.uint64Val <= INT64_MAX
)
);
}
inline int64_t Foam::token::int64Token() const
{
return getIntegral<int64_t>("int64");
}
inline void Foam::token::int64Token(int64_t val)
{
reset();
type_ = tokenType::INTEGER_64;
data_.int64Val = val;
}
inline bool Foam::token::is_uint32() const noexcept
{
return (type_ == tokenType::UNSIGNED_INTEGER_32) ||
(
(type_ == tokenType::INTEGER_32)
? (data_.int32Val >= 0)
: (type_ == tokenType::INTEGER_64)
? (data_.int64Val >= 0 && data_.int64Val <= UINT32_MAX)
:
(
type_ == tokenType::UNSIGNED_INTEGER_64
&& data_.uint64Val <= UINT32_MAX
)
);
}
inline uint32_t Foam::token::uint32Token() const
{
return getIntegral<uint32_t>("uint32");
}
inline void Foam::token::uint32Token(uint32_t val)
{
reset();
type_ = tokenType::UNSIGNED_INTEGER_32;
data_.uint32Val = val;
}
inline bool Foam::token::is_uint64() const noexcept
{
return
(
type_ == tokenType::LABEL
&& value == data_.labelVal
(
type_ == tokenType::UNSIGNED_INTEGER_32
|| type_ == tokenType::UNSIGNED_INTEGER_64
)
||
(
(type_ == tokenType::INTEGER_32) ? (data_.int32Val >= 0)
: (type_ == tokenType::INTEGER_64 && data_.int64Val >= 0)
)
);
}
inline uint64_t Foam::token::uint64Token() const
{
return getIntegral<uint64_t>("uint64");
}
inline void Foam::token::uint64Token(uint64_t val)
{
reset();
type_ = tokenType::UNSIGNED_INTEGER_64;
data_.uint64Val = val;
}
inline bool Foam::token::isLabel() const noexcept
{
if constexpr (sizeof(Foam::label) == sizeof(int32_t))
{
return is_int32();
}
else
{
return is_int64();
}
}
inline bool Foam::token::isULabel() const noexcept
{
if constexpr (sizeof(Foam::label) == sizeof(int32_t))
{
return is_uint32();
}
else
{
return is_uint64();
}
}
inline Foam::label Foam::token::labelToken() const
{
if (type_ == tokenType::LABEL)
{
return data_.labelVal;
}
return getIntegral<label>("label");
}
parseError("label");
return 0;
inline Foam::uLabel Foam::token::uLabelToken() const
{
return getIntegral<uLabel>("uLabel");
}
inline bool Foam::token::isLabel(const label value) const noexcept
{
return
(
(type_ == tokenType::INTEGER_32)
? (value == data_.int32Val)
: (type_ == tokenType::INTEGER_64)
? (value == data_.int64Val)
: (type_ == tokenType::UNSIGNED_INTEGER_32)
? (value >= 0 && uint32_t(value) == data_.uint32Val)
:
(
(type_ == tokenType::UNSIGNED_INTEGER_64)
&& (value >= 0 && uint64_t(value) == data_.uint64Val)
)
);
}
inline void Foam::token::labelToken(const label val)
{
reset();
type_ = tokenType::LABEL;
data_.labelVal = val;
if constexpr (sizeof(Foam::label) == sizeof(int32_t))
{
reset();
type_ = tokenType::INTEGER_32;
data_.int32Val = val;
}
else
{
reset();
type_ = tokenType::INTEGER_64;
data_.int64Val = val;
}
}
@ -685,9 +933,18 @@ inline float Foam::token::floatToken() const
{
return data_.floatVal;
}
else
{
parseError("float"); return 0;
}
}
parseError("float");
return 0;
inline void Foam::token::floatToken(const float val)
{
reset();
type_ = tokenType::FLOAT;
data_.floatVal = val;
}
@ -703,9 +960,18 @@ inline double Foam::token::doubleToken() const
{
return data_.doubleVal;
}
else
{
parseError("double"); return 0;
}
}
parseError("double");
return 0;
inline void Foam::token::doubleToken(const double val)
{
reset();
type_ = tokenType::DOUBLE;
data_.doubleVal = val;
}
@ -721,39 +987,24 @@ inline bool Foam::token::isScalar() const noexcept
inline Foam::scalar Foam::token::scalarToken() const
{
if (type_ == tokenType::FLOAT)
switch (type_)
{
return data_.floatVal;
case tokenType::FLOAT : return data_.floatVal;
case tokenType::DOUBLE : return data_.doubleVal;
default: parseError("scalar"); return 0;
}
else if (type_ == tokenType::DOUBLE)
{
return data_.doubleVal;
}
parseError("scalar");
return 0;
}
inline bool Foam::token::isNumber() const noexcept
{
return (isLabel() || isScalar());
return (is_integerToken(type_) || isScalar());
}
inline Foam::scalar Foam::token::number() const
{
if (isLabel())
{
return labelToken();
}
if (isScalar())
{
return scalarToken();
}
parseError("number (label or scalar)");
return 0;
return getArithmetic<scalar>("number (label or scalar)");
}
@ -934,15 +1185,15 @@ inline Type& Foam::token::transferCompoundToken(const Istream& is)
}
template<class T>
Foam::token::tokenType Foam::token::Compound<T>::typeCode() const
template<class Type>
Foam::token::tokenType Foam::token::Compound<Type>::typeCode() const
{
// Does not cover all possibilities perfectly, but should handle
// most of the common ones (bool, label, scalar, vector lists).
// Something like List<edge> will not be quite correct if we rely
// on nComponents
typedef typename T::value_type valueType;
typedef typename Type::value_type valueType;
if constexpr (std::is_same_v<bool, valueType>)
{
@ -952,26 +1203,26 @@ Foam::token::tokenType Foam::token::Compound<T>::typeCode() const
else if constexpr (is_contiguous_label<valueType>::value)
{
// List<label>, List<labelVector> etc
return token::tokenType::LABEL;
// FUTURE?
// return
// (
// sizeof(int32_t) == sizeof(Foam::label)
// ? token::tokenType::INT32
// : token::tokenType::INT64
// );
if constexpr (sizeof(Foam::label) == sizeof(int32_t))
{
return token::tokenType::INTEGER_32;
}
else
{
return token::tokenType::INTEGER_64;
}
}
else if constexpr (is_contiguous_scalar<valueType>::value)
{
// List<scalar>, List<vector>, List<tensor> etc
return
(
sizeof(float) == sizeof(Foam::scalar)
? token::tokenType::FLOAT
: token::tokenType::DOUBLE
);
if constexpr (sizeof(Foam::scalar) == sizeof(float))
{
return token::tokenType::FLOAT;
}
else
{
return token::tokenType::DOUBLE;
}
}
else if constexpr (std::is_same_v<char, valueType>)
{
@ -1059,9 +1310,35 @@ inline void Foam::token::operator=(const punctuationToken p)
}
inline void Foam::token::operator=(const label val)
inline void Foam::token::operator=(const int32_t val)
{
token::labelToken(val);
reset();
type_ = tokenType::INTEGER_32;
data_.int32Val = val;
}
inline void Foam::token::operator=(const int64_t val)
{
reset();
type_ = tokenType::INTEGER_64;
data_.int64Val = val;
}
inline void Foam::token::operator=(const uint32_t val)
{
reset();
type_ = tokenType::UNSIGNED_INTEGER_32;
data_.uint32Val = val;
}
inline void Foam::token::operator=(const uint64_t val)
{
reset();
type_ = tokenType::UNSIGNED_INTEGER_64;
data_.uint64Val = val;
}
@ -1142,7 +1419,7 @@ inline bool Foam::token::operator==(const token& tok) const
return true;
case tokenType::BOOL:
return data_.labelVal == tok.data_.labelVal;
return data_.flagVal == tok.data_.flagVal;
case tokenType::FLAG:
return data_.flagVal == tok.data_.flagVal;
@ -1150,8 +1427,17 @@ inline bool Foam::token::operator==(const token& tok) const
case tokenType::PUNCTUATION:
return data_.punctuationVal == tok.data_.punctuationVal;
case tokenType::LABEL:
return data_.labelVal == tok.data_.labelVal;
case tokenType::INTEGER_32 :
return data_.int32Val == tok.data_.int32Val;
case tokenType::INTEGER_64 :
return data_.int64Val == tok.data_.int64Val;
case tokenType::UNSIGNED_INTEGER_32 :
return data_.uint64Val == tok.data_.uint64Val;
case tokenType::UNSIGNED_INTEGER_64 :
return data_.uint64Val == tok.data_.uint64Val;
case tokenType::FLOAT:
return equal(data_.floatVal, tok.data_.floatVal);
@ -1200,9 +1486,43 @@ inline bool Foam::token::operator==(const std::string& s) const
}
inline bool Foam::token::operator==(const label val) const noexcept
inline bool Foam::token::operator==(const int32_t val) const noexcept
{
return isLabel(val);
return
(
type_ == tokenType::INTEGER_32
&& data_.int32Val == val
);
}
inline bool Foam::token::operator==(const int64_t val) const noexcept
{
return
(
type_ == tokenType::INTEGER_64
&& data_.int64Val == val
);
}
inline bool Foam::token::operator==(const uint32_t val) const noexcept
{
return
(
type_ == tokenType::UNSIGNED_INTEGER_32
&& data_.uint32Val == val
);
}
inline bool Foam::token::operator==(const uint64_t val) const noexcept
{
return
(
type_ == tokenType::UNSIGNED_INTEGER_64
&& data_.uint64Val == val
);
}
@ -1226,40 +1546,4 @@ inline bool Foam::token::operator==(const double val) const noexcept
}
inline bool Foam::token::operator!=(const token& tok) const
{
return !operator==(tok);
}
inline bool Foam::token::operator!=(const punctuationToken p) const noexcept
{
return !isPunctuation(p);
}
inline bool Foam::token::operator!=(const label val) const noexcept
{
return !operator==(val);
}
inline bool Foam::token::operator!=(const float val) const noexcept
{
return !operator==(val);
}
inline bool Foam::token::operator!=(const double val) const noexcept
{
return !operator==(val);
}
inline bool Foam::token::operator!=(const std::string& s) const
{
return !operator==(s);
}
// ************************************************************************* //

View File

@ -57,16 +57,20 @@ static OS& printTokenInfo(OS& os, const token& tok)
os << "punctuation '" << tok.pToken() << '\'';
break;
// case token::tokenType::INT32:
// os << "int32 " << tok.int32Token();
// break;
//
// case token::tokenType::INT64:
// os << "int64 " << tok.int64Token();
// break;
case token::tokenType::INTEGER_32:
os << "int32 " << tok.int32Token();
break;
case token::tokenType::LABEL:
os << "label " << tok.labelToken();
case token::tokenType::INTEGER_64:
os << "int64 " << tok.int64Token();
break;
case token::tokenType::UNSIGNED_INTEGER_32:
os << "uint32 " << tok.uint32Token();
break;
case token::tokenType::UNSIGNED_INTEGER_64:
os << "uint64 " << tok.uint64Token();
break;
case token::tokenType::FLOAT:
@ -168,9 +172,10 @@ Foam::word Foam::token::name(const token::tokenType tokType)
case token::tokenType::FLAG: return "flag";
case token::tokenType::PUNCTUATION: return "punctuation";
// case token::tokenType::INT32: return "int32";
// case token::tokenType::INT64: return "int64";
case token::tokenType::LABEL: return "label";
case token::tokenType::INTEGER_32: return "int32";
case token::tokenType::INTEGER_64: return "int64";
case token::tokenType::UNSIGNED_INTEGER_32: return "uint32";
case token::tokenType::UNSIGNED_INTEGER_64: return "uint64";
case token::tokenType::FLOAT: return "float";
case token::tokenType::DOUBLE: return "double";
case token::tokenType::WORD: return "word";
@ -219,8 +224,23 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const token& tok)
break;
case token::tokenType::BOOL:
case token::tokenType::LABEL:
os << tok.data_.labelVal;
os << tok.data_.flagVal; // An int value
break;
case token::tokenType::INTEGER_32:
os << tok.data_.int32Val;
break;
case token::tokenType::INTEGER_64:
os << tok.data_.int64Val;
break;
case token::tokenType::UNSIGNED_INTEGER_32:
os << tok.data_.uint32Val;
break;
case token::tokenType::UNSIGNED_INTEGER_64:
os << tok.data_.uint64Val;
break;
case token::tokenType::FLOAT:

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018 OpenFOAM Foundation
Copyright (C) 2018,2024 OpenFOAM Foundation
Copyright (C) 2019-2023 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -151,6 +151,7 @@ bool Foam::functionEntries::ifeqEntry::equalToken
case token::WORD:
case token::DIRECTIVE:
{
if (t2.isWord())
{
return t1.wordToken() == t2.wordToken();
@ -161,8 +162,10 @@ bool Foam::functionEntries::ifeqEntry::equalToken
return w2.match(t1.wordToken());
}
return false;
}
case token::STRING:
{
if (eqType)
{
const wordRe w1(t1.stringToken(), wordRe::DETECT);
@ -175,18 +178,26 @@ bool Foam::functionEntries::ifeqEntry::equalToken
return w1.match(t2.wordToken());
}
return false;
}
case token::VARIABLE:
case token::VERBATIM:
case token::CHAR_DATA:
{
if (t2.isStringType())
{
return t1.stringToken() == t2.stringToken();
}
return false;
}
case token::LABEL:
case token::INTEGER_32 :
{
if (eqType)
{
return t1.int32Token() == t2.int32Token();
}
else if (t2.isLabel())
{
return t1.labelToken() == t2.labelToken();
}
@ -195,8 +206,61 @@ bool Foam::functionEntries::ifeqEntry::equalToken
return t1.labelToken() == t2.scalarToken();
}
return false;
}
case token::INTEGER_64 :
{
if (eqType)
{
return t1.int64Token() == t2.int64Token();
}
else if (t2.isLabel())
{
return t1.labelToken() == t2.labelToken();
}
else if (t2.isScalar())
{
return t1.labelToken() == t2.scalarToken();
}
return false;
}
case token::UNSIGNED_INTEGER_32 :
{
if (eqType)
{
return t1.uint32Token() == t2.uint32Token();
}
else if (t2.isLabel())
{
return t1.labelToken() == t2.labelToken();
}
else if (t2.isScalar())
{
return t1.labelToken() == t2.scalarToken();
}
return false;
}
case token::UNSIGNED_INTEGER_64 :
{
if (eqType)
{
return t1.uint64Token() == t2.uint64Token();
}
else if (t2.isLabel())
{
return t1.labelToken() == t2.labelToken();
}
else if (t2.isScalar())
{
return t1.labelToken() == t2.scalarToken();
}
return false;
}
case token::FLOAT:
{
if (eqType)
{
return equal(t1.floatToken(), t2.floatToken());
@ -210,8 +274,10 @@ bool Foam::functionEntries::ifeqEntry::equalToken
return t1.scalarToken() == t2.scalarToken();
}
return false;
}
case token::DOUBLE:
{
if (eqType)
{
return equal(t1.doubleToken(), t2.doubleToken());
@ -225,6 +291,7 @@ bool Foam::functionEntries::ifeqEntry::equalToken
return t1.scalarToken() == t2.scalarToken();
}
return false;
}
case token::EXPRESSION:
return false;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2014-2016 OpenFOAM Foundation
Copyright (C) 2014-2016,2024 OpenFOAM Foundation
Copyright (C) 2016-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -113,13 +113,13 @@ Foam::Istream& Foam::operator>>(Istream& is, int32_t& val)
}
}
if (t.isLabel())
if (t.is_int32())
{
val = int32_t
(
(prefix == token::MINUS)
? (0 - t.labelToken())
: t.labelToken()
? (0 - t.int32Token())
: t.int32Token()
);
}
else if (t.isScalar())
@ -175,7 +175,7 @@ Foam::Istream& Foam::operator>>(Istream& is, int32_t& val)
Foam::Ostream& Foam::operator<<(Ostream& os, const int32_t val)
{
os.write(label(val));
os.write(val);
os.check(FUNCTION_NAME);
return os;
}
@ -184,7 +184,9 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const int32_t val)
#if (__SIZEOF_LONG__ == 4)
Foam::Istream& Foam::operator>>(Istream& is, long& val)
{
return operator>>(is, reinterpret_cast<int32_t&>(val));
int32_t ival;
is >> ival;
val = static_cast<long>(ival);
}
Foam::Ostream& Foam::operator<<(Ostream& os, const long val)

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2014-2016 OpenFOAM Foundation
Copyright (C) 2014-2016,2024 OpenFOAM Foundation
Copyright (C) 2017-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -113,13 +113,13 @@ Foam::Istream& Foam::operator>>(Istream& is, int64_t& val)
}
}
if (t.isLabel())
if (t.is_int64())
{
val = int64_t
(
(prefix == token::MINUS)
? (0 - t.labelToken())
: t.labelToken()
? (0 - t.int64Token())
: t.int64Token()
);
}
else if (t.isScalar())
@ -176,16 +176,19 @@ Foam::Istream& Foam::operator>>(Istream& is, int64_t& val)
Foam::Ostream& Foam::operator<<(Ostream& os, const int64_t val)
{
os.write(label(val));
os.write(val);
os.check(FUNCTION_NAME);
return os;
}
#ifdef __APPLE__
#ifdef __APPLE__ // (__SIZEOF_LONG__ == 8)
Foam::Istream& Foam::operator>>(Istream& is, long& val)
{
return operator>>(is, reinterpret_cast<int64_t&>(val));
int64_t ival;
is >> ival;
val = static_cast<long>(ival);
return is;
}
Foam::Ostream& Foam::operator<<(Ostream& os, const long val)

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2014-2016 OpenFOAM Foundation
Copyright (C) 2014-2016,2025 OpenFOAM Foundation
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -99,9 +99,9 @@ Foam::Istream& Foam::operator>>(Istream& is, uint32_t& val)
return is;
}
if (t.isLabel())
if (t.is_uint32())
{
val = uint32_t(t.labelToken());
val = t.uint32Token();
}
else if (t.isScalar())
{
@ -148,7 +148,7 @@ Foam::Istream& Foam::operator>>(Istream& is, uint32_t& val)
Foam::Ostream& Foam::operator<<(Ostream& os, const uint32_t val)
{
os.write(label(val));
os.write(val);
os.check(FUNCTION_NAME);
return os;
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2014-2016 OpenFOAM Foundation
Copyright (C) 2014-2016,2024 OpenFOAM Foundation
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
@ -99,9 +99,9 @@ Foam::Istream& Foam::operator>>(Istream& is, uint64_t& val)
return is;
}
if (t.isLabel())
if (t.is_uint64())
{
val = uint64_t(t.labelToken());
val = t.uint64Token();
}
else if (t.isScalar())
{
@ -148,7 +148,7 @@ Foam::Istream& Foam::operator>>(Istream& is, uint64_t& val)
Foam::Ostream& Foam::operator<<(Ostream& os, const uint64_t val)
{
os.write(label(val));
os.write(val);
os.check(FUNCTION_NAME);
return os;
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2024 OpenCFD Ltd.
Copyright (C) 2016-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -381,13 +381,34 @@ Foam::Istream& Foam::ensightReadFile::read(string& value)
}
Foam::Istream& Foam::ensightReadFile::read(label& value)
Foam::Istream& Foam::ensightReadFile::read(int32_t& value)
{
value = getPrimitive<int>(*this);
return *this;
}
Foam::Istream& Foam::ensightReadFile::read(int64_t& value)
{
value = getPrimitive<int>(*this);
return *this;
}
Foam::Istream& Foam::ensightReadFile::read(uint32_t& value)
{
NotImplemented;
return *this;
}
Foam::Istream& Foam::ensightReadFile::read(uint64_t& value)
{
NotImplemented;
return *this;
}
Foam::Istream& Foam::ensightReadFile::read(float& value)
{
value = getPrimitive<float>(*this);

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2016-2024 OpenCFD Ltd.
Copyright (C) 2016-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -133,7 +133,16 @@ public:
virtual Istream& read(string& value) override;
//- Read integer as "%10d" or as binary (narrowed) int
virtual Istream& read(label& value) override;
virtual Istream& read(int32_t& value) override;
//- Read integer as "%10d" or as binary (narrowed) int
virtual Istream& read(int64_t& value) override;
//- Not implemented
virtual Istream& read(uint32_t& value) override;
//- Not implemented
virtual Istream& read(uint64_t& value) override;
//- Read floating-point as "%12.5e" or as binary
virtual Istream& read(float& value) override;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2023 OpenCFD Ltd.
Copyright (C) 2023-2025 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -45,8 +45,12 @@ bool Foam::JSONformatter::writeToken(const token& t)
write(t.boolToken());
break;
case token::tokenType::LABEL:
write(t.labelToken());
case token::tokenType::INTEGER_32 :
write(t.int32Token());
break;
case token::tokenType::INTEGER_64 :
write(t.int64Token());
break;
case token::tokenType::FLOAT: