Commit 5281dd48 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: changed return value to bool for Ostream::write(const token&)

- the return value signals if this method handled this particular type
  of token. This minor change allows this method to be used as a succinct
  prefilter an output token stream. It also provides better encapsulation
  of what the particular output stream handles.

  Eg,

      bool ok = os.write(tok);
      if (!ok)   // or   if (!ok && os.good())
      {
          os << tok;
      }

  instead of

      if (tok.type() == typeA || tok.type() == typeB || ...)
      {
          os.write(tok);
      }
      else
      {
          os << tok;
      }
parent c51ee221
......@@ -96,8 +96,9 @@ public:
// Write functions
//- Write next token to stream
virtual Ostream& write(const token& tok) = 0;
//- Write token to stream or otherwise handle it.
// \return false if the token type was not handled by this method
virtual bool write(const token& tok) = 0;
//- Write character
virtual Ostream& write(const char c) = 0;
......
......@@ -185,25 +185,33 @@ Foam::UOPstream::~UOPstream()
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
Foam::Ostream& Foam::UOPstream::write(const token& tok)
bool Foam::UOPstream::write(const token& tok)
{
// Raw token output only supported for verbatim strings for now
if (tok.type() == token::tokenType::VERBATIMSTRING)
{
writeToBuffer(char(token::tokenType::VERBATIMSTRING));
write(tok.stringToken());
}
else if (tok.type() == token::tokenType::VARIABLE)
{
writeToBuffer(char(token::tokenType::VARIABLE));
write(tok.stringToken());
}
else
// Direct token handling only for some types
switch (tok.type())
{
NotImplemented;
setBad();
case token::tokenType::VERBATIMSTRING :
{
writeToBuffer(char(token::tokenType::VERBATIMSTRING));
write(tok.stringToken());
return true;
}
case token::tokenType::VARIABLE :
{
writeToBuffer(char(token::tokenType::VARIABLE));
write(tok.stringToken());
return true;
}
default:
break;
}
return *this;
return false;
}
......
......@@ -127,7 +127,7 @@ public:
// Inquiry
//- Return flags of output stream
ios_base::fmtflags flags() const
virtual ios_base::fmtflags flags() const
{
return ios_base::fmtflags(0);
}
......@@ -146,25 +146,26 @@ public:
const label communicator = 0
);
//- Write next token to stream
Ostream& write(const token& tok);
//- Write token to stream or otherwise handle it.
// \return false if the token type was not handled by this method
virtual bool write(const token& tok);
//- Write single character. Whitespace is suppressed.
Ostream& write(const char c);
virtual Ostream& write(const char c);
//- Write the word-characters of a character string.
// Sends as a single char, or as word.
Ostream& write(const char* str);
virtual Ostream& write(const char* str);
//- Write word
Ostream& write(const word& str);
virtual Ostream& write(const word& str);
//- Write string
Ostream& write(const string& str);
virtual Ostream& write(const string& str);
//- Write std::string surrounded by quotes.
// Optional write without quotes.
Ostream& writeQuoted
virtual Ostream& writeQuoted
(
const std::string& str,
const bool quoted=true
......@@ -174,70 +175,74 @@ public:
virtual Ostream& write(const int32_t val);
//- Write int64_t as a label
Ostream& write(const int64_t val);
virtual Ostream& write(const int64_t val);
//- Write floatScalar
Ostream& write(const floatScalar val);
virtual Ostream& write(const floatScalar val);
//- Write doubleScalar
Ostream& write(const doubleScalar val);
virtual Ostream& write(const doubleScalar val);
//- Write binary block with 8-byte alignment.
Ostream& write(const char* data, const std::streamsize count);
virtual Ostream& write
(
const char* data,
const std::streamsize count
);
//- Begin marker for low-level raw binary output.
// The count should indicate the number of bytes for subsequent
// writeRaw calls.
Ostream& beginRaw(const std::streamsize count);
virtual Ostream& beginRaw(const std::streamsize count);
//- Low-level raw binary output.
Ostream& writeRaw
virtual Ostream& writeRaw
(
const char* data,
const std::streamsize count
);
//- End marker for low-level raw binary output.
Ostream& endRaw()
virtual Ostream& endRaw()
{
return *this;
}
//- Add indentation characters
void indent()
virtual void indent()
{}
// Stream state functions
//- Flush stream
void flush()
virtual void flush()
{}
//- Add newline and flush stream
void endl()
virtual void endl()
{}
//- Get width of output field
int width() const
virtual int width() const
{
return 0;
}
//- Set width of output field (and return old width)
int width(const int)
virtual int width(const int)
{
return 0;
}
//- Get precision of output field
int precision() const
virtual int precision() const
{
return 0;
}
//- Set precision of output field (and return old precision)
int precision(const int)
virtual int precision(const int)
{
return 0;
}
......
......@@ -30,21 +30,35 @@ License
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::Ostream& Foam::OSstream::write(const token& tok)
bool Foam::OSstream::write(const token& tok)
{
if (tok.type() == token::tokenType::VERBATIMSTRING)
{
write(char(token::HASH));
write(char(token::BEGIN_BLOCK));
writeQuoted(tok.stringToken(), false);
write(char(token::HASH));
write(char(token::END_BLOCK));
}
else if (tok.type() == token::tokenType::VARIABLE)
// Direct token handling only for some types
switch (tok.type())
{
writeQuoted(tok.stringToken(), false);
case token::tokenType::VERBATIMSTRING :
{
write(char(token::HASH));
write(char(token::BEGIN_BLOCK));
writeQuoted(tok.stringToken(), false);
write(char(token::HASH));
write(char(token::END_BLOCK));
return true;
}
case token::tokenType::VARIABLE :
{
writeQuoted(tok.stringToken(), false);
return true;
}
default:
break;
}
return *this;
return false;
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -104,8 +104,9 @@ public:
// Write functions
//- Write next token to stream
virtual Ostream& write(const token& tok);
//- Write token to stream or otherwise handle it.
// \return false if the token type was not handled by this method
virtual bool write(const token& tok);
//- Write character
virtual Ostream& write(const char c);
......
......@@ -65,21 +65,35 @@ void Foam::prefixOSstream::print(Ostream& os) const
}
Foam::Ostream& Foam::prefixOSstream::write(const token& tok)
bool Foam::prefixOSstream::write(const token& tok)
{
if (tok.type() == token::tokenType::VERBATIMSTRING)
{
write(char(token::HASH));
write(char(token::BEGIN_BLOCK));
writeQuoted(tok.stringToken(), false);
write(char(token::HASH));
write(char(token::END_BLOCK));
}
else if (tok.type() == token::tokenType::VARIABLE)
// Direct token handling only for some types
switch (tok.type())
{
writeQuoted(tok.stringToken(), false);
case token::tokenType::VERBATIMSTRING :
{
write(char(token::HASH));
write(char(token::BEGIN_BLOCK));
writeQuoted(tok.stringToken(), false);
write(char(token::HASH));
write(char(token::END_BLOCK));
return true;
}
case token::tokenType::VARIABLE :
{
writeQuoted(tok.stringToken(), false);
return true;
}
default:
break;
}
return *this;
return false;
}
......
......@@ -98,8 +98,9 @@ public:
// Write functions
//- Write next token to stream
virtual Ostream& write(const token& tok);
//- Write token to stream or otherwise handle it.
// \return false if the token type was not handled by this method
virtual bool write(const token& tok);
//- Write character
virtual Ostream& write(const char c);
......
......@@ -200,25 +200,16 @@ void Foam::primitiveEntry::write(Ostream& os, const bool contentsOnly) const
bool addSpace = false; // Separate from previous tokens with a space
for (const token& tok : *this)
{
if (tok.type() == token::tokenType::VERBATIMSTRING)
{
// Bypass token output operator to avoid losing verbatimness.
// Handled in the Ostreams themselves
if (addSpace) os << token::SPACE;
if (addSpace) os << token::SPACE;
os.write(tok);
// Try to output token directly, with special handling in Ostreams.
addSpace = true; // Separate from following tokens
}
else
if (!os.write(tok))
{
if (addSpace) os << token::SPACE;
os << tok;
addSpace = true; // Separate from following tokens
os << tok; // Revert to normal '<<' output operator
}
addSpace = true; // Separate from following tokens
}
if (!contentsOnly)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment