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 2907 additions and 0 deletions
#ifndef __JIO__JOBJECTBINARYIO__
#define __JIO__JOBJECTBINARYIO__
#include "JLang/JObjectIO.hh"
#include "JIO/JFileStreamIO.hh"
/**
* \author mdejong
*/
namespace JIO {}
namespace JPP { using namespace JIO; }
namespace JIO {
/**
* Auxiliary base class for storing and loading a single object to and from a binary 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 JObjectBinaryIO {
/**
* Load from input file.
*
* \param file_name file name
*/
void load(const char* file_name)
{
JLANG::load<JFileStreamReader>(file_name, static_cast<T&>(*this));
}
/**
* Store to output file.
*
* \param file_name file name
*/
void store(const char* file_name) const
{
JLANG::store<JFileStreamWriter>(file_name, static_cast<const T&>(*this));
}
};
}
#endif
#ifndef __JIO__JSERIALISABLE__
#define __JIO__JSERIALISABLE__
#include "JLang/JBinaryIO.hh"
#include "JLang/JObjectID.hh"
#include "JLang/JAbstractObjectStatus.hh"
/**
* \author mdejong
*/
/**
* Auxiliary classes and methods for binary I/O.
*/
namespace JIO {}
namespace JPP { using namespace JIO; }
namespace JIO {
using JLANG::JBinaryInput;
using JLANG::JBinaryOutput;
using JLANG::JAbstractObjectStatus;
class JReader; //!< Forward declaration of binary input.
class JWriter; //!< Forward declaration of binary output.
/**
* Interface class for a data structure with binary I/O.
*/
class JSerialisable {
public:
/**
* Virtual destructor.
*/
virtual ~JSerialisable()
{}
/**
* Read from input.
*
* \param in JReader
* \return JReader
*/
virtual JReader& read(JReader& in) = 0;
/**
* Write to output.
*
* \param out JWriter
* \return JWriter
*/
virtual JWriter& write(JWriter& out) const = 0;
};
/**
* Interface for binary input.
*/
class JReader :
public JLANG::JBinaryInput,
public JLANG::JAbstractObjectStatus
{
public:
/**
* Clear status of reader.
*/
virtual void clear()
{}
/**
* Read serialisable data object.
*
* \param object serialisable data object
* \return JReader
*/
JReader& operator>>(JSerialisable& object)
{
return object.read(*this);
}
JReader& operator>>(bool& value) { read((char*) &value, sizeof(bool)); return *this; }
JReader& operator>>(char& value) { read((char*) &value, sizeof(char)); return *this; }
JReader& operator>>(unsigned char& value) { read((char*) &value, sizeof(unsigned char)); return *this; }
JReader& operator>>(short& value) { read((char*) &value, sizeof(short)); return *this; }
JReader& operator>>(unsigned short& value) { read((char*) &value, sizeof(unsigned short)); return *this; }
JReader& operator>>(int& value) { read((char*) &value, sizeof(int)); return *this; }
JReader& operator>>(unsigned int& value) { read((char*) &value, sizeof(unsigned int)); return *this; }
JReader& operator>>(long int& value) { read((char*) &value, sizeof(long int)); return *this; }
JReader& operator>>(unsigned long int& value) { read((char*) &value, sizeof(unsigned long int)); return *this; }
JReader& operator>>(long long int& value) { read((char*) &value, sizeof(long long int)); return *this; }
JReader& operator>>(unsigned long long int& value) { read((char*) &value, sizeof(unsigned long long int)); return *this; }
JReader& operator>>(float& value) { read((char*) &value, sizeof(float)); return *this; }
JReader& operator>>(double& value) { read((char*) &value, sizeof(double)); return *this; }
JReader& operator>>(long double& value) { read((char*) &value, sizeof(long double)); return *this; }
JReader& operator>>(JLANG::JObjectID& value) { return (*this) >> value.getID(); }
/**
* Read object.
*
* \param object object
* \return this reader
*/
inline JReader& load(JSerialisable& object)
{
return object.read(*this);
}
/**
* Read object.
*
* \param object object
* \return this reader
*/
template<class T>
inline JReader& load(T& object)
{
return *this >> object;
}
};
/**
* Interface for binary output.
*/
class JWriter :
public JLANG::JBinaryOutput,
public JLANG::JAbstractObjectStatus
{
public:
/**
* Write serialisable data object.
*
* \param object serialisable data object
* \return JWriter
*/
JWriter& operator<<(const JSerialisable& object)
{
return object.write(*this);
}
JWriter& operator<<(const bool value) { write((const char*) &value, sizeof(bool)); return *this; }
JWriter& operator<<(const char value) { write((const char*) &value, sizeof(char)); return *this; }
JWriter& operator<<(const unsigned char value) { write((const char*) &value, sizeof(unsigned char)); return *this; }
JWriter& operator<<(const short value) { write((const char*) &value, sizeof(short)); return *this; }
JWriter& operator<<(const unsigned short value) { write((const char*) &value, sizeof(unsigned short)); return *this; }
JWriter& operator<<(const int value) { write((const char*) &value, sizeof(int)); return *this; }
JWriter& operator<<(const unsigned int value) { write((const char*) &value, sizeof(unsigned int)); return *this; }
JWriter& operator<<(const long int value) { write((const char*) &value, sizeof(long int)); return *this; }
JWriter& operator<<(const unsigned long int value) { write((const char*) &value, sizeof(unsigned long int)); return *this; }
JWriter& operator<<(const long long int value) { write((const char*) &value, sizeof(long long int)); return *this; }
JWriter& operator<<(const unsigned long long int value) { write((const char*) &value, sizeof(unsigned long long int)); return *this; }
JWriter& operator<<(const float value) { write((const char*) &value, sizeof(float)); return *this; }
JWriter& operator<<(const double value) { write((const char*) &value, sizeof(double)); return *this; }
JWriter& operator<<(const long double value) { write((const char*) &value, sizeof(long double)); return *this; }
JWriter& operator<<(const JLANG::JObjectID& value) { return (*this) << value.getID(); }
/**
* Write object.
*
* \param object object
* \return this writer
*/
inline JWriter& store(const JSerialisable& object)
{
return object.write(*this);
}
/**
* Write object.
*
* \param object object
* \return this writer
*/
template<class T>
inline JWriter& store(const T& object)
{
return *this << object;
}
};
}
#endif
#ifndef __JIO__JSTREAMIO__
#define __JIO__JSTREAMIO__
#include <istream>
#include <ostream>
#include "JIO/JSerialisable.hh"
/**
* \author mdejong
*/
namespace JIO {}
namespace JPP { using namespace JIO; }
namespace JIO {
/**
* Binary input based on std::istream.
* This class implements the JReader interface.
*/
class JStreamReader :
public JReader
{
public:
/**
* Constructor.
*
* \param __in input stream
*/
JStreamReader(std::istream& __in) :
in(__in)
{}
/**
* Status of reader.
*
* \return status of this reader
*/
virtual bool getStatus() const override
{
return (bool) in;
}
/**
* Clear status of reader.
*/
virtual void clear() override
{
in.clear();
}
/**
* Read byte array.
*
* \param buffer pointer to byte array
* \param length number of bytes
* \return number of bytes read
*/
virtual int read(char* buffer, const int length) override
{
in.read(buffer, length);
return in.gcount();
}
protected:
std::istream& in;
};
/**
* Binary output based on std::ostream.
* This class implements the JWriter interface.
*/
class JStreamWriter :
public JWriter
{
public:
/**
* Constructor.
*
* \param __out output stream
*/
JStreamWriter(std::ostream& __out) :
out(__out)
{}
/**
* Status of writer.
*
* \return status of this writer
*/
virtual bool getStatus() const override
{
return (bool) out;
}
/**
* Write byte array.
*
* \param buffer pointer to byte array
* \param length number of bytes
* \return number of bytes written
*/
virtual int write(const char* buffer, const int length) override
{
out.write(buffer, length);
return length;
}
protected:
std::ostream& out;
};
}
#endif
#ifndef __JLANG__JABSTRACTFILE__
#define __JLANG__JABSTRACTFILE__
#include <stdio.h>
#include "JLang/JComparable.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* The JAbstractFile class encapsulates the c-style file descriptor.
*/
class JAbstractFile :
public JComparable<JAbstractFile>
{
public:
static const int FILE_CLOSED = -1;
/**
* Default constructor.
*/
JAbstractFile() :
fileDescriptor(FILE_CLOSED)
{}
/**
* Constructor.
*
* \param file file descriptor
*/
JAbstractFile(const int file) :
fileDescriptor(file)
{}
/**
* Constructor.
*
* \param stream file stream
*/
JAbstractFile(FILE* stream) :
fileDescriptor(fileno(stream))
{}
/**
* Less than operation.
*
* \param file JAbstractFile to be compared
* \return true if this file descriptor is less; else false
*/
bool less(const JAbstractFile& file) const
{
return getFileDescriptor() < file.getFileDescriptor();
}
/**
* Get file descriptor.
*
* \return file descriptor
*/
int getFileDescriptor() const
{
return fileDescriptor;
}
/**
* Get open status.
*/
bool is_open() const
{
return fileDescriptor != FILE_CLOSED;
}
protected:
int fileDescriptor;
};
}
#endif
#ifndef __JLANG__JABSTRACTIO__
#define __JLANG__JABSTRACTIO__
#include <istream>
#include <ostream>
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Interface for ASCII input using standard streams.
*/
class JStreamInput {
public:
/**
* Virtual destructor.
*/
virtual ~JStreamInput()
{}
/**
* Stream input.
*
* \param in input stream
* \return input stream
*/
virtual std::istream& read(std::istream& in) = 0;
/**
* Read object from input.
*
* \param in input stream
* \param object object
* \return input stream
*/
friend inline std::istream& operator>>(std::istream& in, JStreamInput& object)
{
return object.read(in);
}
};
/**
* Interface for ASCII output using standard streams.
*/
class JStreamOutput {
public:
/**
* Virtual destructor.
*/
virtual ~JStreamOutput()
{}
/**
* Stream output.
*
* \param out output stream
* \return output stream
*/
virtual std::ostream& write(std::ostream& out) const = 0;
/**
* Write object to output.
*
* \param out output stream
* \param object object
* \return output stream
*/
friend inline std::ostream& operator<<(std::ostream& out, const JStreamOutput& object)
{
return object.write(out);
}
};
/**
* Interface for ASCII output with prefix and postfix using standard streams.
*/
class JStreamSuffixOutput {
public:
/**
* Virtual destructor.
*/
virtual ~JStreamSuffixOutput()
{}
/**
* Stream output.
*
* \param out output stream
* \param prefix prefix
* \param postfix postfix
* \return output stream
*/
virtual std::ostream& write(std::ostream& out,
const char* prefix,
const char postfix) const = 0;
};
}
#endif
#ifndef __JLANG__JABSTRACTOBJECTSTATUS__
#define __JLANG__JABSTRACTOBJECTSTATUS__
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Interface for status of object.\n
* This interface implements:
* - type conversion operator <tt>bool ()</tt>; and
* - negate operator <tt>!</tt>.
*/
struct JAbstractObjectStatus {
/**
* Get status of object.
*
* \return status of this object
*/
virtual bool getStatus() const = 0;
/**
* Type conversion operator.
*
* \return status of this object
*/
operator bool() const
{
return this->getStatus();
}
/**
* Negated status of this object.
*
* \return negated status of this object
*/
bool operator!() const
{
return !(this->getStatus());
}
};
}
#endif
#ifndef __JLANG__JABSTRACTPOINTER__
#define __JLANG__JABSTRACTPOINTER__
#include <cstdlib>
#include "JLang/JException.hh"
#include "JLang/JEquals.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Template interface for pointer to object(s).
*/
template<class JClass_t>
class JAbstractPointer :
public JEquals< JAbstractPointer<JClass_t> >
{
protected:
/**
* Default constructor.
*/
JAbstractPointer()
{}
public:
/**
* Virtual destructor.
*/
virtual ~JAbstractPointer()
{}
/**
* Equals.
* The equality is evaluated by comparison of the internal pointers.
*
* \param object abstract pointer
* \return true if equals; else false
*/
virtual bool equals(const JAbstractPointer& object) const
{
return this->get() == object.get();
}
/**
* Get pointer.
*
* \return pointer to object
*/
virtual JClass_t* get() const = 0;
/**
* Set pointer.
*
* \param p pointer to object
*/
virtual void set(JClass_t* p) = 0;
/**
* Reset pointer.
*/
virtual void reset() = 0;
/**
* Check validity of pointer.
*
* \return true if pointer not null; else false
*/
bool is_valid() const
{
return this->get() != NULL;
}
/**
* Reset pointer.
*
* \param p pointer to object
*/
void reset(JClass_t* p)
{
if (this->get() != p) {
this->reset();
if (p != NULL) {
this->set(p);
}
}
}
/**
* Smart pointer operator.
*
* \return pointer to object
*/
JClass_t* operator->() const
{
if (!is_valid())
throw JNullPointerException("JAbstractPointer::operator->()");
else
return this->get();
}
/**
* Type conversion operator.
*
* \return pointer to object
*/
operator JClass_t*() const
{
return this->get();
}
};
}
#endif
#ifndef __JLANG__JANYTYPE__
#define __JLANG__JANYTYPE__
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Auxiliary class for any type definition.
* This class can be used to test the validity of an expression through type conversion.
*/
struct JAnyType {
/**
* Type conversion operation.
*
* \param any_type any type
*/
template<class T>
JAnyType(const T& any_type)
{}
};
}
#endif
#ifndef __JLANG__JASSERT__
#define __JLANG__JASSERT__
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Generation of compiler error.
*/
template<bool, class T = void>
struct JAssert;
/**
* Implementation of valid assertion.
*/
template<class T>
struct JAssert<true, T>
{
static const bool value = true;
typedef T type;
};
}
#define STATIC_CHECK(expr) { JLANG::JAssert<expr>(); }
#endif
#ifndef __JLANG__JBINARYIO__
#define __JLANG__JBINARYIO__
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Interface for binary input.
*/
class JBinaryInput {
public:
/**
* Virtual destructor.
*/
virtual ~JBinaryInput()
{}
/**
* Read byte array.
*
* \param buffer pointer to byte array
* \param length number of bytes
* \return number of bytes read
*/
virtual int read(char* buffer, const int length) = 0;
};
/**
* Interface for binary output.
*/
class JBinaryOutput {
public:
/**
* Virtual destructor.
*/
virtual ~JBinaryOutput()
{}
/**
* Write byte array.
*
* \param buffer pointer to byte array
* \param length number of bytes
* \return number of bytes written
*/
virtual int write(const char* buffer, const int length) = 0;
};
}
#endif
#ifndef __JLANG__JBOOL__
#define __JLANG__JBOOL__
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Auxiliary template class for type bool.
* This class can be used for boolean expressions at compile time.
*/
template<bool __value__>
struct JBool
{
/**
* Type definition of bool value.
*/
typedef JBool<__value__> bool_type;
/**
* Default construcor.
*/
JBool()
{}
/**
* Value.
*/
static const bool value = __value__;
/**
* Type conversion operator.
*
* \return value
*/
operator bool() const
{
return value;
}
/**
* Make logical NOT.
*
* \return logical NOT
*/
static JBool<!value> c_not()
{
return JBool<!value>();
}
/**
* Make logical EQUALS.
*
* \return logical EQUALS
*/
template<bool option>
static JBool<value == option> c_equals()
{
return JBool<value == option>();
}
/**
* Make logical EQUALS.
*
* \param object value
* \return logical EQUALS
*/
template<bool option>
static JBool<value == option> c_equals(const JBool<option>& object)
{
return bool_type::c_equals<option>();
}
/**
* Make logical AND.
*
* \return logical AND
*/
template<bool option>
static JBool<value && option> c_and()
{
return JBool<value && option>();
}
/**
* Make logical AND.
*
* \param object value
* \return logical AND
*/
template<bool option>
static JBool<value && option> c_and(const JBool<option>& object)
{
return bool_type::c_and<option>();
}
/**
* Make logical OR.
*
* \return logical OR
*/
template<bool option>
static JBool<value || option> c_or()
{
return JBool<value || option>();
}
/**
* Make logical OR.
*
* \param object value
* \return logical OR
*/
template<bool option>
static JBool<value || option> c_or(const JBool<option>& object)
{
return bool_type::c_or<option>();
}
/**
* Make logical XOR.
*
* \return logical XOR
*/
template<bool option>
static JBool<value != option> c_xor()
{
return JBool<value != option>();
}
/**
* Make logical XOR.
*
* \param object value
* \return logical XOR
*/
template<bool option>
static JBool<value != option> c_xor(const JBool<option>& object)
{
return bool_type::c_xor<option>();
}
/**
* Make logical SWITCH.
* If value is true, select first, else select second.
*
* \return logical SWITCH
*/
template<bool __first__, bool __second__>
static JBool<(value && __first__) || (!value && __second__)> c_switch()
{
return JBool<(value && __first__) || (!value && __second__)>();
}
/**
* Make logical SWITCH.
* If value is true, select first, else select second.
*
* \param first first value
* \param second second value
* \return logical SWITCH
*/
template<bool __first__, bool __second__>
static JBool<(value && __first__) || (!value && __second__)> c_switch(const JBool<__first__>& first,
const JBool<__second__>& second)
{
return bool_type::c_switch<__first__, __second__>();
}
};
/**
* Make logical NOT.
*
* \param value value
* \return logical NOT
*/
template<bool __value__>
static JBool<!__value__> c_not(const JBool<__value__>& value)
{
return value.c_not();
}
/**
* Make logical EQUALS.
*
* \param first first value
* \param second second value
* \return logical EQUALS
*/
template<bool __first__, bool __second__>
inline JBool<__first__ == __second__> c_equals(const JBool<__first__>& first,
const JBool<__second__>& second)
{
return first.c_equals(second);
}
/**
* Make logical AND.
*
* \param first first value
* \param second second value
* \return logical AND
*/
template<bool __first__, bool __second__>
inline JBool<__first__ == __second__> c_and(const JBool<__first__>& first,
const JBool<__second__>& second)
{
return first.c_and(second);
}
/**
* Make logical OR.
*
* \param first first value
* \param second second value
* \return logical OR
*/
template<bool __first__, bool __second__>
inline JBool<__first__ == __second__> c_or(const JBool<__first__>& first,
const JBool<__second__>& second)
{
return first.c_or(second);
}
/**
* Make logical XOR.
*
* \param first first value
* \param second second value
* \return logical XOR
*/
template<bool __first__, bool __second__>
inline JBool<__first__ == __second__> c_xor(const JBool<__first__>& first,
const JBool<__second__>& second)
{
return first.c_xor(second);
}
typedef JBool<true> JTRUE; //!< True type
typedef JBool<false> JFALSE; //!< False type
static const JTRUE JTrue_t; //!< True value
static const JFALSE JFalse_t; //!< False value
/**
* Type definition of logical EQUALS.
*/
template<class JFirst_t, class JSecond_t>
struct EQUALS;
/**
* Template specialisation for logical EQUALS.
*/
template<bool first, bool second>
struct EQUALS< JBool<first>, JBool<second> > :
public JBool<first == second>
{};
/**
* Type definition of logical NOT.
*/
template<class T>
struct NOT;
/**
* Template specialisation for logical NOT.
*/
template<bool __value__>
struct NOT< JBool<__value__> > :
public JBool<!__value__>
{};
/**
* Type definition of logical AND.
*/
template<class JFirst_t, class JSecond_t>
struct AND;
/**
* Template specialisation for logical AND.
*/
template<bool first, bool second>
struct AND< JBool<first>, JBool<second> > :
public JBool<first && second>
{};
/**
* Type definition of logical OR.
*/
template<class JFirst_t, class JSecond_t>
struct OR;
/**
* Template specialisation for logical OR.
*/
template<bool first, bool second>
struct OR< JBool<first>, JBool<second> > :
public JBool<first || second>
{};
/**
* Type definition of logical XOR.
*/
template<class JFirst_t, class JSecond_t>
struct XOR;
/**
* Template specialisation for logical XOR.
*/
template<bool first, bool second>
struct XOR< JBool<first>, JBool<second> > :
public JBool<first != second>
{};
}
#endif
#ifndef __JLANG_JCC__
#define __JLANG_JCC__
//#include "RVersion.h"
/**
* \file
* Compiler version dependent expressions, macros, etc.
* \author mdejong
*/
//#if __GNUC__ == 4 && __GNUC_MINOR__ < 6
//#endif
//#if ROOT_VERSION_CODE < ROOT_VERSION(6,0,0)
//#endif
#endif
#ifndef __JLANG__JCATEGORY
#define __JLANG__JCATEGORY
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Auxiliary class to define value, reference and pointer types for given data type and category.
*/
template<class T, bool is_constant>
struct JCategory;
/**
* Specialisation of JCategory for constant (i.e.\ non-modifiable) data type.
*/
template<class T>
struct JCategory<T, true> {
typedef const T value_type;
typedef const T& reference_type;
typedef const T* pointer_type;
};
/**
* Specialisation of JCategory for modifiable (i.e.\ non-constant) data type.
*/
template<class T>
struct JCategory<T, false> {
typedef T value_type;
typedef T& reference_type;
typedef T* pointer_type;
};
}
#endif
#ifndef __JLANG__JCLASS__
#define __JLANG__JCLASS__
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Data structure for primitive types.
*/
template<class T>
struct JPrimitive {
typedef const T argument_type;
enum { is_primitive = true };
};
/**
* Data structure for method argument types.
*/
template<class T>
struct JArgument {
typedef const T& argument_type;
enum { is_primitive = false };
};
/**
* Specialisations of JArgument for primitive types.
*/
template<> struct JArgument<bool> : public JPrimitive<bool> {};
template<> struct JArgument<char> : public JPrimitive<char> {};
template<> struct JArgument<unsigned char> : public JPrimitive<unsigned char> {};
template<> struct JArgument<short> : public JPrimitive<short> {};
template<> struct JArgument<unsigned short> : public JPrimitive<unsigned short> {};
template<> struct JArgument<int> : public JPrimitive<int> {};
template<> struct JArgument<unsigned int> : public JPrimitive<unsigned int> {};
template<> struct JArgument<long int> : public JPrimitive<long int> {};
template<> struct JArgument<unsigned long int> : public JPrimitive<unsigned long int> {};
template<> struct JArgument<long long int> : public JPrimitive<long long int> {};
template<> struct JArgument<unsigned long long int> : public JPrimitive<unsigned long long int> {};
template<> struct JArgument<float> : public JPrimitive<float> {};
template<> struct JArgument<double> : public JPrimitive<double> {};
template<> struct JArgument<long double> : public JPrimitive<long double> {};
/**
* Data structure to check whether given data type is an iterator.
*/
template<class T>
struct is_iterator {
static T make();
typedef void* buffer[2];
static buffer& test(...); //!< any type
template<class R> static typename R::iterator_category* test(R); //!< iterator
template<class R> static void* test(R *); //!< pointer
static const bool value = sizeof(test(make())) == sizeof(void *);
};
/**
* Template for generic class types.
*/
template<class T>
struct JClass {
typedef typename JArgument<T>::argument_type argument_type;
typedef T value_type;
typedef value_type& reference_type;
typedef value_type* pointer_type;
enum { is_primitive = JArgument<T>::is_primitive };
enum { is_reference = false };
enum { is_pointer = false };
enum { is_constant = false };
};
/**
* Specialisation of JClass for const class types.
*/
template<class T>
struct JClass<const T> {
typedef typename JArgument<T>::argument_type argument_type;
typedef T value_type;
typedef const value_type& reference_type;
typedef const value_type* pointer_type;
enum { is_primitive = JArgument<T>::is_primitive };
enum { is_reference = false };
enum { is_pointer = false };
enum { is_constant = true };
};
/**
* Specialisation of JClass for reference class types.
*/
template<class T>
struct JClass<T&> {
typedef T& argument_type;
typedef T value_type;
typedef value_type& reference_type;
typedef value_type* pointer_type;
enum { is_primitive = JArgument<T>::is_primitive };
enum { is_reference = true };
enum { is_pointer = false };
enum { is_constant = false };
};
/**
* Specialisation of JClass for const reference class types.
*/
template<class T>
struct JClass<const T&> {
typedef const T& argument_type;
typedef T value_type;
typedef const value_type& reference_type;
typedef const value_type* pointer_type;
enum { is_primitive = JArgument<T>::is_primitive };
enum { is_reference = true };
enum { is_pointer = false };
enum { is_constant = true };
};
/**
* Specialisation of JClass for pointer class types.
*/
template<class T>
struct JClass<T*> {
typedef T* argument_type;
typedef T value_type;
typedef value_type*& reference_type;
typedef value_type* pointer_type;
enum { is_primitive = JArgument<T>::is_primitive };
enum { is_reference = false };
enum { is_pointer = true };
enum { is_constant = false };
};
/**
* Specialisation of JClass for const pointer class types.
*/
template<class T>
struct JClass<const T*> {
typedef const T* argument_type;
typedef T value_type;
typedef const value_type*& reference_type;
typedef const value_type* pointer_type;
enum { is_primitive = JArgument<T>::is_primitive };
enum { is_reference = false };
enum { is_pointer = true };
enum { is_constant = true };
};
/**
* Specialisation of JClass for pointer class types.
*/
template<class T>
struct JClass<T*&> {
typedef T*& argument_type;
typedef T value_type;
typedef value_type*& reference_type;
typedef value_type* pointer_type;
enum { is_primitive = JArgument<T>::is_primitive };
enum { is_reference = true };
enum { is_pointer = true };
enum { is_constant = false };
};
/**
* Specialisation of JClass for const pointer class types.
*/
template<class T>
struct JClass<const T*&> {
typedef const T*& argument_type;
typedef T value_type;
typedef const value_type*& reference_type;
typedef const value_type* pointer_type;
enum { is_primitive = JArgument<T>::is_primitive };
enum { is_reference = true };
enum { is_pointer = true };
enum { is_constant = true };
};
}
#endif
#ifndef __JLANG__JCLONABLE__
#define __JLANG__JCLONABLE__
#include "JLang/JNullType.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Template class for object cloning.
*/
template<class JClonable_t, class JDerived_t = JNullType>
struct JClonable;
/**
* Template specialisation to define base class for interface of object cloning.
*/
template<class JClonable_t>
struct JClonable<JClonable_t, JNullType>
{
/**
* Type definition of return value of method clone().
*/
typedef JClonable_t* clone_type;
/**
* Virtual destructor.
*/
virtual ~JClonable()
{}
/**
* Get clone of this object.
*
* \return pointer to newly created object
*/
virtual clone_type clone() const = 0;
};
/**
* Template specialisation to define base class for implementation of object cloning.
*
* This class derives from the specified clonable class and implements the method clone.
*/
template<class JClonable_t, class JDerived_t>
struct JClonable :
public JClonable_t
{
typedef typename JClonable<JClonable_t>::clone_type clone_type;
/**
* Get clone of this object.
*
* \return pointer to newly created object
*/
virtual clone_type clone() const override
{
return new JDerived_t(static_cast<const JDerived_t&>(*this));
}
};
}
#endif
#ifndef __JLANG__JCOLORFACET__
#define __JLANG__JCOLORFACET__
#include <locale>
#include <ostream>
#include <string>
#include <vector>
#include <map>
#include "JLang/JType.hh"
#include "JLang/JTypeList.hh"
#include "JLang/JSinglePointer.hh"
#include "JLang/JException.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Enumeration of text colors.
*/
enum JColor_t {
RED, //!< red
GREEN, //!< green
BLUE, //!< blue
WHITE, //!< white
CYAN, //!< cyan
PURPLE, //!< purple
YELLOW, //!< yellow
RESET, //!< reset
BOLD //!< bold
};
/**
* Facet interface to specify text color.
* This class extends the std::locale::facet class.
*/
struct JColorFacet :
public std::locale::facet
{
static std::locale::id id;
/**
* Constructor.
*
* \param refs reference count
*/
JColorFacet(std::size_t refs = 0) :
std::locale::facet(refs)
{}
/**
* Print color.
*
* \param color code
* \return text
*/
virtual const char* c_str(const JColor_t color) const = 0;
/**
* Clone this facet.
*
* \return pointer to newly created facet
*/
virtual JColorFacet* clone() const = 0;
/**
* Check color.
*
* \param color code
* \return true if color; else false
*/
static inline bool is_color(const int color)
{
return !is_bold(color) && !is_reset(color);
}
/**
* Check bold.
*
* \param color code
* \return true if bold; else false
*/
static inline bool is_bold(const int color)
{
return color == BOLD;
}
/**
* Check reset.
*
* \param color code
* \return true if reset; else false
*/
static inline bool is_reset(const int color)
{
return color == RESET;
}
private:
JColorFacet(const JColorFacet&); // not defined
void operator=(const JColorFacet&); // not defined
};
/**
* Facet class to specify text color for ASCII.
*/
struct JColorFacetASCII :
public JColorFacet
{
/**
* Get name of facet.
*
* \return name
*/
static inline const char* getName()
{
return "ASCII";
}
/**
* Constructor.
*
* \param refs reference count
*/
JColorFacetASCII(std::size_t refs = 0) :
JColorFacet(refs)
{}
/**
* Print color.
*
* \param color code
* \return text
*/
virtual const char* c_str(const JColor_t color) const override
{
switch (color) {
case RED: return "\033[91m";
case GREEN: return "\033[92m";
case BLUE: return "\033[94m";
case WHITE: return "\033[97m";
case CYAN: return "\033[96m";
case PURPLE: return "\033[95m";
case YELLOW: return "\033[93m";
case BOLD: return "\033[1m";
case RESET: return "\033[0m";
default: return "";
}
}
/**
* Clone this facet.
*
* \return pointer to newly created facet
*/
virtual JColorFacetASCII* clone() const override
{
return new JColorFacetASCII();
}
};
/**
* Facet class to specify text color for ELcode.
*/
struct JColorFacetELcode :
public JColorFacet
{
/**
* Get name of facet.
*
* \return name
*/
static inline const char* getName()
{
return "ELcode";
}
/**
* Constructor.
*
* \param refs reference count
*/
JColorFacetELcode(std::size_t refs = 0) :
JColorFacet(refs)
{}
/**
* Print color.
*
* \param color code
* \return text
*/
virtual const char* c_str(const JColor_t color) const override
{
static std::string buffer;
history.push_back(color);
switch (color) {
case RED: return "[color=red]";
case GREEN: return "[color=green]";
case BLUE: return "[color=blue]";
case WHITE: return "[color=white]";
case CYAN: return "[color=cyan]";
case PURPLE: return "[color=purple]";
case YELLOW: return "[color=yellow]";
case BOLD: return "[bold]";
case RESET:
buffer.clear();
for (std::vector<int>::const_reverse_iterator i = history.rbegin(); i != history.rend(); ++i) {
if (is_color(*i))
buffer += "[/color]";
else if (is_bold (*i))
buffer += "[/bold]";
else
;
}
history.clear();
return buffer.c_str();
default: return "";
}
}
/**
* Clone this facet.
*
* \return pointer to newly created facet
*/
virtual JColorFacetELcode* clone() const override
{
return new JColorFacetELcode();
}
private:
mutable std::vector<int> history;
};
/**
* Typelist of color facets.
*/
typedef JTYPELIST<JColorFacetASCII, JColorFacetELcode>::typelist JColorFacetTypes_t;
/**
* Auxiliary map for color facets.
*/
struct JColorFacetMap_t :
public std::map<std::string, JSinglePointer<JColorFacet> >
{
typedef std::map<std::string, JSinglePointer<JColorFacet> > map_type;
typedef typename map_type::key_type key_type;
typedef typename map_type::mapped_type mapped_type;
typedef typename map_type::value_type value_type;
/**
* Default constructor.
*/
JColorFacetMap_t()
{
for_each(*this, JType<JColorFacetTypes_t>());
}
/**
* Get value for given key.
*
* Note that this method will throw an error if given key is absent.
*
* \param key key
* \return value
*/
const mapped_type& operator[](const key_type& key) const
{
const_iterator p = find(key);
if (p != this->end()) {
return p->second;
}
THROW(JNullPointerException, "Invalid key " << key);
}
/**
* Insert data type.
*
* \param type data type
*/
template<class T>
void operator()(const JType<T>& type)
{
insert(value_type(T::getName(), new T()));
}
};
/**
* Color facets.
*/
static const JColorFacetMap_t color_facets;
}
/**
* Print color.
*
* \param out output stream
* \param color color
* \return output stream
*/
inline std::ostream& operator<<(std::ostream& out, const JLANG::JColor_t color)
{
using namespace std;
using namespace JPP;
const locale& loc = out.getloc();
if (has_facet<JColorFacetASCII>(loc))
return out << use_facet<JColorFacetASCII> (loc).c_str(color);
else if (has_facet<JColorFacetELcode>(loc))
return out << use_facet<JColorFacetELcode>(loc).c_str(color);
else
return out << JColorFacetASCII().c_str(color);
}
#endif
#ifndef __JLANG__JCOMPARABLE__
#define __JLANG__JCOMPARABLE__
#include "JLang/JNullType.hh"
#include "JLang/JClass.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Template definition of auxiliary base class for comparison of data structures.
*
* The various specialisations of this class implement the operators <tt> < > <= >= == != </tt>.
*/
template<class JFirst_t, class JSecond_t = JNullType>
struct JComparable;
/**
* General purpose specialisation of class JComparable for any data type.
*
* The template parameter should have the corresponding member method:
* <pre>
* bool less(const JClass_t&) const;
* </pre>
*
* This class uses in-class friend operators (see Barton-Nackman trick).
*/
template<class JClass_t>
struct JComparable<JClass_t, JNullType>
{
/**
* Less than operator.
*
* \param first first object
* \param second second object
* \return true if first object is less than second object; else false
*/
friend bool operator<(const JClass_t& first,
const JClass_t& second)
{
return first.less(second);
}
/**
* Greater than operator.
*
* \param first first object
* \param second second object
* \return true if first object is greater than second object; else false
*/
friend bool operator>(const JClass_t& first,
const JClass_t& second)
{
return second.less(first);
}
/**
* Less than or equal operator.
*
* \param first first object
* \param second second object
* \return true if first object is less than or equal to second object; else false
*/
friend bool operator<=(const JClass_t& first,
const JClass_t& second)
{
return !second.less(first);
}
/**
* Greater than or equal operator.
*
* \param first first object
* \param second second object
* \return true if first object is greater than or equal to second object; else false
*/
friend bool operator>=(const JClass_t& first,
const JClass_t& second)
{
return !first.less(second);
}
/**
* Equal operator.
*
* \param first first object
* \param second second object
* \return true if two objects are equal; else false
*/
friend bool operator==(const JClass_t& first,
const JClass_t& second)
{
return !first.less(second) && !second.less(first);
}
/**
* Not equal operator.
*
* \param first first object
* \param second second object
* \return true if two objects are not equal; else false
*/
friend bool operator!=(const JClass_t& first,
const JClass_t& second)
{
return first.less(second) || second.less(first);
}
};
/**
* Specialisation of class JComparable for two data types.
*
* The first template parameter should have the corresponding member methods:
* <pre>
* bool less(const JSecond_t&) const;
* bool more(const JSecond_t&) const;
* </pre>
* where <tt>JFirst_t</tt> andd <tt>JSecond_t</tt> refers to the first and second template parameter, respectively.
* The second template parameter may be a primitive data type.
* This class uses in-class friend operators (see Barton-Nackman trick).
*/
template<class JFirst_t, class JSecond_t>
struct JComparable
{
/**
* Less than operator.
*
* \param first first object
* \param second second object
* \return true if first object is less than second object; else false
*/
friend bool operator<(const JFirst_t& first,
typename JClass<JSecond_t>::argument_type second)
{
return first.less(second);
}
/**
* Less than operator.
*
* \param first first object
* \param second second object
* \return true if first object is less than second object; else false
*/
friend bool operator<(typename JClass<JSecond_t>::argument_type first,
const JFirst_t& second)
{
return second.more(first);
}
/**
* Greater than operator.
*
* \param first first object
* \param second second object
* \return true if first object is greater than second object; else false
*/
friend bool operator>(const JFirst_t& first,
typename JClass<JSecond_t>::argument_type second)
{
return first.more(second);
}
/**
* Greater than operator.
*
* \param first first object
* \param second second object
* \return true if first object is greater than second object; else false
*/
friend bool operator>(typename JClass<JSecond_t>::argument_type first,
const JFirst_t& second)
{
return second.less(first);
}
/**
* Less than or equal operator.
*
* \param first first object
* \param second second object
* \return true if first object is less than or equal to second object; else false
*/
friend bool operator<=(const JFirst_t& first,
typename JClass<JSecond_t>::argument_type second)
{
return !first.more(second);
}
/**
* Less than or equal operator.
*
* \param first first object
* \param second second object
* \return true if first object is less than or equal to second object; else false
*/
friend bool operator<=(typename JClass<JSecond_t>::argument_type first,
const JFirst_t& second)
{
return second.more(first);
}
/**
* Greater than or equal operator.
*
* \param first first object
* \param second second object
* \return true if first object is greater than or equal to second object; else false
*/
friend bool operator>=(const JFirst_t& first,
typename JClass<JSecond_t>::argument_type second)
{
return !first.less(second);
}
/**
* Greater than or equal operator.
*
* \param first first object
* \param second second object
* \return true if first object is greater than or equal to second object; else false
*/
friend bool operator>=(typename JClass<JSecond_t>::argument_type first,
const JFirst_t& second)
{
return second.less(first);
}
/**
* Equal operator.
*
* \param first first object
* \param second second object
* \return true if two objects are equal; else false
*/
friend bool operator==(const JFirst_t& first,
typename JClass<JSecond_t>::argument_type second)
{
return !first.less(second) && !first.more(second);
}
/**
* Equal operator.
*
* \param first first object
* \param second second object
* \return true if two objects are equal; else false
*/
friend bool operator==(typename JClass<JSecond_t>::argument_type first,
const JFirst_t& second)
{
return !second.less(first) && !second.more(first);
}
/**
* Not equal operator.
*
* \param first first object
* \param second second object
* \return true if two objects are not equal; else false
*/
friend bool operator!=(const JFirst_t& first,
typename JClass<JSecond_t>::argument_type second)
{
return first.less(second) || first.more(second);
}
/**
* Not equal operator.
*
* \param first first object
* \param second second object
* \return true if two objects are not equal; else false
*/
friend bool operator!=(typename JClass<JSecond_t>::argument_type first,
const JFirst_t& second)
{
return second.less(first) || second.more(first);
}
};
}
#endif
#ifndef __JLANG__JCOMPARISONAVAILABLE__
#define __JLANG__JCOMPARISONAVAILABLE__
#include "JLang/JNullType.hh"
#include "JLang/JAnyType.hh"
#include "JLang/JVoid.hh"
#include "JLang/JTest.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Local namespace for fallback implementations for comparison operators.
*/
namespace JLOCAL {
/**
* Fallback implementations for comparison operators.
*/
inline JNullType operator==(JAnyType, JAnyType) { return JNullType(); }
inline JNullType operator!=(JAnyType, JAnyType) { return JNullType(); }
inline JNullType operator< (JAnyType, JAnyType) { return JNullType(); }
inline JNullType operator> (JAnyType, JAnyType) { return JNullType(); }
inline JNullType operator<=(JAnyType, JAnyType) { return JNullType(); }
inline JNullType operator>=(JAnyType, JAnyType) { return JNullType(); }
/**
* Test availability of comparison operators of non-composite data types.
*/
template<class T, class JType_t = void>
class JComparisonAvailable :
public JTest
{
using JTest::test;
static JTrue test(const bool&);
static T& getReference();
public:
static const bool has_eq = JTEST(test(getReference() == getReference())); //!< true if operator== available; else false
static const bool has_ne = JTEST(test(getReference() != getReference())); //!< true if operator!= available; else false
static const bool has_lt = JTEST(test(getReference() < getReference())); //!< true if operator< available; else false
static const bool has_gt = JTEST(test(getReference() > getReference())); //!< true if operator> available; else false
static const bool has_le = JTEST(test(getReference() <= getReference())); //!< true if operator<= available; else false
static const bool has_ge = JTEST(test(getReference() >= getReference())); //!< true if operator>= available; else false
};
/**
* Test availability of comparison operators of data types which have a type definitions for first_type and second_type.
* This applies to STL containers such as std::map.
* Note that STL provides for the comparison of the container based on comparisons of its elements.
*/
template<class T>
class JComparisonAvailable<T, typename JVoid<typename T::second_type>::type>
{
typedef typename T::first_type first_type;
typedef typename T::second_type second_type;
public:
static const bool has_eq = JComparisonAvailable<first_type>::has_eq && JComparisonAvailable<second_type>::has_eq;
static const bool has_ne = JComparisonAvailable<first_type>::has_ne && JComparisonAvailable<second_type>::has_ne;
static const bool has_lt = JComparisonAvailable<first_type>::has_lt && JComparisonAvailable<second_type>::has_lt;
static const bool has_gt = JComparisonAvailable<first_type>::has_gt && JComparisonAvailable<second_type>::has_gt;
static const bool has_le = JComparisonAvailable<first_type>::has_le && JComparisonAvailable<second_type>::has_le;
static const bool has_ge = JComparisonAvailable<first_type>::has_ge && JComparisonAvailable<second_type>::has_ge;
};
/**
* Test availability of comparison operators of data types which have a type definition for value_type.
* This applies to STL containers such as std::vector and std::set.
* Note that STL provides for the comparison of the container based on comparisons of its elements.
*/
template<class T>
class JComparisonAvailable<T, typename JVoid<typename T::value_type>::type> :
public JComparisonAvailable<typename T::value_type>
{};
/**
* Template base class for data structures without equality capability.
* This class implements the operators <tt> == != </tt>.
*/
template<class T>
class JNoequals {
private:
/**
* Equal operator.
*
* \param first first object
* \param second second object
* \return true if two objects are equal; else false
*/
friend JNullType operator==(const T& first, const T& second)
{
return JNullType();
}
/**
* Not equal operator.
*
* \param first first object
* \param second second object
* \return true if two objects are not equal; else false
*/
friend JNullType operator!=(const T& first, const T& second)
{
return JNullType();
}
};
}
using JLOCAL::JComparisonAvailable;
using JLOCAL::JNoequals;
}
#endif
#ifndef __JLANG__JEQUALS__
#define __JLANG__JEQUALS__
#include "JLang/JNullType.hh"
#include "JLang/JClass.hh"
/**
* \author mdejong
*/
namespace JLANG {}
namespace JPP { using namespace JLANG; }
namespace JLANG {
/**
* Template definition of auxiliary base class for comparison of data structures.
*
* The various specialisations of this class implement the operators <tt> == != </tt>.
*/
template<class JFirst_t, class JSecond_t = JNullType>
struct JEquals;
/**
* General purpose specialisation of class JEquals for any data type.
*
* The template parameter should have the corresponding member method:
* <pre>
* bool equals(const JClass_t&) const;
* </pre>
*
* This class uses in-class friend operators (see Barton-Nackman trick).
*/
template<class JClass_t>
struct JEquals<JClass_t, JNullType>
{
/**
* Equal operator.
*
* \param first first object
* \param second second object
* \return true if two objects are equal; else false
*/
friend bool operator==(const JClass_t& first,
const JClass_t& second)
{
return first.equals(second);
}
/**
* Not equal operator.
*
* \param first first object
* \param second second object
* \return true if two objects are not equal; else false
*/
friend bool operator!=(const JClass_t& first,
const JClass_t& second)
{
return !first.equals(second);
}
};
/**
* Specialisation of class JEquals for two data types.
*
* The first template parameter should have the corresponding member methods:
* <pre>
* bool equals(const JFirst_t&) const;
* bool equals(const JSecond_t&) const;
* </pre>
* where <tt>JFirst_t</tt> and <tt>JSecond_t</tt> refer to the first and second template parameter, respectively.
* The second template parameter may be a primitive data type.
* This class uses in-class friend operators (see Barton-Nackman trick).
*/
template<class JFirst_t, class JSecond_t>
struct JEquals :
public JEquals<JFirst_t>
{
/**
* Equal operator.
*
* \param first first object
* \param second second object
* \return true if two objects are equal; else false
*/
friend bool operator==(const JFirst_t& first,
typename JClass<JSecond_t>::argument_type second)
{
return first.equals(second);
}
/**
* Equal operator.
*
* \param first first object
* \param second second object
* \return true if two objects are equal; else false
*/
friend bool operator==(typename JClass<JSecond_t>::argument_type first,
const JFirst_t& second)
{
return second.equals(first);
}
/**
* Not equal operator.
*
* \param first first object
* \param second second object
* \return true if two objects are not equal; else false
*/
friend bool operator!=(const JFirst_t& first,
typename JClass<JSecond_t>::argument_type second)
{
return !first.equals(second);
}
/**
* Not equal operator.
*
* \param first first object
* \param second second object
* \return true if two objects are not equal; else false
*/
friend bool operator!=(typename JClass<JSecond_t>::argument_type first,
const JFirst_t& second)
{
return !second.equals(first);
}
};
}
#endif
This diff is collapsed.