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 3859 additions and 0 deletions
#ifndef __JLANG__JSHAREDPOINTER__
#define __JLANG__JSHAREDPOINTER__
#include "JLang/JSharedCounter.hh"
#include "JLang/JMemory.hh"
#include "JLang/JStorage.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* The template JSharedPointer class can be used to share a pointer to an object.
* The object pointed to is deleted when the shared counter is zero,
* i.e.\ when no-one shares the object.
* It is possible to create a container with shared pointers.
* The first template argument refers to the data type pointed to
* and the second to the memory management policy.
*/
template<class JClass_t, template<class> class JMemory_t = JNew>
class JSharedPointer :
public JSharedCounter,
public JStorage<JClass_t, JMemory_t>
{
public:
typedef JPointer<JClass_t> pointer_type;
typedef JStorage<JClass_t, JMemory_t> storage_type;
using JAbstractPointer<JClass_t>::reset;
/**
* Default constructor.
*/
JSharedPointer()
{}
/**
* Copy constructor.
* The reference counter of the shared object is incremented by one.
*
* \param object shared pointer
*/
JSharedPointer(const JSharedPointer& object)
{
if (object.is_valid()) {
this->set(object);
}
}
/**
* Assignment constructor.
* If the pointer is valid, the reference counter of the shared object pointed to
* is initialised to one.
*
* \param p pointer to derived class object
*/
template<class JDerived_t>
JSharedPointer(JDerived_t* p)
{
if (p != NULL) {
this->set(p);
}
}
/**
* Destructor.
* The reference counter is decremented by one and
* the object pointed to is deleted when the reference counter is zero.
*/
~JSharedPointer()
{
if (this->detach()) {
storage_type::reset();
}
}
/**
* Get shared pointer.
*
* \return this shared pointer
*/
const JSharedPointer& getSharedPointer() const
{
return static_cast<const JSharedPointer&>(*this);
}
/**
* Get shared pointer.
*
* \return this shared pointer
*/
JSharedPointer& getSharedPointer()
{
return static_cast<JSharedPointer&>(*this);
}
/**
* Set shared pointer.
*
* \param object shared pointer
*/
void setSharedPointer(const JSharedPointer& object)
{
if (this->get() != object.get()) {
this->reset();
if (object.is_valid()) {
this->set(object);
}
}
}
/**
* Assignment operator.
* The reference counter is decremented by one and
* the object pointed to previously is deleted when its reference counter is zero.
* The reference counter of the shared object is incremented by one.
*
* \param object shared pointer
* \return this shared pointer
*/
JSharedPointer& operator=(const JSharedPointer& object)
{
this->setSharedPointer(object);
return *this;
}
/**
* Assignment operator.
* The reference counter is decremented by one and
* the object pointed to previously is deleted when its reference counter is zero.
* If the pointer is valid, the reference counter of the shared object pointed to
* is initialised to one.
*
* \param p pointer to derived class object
* \return this shared pointer
*/
template<class JDerived_t>
JSharedPointer& operator=(JDerived_t* p)
{
this->reset(p);
return *this;
}
/**
* Reset pointer.
* The reference counter is decremented by one and
* the object pointed to previously is deleted when its reference counter is zero.
*/
virtual void reset() override
{
if (this->detach()) {
storage_type::reset();
}
pointer_type::reset();
}
protected:
/**
* Set pointer.
* The reference counter of the shared object pointed to is incremented by one.
*
* \param object shared pointer
*/
void set(const JSharedPointer& object)
{
pointer_type::set(object.get());
this->attach(object);
}
/**
* Set pointer.
* The reference counter of the shared object pointed to is initialised to one.
*
* \param p pointer to derived class object
*/
virtual void set(JClass_t* p) override
{
pointer_type::set(p);
this->initialise();
}
};
}
#endif
#ifndef __JLANG__JSINGLEPOINTER__
#define __JLANG__JSINGLEPOINTER__
#include "JLang/JStorage.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* The template JSinglePointer class can be used to hold a pointer to an object.
* The object pointed to is deleted at destruction.
* It is possible to create a container with single pointers.
* The first template argument refers to the data type pointed to
* and the second to the memory management policy.
*/
template<class JClass_t, template<class> class JMemory_t = JNew>
class JSinglePointer :
public JStorage<JClass_t, JMemory_t>
{
public:
/**
* Default constructor.
*/
JSinglePointer()
{}
/**
* Copy constructor.
* The ownership of the object pointed to is transferred to this single pointer.
*
* \param object single pointer
*/
JSinglePointer(const JSinglePointer& object)
{
this->set(object.get());
const_cast<JSinglePointer&>(object).set(NULL);
}
/**
* Constructor.
*
* \param p pointer to object
*/
JSinglePointer(JClass_t* p)
{
this->set(p);
}
/**
* Destructor.
*/
~JSinglePointer()
{
this->reset();
}
/**
* Assignment operator.
* The object pointed to previously is deleted.
* The ownership of the object pointed to is transferred to this single pointer.
*
* \param object single pointer
* \return this single pointer
*/
JSinglePointer& operator=(const JSinglePointer& object)
{
if (this->get() != object.get()) {
this->reset();
if (object.is_valid()) {
this->set(object.get());
const_cast<JSinglePointer&>(object).set(NULL);
}
}
return *this;
}
/**
* Assignment operator.
* The object pointed to previously is deleted.
*
* \param p pointer to derived class object
* \return this single pointer
*/
template<class JDerived_t>
JSinglePointer& operator=(JDerived_t* p)
{
this->reset(p);
return *this;
}
};
}
#endif
#ifndef __JLANG__JSTORAGE__
#define __JLANG__JSTORAGE__
#include "JLang/JPointer.hh"
#include "JLang/JMemory.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Template storage class.
* This class extends the JPointer class with storage capabilities.
* The first template argument refers to the data type pointed to
* and the second (optional argument) to the memory management policy (see e.g.\ JMemory.hh).
* The method create() can be used to allocate memory;
* the method reset() releases the allocated memory.
*/
template<class JClass_t, template<class> class JMemory_t = JNew>
class JStorage :
public JPointer <JClass_t>,
public JMemory_t<JClass_t>
{
public:
typedef JPointer <JClass_t> pointer_type;
typedef JMemory_t<JClass_t> memory_type;
using pointer_type::reset;
/**
* Reset pointer.
* The allocated memory is released.
*/
virtual void reset() override
{
if (this->is_valid()) {
this->release();
}
pointer_type::reset();
}
/**
* Recreate object in memory.
* A new object is created if no memory is allocated yet,
* else the previously created object is maintained.
*/
void recreate()
{
if (!this->is_valid()) {
this->set(memory_type::create());
}
}
/**
* Create object in memory.
* The memory allocated by a previously created object will be released.
*/
void create()
{
this->reset(memory_type::create());
}
/**
* Create array of objects in memory.
* The memory allocated by previously created objects will be released.
*
* \param size number of elements
*/
void create(const unsigned int size)
{
this->reset(memory_type::create(size));
}
protected:
/**
* Release memory.
*/
void release()
{
memory_type::release(this->get());
}
};
}
#endif
#ifndef __JLANG__JSTREAMAVAILABLE__
#define __JLANG__JSTREAMAVAILABLE__
#include <istream>
#include <ostream>
#include "JLang/JAnyType.hh"
#include "JLang/JNullType.hh"
#include "JLang/JTest.hh"
#include "JLang/JBool.hh"
#include "JLang/JPrintHelper.hh"
/**
* \author mdejong
*/
/**
* Fallback implementation for <tt>std::istream& operator>>(std::istream, T&)</tt>
* for types that don't support the stream operator.
*
* \param in input stream
* \param any_type any type
* \return null type
*/
JLANG::JNullType operator>>(std::istream& in, JLANG::JAnyType any_type);
/**
* \cond NEVER
* Fallback implementation for <tt>std::ostream& operator<<(std::ostream, const T&)</tt>
* for types that don't support the stream operator.
*
* \param out output stream
* \param any_type any type
* \return null type
* \endcond
*/
//JLANG::JNullType operator<<(std::ostream& out, JLANG::JAnyType any_type);
/**
* Test availability of stream operators.
*/
template<class T, bool __str__ = JLANG::JPrintHelper::JMemberMethod<T>::__str__>
class JStreamAvailable :
public JLANG::JTest
{
using JLANG::JTest::test;
static JTrue test(std::istream&);
static JTrue test(std::ostream&);
static T& getReference();
static std::istream& is();
static std::ostream& os();
public:
static const bool has_istream = JTEST(test(is() >> getReference())); //!< true if <tt>std::istream& operator>>(std::istream&, T&)</tt> is defined; else false
static const bool has_ostream = JTEST(test(os() << getReference())); //!< true if <tt>std::ostream& operator<<(std::ostream&, const T&)</tt> is defined; else false
};
/**
* Specialisation of JStreamAvailable for class without member method <tt>__str__</tt>.
*/
template<class T>
class JStreamAvailable<T, true> :
public JLANG::JTest
{
public:
static const bool has_istream = JStreamAvailable<T, false>::has_istream;
static const bool has_ostream = true;
};
/**
* Auxiliary data structure for handling std::ostream.
*/
struct STREAM {
protected:
/**
* Auxiliary class for format stream.
*/
struct JStream
{
/**
* Constructor.
*
* \param out output stream
* \param message message printed in case operator std::ostream<< is unavailable
*/
JStream(std::ostream& out, const std::string& message) :
out (out),
message(message)
{}
/**
* Write value to output stream.
*
* \param value value
* \return this JStream
*/
template<class T>
std::ostream& operator<<(const T& value)
{
using namespace JPP;
return print(out, value, JBool<JStreamAvailable<T>::has_ostream>());
}
private:
/**
* Print value if given option is true.
*
* \param out output stream
* \param value value
* \param option true
* \return output stream
*/
template<class T>
std::ostream& print(std::ostream& out, const T& value, const JLANG::JBool<true>& option)
{
return out << value;
}
/**
* Print value if given option is true.
*
* \param out output stream
* \param value value
* \param option false
* \return output stream
*/
template<class T>
std::ostream& print(std::ostream& out, const T& value, const JLANG::JBool<false>& option)
{
return out << message;
}
std::ostream& out;
std::string message;
};
std::string message;
public:
/**
* Constructor.
*
* \param message message printed in case operator std::ostream<< is unavailable
*/
STREAM(const std::string& message = "") :
message(message)
{}
/**
* Format specifier.
*
* \param out output stream
* \param format format
* \return output stream
*/
friend inline JStream operator<<(std::ostream& out, const STREAM& format)
{
return JStream(out, format.message);
}
};
#endif
#ifndef __JLANG__JSTREAMSTATE__
#define __JLANG__JSTREAMSTATE__
#include <istream>
#include <ostream>
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* This class can be used to temporarily exchange the states between streams.
* The constructor transfers the state from the first stream to the second stream and
* the destructor transfers back the state from the second stream to the first stream.
*/
class JStreamState {
public:
/**
* Constructor.
* The stream state of <tt>from</tt> is transfered to stream state of <tt>to</tt>.
*
* \param from output stream
* \param to output stream
*/
JStreamState(std::ostream& from,
std::ostream& to) :
__from(from),
__to (to)
{
__to.setstate(__from.rdstate());
}
/**
* Constructor.
* The stream state of <tt>from</tt> is transfered to stream state of <tt>to</tt>.
*
* \param from input stream
* \param to input stream
*/
JStreamState(std::istream& from,
std::istream& to) :
__from(from),
__to (to)
{
__to.setstate(__from.rdstate());
}
/**
* Destructor.
* The stream state is transfered back.
*/
~JStreamState()
{
__from.setstate(__to.rdstate());
}
private:
std::ios& __from;
std::ios& __to;
JStreamState(const JStreamState&);
JStreamState(JStreamState&&);
JStreamState& operator=(const JStreamState&);
JStreamState& operator=(JStreamState&&);
};
}
#endif
#ifndef __JLANG__JSTRING__
#define __JLANG__JSTRING__
#include <string>
#include <sstream>
#include <limits>
#include <ctype.h>
#include <stdio.h>
#include "JLang/JStringFacet.hh"
#include "JLang/JException.hh"
#include "JLang/JLangToolkit.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Wrapper class around STL string class.
*/
class JString :
public std::string
{
public:
using std::string::replace;
using std::string::assign;
/**
* Default constructor.
*/
JString() :
std::string()
{}
/**
* Constructor.
*
* \param buffer string
*/
explicit JString(const std::string& buffer) :
std::string(buffer)
{}
/**
* Constructor.
*
* \param c char
*/
explicit JString(const char c):
std::string(1,c)
{}
/**
* Constructor.
*
* \param buffer string
* \param length length
*/
explicit JString(const char* buffer, const std::string::size_type length) :
std::string(buffer, length)
{}
/**
* Constructor.
*
* \param value value
*/
template<class T>
JString(const T& value) :
std::string(valueOf(value))
{}
/**
* Constructor.\n
* Each '%' in the format string will be replaced by the corresponding argument.
*
* \param format format
* \param args values
*/
template<class ...Args>
JString(const char* format, const Args& ...args) :
std::string(format)
{
__set__(args...);
}
/**
* Constructor.\n
* This constructor compiles (see below) the input string.
*
* \param buffer input string
* \param facet facet
*/
JString(const JString& buffer, const JStringFacet& facet) :
std::string(buffer)
{
compile(facet);
}
/**
* Compile token with given facet.\n
* This method uses the given facet to parse the input string.
* The result is then compatible with the definition of a token and may be empty.
*
* \param facet facet
* \result this string
*/
JString& compile(const JStringFacet& facet)
{
using namespace std;
istringstream is(*this);
ios_base::iostate state;
facet.get(is, istreambuf_iterator<char>(), is, state, *this);
return *this;
}
/**
* Test if this string starts with the specified prefix.
*
* \param prefix prefix
* \return true if this string starts with prefix; else false
*/
bool startsWith(const std::string& prefix) const
{
return this->size() >= prefix.size() && this->substr(0, prefix.size()) == prefix;
}
/**
* Test if this string ends with the specified suffix.
*
* \param suffix suffix
* \return true if this string ends with suffix; else false
*/
bool endsWith(const std::string& suffix) const
{
return this->size() >= suffix.size() && this->substr(this->size() - suffix.size()) == suffix;
}
/**
* Replace characters.
*
* \param target target character
* \param replacement replacement character
* \param max maximum number of replacements
* \return this string
*/
JString& replace(const char target,
const char replacement,
const std::size_t max = std::numeric_limits<std::size_t>::max())
{
for (std::size_t i = this->find(target), n = max; i != std::string::npos && n != 0; i = find(target,i), --n) {
(*this)[i] = replacement;
}
return *this;
}
/**
* Replace character sequences.
*
* \param target target string
* \param replacement replacement string
* \param max maximum number of replacements
* \return this string
*/
JString& replace(const std::string& target,
const std::string& replacement,
const std::size_t max = std::numeric_limits<std::size_t>::max())
{
for (std::size_t i = this->find(target), n = max; i != std::string::npos && n != 0; i = this->find(target,i), --n) {
replace(this->begin() + i, this->begin() + i + target.length(), replacement);
}
return *this;
}
/**
* Replace character sequence.
*
* \param target target string
* \param value value
* \param max maximum number of replacements
* \return this string
*/
template<class T>
JString& replace(const std::string& target,
const T& value,
const std::size_t max = std::numeric_limits<std::size_t>::max())
{
for (std::size_t i = this->find(target), n = max; i != std::string::npos && n != 0; i = this->find(target,i), --n) {
replace(this->begin() + i, this->begin() + i + target.length(), JString(value));
}
return *this;
}
/**
* Trim string.\n
* Returns the modified string, with leading and trailing white spaces omitted.
*
* \return this string
*/
JString& trim()
{
*this = JLANG::trim(*this);
return *this;
}
/**
* Trim string.\n
* Returns the modified string, with leading and trailing target characters omitted.
*
* \param c strip character
* \return this string
*/
JString& trim(const char c)
{
*this = JLANG::trim(*this, c);
return *this;
}
/**
* Trim string.\n
* Returns the modified string, with leading and trailing target characters omitted.
*
* \param target character(s) to strip
* \return this string
*/
JString trim(const std::string& target)
{
*this = JLANG::trim(*this, target);
return *this;
}
/**
* Convert all character to upper case.
*
* \return this string
*/
JString& toUpper()
{
for (iterator i = begin(); i != end(); ++i) {
*i = toupper(*i);
}
return *this;
}
/**
* Convert all character to lower case.
*
* \return this string
*/
JString& toLower()
{
for (iterator i = begin(); i != end(); ++i) {
*i = tolower(*i);
}
return *this;
}
/**
* Convert enumeration type to string.
*
* \param input value
* \return string
*/
static JString valueOf(const int input)
{
std::ostringstream os;
if (os << input)
return JString(os.str());
else
throw JException("JString::valueOf()");
}
/**
* Convert value to string.
*
* \param input value
* \return string
*/
template<class T>
static JString valueOf(const T& input)
{
std::ostringstream os;
if (os << input)
return JString(os.str());
else
throw JException("JString::valueOf()");
}
/**
* Convert string to value.
*
* \param input string
* \return value
*/
template<class T>
static const T& toValue(const JString& input)
{
static T value;
std::istringstream is(input);
if (is >> value)
return value;
else
throw JException("JString::toValue<T>()");
}
/**
* Assign (part of) string to value.
*
* \param output value
* \return remaining string
*/
template<class T>
JString& assign(T& output)
{
using namespace std;
istringstream is(*this);
is >> output;
if (!is) {
throw JException("JString::assign()");
}
this->assign(istreambuf_iterator<char>(is),
istreambuf_iterator<char>());
return *this;
}
/**
* Read string from input stream.
*
* \param in input stream
* \param object string
* \return input stream
*/
friend inline std::istream& operator>>(std::istream& in, JString& object)
{
using namespace std;
istream::sentry sentry(in); // skips white spaces
if (sentry) {
const locale& loc = in.getloc();
if (has_facet<JStringFacet>(loc)) {
ios_base::iostate state;
use_facet<JStringFacet>(loc).get(in, istreambuf_iterator<char>(), in, state, object);
if (state != ios_base::goodbit && state != ios_base::eofbit) {
in.setstate(state);
}
} else {
in >> static_cast<string&>(object);
}
}
return in;
}
/**
* Write string to output stream.
*
* \param out output stream
* \param object string
* \return output stream
*/
friend inline std::ostream& operator<<(std::ostream& out, const JString& object)
{
using namespace std;
const locale& loc = out.getloc();
if (has_facet<JStringFacet>(loc)) {
use_facet<JStringFacet>(loc).put(out, out, out.fill(), object);
} else {
out << static_cast<const string&>(object);
}
return out;
}
private:
/**
* Recursive method for formatting string.
*
* \param value next value
* \param args remaining values
*/
template<class T, class ...Args>
void __set__(const T& value, const Args& ...args)
{
using namespace std;
const size_t pos = this->find('%');
if (pos != string::npos) {
replace(pos, 1, JString::valueOf(value));
__set__(args...);
}
}
/**
* Termination method for formatting string.
*/
void __set__() const
{}
};
/**
* Read string from input stream until end of line.
*
* \param in input stream
* \param object string
* \return input stream
*/
inline std::istream& getline(std::istream& in, JString& object)
{
using namespace std;
istream::sentry sentry(in, true); // do not skip white spaces
if (sentry) {
const locale& loc = in.getloc();
if (has_facet<JStringFacet>(loc)) {
use_facet<JStringFacet>(loc).getline(in, object);
} else {
getline(in, static_cast<string&>(object));
}
}
return in;
}
}
#endif
#ifndef __JLANG__JSTRINGFACET__
#define __JLANG__JSTRINGFACET__
#include <istream>
#include <ostream>
#include <locale>
#include <string>
#include <iterator>
#include <limits>
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Facet class to specify parsing of a JLANG::JString object.
* This class extends the std::locale::facet class.
*/
class JStringFacet :
public std::locale::facet
{
public:
static std::locale::id id;
typedef std::istreambuf_iterator<char, std::char_traits<char> > istreambuf_iterator;
typedef std::ostreambuf_iterator<char, std::char_traits<char> > ostreambuf_iterator;
/**
* Constructor.
*
* \param refs reference count
*/
JStringFacet(std::size_t refs = 0) :
std::locale::facet(refs)
{}
/**
* Clone this facet.
*
* \return pointer to newly created facet
*/
virtual JStringFacet* clone() const
{
return new JStringFacet();
}
/**
* 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
*/
istreambuf_iterator get(const istreambuf_iterator __begin,
const istreambuf_iterator __end,
const std::ios_base& format,
std::ios_base::iostate& result,
std::string& buffer) const
{
return do_get(__begin, __end, format, result, buffer);
}
/**
* Put string.
*
* \param out begin position of output stream
* \param format format
* \param c fill character
* \param buffer input string
* \return position of output stream buffer
*/
ostreambuf_iterator put(ostreambuf_iterator out,
const std::ios_base& format,
const char c,
const std::string& buffer) const
{
return do_put(out, format, c, buffer);
}
/**
* Ignore characters until next end of line.
*
* \param in input stream
* \return input stream
*/
inline std::istream& ignore(std::istream& in) const
{
if (do_ignore(in, istreambuf_iterator()) == istreambuf_iterator()) {
in.setstate(std::ios_base::eofbit);
}
return in;
}
/**
* Read characters until next end of line.
*
* \param in input stream
* \param buffer output string
* \return input stream
*/
inline std::istream& getline(std::istream& in,
std::string& buffer) const
{
using namespace std;
ios_base::iostate state = in.rdstate();
if (do_getline(in, istreambuf_iterator(), state, buffer) == istreambuf_iterator()) {
in.setstate(std::ios_base::eofbit);
}
if (state != ios_base::goodbit && state != ios_base::eofbit) {
in.setstate(state);
}
return in;
}
/**
* Get index for stream associated facet data.
*
* \return index
*/
static int getIndex()
{
static int i = std::ios_base::xalloc();
return i;
}
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
{
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;
if (i == __end) {
result |= ios_base::failbit;
result |= ios_base::eofbit;
} else {
buffer.clear();
buffer.push_back(*i);
for (++i, --n; i != __end && n != 0; ++i, --n) {
buffer.push_back(*i);
}
if (i == __end) {
result |= ios_base::eofbit;
}
}
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
{
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;
}
/**
* 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
*/
virtual istreambuf_iterator do_ignore(const istreambuf_iterator __begin,
const istreambuf_iterator __end) const
{
istreambuf_iterator i = __begin;
while (i != __end && *i != '\n') {
++i;
}
while (i != __end && *i == '\n') {
++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
{
using namespace std;
istreambuf_iterator i = __begin;
if (i == __end) {
result |= ios_base::failbit;
result |= ios_base::eofbit;
} else {
buffer.clear();
for ( ; i != __end && *i != '\n'; ++i) {
buffer.push_back(*i);
}
if (i != __end) {
++i; // skip end of line
}
}
return i;
}
private:
JStringFacet(const JStringFacet&); // not defined
void operator=(const JStringFacet&); // not defined
};
}
#endif
#ifndef __JLANG__JSTRINGSTREAM__
#define __JLANG__JSTRINGSTREAM__
#include <sstream>
#include <fstream>
#include "JLang/JStreamState.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Wrapper class around STL stringstream class to facilitate optional loading of data from file.
*/
class JStringStream :
public std::stringstream,
public JStreamState
{
public:
/**
* Constructor.
*
* This constructor loads all data from given input stream.
*
* \param in input stream
*/
JStringStream(std::istream& in) :
std::stringstream(),
JStreamState(in, static_cast<std::istream&>(*this))
{
using namespace std;
istream::sentry sentry(in); // skips white spaces
if (sentry) {
(*this) << in.rdbuf();
}
}
/**
* Load data from file with name corresponding to current contents.
*
* Note that if the file exists but is empty,
* the state of the stream is set to fail by C++ standard.
*/
void load()
{
using namespace std;
ifstream in(this->str().c_str());
if (in) {
this->str(""); // reset
(*this) << in.rdbuf(); // copy
in.close();
}
}
};
}
#endif
#ifndef __JLANG__JTEST__
#define __JLANG__JTEST__
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Auxiliary base class for compile time evaluation of test.
*
* The derived class should implement the static method JTest::test,
* returning JTest::JTrue.
* The macro JTest::JTEST will then return true if the given test is successful.
*/
struct JTest {
protected:
struct JTrue { char buffer; }; //!< definition of true
struct JFalse { int buffer; }; //!< definition of false
static JFalse test(...); //!< default false
template<class __T__>
static JFalse test(...); //!< default false
/**
* Auxiliary class for type checking.
*/
template<class U, U> struct JTypecheck;
/**
* Test macro.
*
* \param __A__ test
* \return true if test successful; else false
*/
#define JTEST(__A__) (sizeof(__A__) == sizeof(JTrue))
};
}
#endif
#ifndef __JLANG__JTIMEVAL__
#define __JLANG__JTIMEVAL__
#include <sys/time.h>
#include <istream>
#include <ostream>
#include <limits>
#include "JLang/JComparable.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Auxiliary class for time values.
* This class encapsulates the <tt>timeval</tt> data structure.
*/
class JTimeval :
public timeval,
public JComparable<JTimeval>
{
public:
/**
* Default constructor.
*/
JTimeval()
{
this->tv_sec = 0;
this->tv_usec = 0;
}
/**
* Constructor.
*
* \param tv_us time [us]
*/
JTimeval(const int tv_us)
{
this->tv_sec = 0;
this->tv_usec = tv_us;
}
/**
* Constructor.
*
* \param tv_s time [s]
* \param tv_us time [us]
*/
JTimeval(const int tv_s, const int tv_us)
{
this->tv_sec = tv_s;
this->tv_usec = tv_us;
}
/**
* Get time value.
*
* \return time value
*/
const JTimeval& getTimeval() const
{
return static_cast<const JTimeval&>(*this);
}
/**
* Get time value.
*
* \return time value
*/
JTimeval& getTimeval()
{
return static_cast<JTimeval&>(*this);
}
/**
* Set time value.
*
* \param timeval time value
*/
void setTimeval(const JTimeval& timeval)
{
static_cast<JTimeval&>(*this) = timeval;
}
/**
* Less than method.
*
* \param value time value
* \result true if this time value less than given time value; else false
*/
inline bool less(const JTimeval& value) const
{
if (this->tv_sec == value.tv_sec)
return this->tv_usec < value.tv_usec;
else
return this->tv_sec < value.tv_sec;
}
/**
* Get minimal time value.
*
* \return time value
*/
static JTimeval min()
{
return JTimeval(0, 0);
}
/**
* Get maximal time value.
*
* \return time value
*/
static JTimeval max()
{
return JTimeval(std::numeric_limits<int>::max(), std::numeric_limits<int>::max());
}
/**
* Get pointer to time value.
*
* \return pointer to time value
*/
const timeval* get() const
{
return static_cast<const timeval*>(this);
}
/**
* Get pointer to time value.
*
* \return pointer to time value
*/
timeval* get()
{
return static_cast<timeval*>(this);
}
/**
* Address of operator.
*
* \return pointer to time value
*/
const timeval* operator &() const
{
return get();
}
/**
* Address of operator.
*
* \return pointer to time value
*/
timeval* operator &()
{
return get();
}
/**
* Read time value from input.
*
* \param in input stream
* \param time time value
* \return input stream
*/
friend inline std::istream& operator>>(std::istream& in, JTimeval& time)
{
return in >> time.tv_sec >> time.tv_usec;
}
/**
* Write time value to output.
*
* \param out output stream
* \param time time value
* \return output stream
*/
friend inline std::ostream& operator<<(std::ostream& out, const JTimeval& time)
{
return out << time.tv_sec << ' ' << time.tv_usec;
}
};
}
#endif
#ifndef __JLANG__JTYPE__
#define __JLANG__JTYPE__
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Auxiliary class for a type holder.
* This class can be used to transfer a template class to a method argument.
*/
template<class T>
struct JType {
typedef T data_type;
};
/**
* Get type.
*
* \return type
*/
template<class T>
inline JType<T> getType()
{
return JType<T>();
}
}
#endif
#ifndef __JLANG__JTYPELIST__
#define __JLANG__JTYPELIST__
#include "JLang/JNullType.hh"
#include "JLang/JType.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Type list.
*/
template<class JHead_t = JNullType, class JTail_t = JNullType>
struct JTypeList
{
typedef JHead_t head_type;
typedef JTail_t tail_type;
};
/**
* List of identical types.
*/
template<unsigned int N, class T>
struct JMultipleType
{
typedef JTypeList<T, typename JMultipleType<N-1, T>::typelist> typelist;
};
/**
* Terminator class of list of identical types.
*/
template<class T>
struct JMultipleType<1, T>
{
typedef JTypeList<T, JNullType> typelist;
};
/**
* Append to type list.
*
* Source code is taken from reference:
* A. Alexandrescu, Modern C++ Design, Addison Wesley.
*/
/**
* Template specialisation of append to type list.
*/
template<class JHead_t, class JTail_t>
struct JAppend
{
typedef JTypeList<JHead_t, JTail_t> typelist;
};
/**
* Template specialisation of append to type list.
*/
template<>
struct JAppend<JNullType, JNullType>
{
typedef JTypeList<> typelist;
};
/**
* Template specialisation of append to type list.
*/
template<class T>
struct JAppend<JNullType, T>
{
typedef JTypeList<T> typelist;
};
/**
* Template specialisation of append to type list.
*/
template<class JHead_t, class JTail_t>
struct JAppend<JNullType, JTypeList<JHead_t, JTail_t> >
{
typedef JTypeList<JHead_t, JTail_t> typelist;
};
/**
* Template specialisation of append to type list.
*/
template<class JHead_t, class JTail_t, class T>
struct JAppend<JTypeList<JHead_t, JTail_t>, T>
{
typedef JTypeList<JHead_t, typename JAppend<JTail_t, T>::typelist> typelist;
};
/**
* Removal of data type from type list.
*
* Source code is taken from reference:
* A. Alexandrescu, Modern C++ Design, Addison Wesley.
*/
template<class JTypelist_t, class T>
struct JRemove;
/**
* Template specialisation of removal of data type from type list.
*/
template<class T>
struct JRemove<JNullType, T>
{
typedef JTypeList<> typelist;
};
/**
* Template specialisation of removal of data type from type list.
*/
template<class T, class JTail_t>
struct JRemove<JTypeList<T, JTail_t>, T>
{
typedef JTail_t typelist;
};
/**
* Template specialisation of removal of data type from type list.
*/
template<class JHead_t, class JTail_t, class T>
struct JRemove<JTypeList<JHead_t, JTail_t>, T>
{
typedef JTypeList<JHead_t, typename JRemove<JTail_t, T>::typelist> typelist;
};
/**
* Template specialisation of removal of type list from type list.
*/
template<class JHead_t1, class JTail_t1, class JHead_t2, class JTail_t2>
struct JRemove<JTypeList<JHead_t1, JTail_t1>,
JTypeList<JHead_t2, JTail_t2> >
{
typedef typename JRemove<typename JRemove<JTypeList<JHead_t1, JTail_t1>, JHead_t2>::typelist,
JTail_t2>::typelist typelist;
};
/**
* Template specialisation of removal of type list from type list.
*/
template<class JHead_t1, class JTail_t1, class JHead_t2>
struct JRemove<JTypeList<JHead_t1, JTail_t1>,
JTypeList<JHead_t2, JNullType> >
{
typedef typename JRemove<JTypeList<JHead_t1, JTail_t1>, JHead_t2>::typelist typelist;
};
/**
* Length of type list.
*
* Source code is taken from reference:
* A. Alexandrescu, Modern C++ Design, Addison Wesley.
*/
template<class JTypeList_t> struct JLength {};
/**
* Recursive length of type list.
*/
template<class JHead_t, class JTail_t>
struct JLength< JTypeList<JHead_t, JTail_t> >
{
enum { value = 1 + JLength<JTail_t>::value };
};
/**
* Terminator class of length of type list.
*/
template<>
struct JLength<JNullType>
{
enum { value = 0 };
};
/**
* Test presence of data type in type list.
*/
template<class JTypeList_t, class T>
struct JHasType;
/**
* Recursive test of presence data type in type list.
*/
template<class JHead_t, class JTail_t, class T>
struct JHasType<JTypeList<JHead_t, JTail_t>, T>
{
enum { value = JHasType<JTail_t, T>::value };
};
/**
* Identify presence data type in type list.
*/
template<class JTail_t, class T>
struct JHasType<JTypeList<T, JTail_t>, T>
{
enum { value = true };
};
/**
* Termination of recursive test of presence data type in type list.
*/
template<class T>
struct JHasType<JNullType, T>
{
enum { value = false };
};
/**
* Specialisation of JHasType for single class type.
*/
template<class T>
struct JHasType<T, T>
{
enum { value = true };
};
/**
* Resolve template class to JTypeList.
*/
template<class T>
struct JResolveTypeList
{
typedef JTypeList<T> typelist;
};
/**
* Resolve JTypeList to JTypeList.
*/
template<class JHead_t, class JTail_t>
struct JResolveTypeList< JTypeList<JHead_t, JTail_t> >
{
typedef JTypeList<JHead_t, JTail_t> typelist;
};
/**
* Extraction of data type from type list.
*
* Source code is taken from reference:
* A. Alexandrescu, Modern C++ Design, Addison Wesley.
*/
template<class JTypelist_t, unsigned int index, bool range_check = true>
struct JTypeAt;
/**
* Recursive extraction of data type from type list.
*/
template<class JHead_t, class JTail_t, unsigned int index, bool range_check>
struct JTypeAt<JTypeList<JHead_t, JTail_t>, index, range_check>
{
typedef typename JTypeAt<JTail_t, index - 1, range_check>::value_type value_type;
};
/**
* Termination of recursive extraction of data type from type list.
*/
template<class JHead_t, class JTail_t, bool range_check>
struct JTypeAt<JTypeList<JHead_t, JTail_t>, 0, range_check>
{
typedef JHead_t value_type;
};
/**
* Termination of recursive extraction of data type from type list.
*/
template<unsigned int index>
struct JTypeAt<JNullType, index, false>
{
typedef JNullType value_type;
};
/**
* Indexing of data type in type list.
*/
template<class JTypeList_t, class T>
struct JIndexOf;
/**
* Recursive indexing of data type in type list.
*/
template<class JHead_t, class JTail_t, class T>
struct JIndexOf<JTypeList<JHead_t, JTail_t>, T>
{
private:
enum { tmp = JIndexOf<JTail_t, T>::value };
public:
enum { value = (tmp == -1 ? -1 : tmp + 1) };
};
/**
* Identify indexi of data type in type list.
*/
template<class JTail_t, class T>
struct JIndexOf<JTypeList<T, JTail_t>, T>
{
enum { value = 0 };
};
/**
* Termination of recursive indexing of data type in type list.
*/
template<class T>
struct JIndexOf<JNullType, T>
{
enum { value = -1 };
};
/**
* Auxiliary class for recursive type list generation.
*/
template<class T, class ...Args>
struct JTYPELIST {
typedef JTypeList<T, typename JTYPELIST<Args...>::typelist> typelist;
};
/**
* Template specialisation for expanding type list.
*/
template<class JHead_t, class JTail_t, class T, class ...Args>
struct JTYPELIST<JTypeList<JHead_t, JTail_t>, T, Args...> {
typedef JTypeList<JHead_t, typename JTYPELIST<JTail_t, T, Args...>::typelist> typelist;
};
/**
* Template specialisation for expanding type list.
*/
template<class JHead_t, class T, class ...Args>
struct JTYPELIST<JTypeList<JHead_t, JNullType>, T, Args...> {
typedef JTypeList<JHead_t, typename JTYPELIST<T, Args...>::typelist> typelist;
};
/**
* Template specialisation for expanding type list.
*/
template<class JHead_t, class JTail_t>
struct JTYPELIST< JTypeList<JHead_t, JTail_t> > {
typedef JTypeList<JHead_t, JTail_t> typelist;
};
/**
* Termination class for type list generation.
*/
template<class T>
struct JTYPELIST<T> {
typedef JTypeList<T> typelist;
};
/**
* Termination class for type list generation.
*/
template<>
struct JTYPELIST<JNullType> {
typedef JTypeList<> typelist;
};
/**
* For each data type method.
*
* The given object should provide for the function object operator
* <pre>
* template<class T>
* void object()(JType<T> type);
* </pre>
*
* \param object object
* \param typelist type list
* \return object
*/
template<class JObject_t, class JHead_t, class JTail_t>
JObject_t& for_each(JObject_t& object, JType< JTypeList<JHead_t, JTail_t> > typelist)
{
for_each(object, JType<JHead_t>());
for_each(object, JType<JTail_t>());
return object;
}
/**
* For each data type method.
*
* The given object should provide for the function object operator
* <pre>
* template<class T>
* void object()(JType<T> type);
* </pre>
*
* \param object object
* \param type type
* \return object
*/
template<class JObject_t, class T>
JObject_t& for_each(JObject_t& object, JType<T> type)
{
object(type);
return object;
}
/**
* Termination method of for each data type method.
*
* \param object object
* \param type null type
* \return object
*/
template<class JObject_t>
JObject_t& for_each(JObject_t& object, JType<JNullType> type)
{
return object;
}
}
#endif
#ifndef __JLANG__JVALUE__
#define __JLANG__JVALUE__
#include <istream>
#include <ostream>
#include <sstream>
#include <string>
#include "JLang/JAbstractIO.hh"
#include "JLang/JEquationFacet.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Forward declaration for friend declaration of JValueOutput inside JValueInput.
*/
template<class T>
class JValueOutput;
/**
* Wrapper class around template object.
* This class implements the JStreamInput interface.
* Note that this class can be used in conjuction with the JEquationFacet.
*/
template<class T>
class JValueInput :
public JStreamInput
{
public:
friend class JValueOutput<T>;
/**
* Constructor.
*
* \param object input object
*/
JValueInput(T& object) :
p(&object)
{}
/**
* Constructor.
*
* \param ps pointer to valid object
*/
JValueInput(void* ps) :
p((T*) ps)
{}
operator const T&() const { return *p; } //!< type conversion operator
operator T&() { return *p; } //!< type conversion operator
/**
* Stream input.
*
* \param in input stream
* \return input stream
*/
virtual std::istream& read(std::istream& in) override
{
return in >> *p;
}
protected:
T* p;
};
/**
* Wrapper class around template object.
* This class implements the JStreamOutput interface.
* Note that this class can be used in conjuction with the JEquationFacet.
*/
template<class T>
class JValueOutput :
public JStreamOutput
{
public:
/**
* Constructor.
*
* \param object input object
*/
JValueOutput(const T& object) :
p(&object)
{}
/**
* Constructor.
*
* \param object input object
*/
JValueOutput(const JValueInput<T>& object) :
p(object.p)
{}
/**
* Constructor.
*
* \param ps pointer to valid object
*/
JValueOutput(const void* ps) :
p((const T*) ps)
{}
operator const T&() const { return *p; } //!< type conversion operator
/**
* Stream output.
*
* \param out output stream
* \return output stream
*/
virtual std::ostream& write(std::ostream& out) const override
{
return out << *p;
}
protected:
const T* p;
};
/**
* Wrapper class around template object.
* This class implements the JStreamInput and JStreamOutput interfaces.
*/
template<class T>
class JValue :
public JValueInput <T>,
public JValueOutput<T>
{
public:
/**
* Constructor.
*
* \param object input object
*/
JValue(T& object) :
JValueInput <T>(&object),
JValueOutput<T>(&object)
{}
/**
* Constructor.
*
* \param ps pointer to valid object
*/
JValue(void* ps) :
JValueInput <T>(ps) ,
JValueOutput<T>(ps)
{}
};
/**
* Read JStreamInput from input stream.
*
* \param in input stream
* \param object JStreamInput
* \return input stream
*/
template<class T>
inline std::istream& operator>>(std::istream& in, JValueInput<T>& object)
{
using namespace std;
istream::sentry sentry(in); // skips white spaces
if (sentry) {
const locale& loc = in.getloc();
if (has_facet<JEquationFacet>(loc)) {
string buffer;
const JEquationFacet& facet = use_facet<JEquationFacet>(loc);
facet.getline(in, buffer);
istringstream is(buffer);
is.imbue(locale(in.getloc(), facet.clone()));
object.read(is);
if (is.fail()) {
in.setstate(ios_base::badbit);
}
} else {
object.read(in);
}
}
return in;
}
/**
* Write JStreamOutput to output stream.
*
* \param out output stream
* \param object JStreamOutput
* \return output stream
*/
template<class T>
inline std::ostream& operator<<(std::ostream& out, const JValueOutput<T>& object)
{
return object.write(out);
}
}
#endif
#ifndef __JLANG__JVECTORIZE__
#define __JLANG__JVECTORIZE__
#include <ostream>
#include <vector>
#include <iterator>
#include "JLang/JSTDTypes.hh"
#include "JLang/JClass.hh"
/**
* \file
* Auxiliary methods to convert data members or return values of member methods of a set of objects to a single vector.
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Auxiliary data structure for return type of make methods.
*/
template<class JElement_t, class JAllocator_t = std::allocator<JElement_t> >
struct array_type :
public std::vector<JElement_t, JAllocator_t>
{
/**
* Write array to output stream.
*
* \param out output stream
* \param object array
* \return output stream
*/
friend inline std::ostream& operator<<(std::ostream& out, const array_type& object)
{
for (typename array_type::const_iterator i = object.begin(); i != object.end(); ++i) {
out << ' ' << *i;
}
return out;
}
};
/**
* Method to create array of values.
*
* \param array c-array of values
* \return data
*/
template<class JValue_t, size_t N>
inline const array_type<JValue_t>& make_array(const JValue_t (&array)[N])
{
static array_type<JValue_t> buffer;
buffer.resize(N);
for (size_t i = 0; i != N; ++i) {
buffer[i] = array[i];
}
return buffer;
}
/**
* Method to create array of values.
*
* \param __begin begin of data
* \param __end end of data
* \return data
*/
template<class T>
inline const array_type<typename std::iterator_traits<T>::value_type>& make_array(T __begin, T __end)
{
static array_type<typename std::iterator_traits<T>::value_type> buffer;
buffer.assign(__begin, __end);
return buffer;
}
/**
* Method to create array of values of data member.
*
* \param __begin begin of data
* \param __end end of data
* \param value pointer to data member
* \return data
*/
template<class T, class JType_t, class JValue_t>
inline const array_type<typename JClass<JValue_t>::value_type>& make_array(T __begin, T __end, JValue_t const JType_t::*value)
{
static array_type<typename JClass<JValue_t>::value_type> buffer;
buffer.clear();
for (T __p = __begin; __p != __end; ++__p) {
buffer.push_back(*__p.*value);
}
return buffer;
}
/**
* Method to create array of return values of member method.
*
* \param __begin begin of data
* \param __end end of data
* \param function pointer to member method
* \return data
*/
template<class T, class JType_t, class JValue_t>
inline const array_type<typename JClass<JValue_t>::value_type>& make_array(T __begin, T __end, JValue_t (JType_t::*function)() const)
{
static array_type<typename JClass<JValue_t>::value_type> buffer;
buffer.clear();
for (T __p = __begin; __p != __end; ++__p) {
buffer.push_back((*__p.*function)());
}
return buffer;
}
/**
* Method to create array of keys of map.
*
* \param data data
* \return data
*/
template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
inline const array_type<JKey_t>& get_keys(const std::map<JKey_t, JValue_t, JComparator_t, JAllocator_t>& data)
{
return make_array(data.begin(), data.end(), &std::map<JKey_t, JValue_t, JComparator_t, JAllocator_t>::value_type::first);
}
/**
* Method to create array of values of map.
*
* \param data data
* \return data
*/
template<class JKey_t, class JValue_t, class JComparator_t, class JAllocator_t>
inline const array_type<JValue_t>& get_values(const std::map<JKey_t, JValue_t, JComparator_t, JAllocator_t>& data)
{
return make_array(data.begin(), data.end(), &std::map<JKey_t, JValue_t, JComparator_t, JAllocator_t>::value_type::second);
}
/**
* Method to exclude outliers from already sorted data.\n
* Note that the part after the returned iterator will be overwritten.
*
* \param __begin begin of data
* \param __end end of data
* \param value pointer to data member,
* \param comparator comparison method
* \return end of sorted data
*/
template<class T, class JResult_t, class JComparator_t>
T make_set(T __begin,
T __end,
JResult_t std::iterator_traits<T>::value_type::*value,
const JComparator_t& comparator)
{
T p2 = __begin;
T p0 = p2++;
T p1 = p2++;
if (p0 == __end) { return p0; }
if (p1 == __end) { return p1; }
if (p2 == __end) {
if (comparator((*p0).*value, (*p1).*value))
return p2;
else
return p0;
}
for ( ; p2 != __end; ++p2) {
if (comparator((*p0).*value, (*p1).*value)) {
if (comparator((*p1).*value, (*p2).*value)) {
*(++p0) = *p1;
*(++p1) = *p2;
} else if (comparator((*p0).*value, (*p2).*value)) {
*p1 = *p2;
}
} else {
if (comparator((*p2).*value, (*p0).*value)) {
*p0 = *p1;
}
*p1 = *p2;
}
}
return ++p1;
}
}
#endif
#ifndef __JLANG__JVOID__
#define __JLANG__JVOID__
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Auxiliary class for void type definition.
* This class can be used to evaluate the validity of a type definition.
*/
template<class T>
struct JVoid
{
typedef void type;
};
}
#endif
/**
* \author mdejong
*/
// ============================================================================
// gzstream, C++ iostream classes wrapping the zlib compression library.
// Copyright (C) 2001 Deepak Bandyopadhyay, Lutz Kettner
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// ============================================================================
//
// File : gzstream.h
// Revision : $Revision: 1.5 $
// Revision_date : $Date: 2002/04/26 23:30:15 $
// Author(s) : Deepak Bandyopadhyay, Lutz Kettner
//
// Standard streambuf implementation following Nicolai Josuttis, "The
// Standard C++ Library".
// ============================================================================
#ifndef GZSTREAM_H
#define GZSTREAM_H 1
// standard C++ with new header file names and std:: namespace
#include <iostream>
#include <fstream>
#include <zlib.h>
#include <string>
#include <string.h>
#ifdef GZSTREAM_NAMESPACE
namespace GZSTREAM_NAMESPACE {
#endif
// ----------------------------------------------------------------------------
// Internal classes to implement gzstream. See below for user classes.
// ----------------------------------------------------------------------------
class gzstreambuf : public std::streambuf {
private:
static const int bufferSize = 47+256; // size of data buff
// totals 512 bytes under g++ for igzstream at the end.
gzFile file; // file handle for compressed file
char buffer[bufferSize]; // data buffer
char opened; // open/close state of stream
int mode; // I/O mode
int flush_buffer() {
// Separate the writing of the buffer from overflow() and
// sync() operation.
int w = pptr() - pbase();
if ( gzwrite( file, pbase(), w) != w)
return EOF;
pbump( -w);
return w;
}
public:
gzstreambuf() : opened(0) {
setp( buffer, buffer + (bufferSize-1));
setg( buffer + 4, // beginning of putback area
buffer + 4, // read position
buffer + 4); // end position
// ASSERT: both input & output capabilities will not be used together
}
int is_open() const { return opened; }
~gzstreambuf() { close(); }
gzstreambuf* open( const char* name, int open_mode) {
if ( is_open())
return (gzstreambuf*)0;
mode = open_mode;
// no append nor read/write mode
if ((mode & std::ios::ate) || (mode & std::ios::app)
|| ((mode & std::ios::in) && (mode & std::ios::out)))
return (gzstreambuf*)0;
char fmode[10];
char* fmodeptr = fmode;
if ( mode & std::ios::in)
*fmodeptr++ = 'r';
else if ( mode & std::ios::out)
*fmodeptr++ = 'w';
*fmodeptr++ = 'b';
*fmodeptr = '\0';
file = gzopen( name, fmode);
if (file == 0)
return (gzstreambuf*)0;
opened = 1;
setg( buffer + 4, // beginning of putback area
buffer + 4, // read position
buffer + 4); // end position
return this;
}
gzstreambuf * close() {
if ( is_open()) {
sync();
opened = 0;
if ( gzclose( file) == Z_OK)
return this;
}
return (gzstreambuf*)0;
}
virtual int underflow() { // used for input buffer only
if ( gptr() && ( gptr() < egptr()))
return * reinterpret_cast<unsigned char *>( gptr());
if ( ! (mode & std::ios::in) || ! opened)
return EOF;
// Josuttis' implementation of inbuf
int n_putback = gptr() - eback();
if ( n_putback > 4)
n_putback = 4;
memcpy( buffer + (4 - n_putback), gptr() - n_putback, n_putback);
int num = gzread( file, buffer+4, bufferSize-4);
if (num <= 0) // ERROR or EOF
return EOF;
// reset buffer pointers
setg( buffer + (4 - n_putback), // beginning of putback area
buffer + 4, // read position
buffer + 4 + num); // end of buffer
// return next character
return * reinterpret_cast<unsigned char *>( gptr());
}
virtual int overflow( int c=EOF) { // used for output buffer only
if ( ! ( mode & std::ios::out) || ! opened)
return EOF;
if (c != EOF) {
*pptr() = c;
pbump(1);
}
if ( flush_buffer() == EOF)
return EOF;
return c;
}
virtual int sync() {
// Changed to use flush_buffer() instead of overflow( EOF)
// which caused improper behavior with std::endl and flush(),
// bug reported by Vincent Ricard.
if ( pptr() && pptr() > pbase()) {
if ( flush_buffer() == EOF)
return -1;
}
return 0;
}
};
class gzstreambase : virtual public std::ios {
protected:
gzstreambuf buf;
public:
gzstreambase() { init(&buf); }
gzstreambase( const char* name, int mode) {
init( &buf);
open( name, mode);
}
~gzstreambase() {
buf.close();
}
void open( const char* name, int open_mode) {
if ( ! buf.open( name, open_mode))
clear( rdstate() | std::ios::badbit);
}
void close() {
if ( buf.is_open()) {
if ( ! buf.close())
clear( rdstate() | std::ios::badbit );
else
clear();
}
}
gzstreambuf* rdbuf() { return &buf; }
bool is_open() const { return buf.is_open(); }
};
// ----------------------------------------------------------------------------
// User classes. Use igzstream and ogzstream analogously to ifstream and
// ofstream respectively. They read and write files based on the gz*
// function interface of the zlib. Files are compatible with gzip compression.
// ----------------------------------------------------------------------------
class igzstream : public gzstreambase, public std::istream {
public:
igzstream() : std::istream( &buf) {}
igzstream( const char* name, int open_mode = std::ios::in)
: gzstreambase( name, open_mode), std::istream( &buf) {}
void open( const char* name, int open_mode = std::ios::in) {
gzstreambase::open( name, open_mode);
}
};
class ogzstream : public gzstreambase, public std::ostream {
public:
ogzstream() : std::ostream( &buf) {}
ogzstream( const char* name, int mode = std::ios::out)
: gzstreambase( name, mode), std::ostream( &buf) {}
void open( const char* name, int open_mode = std::ios::out) {
gzstreambase::open( name, open_mode);
}
};
#ifdef GZSTREAM_NAMESPACE
} // namespace GZSTREAM_NAMESPACE
#endif
#endif // GZSTREAM_H
// ============================================================================
// EOF //
#ifndef __JMATH__JCALCULATOR__
#define __JMATH__JCALCULATOR__
/**
* \author mdejong
*/
namespace JMATH {}
namespace JPP { using namespace JMATH; }
namespace JMATH {
/**
* Auxiliary class for arithmetic operations on objects.
*/
template<class T, int N = 0>
struct JCalculator :
public T // object
{
/**
* Set calculator value.
*
* \param value value
* \return this calculator
*/
JCalculator& set(const T& value)
{
static_cast<T&>(*this) = value;
return *this;
}
static JCalculator calculator; // calculator
};
/**
* Calculator.
*/
template<class T, int N>
JCalculator<T,N> JCalculator<T,N>::calculator;
/**
* Product evaluation of objects.
*
* \param first first object
* \param second second object
* \return calculator
*/
template<class T>
inline const JCalculator<T, 1>& operator*(const T& first, const T& second)
{
JCalculator<T, 1>::calculator.mul(first, second);
return JCalculator<T, 1>::calculator;
}
/**
* Recursive product evaluation of objects.
*
* \param first first object
* \param second second object
* \return calculator
*/
template<class T, int N>
inline const JCalculator<T, N+1>& operator*(const T& first, const JCalculator<T, N>& second)
{
JCalculator<T, N+1>::calculator.mul(first, second);
return JCalculator<T, N+1>::calculator;
}
/**
* Recursive product evaluation of objects.
*
* \param first first object
* \param second second object
* \return calculator
*/
template<class T, int N>
inline const JCalculator<T, N+1>& operator*(const JCalculator<T, N>& first, const T& second)
{
JCalculator<T, N+1>::calculator.mul(first, second);
return JCalculator<T, N+1>::calculator;
}
}
#endif
#ifndef __JMATH__JCONSTANTS__
#define __JMATH__JCONSTANTS__
#include <math.h>
/**
* \file
* Mathematical constants.
* \author mdejong
*/
namespace JMATH {}
namespace JPP { using namespace JMATH; }
namespace JMATH {
/**
* Mathematical constants.
*/
static const double PI = acos(-1.0); //!< pi
static const double EULER = 0.577215664901533; //!< Euler number
/**
* Computing quantities.
*/
static const long long int KILOBYTE = 1024; //! Number of bytes in a kilo-byte
static const long long int MEGABYTE = KILOBYTE*KILOBYTE; //! Number of bytes in a mega-byte
static const long long int GIGABYTE = MEGABYTE*KILOBYTE; //! Number of bytes in a giga-byte
}
#endif
#ifndef __JMATH__JLIMITS__
#define __JMATH__JLIMITS__
#include <limits>
/**
* \file
*
* Definition of minimum and maximum values for any class.
* \author mdejong
*/
namespace JMATH {}
namespace JPP { using namespace JMATH; }
namespace JMATH {
/**
* Auxiliary class for minimum and maximum values for any class.
*/
template<class T, bool __is_specialized__ = std::numeric_limits<T>::is_specialized>
struct JLimits;
/**
* Template spacialisation of JMATH::JLimits for numerical values.
*/
template<class T>
struct JLimits<T, true> {
/**
* Get minimum possible value.
*
* \return minimum possible value
*/
static T min()
{
return std::numeric_limits<T>::min();
}
/**
* Get maximum possible value.
*
* \return maximum possible value
*/
static T max()
{
return std::numeric_limits<T>::max();
}
static const bool is_specialized = true;
};
/**
* Template spacialisation of JMATH::JRandom for other data types.
*
* The given template class should provide for the methods:
* <pre>
* static T %min();
* static T %max();
* </pre>
*/
template<class T>
struct JLimits<T, false> {
/**
* Get minimum possible value.
*
* \return minimum possible value
*/
static T min()
{
return T::min();
}
/**
* Get maximum possible value.
*
* \return maximum possible value
*/
static T max()
{
return T::max();
}
static const bool is_specialized = false;
};
/**
* Get minimum possible value.
*
* \return minimum possible value
*/
template<>
inline float JLimits<float, true>::min()
{
return std::numeric_limits<float>::lowest();
}
/**
* Get minimum possible value.
*
* \return minimum possible value
*/
template<>
inline double JLimits<double, true>::min()
{
return std::numeric_limits<double>::lowest();
}
/**
* Get minimum possible value.
*
* \return minimum possible value
*/
template<>
inline long double JLimits<long double, true>::min()
{
return std::numeric_limits<long double>::lowest();
}
}
#endif
#ifndef __JMATH__JMATH__
#define __JMATH__JMATH__
#include <cmath>
#include <iterator>
#include "JLang/JNullType.hh"
#include "JLang/JClass.hh"
#include "JLang/JBool.hh"
#include "JLang/JVectorize.hh"
#include "JLang/JException.hh"
#include "JMath/JZero.hh"
#include "JMath/JCalculator.hh"
/**
* \file
*
* Base class for data structures with artithmetic capabilities.
* \author mdejong
*/
namespace JMATH {}
namespace JPP { using namespace JMATH; }
namespace JGEOMETRY3D { class JQuaternion3D; }
namespace JMATH {
using JLANG::JNullType;
using JLANG::array_type;
using JLANG::JDivisionByZero;
/**
* Power \f$ x^{y} \f$.
*
* \param x value
* \param y power
* \return result
*/
template<class T>
inline T pow(const T& x, const double y);
/**
* Auxiliary class to hide data type specific methods.
*/
struct JMath_t {
/**
* Friend declaration of global method.
*/
template<class T>
friend T JMATH::pow(const T&, const double y);
private:
/**
* Power \f$ x^{y} \f$.
* This method corresponds to primitive data types.
*
* \param x value
* \param y power
* \param option true
* \return result
*/
template<class T>
static inline T pow(const T& x, const double y, const JLANG::JBool<true> option)
{
return std::pow(x, y);
}
/**
* Power \f$ x^{y} \f$.
*
* This method corresponds to non-primitive data types.
*
* \param x value
* \param y power
* \param option false
* \return result
*/
template<class T>
static inline T pow(const T& x, const double y, const JLANG::JBool<false> option)
{
return T(x).pow(y);
}
};
/**
* Power \f$ x^{y} \f$.
*
* \param x value
* \param y power
* \return result
*/
template<class T>
inline T pow(const T& x, const double y)
{
using namespace JPP;
return JMath_t::pow(x, y, JBool<JClass<T>::is_primitive>());
}
/**
* Auxiliary base class for aritmetic operations of derived class types.
*/
template<class JFirst_t, class JSecond_t = JNullType>
struct JMath;
/**
* Template base class for data structures with arithmetic capabilities.
*
* This class provides for the operators <tt> - += -= *= /= + - * / </tt>.\n
* To this end, the template parameter should privide for the corresponding member methods:
* <pre>
* T& negate();
* T& add(const T& object);
* T& sub(const T& object);
* T& mul(const double factor);
* T& div(const double factor);
* </pre>
*
* This class also provides for the object multiplication operators <tt>*= *</tt>.\n
* To this end, the template parameter should then also provide for the member method:
* <pre>
* T& mul(const T&, const T&);
* </pre>
*
* This class adds interpolation functionality.\n
* This class uses in-class friend operators (see Barton-Nackman trick).
*/
template<class T>
struct JMath<T, JNullType> {
/**
* Affirm operator.
*
* \param object this object
* \return affirmed object
*/
friend T operator+(const T& object)
{
return T(object);
}
/**
* Negate operator.
*
* \param object this object
* \return negated object
*/
friend T operator-(const T& object)
{
return T(object).negate();
}
/**
* Add object.
*
* \param object this object
* \param value value
* \return this object
*/
friend T& operator+=(T& object, const T& value)
{
return object.add(value);
}
/**
* Subtract object.
*
* \param object this object
* \param value value
* \return this object
*/
friend T& operator-=(T& object, const T& value)
{
return object.sub(value);
}
/**
* Scale object.
*
* \param object this object
* \param factor factor
* \return this object
*/
friend T& operator*=(T& object, const double factor)
{
return object.mul(factor);
}
/**
* Scale object.
*
* \param object this object
* \param factor factor
* \return this object
*/
friend T& operator/=(T& object, const double factor)
{
return object.div(factor);
}
/**
* Add objects.
*
* \param first first object
* \param second second object
* \return result object
*/
friend T operator+(const T& first, const T& second)
{
return JCalculator<T>::calculator.set(first).add(second);
}
/**
* Subtract objects.
*
* \param first first object
* \param second second object
* \return result object
*/
friend T operator-(const T& first, const T& second)
{
return JCalculator<T>::calculator.set(first).sub(second);
}
/**
* Scale object.
*
* \param object object
* \param factor factor
* \return object
*/
friend T operator*(const T& object, const double factor)
{
return JCalculator<T>::calculator.set(object).mul(factor);
}
/**
* Scale object.
*
* \param factor factor
* \param object object
* \return object
*/
friend T operator*(const double factor, const T& object)
{
return JCalculator<T>::calculator.set(object).mul(factor);
}
/**
* Scale object.
*
* \param object object
* \param factor factor
* \return object
*/
friend T operator/(const T& object, const double factor)
{
return JCalculator<T>::calculator.set(object).div(factor);
}
/**
* Multiply with object.
*
* \param object object
* \return result object
*/
T& mul(const T& object)
{
return static_cast<T&>(*this) = JCalculator<T>::calculator.mul(static_cast<const T&>(*this), object);
}
/**
* Multiply with object.
*
* \param first first object
* \param second second object
* \return result object
*/
friend T& operator*=(T& first, const T& second)
{
return static_cast<JMath<T>&>(first).mul(second);
}
/**
* Multiply objects.
*
* \param first first object
* \param second second object
* \return calculator
*/
friend const JCalculator<T, 1>& operator*(const T& first, const T& second)
{
JCalculator<T, 1>::calculator.mul(first, second);
return JCalculator<T, 1>::calculator;
}
/**
* Interpolation between objects.
* The result is equal to <tt>*this = (1 - alpha) * (*this) + (alpha) * (object)</tt>.
*
* \param object object
* \param alpha interpolation factor <tt>[0, 1]</tt>
* \return this object
*/
T& interpolate(const T& object, const double alpha)
{
static_cast<T*>(this)->mul(1.0 - alpha);
static_cast<T*>(this)->add(T(object).mul(alpha));
return static_cast<T&>(*this);
}
};
/**
* Specialisation of JMath for two data types.
*
* This class provides for the object multiplication operators <tt>*= *</tt>.
* The template parameter should then have the member method:
* <pre>
* JFirst_t& mul(const JFirst_t&, const JSecond_t&);
* </pre>
* where <tt>JFirst_t</tt> and <tt>JSecond_t</tt> refer to the first and second template parameter, respectively.
*
* This class uses in-class friend operators (see Barton-Nackman trick).
*/
template<class JFirst_t, class JSecond_t>
struct JMath
{
/**
* Multiply with object.
*
* \param object object
* \return result object
*/
JFirst_t& mul(const JSecond_t& object)
{
return static_cast<JFirst_t&>(*this) = JCalculator<JFirst_t>::calculator.mul(static_cast<const JFirst_t&>(*this), object);
}
/**
* Multiply with object.
*
* \param first first object
* \param second second object
* \return result object
*/
friend JFirst_t& operator*=(JFirst_t& first, const JSecond_t& second)
{
return first.mul(second);
}
/**
* Multiply objects.
*
* \param first first object
* \param second second object
* \return result object
*/
friend JFirst_t operator*(const JFirst_t& first, const JSecond_t& second)
{
return JFirst_t(first).mul(second);
}
};
/**
* Interpolation between objects.
* The result is equal to <tt>result = (1 - alpha) * (first) + (alpha) * (second)</tt>.
*
* \param first first object
* \param second second object
* \param alpha interpolation factor <tt>[0, 1]</tt>
* \return result
*/
template<class T>
inline T interpolate(const T& first,
const T& second,
const double alpha)
{
return T(first).interpolate(second, alpha);
}
/**
* Auxiliary class to determine average of set of values.
*/
template<class JValue_t>
struct JAverage {
/**
* Default constructor.
*/
JAverage() :
value (getZero<JValue_t>()),
weight(0.0)
{}
/**
* Constructor.
*
* \param __begin begin of data
* \param __end end of data
*/
template<class T>
JAverage(T __begin, T __end) :
value (getZero<JValue_t>()),
weight(0.0)
{
for (T i = __begin; i != __end; ++i) {
value += (*i);
weight += 1.0;
}
}
/**
* Reset.
*/
void reset()
{
this->value = getZero<JValue_t>();
this->weight = 0.0;
}
/**
* Type conversion operator.
*
* \return value
*/
operator JValue_t () const
{
if (weight != 0.0)
return value * (1.0 / weight);
else
THROW(JDivisionByZero, "Invalid weight.");
}
/**
* Put value.
*
* \param value value
* \param w weight
*/
void put(const JValue_t& value, const double w = 1.0)
{
this->value += value;
this->weight += w;
}
private:
JValue_t value;
double weight;
};
/**
* Template definition so that compiler error is generated if implementation is missing (see JEigen3D.hh).
*/
template<>
struct JAverage<JGEOMETRY3D::JQuaternion3D>;
/**
* Get average.
*
* \param __begin begin of data
* \param __end end of data
* \return average value
*/
template<class T>
typename std::iterator_traits<T>::value_type getAverage(T __begin, T __end)
{
typedef typename std::iterator_traits<T>::value_type value_type;
return JAverage<value_type>(__begin, __end);
}
/**
* Get average.
*
* \param array c-array of values
* \return average value
*/
template<class JValue_t, size_t N>
inline JValue_t getAverage(const JValue_t (&array)[N])
{
typedef JValue_t value_type;
return JAverage<value_type>((const value_type*) array, (const value_type*) array + N);
}
/**
* Get average.
*
* \param buffer input data
* \return average value
*/
template<class JElement_t, class JAllocator_t>
JElement_t getAverage(const array_type<JElement_t, JAllocator_t>& buffer)
{
return JAverage<JElement_t>(buffer.begin(), buffer.end());
}
/**
* Get average.
*
* \param __begin begin of data
* \param __end end of data
* \param value default value
* \return average value
*/
template<class T>
typename std::iterator_traits<T>::value_type getAverage(T __begin, T __end, typename std::iterator_traits<T>::value_type value)
{
try {
return getAverage(__begin, __end);
}
catch(const std::exception&) {
return value;
}
}
/**
* Get average.
*
* \param array c-array of values
* \param value default value
* \return average value
*/
template<class JValue_t, size_t N>
inline JValue_t getAverage(const JValue_t (&array)[N], typename JLANG::JClass<JValue_t>::argument_type value)
{
try {
return getAverage(array);
}
catch(const std::exception&) {
return value;
}
}
/**
* Get average.
*
* \param buffer input data
* \param value default value
* \return average value
*/
template<class JElement_t, class JAllocator_t>
JElement_t getAverage(const array_type<JElement_t, JAllocator_t>& buffer, typename JLANG::JClass<JElement_t>::argument_type value)
{
try {
return getAverage(buffer);
}
catch(const std::exception&) {
return value;
}
}
}
#endif