# PhasicFlow: src/phasicFlow/streams/token/tokenIO.cpp Source File

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
pFlow::token::type
tokenType type() const
Definition: tokenI.hpp:284
pFlow::iIstream::read
virtual iIstream & read(token &)=0
pFlow::token
Definition: token.hpp:42
pFlow::token::pToken
punctuationToken pToken() const
Definition: tokenI.hpp:452
iIstream.hpp
pFlow::printTokenInfo
static OS & printTokenInfo(OS &os, const token &tok)
Definition: tokenIO.cpp:32
warningInFunction
#define warningInFunction
Definition: error.hpp:55
pFlow::token::punctuationToken
punctuationToken
Definition: token.hpp:81
pFlow::token::stringToken
const word & stringToken() const
Definition: tokenI.hpp:624
token.hpp
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
FUNCTION_NAME
#define FUNCTION_NAME
Definition: pFlowMacros.hpp:29
pFlow::token::int64Token
int64 int64Token() const
Definition: tokenI.hpp:484
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
pFlow
Definition: demComponent.hpp:28
pFlow::IOstream::check
virtual bool check(const char *operation) const
Definition: IOstream.cpp:42
pFlow::token::reset
void reset()
Definition: tokenI.hpp:245
pFlow::token::token
constexpr token() noexcept
Definition: tokenI.hpp:89
pFlow::token::doubleToken
double doubleToken() const
Definition: tokenI.hpp:524
pFlow::iIstream
Definition: iIstream.hpp:33
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
Definition: tokenI.hpp:506
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
iOstream.hpp
pFlow::token::name
word name() const
Definition: tokenIO.cpp:110
pFlow::token::type_
tokenType type_
Definition: token.hpp:182
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::token::wordToken
const word & wordToken() const
Definition: tokenI.hpp:600
pFlow::token::lineNumber
int32 lineNumber() const
Definition: tokenI.hpp:360
error.hpp