Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • km3py/jppy
  • hwarnhofer/jppy_hannes
2 results
Show changes
Showing
with 4985 additions and 0 deletions
#ifndef __JLANG__JEQUATIONFACET__
#define __JLANG__JEQUATIONFACET__
#include <istream>
#include <ostream>
#include <locale>
#include <string>
#include <iterator>
#include <cstdio>
#include <limits>
#include "JLang/JStringFacet.hh"
#include "JLang/JEquationParameters.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Facet class to specify parsing of equations in currect locale (see class JLANG::JEquation).
* Tokens are defined as a piece of text delimited by various markers according the facet.
* The list of markers is defined by the JLANG::JEquationParameters data structure.
* This class extends the JLANG::JStringFacet and JLANG::JEquationParameters classes.
*/
class JEquationFacet:
public JStringFacet,
public JEquationParameters
{
public:
using JStringFacet::get;
using JStringFacet::put;
/**
* Default constructor.
*/
JEquationFacet() :
JStringFacet(),
JEquationParameters()
{}
/**
* Constructor.
*
* \param parameters equation parameters
*/
JEquationFacet(const JEquationParameters& parameters) :
JStringFacet(),
JEquationParameters(parameters)
{}
/**
* Clone this facet.
*
* \return pointer to newly created facet
*/
virtual JEquationFacet* clone() const override
{
return new JEquationFacet(static_cast<const JEquationParameters&>(*this));
}
/**
* Get character.
*
* \param __begin begin position of input stream
* \param __end end position of input stream
* \param format format
* \param result status after input operation
* \param buffer output character
* \return position of input stream
*/
istreambuf_iterator get(const istreambuf_iterator __begin,
const istreambuf_iterator __end,
const std::ios_base& format,
std::ios_base::iostate& result,
char& buffer) const
{
return do_get(__begin, __end, format, result, buffer);
}
/**
* Put character.
*
* \param out begin position of output stream
* \param format format
* \param c fill character
* \param buffer input character
* \return position of output stream buffer
*/
ostreambuf_iterator put(ostreambuf_iterator out,
const std::ios_base& format,
const char c,
const char buffer) const
{
return do_put(out, format, c, buffer);
}
/**
* Get combined prefix for output.
*
* \param prefix prefix
* \param name name
* \return prefix
*/
const std::string getPrefix(const std::string& prefix, const std::string& name) const
{
if (prefix.empty())
return name;
else
return prefix + getDefaultDivision() + name;
}
/**
* Pop white spaces.
*
* \param in input stream
* \return input stream
*/
std::istream& pop(std::istream& in) const
{
try {
for (int c; (c = in.peek()) != EOF && isWhiteSpace((char) c); ) { in.get(); }
}
catch(const std::exception& error) {};
return in;
}
protected:
/**
* Get string.
*
* \param __begin begin position of input stream
* \param __end end position of input stream
* \param format format
* \param result status after input operation
* \param buffer output string
* \return position of input stream
*/
virtual istreambuf_iterator do_get(const istreambuf_iterator __begin,
const istreambuf_iterator __end,
const std::ios_base& format,
std::ios_base::iostate& result,
std::string& buffer) const override
{
using namespace std;
result = (ios_base::iostate) 0; // reset I/O status
streamsize n = format.width(); // number of characters to read
if (n == 0) {
n = numeric_limits<streamsize>::max();
}
istreambuf_iterator i = __begin;
while (i != __end && isWhiteSpace(*i)) {
++i;
}
if (i == __end) {
result |= ios_base::failbit;
result |= ios_base::eofbit;
} else if (isSeparator(*i) ||
isEndOfLine(*i) ||
isDivision (*i)) {
result |= ios_base::failbit;
} else {
buffer.clear();
buffer.push_back(*i);
for (++i, --n; i != __end && n != 0 && (!isWhiteSpace(*i) &&
!isSeparator (*i) &&
!isEndOfLine (*i) &&
!isDivision (*i)); ++i, --n)
buffer.push_back(*i);
if (i == __end) {
result |= ios_base::eofbit;
}
}
return i;
}
/**
* Get character.
*
* \param __begin begin position of input stream
* \param __end end position of input stream
* \param format format
* \param result status after input operation
* \param buffer output character
* \return position of input stream
*/
virtual istreambuf_iterator do_get(const istreambuf_iterator __begin,
const istreambuf_iterator __end,
const std::ios_base& format,
std::ios_base::iostate& result,
char& buffer) const
{
using namespace std;
result = (ios_base::iostate) 0; // reset I/O status
istreambuf_iterator i = __begin;
while (i != __end && isWhiteSpace(*i)) {
++i;
}
if (i == __end) {
result |= ios_base::failbit;
result |= ios_base::eofbit;
} else if (!isDivision(*i) && !isSeparator(*i)) {
result |= ios_base::failbit;
} else {
buffer = *i++;
}
return i;
}
/**
* Put string.
*
* \param out begin position of output stream
* \param format format
* \param c fill character
* \param buffer input string
* \return current position of output stream
*/
virtual ostreambuf_iterator do_put(ostreambuf_iterator out,
const std::ios_base& format,
const char c,
const std::string& buffer) const override
{
using namespace std;
if (format.flags() & ios_base::right) {
for (streamsize i = buffer.size(); i < format.width(); ++i, ++out) {
*out = c;
}
}
for (string::const_iterator i = buffer.begin(); i != buffer.end(); ++i, ++out) {
*out = *i;
}
if (format.flags() & ios_base::left) {
for (streamsize i = buffer.size(); i < format.width(); ++i, ++out) {
*out = c;
}
}
return out;
}
/**
* Put character.
*
* \param out begin position of output stream
* \param format format
* \param c fill character
* \param buffer input character
* \return current position of output stream
*/
virtual ostreambuf_iterator do_put(ostreambuf_iterator out,
const std::ios_base& format,
const char c,
const char buffer) const
{
using namespace std;
if (format.flags() & ios_base::right) {
for (streamsize i = 1; i < format.width(); ++i, ++out) {
*out = c;
}
}
*out = buffer;
++out;
if (format.flags() & ios_base::left) {
for (streamsize i = 1; i < format.width(); ++i, ++out) {
*out = c;
}
}
return out;
}
/**
* \cond NEVER
* Ignore characters until next end of line.
*
* \param __begin begin position of input stream
* \param __end end position of input stream
* \return position of input stream
* \endcond
*/
/*
virtual istreambuf_iterator do_ignore(const istreambuf_iterator __begin,
const istreambuf_iterator __end) const
{
istreambuf_iterator i = __begin;
while (i != __end && !isEndOfLine(*i)) {
++i;
}
while (i != __end && isEndOfLine(*i)) {
++i; // skip end of line(s)
}
return i;
}
*/
/**
* Read string.
*
* \param __begin begin position of input stream
* \param __end end position of input stream
* \param result status after input operation
* \param buffer output string
* \return position of input stream
*/
virtual istreambuf_iterator do_getline(const istreambuf_iterator __begin,
const istreambuf_iterator __end,
std::ios_base::iostate& result,
std::string& buffer) const override
{
using namespace std;
result = (ios_base::iostate) 0; // reset I/O status
istreambuf_iterator i = __begin;
while (i != __end && isWhiteSpace(*i) && !isEndOfLine(*i)) {
++i;
}
if (i == __end) {
result |= ios_base::failbit;
result |= ios_base::eofbit;
} else if (isEndOfLine(*i)) {
buffer.clear();
} else {
buffer.clear();
for (int count = 0; i != __end && (count != 0 || !isEndOfLine(*i)); ++i) {
buffer.push_back(*i);
if (isLeftBracket (*i)) {
++count;
} else if (isRightBracket(*i)) {
--count;
}
}
while (i != __end && isEndOfLine(*i)) {
++i; // skip end of line(s)
}
}
return i;
}
private:
JEquationFacet(const JEquationFacet&); // not defined
void operator=(const JEquationFacet&); // not defined
};
/**
* Auxiliary class for end of line.
*/
struct JEndOfLine
{
/**
* Default constructor.
*/
JEndOfLine() :
index(0)
{}
/**
* Constructor.
*
* \param i index
* \return this JEndOfLine
*/
const JEndOfLine& operator()(const unsigned int i) const
{
index = i;
return *this;
}
/**
* Print end of line.
*
* \param out output stream
* \param eol end of line
* \return output stream
*/
friend inline std::ostream& operator<<(std::ostream& out, const JEndOfLine& eol)
{
using namespace std;
if (has_facet<JEquationFacet>(out.getloc()))
out << use_facet<JEquationFacet>(out.getloc()).getPreferredEndOfLine(eol.index);
else
out << '\n';
eol.index = 0;
return out;
}
private:
mutable unsigned int index;
};
/**
* Type definition of stream manipulator for equational I/O.
*/
typedef JLANG::JEquationFacet setequation;
/**
* Parse facet.
*
* \param in input stream
* \param facet facet
* \return input stream
*/
inline std::istream& operator>>(std::istream& in, const JLANG::JEquationFacet& facet)
{
using namespace std;
in.imbue(locale(in.getloc(), facet.clone()));
return in;
}
/**
* Parse facet.
*
* \param out output stream
* \param facet facet
* \return output stream
*/
inline std::ostream& operator<<(std::ostream& out, const JLANG::JEquationFacet& facet)
{
using namespace std;
out.imbue(locale(out.getloc(), facet.clone()));
return out;
}
/**
* Print white space.
*
* \param out output stream
* \return output stream
*/
inline std::ostream& white_space(std::ostream& out)
{
using namespace std;
using namespace JLANG;
if (has_facet<JEquationFacet>(out.getloc()))
out << use_facet<JEquationFacet>(out.getloc()).getDefaultWhiteSpace();
else
out << ' ';
return out;
}
/**
* Print division.
*
* \param out output stream
* \return output stream
*/
inline std::ostream& division(std::ostream& out)
{
using namespace std;
using namespace JLANG;
if (has_facet<JEquationFacet>(out.getloc()))
out << use_facet<JEquationFacet>(out.getloc()).getDefaultDivision();
else
out << '.';
return out;
}
/**
* Print separator.
*
* \param out output stream
* \return output stream
*/
inline std::ostream& separator(std::ostream& out)
{
using namespace std;
using namespace JLANG;
if (has_facet<JEquationFacet>(out.getloc()))
out << use_facet<JEquationFacet>(out.getloc()).getDefaultSeparator();
else
out << '=';
return out;
}
/**
* Print end of line.
*/
static const JLANG::JEndOfLine end_of_line;
/**
* Print left bracket.
*
* \param out output stream
* \return output stream
*/
inline std::ostream& left_bracket(std::ostream& out)
{
using namespace std;
using namespace JLANG;
if (has_facet<JEquationFacet>(out.getloc()))
out << use_facet<JEquationFacet>(out.getloc()).getLeftBracket();
else
out << '(';
return out;
}
/**
* Print right bracket.
*
* \param out output stream
* \return output stream
*/
inline std::ostream& right_bracket(std::ostream& out)
{
using namespace std;
using namespace JLANG;
if (has_facet<JEquationFacet>(out.getloc()))
out << use_facet<JEquationFacet>(out.getloc()).getRightBracket();
else
out << ')';
return out;
}
}
#endif
#ifndef __JLANG__JEQUATIONPARAMETERS__
#define __JLANG__JEQUATIONPARAMETERS__
#include <string>
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Simple data structure to support I/O of equations (see class JLANG::JEquation).
*/
class JEquationParameters {
public:
/**
* Default constructor.
*/
JEquationParameters()
{
this->sep = "=";
this->eol = "\n\r;";
this->div = "./";
this->skip = "#";
this->left = '(';
this->right = ')';
this->ws = " \t\n\v\f\r";
}
/**
* Constructor.
*
* \param sep separator characters
* \param eol end of line characters
* \param div division characters
* \param skip skip line characters
* \param left left bracket
* \param right right bracket
* \param ws white space characters
*/
JEquationParameters(const std::string& sep,
const std::string& eol,
const std::string& div,
const std::string& skip,
const char left = '(',
const char right = ')',
const std::string& ws = " \t\n\v\f\r")
{
this->sep = sep;
this->eol = eol;
this->div = div;
this->skip = skip;
this->left = left;
this->right = right;
this->ws = ws;
}
/**
* Get equation parameters.
*
* \return equation parameters
*/
const JEquationParameters& getEquationParameters() const
{
return *this;
}
/**
* Set equation parameters.
*
* \param buffer equation parameters
*/
void setEquationParameters(const JEquationParameters& buffer)
{
static_cast<JEquationParameters&>(*this) = buffer;
}
/**
* Get default separator character.
*
* \return separator between parameter and its value
*/
const char getDefaultSeparator() const
{
if (sep.empty())
return '=';
else
return sep[0];
}
/**
* Get separator characters.
*
* \return separator between parameter and its value
*/
const std::string& getSeparator() const
{
return sep;
}
/**
* Set separator character(s).
*
* \param sep separator between parameter and its value
*/
void setSeparator(const std::string& sep)
{
this->sep = sep;
}
/**
* Get default end of line character.
*
* \return end of line character
*/
const char getDefaultEndOfLine() const
{
if (eol.empty())
return '\n';
else
return eol[0];
}
/**
* Get preferred end of line character.
*
* \param index index
* \return end of line character
*/
const char getPreferredEndOfLine(const unsigned int index) const
{
if (eol.empty())
return '\n';
else if (index < eol.size())
return eol[index];
else
return eol[0];
}
/**
* Get end of line characters.
*
* \return end of line characters
*/
const std::string& getEndOfLine() const
{
return eol;
}
/**
* Set end of line characters.
*
* \param eol end of line character
*/
void setEndOfLine(const std::string& eol)
{
this->eol = eol;
}
/**
* Get default division character.
*
* \return division character
*/
const char getDefaultDivision() const
{
if (div.empty())
return '.';
else
return div[0];
}
/**
* Get division characters.
*
* \return division characters
*/
const std::string& getDivision() const
{
return div;
}
/**
* Set division characters.
*
* \param div division characters
*/
void setDivision(const std::string& div)
{
this->div = div;
}
/**
* Get default skip line character.
*
* \return skip line character
*/
const char getDefaultSkipLine() const
{
if (skip.empty())
return '#';
else
return skip[0];
}
/**
* Get skip line characters.
*
* \return skip line characters
*/
const std::string& getSkipLine() const
{
return skip;
}
/**
* Set skip line characters.
*
* \param skip skip line characters
*/
void setSkipLine(const std::string& skip)
{
this->skip = skip;
}
/**
* Set brackets.
*
* \param left left bracket
* \param right right bracket
*/
void setBrackets(const char left, const char right)
{
this->left = left;
this->right = right;
}
/**
* Get left bracket.
*
* \return left bracket
*/
char getLeftBracket() const
{
return left;
}
/**
* Get right bracket.
*
* \return right bracket
*/
char getRightBracket() const
{
return right;
}
/**
* Get default white space character.
*
* \return white space character
*/
const char getDefaultWhiteSpace() const
{
if (ws.empty())
return ' ';
else
return ws[0];
}
/**
* Get white space characters.
*
* \return white space characters
*/
const std::string& getWhiteSpace() const
{
return ws;
}
/**
* Set white space characters.
*
* \param ws white space characters
*/
void setWhiteSpace(const std::string& ws)
{
this->ws = ws;
}
/**
* Join equation parameters.
*
* \param value equation parameters
*/
JEquationParameters& join(const JEquationParameters& value)
{
using namespace std;
for (string::const_iterator i = value.sep.begin(); i != value.sep.end(); ++i) {
if (!isSeparator(*i)) {
sep += *i;
}
}
for (string::const_iterator i = value.eol.begin(); i != value.eol.end(); ++i) {
if (!isEndOfLine(*i)) {
eol += *i;
}
}
for (string::const_iterator i = value.div.begin(); i != value.div.end(); ++i) {
if (!isDivision(*i)) {
div += *i;
}
}
for (string::const_iterator i = value.skip.begin(); i != value.skip.end(); ++i) {
if (!isSkipLine(*i)) {
skip += *i;
}
}
for (string::const_iterator i = value.ws.begin(); i != value.ws.end(); ++i) {
if (!isWhiteSpace(*i)) {
ws += *i;
}
}
return *this;
}
/**
* Test for separator character.
*
* \param c character
* \result true if separator; else false
*/
inline bool isSeparator(const char c) const
{
return sep .find(c) != std::string::npos;
}
/**
* Test for end of line character.
*
* \param c character
* \result true if end of line; else false
*/
inline bool isEndOfLine (const char c) const { return eol .find(c) != std::string::npos; }
/**
* Test for division character.
*
* \param c character
* \result true if division; else false
*/
inline bool isDivision(const char c) const
{
return div .find(c) != std::string::npos;
}
/**
* Test for skip line character.
*
* \param c character
* \result true if skip line; else false
*/
inline bool isSkipLine(const char c) const
{
return skip.find(c) != std::string::npos;
}
/**
* Test for left bracket character.
*
* \param c character
* \result true if left bracket; else false
*/
inline bool isLeftBracket(const char c) const
{
return c == left;
}
/**
* Test for right bracket character.
*
* \param c character
* \result true if right bracket; else false
*/
inline bool isRightBracket(const char c) const
{
return c == right;
}
/**
* Test for white space character.
*
* \param c character
* \result true if white space; else false
*/
inline bool isWhiteSpace(const char c) const
{
return ws .find(c) != std::string::npos;
}
protected:
std::string sep;
std::string eol;
std::string div;
std::string skip;
char left;
char right;
std::string ws;
};
}
#endif
#ifndef __JLANG__JEXCEPTION__
#define __JLANG__JEXCEPTION__
#include <exception>
#include <string>
#include <ostream>
#include <sstream>
/**
* \file
* Exceptions.
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* General exception
*/
class JException : public std::exception {
public:
/**
* Constructor.
*
* \param error error message
*/
JException(const std::string& error) :
std::exception(),
buffer(error)
{}
/**
* Destructor.
*/
~JException() throw()
{}
/**
* Get error message.
*
* \return error message
*/
virtual const char* what() const throw() override
{
return buffer.c_str();
}
/**
* Print error message of JException.
*
* \param out output stream
* \param exception exception
*/
friend inline std::ostream& operator<<(std::ostream& out, const JException& exception)
{
return out << exception.what();
}
/**
* Get output stream for conversion of exception.
*
* Note that the ostream is emptied before use.
*
* \return ostream
*/
static inline std::ostream& getOstream()
{
static std::ostringstream buffer;
buffer.str("");
return buffer;
}
private:
const std::string buffer;
};
/**
* Exception for accessing an index in a collection that is outside of its range.
*/
class JIndexOutOfRange :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JIndexOutOfRange(const std::string& error) :
JException(error)
{}
};
/**
* Exception for accessing an invalid pointer.
*/
class JPointerException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JPointerException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for a functional operation.
*/
class JFunctionalException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JFunctionalException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for an empty collection.
*/
class JEmptyCollection :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JEmptyCollection(const std::string& error) :
JException(error)
{}
};
/**
* Exception for accessing a value in a collection that is outside of its range.
*/
class JValueOutOfRange :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JValueOutOfRange(const std::string& error) :
JException(error)
{}
};
/**
* Exception for parsing value.
*/
class JParseError :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JParseError(const std::string& error) :
JException(error)
{}
};
/**
* Exception for missing value.
*/
class JNoValue :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JNoValue(const std::string& error) :
JException(error)
{}
};
/**
* Exception for null pointer operation.
*/
class JNullPointerException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JNullPointerException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for cast operation.
*/
class JCastException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JCastException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for numerical precision error.
*/
class JNumericalPrecision :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JNumericalPrecision(const std::string& error) :
JException(error)
{}
};
/**
* Exception for division by zero.
*/
class JDivisionByZero :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JDivisionByZero(const std::string& error) :
JException(error)
{}
};
/**
* Exception for failure of memory allocation.
*/
class JMallocException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JMallocException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for failure of memory allocation.
*/
class JNewException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JNewException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for I/O.
*/
class JIOException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JIOException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for opening of file.
*/
class JFileOpenException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JFileOpenException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for recovery of file.
*/
class JFileRecoveryException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JFileRecoveryException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for reading of file.
*/
class JFileReadException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JFileReadException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for end of file.
*/
class JEndOfFile :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JEndOfFile(const std::string& error) :
JException(error)
{}
};
/**
* Exception for opening of pipe.
*/
class JPipeOpenException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JPipeOpenException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for select call.
*/
class JSelectException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JSelectException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for socket.
*/
class JSocketException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JSocketException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for ControlHost.
*/
class JControlHostException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JControlHostException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for socket channel.
*/
class JSocketChannelException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JSocketChannelException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for creation of fork.
*/
class JForkException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JForkException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for system call.
*/
class JSystemException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JSystemException(const std::string& error) :
JException(error)
{}
};
/**
* Exception when parsing a value.
*/
class JParserException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JParserException(const std::string& error) :
JException(error)
{}
};
/**
* Exception when parsing a value.
*/
class JPropertiesException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JPropertiesException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for missing entry in dictionary.
*/
class JDictionaryEntryNotFound :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JDictionaryEntryNotFound(const std::string& error) :
JException(error)
{}
};
/**
* Exception for duplicate entry in dictionary.
*/
class JDictionaryDuplicateEntry :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JDictionaryDuplicateEntry(const std::string& error) :
JException(error)
{}
};
/**
* Run time exception.
*/
class JRunTimeException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JRunTimeException(const std::string& error) :
JException(error)
{}
};
/**
* Exception for absence of type information.
*/
class JTypeInformationException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JTypeInformationException(const std::string& error) :
JException(error)
{}
};
/**
* Protocol exception.
*/
class JProtocolException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JProtocolException(const std::string& error) :
JException(error)
{}
};
/**
* Database exception.
*/
class JDatabaseException :
public JException
{
public:
/**
* Constructor.
*
* \param error error message
*/
JDatabaseException(const std::string& error) :
JException(error)
{}
};
}
/**
* Make exception.
*
* \param JException_t exception
* \param A message
*/
#define MAKE_EXCEPTION(JException_t, A) JException_t(static_cast<std::ostringstream&>(JLANG::JException::getOstream() << __FILE__ << ':' << __LINE__ << std::endl << A).str())
/**
* Marco for throwing exception with std::ostream compatible message.
*
* \param JException_t exception
* \param A message
*/
#ifndef THROW
#define THROW(JException_t, A) do { throw MAKE_EXCEPTION(JException_t, A); } while(0)
#endif
#endif
#ifndef __JLANG__JFILE__
#define __JLANG__JFILE__
#include <fcntl.h>
#include "JLang/JBinaryIO.hh"
#include "JLang/JAbstractFile.hh"
#include "JLang/JFileDescriptorMask.hh"
#include "JLang/JTimeval.hh"
#include <unistd.h>
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* The JFile class extends the JAbstractFile class.
* This class implements the JBinaryInput and JBinaryOutput interfaces.
*/
class JFile :
public JAbstractFile,
public JBinaryInput,
public JBinaryOutput
{
public:
/**
* Default constructor.
*/
JFile() :
JAbstractFile(),
result(0)
{}
/**
* Constructor.
*
* \param file file descriptor
*/
JFile(const JAbstractFile& file) :
JAbstractFile(file),
result(1)
{}
/**
* Close file.
*/
void close()
{
if (fileDescriptor != FILE_CLOSED) {
::close(fileDescriptor);
fileDescriptor = FILE_CLOSED;
result = 0;
}
}
/**
* Read data from file.
*
* \param buffer buffer
* \param length number of bytes to read
* \return number of bytes read
*/
virtual int read(char* buffer, const int length)
{
return (result = ::read(fileDescriptor, buffer, length));
}
/**
* Write data to file.
*
* \param buffer buffer
* \param length number of bytes to write
* \return number of bytes written
*/
virtual int write(const char* buffer, const int length)
{
return (result = ::write(fileDescriptor, buffer, length));
}
/**
* Check availability of input.
* This method returns true if at least one byte can be read without blocking.
*
* \param timeout timeout
* \return true if ready to read; else false
*/
bool in_avail(JTimeval timeout = JTimeval::min()) const
{
return JFileDescriptorMask(*this).in_avail(timeout);
}
/**
* Check availability of output.
* This method returns true if at least one byte can be written without blocking.
*
* \param timeout timeout
* \return true if ready to write; else false
*/
bool out_avail(JTimeval timeout = JTimeval::min()) const
{
return JFileDescriptorMask(*this).out_avail(timeout);
}
/**
* Check status.
*
* \return true if last I/O operation successful; else false
*/
virtual bool good() const
{
return is_open() && !eof() && !bad();
}
/**
* Check status.
*
* \return true if last I/O operation caused logical error; else false
*/
virtual bool fail() const
{
return result == 0;
}
/**
* Check status.
*
* \return true if last I/O operation caused read/write error; else false
*/
virtual bool bad() const
{
return fail();
}
/**
* Check end of file.
*
* \return true if end of file; else false
*/
virtual bool eof() const
{
return result == EOF;
}
private:
int result;
};
}
#endif
#ifndef __JLANG__JFILEDESCRIPTORMASK__
#define __JLANG__JFILEDESCRIPTORMASK__
#include <sys/select.h>
#include "JLang/JAbstractFile.hh"
#include "JLang/JTimeval.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Auxiliary class for method select.
* This class encapsulates the <tt>fd_set</tT> data structure.
*/
class JFileDescriptorMask :
protected fd_set
{
public:
static const int MAXIMUM_FILE_DESCRIPTOR = FD_SETSIZE;
/**
* Default constructor.
*/
JFileDescriptorMask() :
maximum_file_descriptor (0),
number_of_file_descriptors(0)
{
FD_ZERO(get());
}
/**
* Constructor.
*
* \param file file descriptor
*/
JFileDescriptorMask(const JAbstractFile& file) :
maximum_file_descriptor (0),
number_of_file_descriptors(0)
{
FD_ZERO(get());
set(file);
}
/**
* Constructor.
*
* \param file_descriptor file descriptor
*/
JFileDescriptorMask(const int file_descriptor) :
maximum_file_descriptor (0),
number_of_file_descriptors(0)
{
FD_ZERO(get());
set(file_descriptor);
}
/**
* Get pointer to mask.
*
* \return pointer to mask
*/
const fd_set* get() const
{
return static_cast<const fd_set*>(this);
}
/**
* Get pointer to mask.
*
* \return pointer to mask
*/
fd_set* get()
{
return static_cast<fd_set*>(this);
}
/**
* Address of operator.
*
* \return pointer to mask
*/
const fd_set* operator &() const
{
return get();
}
/**
* Address of operator.
*
* \return pointer to mask
*/
fd_set* operator &()
{
return get();
}
/**
* Reset mask.
* A hard reset causes the reset of the complete mask whereas
* a soft reset causes the reset of the internal parameters only.
*
* \param option true hard reset; else soft reset
*/
void reset(const bool option = true)
{
if (option) {
switch (number_of_file_descriptors) {
case 0:
break;
case 1:
FD_CLR(maximum_file_descriptor, get());
break;
default:
FD_ZERO(get());
break;
}
}
maximum_file_descriptor = 0;
number_of_file_descriptors = 0;
}
/**
* Set file descriptor.
*
* \param file_descriptor file descriptor
*/
void set(const int file_descriptor)
{
if (!has(file_descriptor)) {
FD_SET(file_descriptor, get());
if (file_descriptor > maximum_file_descriptor) {
maximum_file_descriptor = file_descriptor;
}
++number_of_file_descriptors;
}
}
/**
* Set file.
*
* \param file file
*/
void set(const JAbstractFile& file)
{
set(file.getFileDescriptor());
}
/**
* Reset file descriptor.
*
* \param file_descriptor file descriptor
*/
void reset(const int file_descriptor)
{
if (has(file_descriptor)) {
FD_CLR(file_descriptor, get());
while (!has(maximum_file_descriptor) && maximum_file_descriptor != 0)
--maximum_file_descriptor;
--number_of_file_descriptors;
}
}
/**
* Reset file.
*
* \param file file
*/
void reset(const JAbstractFile& file)
{
reset(file.getFileDescriptor());
}
/**
* Has file descriptor.
*
* \param file_descriptor file descriptor
* \return true if file descriptor set; else false
*/
bool has(const int file_descriptor) const
{
return FD_ISSET(file_descriptor, get());
}
/**
* Has file.
*
* \param file file
* \return true if file set; else false
*/
bool has(const JAbstractFile& file) const
{
return has(file.getFileDescriptor());
}
/**
* Get number of file descriptors.
*
* \return number of file descriptors
*/
int getNumberOfFileDescriptors() const
{
return maximum_file_descriptor;
}
/**
* Check setting of file descriptors.
*
* \return true if no file descriptors are set; else false
*/
bool empty() const
{
return number_of_file_descriptors == 0;
}
/**
* Check availability of input.
* This method returns true is at least one byte can be read without blocking.
* Following a select() call, this method overwrites the internal mask!
*
* \param timeout timeout
* \return true if ready to read; else false
*/
bool in_avail(JTimeval timeout = JTimeval::min())
{
return ::select(getNumberOfFileDescriptors() + 1, get(), NULL, NULL, &timeout) > 0;
}
/**
* Check availability of output.
* This method returns true is at least one byte can be written without blocking.
* Following a select() call, this method overwrites the internal mask!
*
* \param timeout timeout
* \return true if ready to write; else false
*/
bool out_avail(JTimeval timeout = JTimeval::min())
{
return ::select(getNumberOfFileDescriptors() + 1, NULL, get(), NULL, &timeout) > 0;
}
private:
int maximum_file_descriptor;
int number_of_file_descriptors;
};
}
#endif
#ifndef __JLANG__JFILESTREAM__
#define __JLANG__JFILESTREAM__
#include <istream>
#include <ostream>
#include "JLang/JFile.hh"
#include "JLang/JFileStreamBuffer.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Streaming of input.
*/
class JFileInputStream :
protected JFileInputStreamBuffer,
public std::istream
{
public:
/**
* Constructor.
*
* \param file file
* \param size size of internal buffer
*/
JFileInputStream(const JAbstractFile& file,
const std::size_t size = 65536) :
JFileInputStreamBuffer(file, size),
std::istream(this)
{}
};
/**
* Streaming of output.
*/
class JFileOutputStream :
protected JFileOutputStreamBuffer,
public std::ostream
{
public:
/**
* Constructor.
*
* \param file file
* \param size size of internal buffer
*/
JFileOutputStream(const JAbstractFile& file,
const std::size_t size = 65536) :
JFileOutputStreamBuffer(file, size),
std::ostream(this)
{}
};
/**
* Streaming of input and output.
*/
class JFileStream :
protected JFileStreamBuffer,
public std::iostream
{
public:
/**
* Constructor.
*
* \param in input file
* \param out output file
* \param size size of internal buffer
*/
JFileStream(const JAbstractFile& in,
const JAbstractFile& out,
const std::size_t size = 65536) :
JFileStreamBuffer (in, out, size),
std::iostream(this)
{}
};
}
#endif
#ifndef __JLANG__JFILESTREAMBUFFER__
#define __JLANG__JFILESTREAMBUFFER__
#include <streambuf>
#include <stdio.h>
#include <string.h>
#include <vector>
#include "JLang/JFile.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Input file stream buffer.
*/
class JFileInputStreamBuffer :
protected virtual std::vector<char>,
public virtual std::streambuf
{
public:
typedef traits_type::int_type int_type;
/**
* Constructor.
*
* \param file file
* \param size size of internal buffer
* \param put_back number of put back characters
*/
JFileInputStreamBuffer(const JAbstractFile& file,
const std::size_t size = 65536,
const std::size_t put_back = 8) :
in (file),
memory(put_back)
{
resize(size + put_back),
setg(this->data(), this->data(), this->data());
}
/**
* Check underflow.
* This method reads as many bytes as possible.
*
* \return first character if OK; else EOF
*/
virtual int_type underflow() override
{
if (gptr() >= egptr()) {
char* __begin = this->data();
if (eback() == __begin) {
// push put back characters
memmove(__begin, egptr() - memory, memory);
__begin += memory;
}
const size_t len = in.read(__begin, this->size() - (__begin - this->data()));
if (len != 0)
setg(this->data(), __begin, __begin + len);
else
return traits_type::eof();
}
return *gptr();
}
/**
* Check availability of input.
* This method returns true if at least one byte can be read without blocking.
*
* \param timeout timeout
* \return true if ready to read; else false
*/
bool in_avail(JTimeval timeout = JTimeval::min()) const
{
return JFileDescriptorMask(in).in_avail(timeout);
}
protected:
JFile in;
std::size_t memory;
private:
JFileInputStreamBuffer(const JFileInputStreamBuffer&);
JFileInputStreamBuffer(JFileInputStreamBuffer&&);
JFileInputStreamBuffer& operator=(const JFileInputStreamBuffer&);
JFileInputStreamBuffer& operator=(JFileInputStreamBuffer&&);
};
/**
* Output file stream buffer.
*/
class JFileOutputStreamBuffer :
protected virtual std::vector<char>,
public virtual std::streambuf
{
public:
typedef traits_type::int_type int_type;
typedef std::streamsize streamsize;
/**
* Constructor.
*
* \param file file
* \param size size of internal buffer
*/
JFileOutputStreamBuffer(const JAbstractFile& file,
const std::size_t size = 65536) :
out (file)
{
// reserve one byte for overflow character
resize(size);
setp(this->data(), this->data() + this->size() - 1);
}
/**
* Destructor.
*/
virtual ~JFileOutputStreamBuffer()
{
sync();
}
/**
* Check overflow.
* This method writes one byte if possible.
*
* \param c character
* \return c if OK; else EOF
*/
virtual int_type overflow(int_type c) override
{
if (c != traits_type::eof()) {
*pptr() = (char) c;
pbump(1);
if (flush() == traits_type::eof()) {
return traits_type::eof();
}
}
return c;
}
/**
* Synchronise buffer.
*/
virtual int sync() override
{
if (flush() == traits_type::eof())
return -1;
else
return 0;
}
/**
* Check availability of output.
* This method returns true if at least one byte can be written without blocking.
*
* \param timeout timeout
* \return true if ready to write; else false
*/
bool out_avail(JTimeval timeout = JTimeval::min()) const
{
return JFileDescriptorMask(out).out_avail(timeout);
}
protected:
/**
* Flush internal buffer.
*/
int flush()
{
const int len = pptr() - pbase();
if (len != 0) {
if (out.write(this->data(), len) != len) {
return traits_type::eof();
}
pbump(-len);
}
return len;
}
JFile out;
private:
JFileOutputStreamBuffer(const JFileOutputStreamBuffer&);
JFileOutputStreamBuffer(JFileOutputStreamBuffer&&);
JFileOutputStreamBuffer& operator=(const JFileOutputStreamBuffer&);
JFileOutputStreamBuffer& operator=(JFileOutputStreamBuffer&&);
};
/**
* Input and output file stream buffer.
*/
class JFileStreamBuffer :
public JFileInputStreamBuffer,
public JFileOutputStreamBuffer
{
public:
/**
* Constructor.
*
* \param in input file
* \param out output file
* \param size size of internal buffer
* \param put_back number of put back characters
*/
JFileStreamBuffer(const JAbstractFile& in,
const JAbstractFile& out,
const std::size_t size = 65536,
const std::size_t put_back = 8) :
JFileInputStreamBuffer (in, size, put_back),
JFileOutputStreamBuffer(out, size)
{}
};
}
#endif
#ifndef __JLANG__JFORWARDITERATOR__
#define __JLANG__JFORWARDITERATOR__
#include <cstddef>
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Template interface for method bool increment().
* This interface implements the pre-fix and post-fix operators <tt>++</tt>.
*/
template<class T>
class JForwardIterator {
public:
/**
* Virtual destructor.
*/
virtual ~JForwardIterator()
{}
/**
* Increment iterator.
*
* \return this iterator
*/
T& operator++()
{
this->increment();
return static_cast<T&>(*this);
}
/**
* Increment iterator.
*
* \return previous iterator
*/
T operator++(int)
{
const T tmp(static_cast<const T&>(*this));
this->increment();
return tmp;
}
/**
* Advance iterator.
*
* \param offset offset
* \return iterator
*/
T& operator+=(const size_t offset)
{
this->increment(offset);
return static_cast<T&>(*this);
}
/**
* Advance operator.
*
* \param object iterator
* \param offset offset
* \return iterator
*/
friend inline T operator+(const T& object, const size_t offset)
{
T tmp(object);
tmp.increment(offset);
return tmp;
}
/**
* Increment iterator.
*
* \return true if incremented; else false
*/
virtual bool increment() = 0;
/**
* Increment iterator.
*
* \param offset offset
* \return true if incremented; else false
*/
virtual bool increment(const size_t offset)
{
size_t i = 0;
while (i != offset && this->increment()) {
++i;
}
return i == offset;
}
};
}
#endif
#ifndef __JLANG__JLANGTOOLKIT__
#define __JLANG__JLANGTOOLKIT__
#include <string>
#include <sstream>
#include <iterator>
#include <ctype.h>
/**
* \author mdejong
*/
/**
* Auxiliary classes and methods for language specific functionality.
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
using std::size_t;
/**
* Get size of c-array.
*
* \param array array
* \return size
*/
template<class T, size_t N>
inline size_t getSize(T (&array)[N])
{
return N;
}
/**
* Check if two objects are indentical.
*
* \param first first object
* \param second second object
* \return true if addresses are equal; else false
*/
template<class JFirst_t, class JSecond_t>
inline bool is_identical(JFirst_t& first, JSecond_t& second)
{
return (void*) &first == (void*) &second;
}
/**
* Check if string is an integer.\n
*
* \param buffer input string
* \return true if integer; else false
*/
inline bool is_integer(const std::string& buffer)
{
using namespace std;
for (string::const_iterator i = buffer. begin(); i != buffer.end(); ++i) {
if (!isdigit(*i)) {
return false;
}
}
return !buffer.empty();
}
/**
* Trim string.\n
* Returns a copy of the string, with leading and trailing white spaces omitted.
*
* \param buffer input string
* \return modified string
*/
inline std::string trim(const std::string& buffer)
{
using namespace std;
string::const_iterator p = buffer. begin();
string::const_reverse_iterator q = buffer.rbegin();
while (p != q.base() && isspace(*p)) { ++p; }
while (p != q.base() && isspace(*q)) { ++q; }
return string(p,q.base());
}
/**
* Trim string.\n
* Returns a copy of the string, with leading and trailing target characters omitted.
*
* \param buffer input string
* \param c strip character
* \return modified string
*/
inline std::string trim(const std::string& buffer, const char c)
{
using namespace std;
string::const_iterator p = buffer. begin();
string::const_reverse_iterator q = buffer.rbegin();
while (p != q.base() && *p == c) { ++p; }
while (p != q.base() && *q == c) { ++q; }
return string(p,q.base());
}
/**
* Replace tokens in string.\n
* Returns a copy of the string, with all occurrences of <tt>target</tt> replaced by <tt>replacement</tt>.
*
* \param input input string
* \param target target string
* \param replacement replacement string
* \return modified string
*/
inline std::string replace(const std::string& input, const std::string& target, const std::string& replacement)
{
using namespace std;
string buffer = input;
for (size_t i = buffer.find(target); i != string::npos; i = buffer.find(target,i)) {
buffer.replace(buffer.begin() + i, buffer.begin() + i + target.length(), replacement);
}
return buffer;
}
/**
* Replace characters in string.\n
* Returns a copy of the string, with all occurrences of <tt>target</tt> replaced by <tt>replacement</tt>.
*
* \param input input string
* \param target target character
* \param replacement replacement string
* \return modified string
*/
inline std::string replace(const std::string& input, const char target, const std::string& replacement)
{
return replace(input, std::string(1, target), replacement);
}
/**
* Replace characters in string.\n
* Returns a copy of the string, with all occurrences of <tt>target</tt> replaced by <tt>replacement</tt>.
*
* \param input input string
* \param target target character
* \param replacement replacement character
* \return modified string
*/
inline std::string replace(const std::string& input, const char target, const char replacement)
{
using namespace std;
string buffer = input;
for (string::iterator i = buffer.begin(); i != buffer.end(); ++i) {
if (*i == target) {
*i = replacement;
}
}
return buffer;
}
/**
* Trim string.\n
* Returns a copy of the string, with leading and trailing target characters omitted.
*
* \param buffer input string
* \param target strip character(s)
* \return modified string
*/
inline std::string trim(const std::string& buffer, const std::string& target)
{
using namespace std;
string::const_iterator p = buffer. begin();
string::const_reverse_iterator q = buffer.rbegin();
while (p != q.base() && target.find(*p) != string::npos) { ++p; }
while (p != q.base() && target.find(*q) != string::npos) { ++q; }
return string(p,q.base());
}
/**
* Convert value to string.
*
* \param value value
* \return string
*/
template<class T>
inline std::string to_string(const T& value)
{
using namespace std;
ostringstream os;
os << value;
return os.str();
}
/**
* Convert string to value.
*
* \param input string
* \return value
*/
template<class T>
inline T to_value(const std::string& input)
{
using namespace std;
T value;
istringstream(input) >> value;
return value;
}
/**
* Convert all character to upper case.
*
* \param value value
* \return string
*/
inline std::string to_upper(const std::string& value)
{
using namespace std;
string buffer(value);
for (string::iterator i = buffer.begin(); i != buffer.end(); ++i) {
*i = toupper(*i);
}
return buffer;
}
/**
* Convert all character to lower case.
*
* \param value value
* \return string
*/
inline std::string to_lower(const std::string& value)
{
using namespace std;
string buffer(value);
for (string::iterator i = buffer.begin(); i != buffer.end(); ++i) {
*i = tolower(*i);
}
return buffer;
}
/**
* Count number of white space separated tokens.
*
* \param buffer input string
* \return number of tokens
*/
inline size_t get_number_of_tokens(const std::string& buffer)
{
using namespace std;
istringstream is(buffer);
return distance(istream_iterator<string>(is), istream_iterator<string>());
}
/**
* Check quotation.
*
* \param value value
* \return true if quoted; else false
*/
inline bool is_single_quoted(const std::string& value)
{
return (value.size() > 1 && *value.begin() == '\'' && *value.rbegin() == '\'');
}
/**
* Check quotation.
*
* \param value value
* \return true if quoted; else false
*/
inline bool is_double_quoted(const std::string& value)
{
return (value.size() > 1 && *value.begin() == '\"' && *value.rbegin() == '\"');
}
/**
* Quote string.
*
* \param value value
* \return string
*/
inline std::string single_quote(const std::string& value)
{
if (!is_single_quoted(value))
return "\'" + value + "\'";
else
return value;
}
/**
* Quote string.
*
* \param value value
* \return string
*/
inline std::string double_quote(const std::string& value)
{
if (!is_double_quoted(value))
return "\"" + value + "\"";
else
return value;
}
}
#endif
#ifndef __JLANG_JMANIP__
#define __JLANG_JMANIP__
#include <string>
#include <ostream>
#include <sstream>
#include <iomanip>
#include <functional>
/**
* \file
* I/O manipulators.
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Get index for user I/O manipulation.
*
* \return index
*/
inline int getIndex()
{
static const int index = std::ios_base::xalloc();
return index;
}
/**
* Print options.
*/
enum JPrintOption_t {
SHORT_PRINT = 1, //!< short print
MEDIUM_PRINT = 2, //!< medium print
LONG_PRINT = 3 //!< long print
};
}
/**
* Get print option.
*
* \param out output stream
* \return print option
*/
inline int getPrintOption(std::ostream& out)
{
return out.iword(JLANG::getIndex());
}
/**
* Set print option.
*
* \param out output stream
* \param option print option
*/
inline void setPrintOption(std::ostream& out, const int option)
{
out.iword(JLANG::getIndex()) = option;
}
/**
* Get short print option.
*
* \param out output stream
* \return true if short print option is on; else false
*/
inline bool getShortprint(std::ostream& out)
{
return getPrintOption(out) == JLANG::SHORT_PRINT;
}
/**
* Set short print option.
*
* \param out output stream
*/
inline void setShortprint(std::ostream& out)
{
return setPrintOption(out, JLANG::SHORT_PRINT);
}
/**
* Get medium print option.
*
* \param out output stream
* \return true if medium print option is on; else false
*/
inline bool getMediumprint(std::ostream& out)
{
return getPrintOption(out) == JLANG::MEDIUM_PRINT;
}
/**
* Set medium print option.
*
* \param out output stream
*/
inline void setMediumprint(std::ostream& out)
{
return setPrintOption(out, JLANG::MEDIUM_PRINT);
}
/**
* Get long print option.
*
* \param out output stream
* \return true if long print option is on; else false
*/
inline bool getLongprint(std::ostream& out)
{
return getPrintOption(out) == JLANG::LONG_PRINT;
}
/**
* Set long print option.
*
* \param out output stream
*/
inline void setLongprint(std::ostream& out)
{
return setPrintOption(out, JLANG::LONG_PRINT);
}
/**
* Set short printing.
*
* \param out output stream
* \return output stream
*/
inline std::ostream& shortprint(std::ostream& out)
{
setShortprint(out);
return out;
}
/**
* Set medium printing.
*
* \param out output stream
* \return output stream
*/
inline std::ostream& mediumprint(std::ostream& out)
{
setMediumprint(out);
return out;
}
/**
* Set long printing.
*
* \param out output stream
* \return output stream
*/
inline std::ostream& longprint(std::ostream& out)
{
setLongprint(out);
return out;
}
/**
* Print newline character.
*
* \param out output stream
* \return output stream
*/
inline std::ostream& newline(std::ostream& out)
{
return out << '\n';
}
/**
* Print white space character.
*
* \param out output stream
* \return output stream
*/
inline std::ostream& whitespace(std::ostream& out)
{
return out << ' ';
}
/**
* Print tab character.
*
* \param out output stream
* \return output stream
*/
inline std::ostream& tab(std::ostream& out)
{
return out << '\t';
}
/**
* Rewind character.
*
* \param out output stream
* \return output stream
*/
inline std::ostream& rewind(std::ostream& out)
{
return (out << '\r').flush();
}
/**
* Auxiliary data structure for alignment of data.
*/
struct WIDTH {
/**
* Constructor.
*
* \param width width
*/
WIDTH(const int width)
{
this->width = width;
}
/**
* Format specifier.
*
* \param out output stream
* \param format format
* \return output stream
*/
friend inline std::ostream& operator<<(std::ostream& out, const WIDTH& format)
{
using namespace std;
return out << setw(format.width);
}
int width;
};
/**
* Auxiliary data structure for alignment of data.
*/
struct LEFT :
public WIDTH
{
/**
* Constructor.
*
* \param width width
*/
LEFT(const int width) :
WIDTH(width)
{}
/**
* Format specifier.
*
* \param out output stream
* \param format format
* \return output stream
*/
friend inline std::ostream& operator<<(std::ostream& out, const LEFT& format)
{
using namespace std;
return out << setw(format.width) << left;
}
};
/**
* Auxiliary data structure for alignment of data.
*/
struct RIGHT :
public WIDTH
{
/**
* Constructor.
*
* \param width width
*/
RIGHT(const int width) :
WIDTH(width)
{}
/**
* Format specifier.
*
* \param out output stream
* \param format format
* \return output stream
*/
friend inline std::ostream& operator<<(std::ostream& out, const RIGHT& format)
{
using namespace std;
return out << setw(format.width) << right;
}
};
/**
* Auxiliary data structure for sequence of same character.
*/
struct FILL :
public WIDTH
{
/**
* Constructor.
*
* \param width width
* \param fill fill character
*/
FILL(const int width = 0,
const char fill = ' ') :
WIDTH(width)
{
this->fill = fill;
}
/**
* Format specifier.
*
* \param out output stream
* \param format format
* \return output stream
*/
friend inline std::ostream& operator<<(std::ostream& out, const FILL& format)
{
using namespace std;
return out << setfill(format.fill) << setw(format.width);
}
char fill;
};
/**
* Auxiliary data structure for alignment of data.
*/
struct CENTER :
public WIDTH
{
protected:
/**
* Auxiliary class for format center.
*/
struct JCenter :
public WIDTH
{
/**
* Constructor.
*
* \param out output stream
* \param format format center
*/
JCenter(std::ostream& out, const WIDTH& format) :
WIDTH(format),
out (out)
{}
/**
* Write value to output stream.
*
* \param value value
* \return this JCenter
*/
template<class T>
std::ostream& operator<<(const T& value)
{
using namespace std;
ostringstream os;
os.copyfmt(out);
os << value;
const int w = this->width - os.str().size();
const char c = this->out.fill();
if (w > 0)
return this->out << FILL(w/2) << ' ' << os.str() << FILL((w+1)/2) << ' ' << setfill(c);
else
return this->out << os.str();
}
private:
std::ostream& out;
};
public:
/**
* Constructor.
*
* \param width width
*/
CENTER(const int width) :
WIDTH(width)
{}
/**
* Format specifier.
*
* \param out output stream
* \param format format
* \return output stream
*/
friend inline JCenter operator<<(std::ostream& out, const CENTER& format)
{
return JCenter(out, format);
}
};
/**
* Auxiliary data structure for floating point format specification.
*/
struct FIXED :
public WIDTH
{
/**
* Constructor.
*
* \param width width
* \param precision precision
*/
FIXED(const int width,
const int precision) :
WIDTH(width)
{
this->precision = precision;
}
/**
* Format specifier.
*
* \param out output stream
* \param format format
* \return output stream
*/
friend inline std::ostream& operator<<(std::ostream& out, const FIXED& format)
{
using namespace std;
return out << fixed << right << setw(format.width) << setprecision(format.precision);
}
int precision;
};
/**
* Auxiliary data structure for floating point format specification.
*/
struct SCIENTIFIC :
public WIDTH
{
/**
* Constructor.
*
* \param width width
* \param precision precision
*/
SCIENTIFIC(const int width,
const int precision) :
WIDTH(width)
{
this->precision = precision;
}
/**
* Format specifier.
*
* \param out output stream
* \param format format
* \return output stream
*/
friend inline std::ostream& operator<<(std::ostream& out, const SCIENTIFIC& format)
{
using namespace std;
return out << scientific << right << setw(format.width) << setprecision(format.precision);
}
int precision;
};
/**
* Data structure for format specifications.
*/
struct JFormat_t {
typedef std::ios_base::fmtflags fmtflags;
/**
* Default constructor.
*/
JFormat_t() :
width (0),
precision(0),
flags (),
fill (' ')
{}
/**
* Constructor.
*
* \param width width
* \param precision precision
* \param flags flags
* \param fill fill character
*/
JFormat_t(const int width,
const int precision = 0,
const fmtflags flags = fmtflags(),
const char fill = ' ') :
width (width),
precision(precision),
flags (flags),
fill (fill)
{}
/**
* Constructor.
*
* \param out output stream
*/
JFormat_t(std::ostream& out)
{
get(out);
}
/**
* Check validity of this manipulator.
*
* \return true if valid; else false
*/
inline bool is_valid() const
{
return (width > 0);
}
/**
* Get format specificaton from given output stream.
*
* \param out output stream
*/
void get(std::ostream& out)
{
this->width = out.width();
this->precision = out.precision();
this->flags = out.flags();
this->fill = out.fill();
}
/**
* Put format specificaton to given output stream.
*
* \param out output stream
*/
void put(std::ostream& out) const
{
out.width (this->width);
out.precision(this->precision);
out.flags (this->flags);
out.fill (this->fill);
}
/**
* Format specifier.
*
* \param out output stream
* \param format format
* \return output stream
*/
friend inline std::ostream& operator<<(std::ostream& out, const JFormat_t& format)
{
format.put(out);
return out;
}
int width;
int precision;
fmtflags flags;
char fill;
};
/**
* Auxiliary class to temporarily define format specifications.
*
* The format specification of the output stream in use will be restored when this object is destroyed.
*/
struct JFormat :
public JFormat_t
{
/**
* Constructor.
*
* \param out output stream
*/
JFormat(std::ostream& out) :
JFormat_t(),
out (out),
format (out)
{}
/**
* Constructor.
*
* \param out output stream
* \param format format
*/
JFormat(std::ostream& out, const JFormat_t& format) :
JFormat_t(format),
out (out),
format (out)
{}
/**
* Destructor.
*/
~JFormat()
{
format.put(out);
}
private:
std::ostream& out;
const JFormat_t format;
};
/**
* Get format for given type.
*
* \return format
*/
template<class T>
inline JFormat_t& getFormat()
{
static JFormat_t manip;
return manip;
}
/**
* Get format for given type.
*
* \param format default format
* \return actual format
*/
template<class T>
inline JFormat_t getFormat(const JFormat_t& format)
{
const JFormat_t& buffer = getFormat<T>();
if (buffer.is_valid())
return buffer;
else
return format;
}
/**
* Set format for given type.
*
* \param format format
*/
template<class T>
inline void setFormat(const JFormat_t& format)
{
getFormat<T>() = format;
}
/**
* Auxiliary data structure to convert (lambda) function to printable object.
*
* The (lambda) function should conform with the type definition LAMBDA::function_type.\n
* This data structure acts as a simple "wrapper" and should be used if the lambda has capture functionality.
*/
struct LAMBDA {
/**
* Type definition of print function.
*/
typedef std::function<void(std::ostream&)> function_type;
/**
* Constructor.
*
* \param f1 (lambda) function
*/
LAMBDA(const function_type& f1) :
f1(f1)
{}
/**
* Write printable object to output stream.
*
* \param out output stream
* \param object printable object
* \return output stream
*/
friend inline std::ostream& operator<<(std::ostream& out, const LAMBDA& object)
{
object.f1(out);
return out;
}
private:
const function_type& f1;
};
#endif
#ifndef __JLANG__JMEMORY__
#define __JLANG__JMEMORY__
#include <stdlib.h>
/**
* \file
*
* Base class for memory management.
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Memory management class for create/release policy based on new/delete.
*/
template<class JClass_t>
class JNew {
public:
/**
* Create object in memory.
*
* \return pointer to data
*/
static inline JClass_t* create()
{
return new JClass_t();
}
/**
* Release memory.
*
* \param p pointer to data
*/
static inline void release(JClass_t* p)
{
delete p;
}
};
/**
* Memory management class for create/release policy based on new []/delete [].
*/
template<class JClass_t>
class JNewCArray {
public:
/**
* Create array of objects in memory.
*
* \param size number of elements
* \return pointer to data
*/
static inline JClass_t* create(const unsigned int size)
{
return new JClass_t[size];
}
/**
* Release memory.
*
* \param p pointer to data
*/
static inline void release(JClass_t* p)
{
delete [] p;
}
};
/**
* Memory management class for create/release policy based on malloc()/free().
*/
template<class JClass_t>
class JMalloc {
public:
/**
* Create object in memory.
*
* \return pointer to data
*/
static inline JClass_t* create()
{
return (JClass_t*) malloc(sizeof(JClass_t));
}
/**
* Create array of objects in memory.
*
* \param size number of elements
* \return pointer to data
*/
static inline JClass_t* create(const unsigned int size)
{
return (JClass_t*) malloc(size * sizeof(JClass_t));
}
/**
* Release memory.
*
* \param p pointer to data
*/
static inline void release(JClass_t* p)
{
free((void*) p);
}
};
}
#endif
#ifndef __JLANG__JNULLTYPE__
#define __JLANG__JNULLTYPE__
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Auxiliary class for no type definition.
* This class can be used to terminate a type list, define a default template argument, etc.
*/
struct JNullType {};
}
#endif
#ifndef __JLANG__JOBJECTID__
#define __JLANG__JOBJECTID__
#include <istream>
#include <ostream>
#include "JLang/JComparable.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Auxiliary class for object identification.
*/
class JObjectID :
public JComparable<JObjectID>,
public JComparable<JObjectID, int>
{
public:
/**
* Default constructor.
*/
JObjectID() :
__id(-1)
{}
/**
* Constructor.
*
* \param id identifier
*/
JObjectID(const int id) :
__id(id)
{}
/**
* Get identifier.
*
* \return identifier
*/
int getID() const
{
return __id;
}
/**
* Get identifier.
*
* \return identifier
*/
int& getID()
{
return __id;
}
/**
* Set identifier.
*
* \param id identifier
*/
void setID(const int id)
{
this->__id = id;
}
/**
* Less than method.
*
* \param object object identifier
* \return true if this identifier less than given identifier; else false
*/
inline bool less(const JObjectID& object) const
{
return this->getID() < object.getID();
}
/**
* Less than method.
*
* \param id identifier
* \return true if this identifier less than given identifier; else false
*/
inline bool less(const int id) const
{
return this->getID() < id;
}
/**
* More than method.
*
* \param id identifier
* \return true if this identifier greater than given identifier; else false
*/
inline bool more(const int id) const
{
return this->getID() > id;
}
/**
* Read object identifier from input.
*
* \param in input stream
* \param object object identifier
* \return input stream
*/
friend inline std::istream& operator>>(std::istream& in, JObjectID& object)
{
return in >> object.__id;
}
/**
* Write object identifier to output.
*
* \param out output stream
* \param object object identifier
* \return output stream
*/
friend inline std::ostream& operator<<(std::ostream& out, const JObjectID& object)
{
return out << object.__id;
}
protected:
int __id;
};
/**
* Get undefined object identifier.
*
* \return undefined object identifier
*/
inline const JObjectID& getUndefinedObjectID()
{
static JObjectID id;
return id;
}
}
#endif
#ifndef __JLANG__JOBJECTIO__
#define __JLANG__JOBJECTIO__
#include <string>
#include <fstream>
#include "JLang/JType.hh"
#include "JLang/JException.hh"
/**
* \file
*
* General methods for loading and storing a single object from and to a file, respectively.
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Get error status of reader.
*
* \param reader reader
* \return true if error; else false
*/
template<class JReader_t>
inline bool getError(const JReader_t& reader)
{
return !reader;
}
/**
* Get error status of reader.
*
* \param reader reader
* \return true if error; else false
*/
inline bool getError(const std::ifstream& reader)
{
return reader.bad() || (reader.fail() && !reader.eof());
}
/**
* Load object from input file.
*
* \param file_name file name
* \param object object to be read
*/
template<class JReader_t, class T>
inline void load(const std::string& file_name, T& object)
{
load(file_name, object, JType<JReader_t>());
}
/**
* Store object to output file.
*
* \param file_name file name
* \param object object to be written
*/
template<class JWriter_t, class T>
inline void store(const std::string& file_name, const T& object)
{
store(file_name, object, JType<JWriter_t>());
}
/**
* Load object from input file.
*
* This method makes use of the STD protocol for the given type reader, namely:
* <pre>
* JReader_t in(file_name);
*
* in >> object;
*
* in.close();
* </pre>
* This method should be overloaded if the type reader requires a different protocol.
*
* \param file_name file name
* \param object object to be read
* \param type reader type
*/
template<class JReader_t, class T>
inline void load(const std::string& file_name, T& object, JType<JReader_t> type)
{
JReader_t in(file_name.c_str());
if (!in) {
THROW(JFileOpenException, "Error opening file: " << file_name);
}
in >> object;
if (getError(in)) {
THROW(JFileReadException, "Error reading file: " << file_name);
}
in.close();
}
/**
* Store object to output file.
*
* This method makes use of the STD protocol for the given type writer, namely:
* <pre>
* JWriter_t out(file_name);
*
* out << object;
*
* out.close();
* </pre>
* This method should be overloaded if the type writer requires a different protocol.
*
* \param file_name file name
* \param object object to be written
* \param type writer type
*/
template<class JWriter_t, class T>
inline void store(const std::string& file_name, const T& object, JType<JWriter_t> type)
{
JWriter_t out(file_name.c_str());
if (!out) {
THROW(JFileOpenException, "Error opening file: " << file_name);
}
out << object;
out.close();
}
/**
* Load object from input file.
*
* \param file_name file name
* \param object object to be read
*/
template<class T>
inline void load(const std::string& file_name, T& object)
{
load(file_name, object, JType<std::ifstream>());
}
/**
* Store object to output file.
*
* \param file_name file name
* \param object object to be written
*/
template<class T>
inline void store(const std::string& file_name, const T& object)
{
store(file_name, object, JType<std::ofstream>());
}
}
#endif
#ifndef __JLANG__JOBJECTSTREAMIO__
#define __JLANG__JOBJECTSTREAMIO__
#include <fstream>
#include "JLang/JObjectIO.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Auxiliary base class for storing and loading a single object to and from an ASCII file, respectively.
* The implementation of this functionality is based on a static cast of
* the given template parameter (known as "curiously recurring template pattern").
*/
template<class T>
struct JObjectStreamIO {
/**
* Load from input file.
*
* \param file_name file name
*/
void load(const char* file_name)
{
JLANG::load<std::ifstream>(file_name, static_cast<T&>(*this));
}
/**
* Store to output file.
*
* \param file_name file name
*/
void store(const char* file_name) const
{
JLANG::store<std::ofstream>(file_name, static_cast<const T&>(*this));
}
};
}
#endif
#ifndef __JLANG__JPARAMETER__
#define __JLANG__JPARAMETER__
#include <istream>
#include <ostream>
#include "JLang/JClass.hh"
#include "JLang/JComparable.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Parameter class.
*
* This class is a simple wrapper around the template parameter with an additional status value.\n
* The status value indicates whether the parameter has been defined or not.\n
* A parameter is defined when a value has been assigned or correctly read.\n
* Note that the comparison between parameter objects is based on the philosophy "undefined = any value".\n
* Hence, if any of the two parameter values is undefined, they are considered equal.\n
* The comparison between a parameter object with a template value is based
* on the internal value of the parameter object via implicit type conversion,
* regardless of its state.
*/
template<class T>
class JParameter :
public JComparable< JParameter<T> >
{
public:
typedef typename JClass<T>::argument_type argument_type;
/**
* Default constructor.
*/
JParameter() :
__value(),
is_defined(false)
{}
/**
* Constructor.
*
* \param value value
*/
explicit JParameter(const argument_type& value) :
__value(value),
is_defined(true)
{}
/**
* Assignment operator.
*
* \param value value
* \return this parameter
*/
JParameter<T>& operator=(const argument_type& value)
{
setValue(value);
return *this;
}
/**
* Get value of parameter.
*
* \return value
*/
const T& getValue() const
{
return __value;
}
/**
* Get value of parameter.
*
* \return value
*/
T& getValue()
{
return __value;
}
/**
* Set value.
*
* \param value value
*/
void setValue(const argument_type& value)
{
__value = value;
is_defined = true;
}
/**
* Type conversion operator.
*
* \return value
*/
operator const T&() const
{
return getValue();
}
/**
* Type conversion operator.
*
* \return value
*/
operator T&()
{
return getValue();
}
/**
* Get status of parameter.
*
* \return true if value has been defined (by read or assignment); else false
*/
const bool isDefined() const
{
return is_defined;
}
/**
* Less than method.
*
* This method evaluates to true if both parameter values are defined and
* this value is less than the value of the given parameter object.
*
* \param parameter parameter
* \return true if both defined and first value less than second value; else false
*/
inline bool less(const JParameter<T>& parameter) const
{
return this->isDefined() && parameter.isDefined() && this->getValue() < parameter.getValue();
}
/**
* Stream input.
*
* \param in input stream
* \param parameter parameter
* \return input stream
*/
friend inline std::istream& operator>>(std::istream& in, JParameter<T>& parameter)
{
in >> parameter.__value;
parameter.is_defined = (bool) in;
return in;
}
/**
* Stream output.
*
* \param out output stream
* \param parameter parameter
* \return output stream
*/
friend inline std::ostream& operator<<(std::ostream& out, const JParameter<T>& parameter)
{
if (parameter.is_defined) {
out << parameter.__value;
}
return out;
}
protected:
T __value;
bool is_defined;
};
}
#endif
#ifndef __JLANG__JPOINTER__
#define __JLANG__JPOINTER__
#include "JLang/JAbstractPointer.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Template implementation of class that holds pointer to object(s).
*
* This class implements the JAbstractPointer interface.
*/
template<class JClass_t>
class JPointer :
public JAbstractPointer<JClass_t>
{
public:
using JAbstractPointer<JClass_t>::set;
using JAbstractPointer<JClass_t>::reset;
/**
* Default constructor.
*/
JPointer() :
__p(NULL)
{}
/**
* Constructor.
*
* \param p pointer to object
*/
JPointer(JClass_t* p) :
__p(p)
{}
/**
* Constructor.
*
* \param pointer pointer to object
*/
template<class T>
JPointer(JPointer<T> pointer) :
__p(pointer.get())
{}
/**
* Get pointer.
*
* \return pointer to object
*/
virtual JClass_t* get() const override
{
return this->__p;
}
/**
* Set pointer.
*
* \param p pointer to object
*/
virtual void set(JClass_t* p) override
{
this->__p = p;
}
/**
* Reset pointer.
*/
virtual void reset() override
{
this->__p = NULL;
}
/**
* Set pointer.
*
* \param pointer pointer to object
*/
template<class T>
void set(const JPointer<T>& pointer)
{
this->set(pointer.get());
}
/**
* Reset pointer.
*
* \param pointer pointer to object
*/
template<class T>
void reset(const JPointer<T>& pointer)
{
this->reset(pointer.get());
}
/**
* Get rereference to internal pointer.
*
* \return reference to internal pointer
*/
JClass_t* const& getReference() const
{
return __p;
}
/**
* Get rereference to internal pointer.
*
* \return reference to internal pointer
*/
JClass_t* & getReference()
{
return __p;
}
protected:
JClass_t* __p; //!< pointer to object
};
}
#endif
#ifndef __JLANG__JPRINTHELPER__
#define __JLANG__JPRINTHELPER__
#include <ostream>
#include "JLang/JType.hh"
#include "JLang/JBool.hh"
#include "JLang/JTest.hh"
#include "JLang/JClass.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Auxiliary class to print via member method <tt>const char* __str__() const;</tt>.
*/
class JPrintHelper {
/**
* Print interface.
*/
struct JTypeout {
/**
* Virtual destructor.
*/
virtual ~JTypeout()
{}
/**
* Print object.
*
* \param out output stream
* \param p pointer to object
* \return output stream
*/
virtual std::ostream& print(std::ostream& out, const void* p) const = 0;
};
/**
* Type writer implementation of interface JTypeout based on member method <tt>const char* __str__() const;</tt>
*/
template<class T>
struct JTypewriter :
public JTypeout
{
/**
* Print object.
*
* \param out output stream
* \param p pointer to object
* \return output stream
*/
virtual std::ostream& print(std::ostream& out, const void* p) const override
{
return out << ((const T*) p)->__str__();;
}
};
/**
* Get type writer.
*
* \param type type
* \param option true
*/
template<class T>
static JTypeout* get(JType<T> type, JBool<true> option)
{
return new JTypewriter<T>();
}
const void* p; //!< pointer to object
JTypeout* typeout; //!< pointer to printer interface
public:
/**
* Test for availability of member method <tt>const char* __str__() const;</tt>.
*/
template<class T, bool is_primitive = JClass<T>::is_primitive>
class JMemberMethod :
public JTest
{
using JTest::test;
template<class U>
static JTrue test(JTypecheck<const char* (U::*)() const, &U::__str__>*);
public:
static const bool __str__ = JTEST(test<T>(0));
};
/**
* Specialisation of JMemberMethod for primitive data types.
*/
template<class T>
struct JMemberMethod<T, true>
{
static const bool __str__ = false;
};
/**
* Constructor
*
* \param object object
*/
template<class T>
JPrintHelper(const T& object) :
p(&object),
typeout(get(JType<T>(), JBool<JMemberMethod<T>::__str__>()))
{}
/**
* Destructor.
*/
~JPrintHelper()
{
delete typeout;
}
/**
* Print object.
*
* \param out output stream
* \return output stream
*/
inline std::ostream& print(std::ostream& out) const
{
return typeout->print(out, p);
}
private:
JPrintHelper(const JPrintHelper&);
JPrintHelper(JPrintHelper&&);
JPrintHelper& operator=(const JPrintHelper&);
JPrintHelper& operator=(JPrintHelper&&);
};
/**
* Auxiliary class to temporarily replace std::ostream.
*/
struct JPrinter {
/**
* Constructor.
*
* \param out output stream
*/
JPrinter(std::ostream& out) :
__out(out)
{}
/**
* Type definition of I/O operator.
*/
typedef std::ostream& (*io_manip) (std::ostream&);
/**
* Parse I/O manipulator.
*
* \param manip I/O manipulator
* \return output stream
*/
inline std::ostream& operator<<(io_manip manip)
{
return __out << manip;
}
/**
* Parse object.
*
* \param object object
* \return output stream
*/
template<class T>
inline std::ostream& operator<<(const T& object)
{
return __out << object;
}
private:
std::ostream& __out;
};
}
/**
* Print object via helper.
*
* \param out output stream
* \param object object
* \return output stream
*/
inline JLANG::JPrinter operator<<(std::ostream& out, const JLANG::JPrintHelper& object)
{
return object.print(out);
}
#endif
#ifndef __JLANG__JSTDTYPES__
#define __JLANG__JSTDTYPES__
/**
* \file
*
* Forward declarations of STD containers.
* \author mdejong
*/
namespace std {
template<class JElement_t, class JAllocator_t> class vector;
template<class JElement_t, class JComparator_t, class JAllocator_t> class set;
template<class JElement_t, class JComparator_t, class JAllocator_t> class multiset;
template<class JFirst_t, class JSecond_t> class pair;
template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t> class map;
template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t> class multimap;
template<class Category, class T, class Distance, class Pointer, class Reference> struct iterator;
#ifdef _GLIBCXX_USE_CXX11_ABI
inline namespace __cxx11 {
template<class JElement_t, class JAllocator_t> class list;
}
#else
template<class JElement_t, class JAllocator_t> class list;
#endif
}
#endif
#ifndef __JLANG__JSHAREDCOUNTER__
#define __JLANG__JSHAREDCOUNTER__
#include <cstdlib>
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Shared counter.
*/
class JSharedCounter
{
public:
/**
* Default constructor.
*/
JSharedCounter() :
counter(NULL)
{}
/**
* Initialise counter.
*/
void initialise()
{
detach();
counter = new int(1);
}
/**
* Attach this counter to given shared counter object.
*
* \param object shared counter
*/
void attach(const JSharedCounter& object)
{
detach();
counter = object.counter;
if (counter != NULL) {
++(*counter);
}
}
/**
* Detach.
*
* \return true if counter at zero; else false
*/
bool detach()
{
if (counter != NULL) {
if (--(*counter) == 0) {
delete counter;
counter = NULL;
return true;
}
counter = NULL;
}
return false;
}
/**
* Get count
*
* \return count
*/
const int getCount()
{
return (counter != NULL ? *counter : 0);
}
protected:
int* counter;
};
}
#endif