# PhasicFlow: src/phasicFlow/streams/token/tokenIO.cpp Source File
PhasicFlow is an open-source parallel DEM (discrete element method) package for simulating granular flow. It is developed in C++ and can be exectued on both GPU (like CUDA) and CPU.

tokenIO.cpp
Go to the documentation of this file.
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
// based on OpenFOAM stream, with some modifications/simplifications
// to be tailored to our needs
#include "error.hpp"
#include "token.hpp"
#include "iIstream.hpp"
#include "iOstream.hpp"
namespace pFlow
{
template<class OS>
static OS& printTokenInfo(OS& os, const token& tok)
{
os << "on line " << tok.lineNumber() << ": ";
switch (tok.type())
{
case token::tokenType::UNDEFINED:
os << "UNDEFINED";
warningInFunction
<< "Undefined token" << endl;
break;
case token::tokenType::FLAG:
// Swallow the flag
break;
case token::tokenType::PUNCTUATION:
os << tok.pToken();
break;
case token::tokenType::BOOL:
case token::tokenType::INT64:
os << tok.int64Token();
break;
case token::tokenType::FLOAT:
os << tok.floatToken();
break;
case token::tokenType::DOUBLE:
os <<tok.doubleToken();
break;
// Different behaviour for (serial/parallel) streams: preserve types
case token::tokenType::DIRECTIVE:
case token::tokenType::VARIABLE:
//case token::tokenType::VERBATIMSTRING:
os << tok.stringToken();
break;
case token::tokenType::WORD:
os << tok.wordToken();
break;
case token::tokenType::STRING:
os << tok.stringToken();
break;
case token::tokenType::ERROR:
os << "ERROR";
warningInFunction
<< "Error token" << endl;
break;
default:
os << "UNKNOWN";
warningInFunction
<< "Unknown token" << endl;
}
return os;
}
} // pFlow
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
pFlow::token::token(iIstream& is)
:
token()
{
is.read(*this);
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
pFlow::word pFlow::token::name() const
{
switch (type_)
{
case token::tokenType::UNDEFINED: return "undefined";
case token::tokenType::BOOL: return "bool";
case token::tokenType::FLAG: return "flag";
case token::tokenType::PUNCTUATION: return "punctuation";
case token::tokenType::INT64: return "int64/int32";
case token::tokenType::FLOAT: return "float";
case token::tokenType::DOUBLE: return "double";
case token::tokenType::WORD: return "word";
case token::tokenType::DIRECTIVE: return "directive";
case token::tokenType::STRING: return "string";
//case token::tokenType::VERBATIM: return "verbatim";
case token::tokenType::VARIABLE: return "variable";
//case token::tokenType::COMPOUND: return "compound";
case token::tokenType::ERROR: return "error";
default:
break;
}
return "unknown(" + std::to_string(int(type_)) + ")";
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
pFlow::iIstream& pFlow::operator>>(iIstream& is, token& tok)
{
tok.reset();
return is.read(tok);
}
pFlow::iOstream& pFlow::operator<<(iOstream& os, const token& tok)
{
switch (tok.type_)
{
case token::tokenType::UNDEFINED:
os << "UNDEFINED";
warningInFunction
<< "Undefined token" << endl;
break;
case token::tokenType::FLAG:
// Swallow the flag
break;
case token::tokenType::PUNCTUATION:
os << tok.pToken();
break;
case token::tokenType::BOOL:
case token::tokenType::INT64:
os << tok.int64Token();
break;
case token::tokenType::FLOAT:
os << tok.floatToken();
break;
case token::tokenType::DOUBLE:
os <<tok.doubleToken();
break;
// Different behaviour for (serial/parallel) streams: preserve types
case token::tokenType::DIRECTIVE:
case token::tokenType::VARIABLE:
//case token::tokenType::VERBATIMSTRING:
os.write(tok);
break;
case token::tokenType::WORD:
os << tok.wordToken();
break;
case token::tokenType::STRING:
os << tok.stringToken();
break;
case token::tokenType::ERROR:
os << "ERROR";
warningInFunction
<< "Error token" << endl;
break;
default:
os << "UNKNOWN";
warningInFunction
<< "Unknown token" << endl;
}
os.check(FUNCTION_NAME);
return os;
}
pFlow::iOstream& pFlow::operator<<(iOstream& os, const token::punctuationToken& pt)
{
return os << char(pt);
}
std::ostream& pFlow::operator<<(std::ostream& os, const token::punctuationToken& pt)
{
return os << char(pt);
}
std::ostream& pFlow::operator<<(std::ostream& os, const token& tok)
{
return printTokenInfo(os, tok);
}
pFlow::iOstream& pFlow::token::printInfo(iOstream& os)const
{
return printTokenInfo(os, *this);
}
std::ostream& pFlow::token::printInfo(std::ostream& os)const
{
return printTokenInfo(os, *this);
}
pFlow::iOstream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
pFlow::token::type
tokenType type() const
Return the token type.
Definition: tokenI.hpp:302
pFlow::iIstream::read
virtual iIstream & read(token &)=0
Return next token from stream.
pFlow::token
Token class based on OpenFOAM stream, with some modifications/simplifications to be tailored to our n...
Definition: token.hpp:44
pFlow::token::pToken
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.hpp:470
iIstream.hpp
pFlow::printTokenInfo
static OS & printTokenInfo(OS &os, const token &tok)
Definition: tokenIO.cpp:32
warningInFunction
#define warningInFunction
Report a warning.
Definition: error.hpp:95
pFlow::token::punctuationToken
punctuationToken
Standard punctuation tokens (a character)
Definition: token.hpp:83
pFlow::token::stringToken
const word & stringToken() const
Return const reference to the string contents.
Definition: tokenI.hpp:642
token.hpp
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
FUNCTION_NAME
#define FUNCTION_NAME
Definition: pFlowMacros.hpp:29
pFlow::token::int64Token
int64 int64Token() const
Return int64 value.
Definition: tokenI.hpp:502
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow
Definition: demGeometry.hpp:27
pFlow::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.cpp:42
pFlow::token::reset
void reset()
Reset token to UNDEFINED and clear any allocated storage.
Definition: tokenI.hpp:263
pFlow::token::token
constexpr token() noexcept
Default construct, initialized to an UNDEFINED token.
Definition: tokenI.hpp:89
pFlow::token::doubleToken
double doubleToken() const
Return double value.
Definition: tokenI.hpp:542
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
pFlow::token::printInfo
iOstream & printInfo(iOstream &os) const
Definition: tokenIO.cpp:224
pFlow::operator>>
INLINE_FUNCTION iIstream & operator>>(iIstream &str, AB3History &ab3)
Definition: AdamsBashforth3.hpp:41
pFlow::token::floatToken
float floatToken() const
Return float value.
Definition: tokenI.hpp:524
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
iOstream.hpp
pFlow::token::name
word name() const
Return the name of the token type.
Definition: tokenIO.cpp:110
pFlow::token::type_
tokenType type_
The token type.
Definition: token.hpp:184
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
pFlow::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.hpp:618
pFlow::token::lineNumber
int32 lineNumber() const
The line number for the token.
Definition: tokenI.hpp:378
error.hpp