Commit a1fc55cc authored by thomas.forbriger's avatar thomas.forbriger
Browse files

ticket10: copied files from libtfxx to libtsxx

This is a legacy commit from before 2015-03-01.
It may be incomplete as well as inconsistent.
See COPYING.legacy and README.history for details.
parent 88a56a63
/*! \file error.cc
* \brief exceptions and error handling macros (implementation)
*
* ----------------------------------------------------------------------------
*
* $Id$
* \author Thomas Forbriger
* \date 27/11/2002
*
* exceptions and error handling macros (implementation)
*
* Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt)
*
* ----
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* ----
*
* REVISIONS and CHANGES
* - 27/11/2002 V1.0 Thomas Forbriger
*
* ============================================================================
*/
#define TF_ERROR_CC_VERSION \
"TF_ERROR_CC V1.0 "
#define TF_ERROR_CC_CVSID \
"$Id$"
#include <iostream>
#include <tfxx/error.h>
using std::cerr;
using std::endl;
namespace tfxx {
namespace error {
//! initialize and instantiate
bool Exception::Mreport_on_construct=true;
std::stack<bool> Exception::Mprevious_report_state;
//! construct from nothing
Exception::Exception():
Mmessage(NULL), Mfile(NULL), Mline(0), Mcondition(NULL)
{ if (this->report_on_construct_is_true()) { base_report(); } }
//! construct with message
Exception::Exception(const char* message):
Mmessage(message), Mfile(NULL), Mline(0), Mcondition(NULL)
{ if (this->report_on_construct_is_true()) { base_report(); } }
//! construct with message and file info
Exception::Exception(const char* message,
const char* condition):
Mmessage(message), Mfile(NULL), Mline(0), Mcondition(condition)
{ if (this->report_on_construct_is_true()) { base_report(); } }
//! construct with message and file info
Exception::Exception(const char* message,
const char* file,
const int& line):
Mmessage(message), Mfile(file), Mline(line), Mcondition(NULL)
{ if (this->report_on_construct_is_true()) { base_report(); } }
//! construct with message and file info and condition
Exception::Exception(const char* message,
const char* file,
const int& line,
const char* condition):
Mmessage(message), Mfile(file), Mline(line), Mcondition(condition)
{ if (this->report_on_construct_is_true()) { base_report(); } }
//! switch on
void Exception::report_on_construct()
{
Mprevious_report_state.push(Mreport_on_construct);
Mreport_on_construct=true;
}
//! switch off
void Exception::dont_report_on_construct()
{
Mprevious_report_state.push(Mreport_on_construct);
Mreport_on_construct=false;
}
//! restore
void Exception::restore_report_state()
{
if (!Mprevious_report_state.empty())
{
Mreport_on_construct=Mprevious_report_state.top();
Mprevious_report_state.pop();
}
}
//! report
void Exception::report() const
{
base_report();
}
//! report
void Exception::base_report() const
{
std::cout.flush();
cerr << "Exception report:" << endl;
if (Mmessage==NULL)
{
cerr << " No message" << endl;
}
else
{
cerr << " message: " << Mmessage << endl;
}
if (Mfile!=NULL)
{
cerr << " triggered in \"" << Mfile << "\" at line #" << Mline << endl;
}
if (Mcondition!=NULL)
{
cerr << " by test condition:" << endl
<< " \"" << Mcondition << "\"" << endl;
}
cerr.flush();
}
} // namespace error
} // namespace tfxx
/* ----- END OF error.cc ----- */
/*! \file error.h
* \brief exceptions and error handling macros (prototypes)
*
* \ingroup error_h
* ----------------------------------------------------------------------------
*
* $Id$
* \author Thomas Forbriger
* \date 27/11/2002
*
* exceptions and error handling macros (prototypes)
*
* Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt)
*
* ----
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* ----
*
* REVISIONS and CHANGES
* - 27/11/2002 V1.0 Thomas Forbriger
* - 28/04/2006 V1.1 provide explicit virtual destructor
* - 11/0/2007 V1.2 added a few macros
*
* ============================================================================
*/
// include guard
#ifndef TF_ERROR_H_VERSION
#define TF_ERROR_H_VERSION \
"TF_ERROR_H V1.2"
#define TF_ERROR_H_CVSID \
"$Id$"
#include<stack>
namespace tfxx {
/*! \defgroup group_error Error handling module
*/
/*! \brief Interface provided through error.h
* \defgroup error_h Interface provided through error.h
* \ingroup group_error
*/
/*! \brief Error handling
*
* \ingroup group_error, error_h
* You may find the exception base class here.
*/
namespace error {
/*! \brief Base class for exceptions
*
* This is an exception base class. It holds some information about the
* reason for throwing the exception. The information is printed to cerr
* through function report(). This function may be overloaded by a derived
* type. But its functionality is still accessible through base_report().
*
* The standard behaviour is to print ou the message during object
* initialization. If you don't like this, call dont_report_on_construct().
*
* \ingroup group_error, error_h
* \sa TFXX_Xassert
*/
class Exception
{
public:
//! Creates exception with no explaning comments
Exception();
//! Creates an exception with an explanation message
Exception(const char* message);
//! Creates an exception with message and failed assertion
Exception(const char* message,
const char* condition);
//! Create with message, failed assertion, and code position
Exception(const char* message,
const char* file,
const int& line,
const char* condition);
//! Create with message and code position
Exception(const char* message,
const char* file,
const int& line);
//! provide explicit virtual destructor
virtual ~Exception() { }
//! Screen report
virtual void report() const;
//! Issue a screen report on construction of exception
static void report_on_construct();
//! Issue NO screen report on construction of exception
static void dont_report_on_construct();
//! Restore previous report state
static void restore_report_state();
protected:
bool report_on_construct_is_true() const
{ return Mreport_on_construct; }
private:
//! Screen report
void base_report() const;
//! Shall we print to cerr at construction time?
static bool Mreport_on_construct;
//! pointer to message string
const char* Mmessage;
//! pointer to file name string
const char* Mfile;
//! pointer to line number in source file
int Mline;
//! pointer to assertion condition text string
const char* Mcondition;
//! a place to store previous report state
static std::stack<bool> Mprevious_report_state;
}; // class exception
} // namespace error
} // namespace tfxx
/*======================================================================*/
//
// preprocessor macros
// ===================
/*! \brief Check an assertion and report by throwing an exception
*
* \ingroup group_error, error_h
* \param C assert condition
* \param M message of type char*
* \param E exception class to throw
*/
#define TFXX_Xassert(C,M,E) \
if (!(C)) { throw( E ( M , __FILE__, __LINE__, #C )); }
/*! \brief Check an assertion and report by throwing an exception
*
* \ingroup group_error, error_h
* \param C assert condition
* \param M message of type char*
*/
#define TFXX_assert(C,M) TFXX_Xassert( C , M , tfxx::error::Exception )
/*! \brief Abort and give a message
*
* \ingroup group_error, error_h
* \param M message of type char*
* \param E exception class to throw
*/
#define TFXX_abort(M) \
throw( tfxx::error::Exception ( M , __FILE__, __LINE__ ))
#define TFXX_illegal TFXX_abort("illegal call!")
/*! \brief Check an assertion and report only.
*
* \ingroup group_error
* \param C assert condition
* \param M message of type char*
* \param V any values that should be output (comment)
* a sequence of values and output operators
*/
#define TFXX_report_assert(C,M,V) \
if (!(C)) { \
tfxx::report_violation(M, __FILE__, __LINE__, #C); \
std::cerr << "* comment: " << V << std::endl; \
std::cerr << std::endl; \
std::cerr.flush(); \
}
/*! \brief Macro to distinguish between fatal and non fatal assertions.
*
* \ingroup group_error
* \param F true for non fatal behaviour
* \param C assert condition
* \param M message of type char*
* \param V any values that should be output (comment)
* a sequence of values and output operators
*/
#define TFXX_nonfatal_assert(F,C,M,V) \
if (F) { TFXX_report_assert(C,M,V) } else { TFXX_assert(C,M) }
#endif // TF_ERROR_H_VERSION (includeguard)
/* ----- END OF error.h ----- */
/*! \file handle.h
* \brief provide a pointer with reference counting (prototypes)
*
* \addtogroup handle_h
* ----------------------------------------------------------------------------
*
* $Id$
* \author Thomas Forbriger
* \date 30/06/2005
*
* provide a pointer with reference counting (prototypes)
*
* Copyright (c) 2005 by Thomas Forbriger (BFO Schiltach)
*
* ----
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* ----
*
* REVISIONS and CHANGES
* - 30/06/2005 V1.0 Thomas Forbriger
*
* ============================================================================
*/
// include guard
#ifndef TF_HANDLE_H_VERSION
#define TF_HANDLE_H_VERSION \
"TF_HANDLE_H V1.0 "
#define TF_HANDLE_H_CVSID \
"$Id$"
namespace tfxx {
/*! \brief Interface provided through handle.h
*
* \defgroup handle_h Interface provided through handle.h
*/
/*@{*/
/** template #tfxx::THandle<class TObj>#:
*
* THandle is a class template published by B. Stroustrup in Chapter 25.7 of
* "The C++ programming Language" (3rd edition). This is a slightly modified
* version.
*
* Example:
* To create a handle #hs# to a string #s# and then output the string value
* you would code:
*
* #THandle<string> hs(new string(s)); cout << *hs << endl;#
*
* Thus the handle always represents a kind of pointer to your string. You may
* copy and pass this pointer as you like. And all these copies will allocated
* just memory for that pointer and any modification applied to the string
* will be accessible through every of these handles. However if the last
* handle is removed during program execution it will also remove the string
* representation from the memory. This is an convenient way to pass large
* objects without worrying about memory usage and management.
*
* It is not possible to create an empty handle - that wouldn't make any
* sense. You always have to provide another handle to copy from or an element
* to be handled.
*
* \sa tfxx::Handle
*
* @author Bjarne Stroustrup,
* @author Thomas Forbriger
* @version V1.0 (#$Revision: 1.6 $ $Date: 2006-03-28 18:14:15 $#)
* @memo template providing a handle to an object of any type
*/
template<class X>
class ConstHandle {
public:
typedef X Tmutableobject;
typedef const X Tobject;
typedef ConstHandle<Tobject> Tcontainer;
typedef Tcontainer Tcoc;
typedef Tcoc Tcontainer_of_const;
typedef const Tobject* Tpointer;
typedef const Tobject& Treference;
/** constructor #tfxx::THandle<class X>::THandle#:
*
* This constructor is used in the following way:
*
* #THandle<X> handle(X);#
*
* @memo init constructor
*/
ConstHandle(Treference p):
Mrep(new Tmutableobject(p)), Mpcount(new int(1)) { }
/*! Constructor to handle virtual base classes
*
* This constructor \b must be used in the following way:
*
* #THandle<X> handle(new X);#
*
* \note
* Do not pass any other pointer, since the destructor will call the
* delete operator on p.
*/
ConstHandle(Tmutableobject* p):
Mrep(p), Mpcount(new int(1)) { }
/** constructor #tfxx::THandle<class X>::THandle#:
*
* This constructor is used in the following way together with another
* #handle2# of type #THandle<X>#:
*
* #THandle<X> handle2=handle1;#
*
* @memo copy constructor
*/
ConstHandle(const ConstHandle& h): Mrep(h.Mrep), Mpcount(h.Mpcount)
{ (*Mpcount)++; }
/** desctructor #tfxx::THandle<class X>::~THandle#
* @memo book-keeping destructor
*/
~ConstHandle() { if (--(*Mpcount)==0) { delete Mrep; delete Mpcount; } }
/** operator #tfxx::THandle<class X>::operator->()#
* @return returns pointer to object of type #class X#
* @memo dereferencing operator
*/
Tpointer operator->() const { return Mrep; }
/** operator #tfxx::THandle<class X>::operator*()#
* @return returns reference to object of type #class X#
* @memo dereferencing operator
*/
Treference operator*() const { return *Mrep; }
/** operator #tfxx::THandle<class X>::operator=()#
* @memo book-keeping asignment operator
*/
ConstHandle& operator=(const ConstHandle& h)
{
if (Mrep == h.Mrep) return *this;
if (--(*Mpcount) == 0) { delete Mrep; delete Mpcount; }
Mrep=h.Mrep;
Mpcount=h.Mpcount;
(*Mpcount)++;
return *this;
}
protected:
//! expose the pointer to derived classes
Tmutableobject* pointer() const { return Mrep;}
//! expose the object to derived classes
Tmutableobject& reference() const { return *Mrep;}
private:
/// internal pointer to the handled object
Tmutableobject* Mrep;
/// usage counter
int* Mpcount;
}; // template class ConstHandle
/*! The handle class
*/
template<class X>
class Handle: public ConstHandle<X> {
public:
typedef X Tobject;
typedef ConstHandle<Tobject> Tbase;
typedef Handle<Tobject> Tcontainer;
typedef Tbase Tcoc;
typedef Tcoc Tcontainer_of_const;
typedef Tobject* Tpointer;
typedef Tobject& Treference;
/** constructor #tfxx::THandle<class X>::THandle#:
*
* This constructor is used in the following way:
*
* #THandle<X> handle(new X);#
*
* @memo init constructor
Handle(X* p): Mrep(p), Mpcount(new int(1)) { }
*/
Handle(const Treference p): Tbase(p) { }
/*! Constructor to handle virtual base classes
*
* This constructor \b must be used in the following way:
*
* #THandle<X> handle(new X);#
*
* \note
* Do not pass any other pointer, since the destructor will call the
* delete operator on p.
*/
Handle(Tpointer p): Tbase(p) { }
/** constructor #tfxx::THandle<class X>::THandle#:
*
* This constructor is used in the following way together with another
* #handle2# of type #THandle<X>#:
*
* #THandle<X> handle2=handle1;#
*
* @memo copy constructor
*/
Handle(const Handle& h): Tbase(h) { }
/** operator #tfxx::THandle<class X>::operator->()#
* @return returns pointer to object of type #class X#
* @memo dereferencing operator
*/
Tpointer operator->() const { return Tbase::pointer(); }
/** operator #tfxx::THandle<class X>::operator*()#
* @return returns reference to object of type #class X#
* @memo dereferencing operator
*/
Treference operator*() const { return Tbase::reference(); }
/** operator #tfxx::THandle<class X>::operator=()#
* @memo book-keeping asignment operator
*/
Handle& operator=(const Handle& h)
{
Tbase::operator=(h);
return *this;
}
}; // template class Handle
/*@}*/
} // namespace tfxx
#endif // TF_HANDLE_H_VERSION (includeguard)
/* ----- END OF handle.h ----- */
/*! \file range.h
* \brief deal with number ranges
*
* ----------------------------------------------------------------------------
*
* $Id$
* \author Thomas Forbriger
* \date 09/06/2002
*
* deal with number ranges
*
* Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt)
*
* ----
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* ----
*
* REVISIONS and CHANGES
* - 09/06/2002 V1.0 Thomas Forbriger
* - 27/11/2002 V1.1 copied and modified from libcontxx sources
* - 27/04/2009 V1.2 added range stepper
*
* ============================================================================
*/
// include guard
#ifndef TFXX_RANGE_H_VERSION
#define TFXX_RANGE_H_VERSION \
"TFXX_RANGE_H V1.2"
#define TFXX_RANGE_H_CVSID \
"$Id$"