Commit 664a39c5 authored by thomas.forbriger's avatar thomas.forbriger
Browse files

libdatrwxx [WP][API]: move exception class to separate source files

Place excetion class in separate header and compilation unit.

The library shall produce more comprehensible error output. To achieve this,
it will be necessary to compile error message string by writing to string
streams and passing the resulting stream to a reporting function or exception.
All preprocessor macros which will make use of string streams are placed in
error.h and are used only internally in the library. A library user, however,
might need to access the definition of the execption class in case he likes to
catch and handle exceptions. The only way to keep the string stream overhead
away from users is to place the declaration of the exception class in a
separate header. This only confronts users with the additional definition of
strings, which is necessary for exceptions to work as intended.
parent 8e3a1607
......@@ -35,11 +35,13 @@
* - operate with string containers rather than with
* pointer to character arrays
* - revise error output
* - 07/07/2016 V1.5 thof: move class exception to separate compilation
* unit
*
* ============================================================================
*/
#define DATRW_ERROR_CC_VERSION \
"DATRW_ERROR_CC V1.4"
"DATRW_ERROR_CC V1.5"
#include <iostream>
#include <datrwxx/error.h>
......@@ -51,85 +53,6 @@ using std::endl;
namespace datrw {
//! initialize and instantiate
bool Exception::Mreport_on_construct=true;
//! construct from nothing
Exception::Exception():
Mmessage(0), Mfile(0), Mline(0), Mcondition(0)
{ if (Mreport_on_construct) { report(); } }
//! construct with message
Exception::Exception(const std::string& message):
Mmessage(message), Mfile(0), Mline(0), Mcondition(0)
{ if (Mreport_on_construct) { report(); } }
//! construct with message and file info
Exception::Exception(const std::string& message,
const std::string& condition):
Mmessage(message), Mfile(0), Mline(0), Mcondition(condition)
{ if (Mreport_on_construct) { report(); } }
//! construct with message and file info
Exception::Exception(const std::string& message,
const std::string& file,
const int& line):
Mmessage(message), Mfile(file), Mline(line), Mcondition(0)
{ if (Mreport_on_construct) { report(); } }
//! construct with message and file info and condition
Exception::Exception(const std::string& message,
const std::string& file,
const int& line,
const std::string& condition):
Mmessage(message), Mfile(file), Mline(line), Mcondition(condition)
{ if (Mreport_on_construct) { report(); } }
//! switch on
void Exception::report_on_construct()
{
Mreport_on_construct=true;
}
//! switch off
void Exception::dont_report_on_construct()
{
Mreport_on_construct=false;
}
//! report
void Exception::report() const
{
base_report();
}
//! report
void Exception::base_report() const
{
cerr << "ERROR message from " << datrw::libversion << ":" << endl;
cerr << " A problem occured while "
"reading or writing time series data!" << endl;
if (Mmessage.empty())
{
cerr << " No message" << endl;
}
else
{
cerr << " " << Mmessage << endl;
}
if (!Mcondition.empty())
{
cerr << " Violation of condition" << endl
<< " \"" << Mcondition << "\"" << endl;
}
if (!Mfile.empty())
{
cerr << " triggered in \"" << Mfile << "\" at line #" << Mline << endl;
}
}
/*----------------------------------------------------------------------*/
//! report violation of assertion
void report_violation(const std::string& message,
const std::string& file,
......
......@@ -35,6 +35,7 @@
* provide warning macro
* - 05/07/2016 V1.5 thof: operate with string containers rather than
* with pointer to character arrays
* - 07/07/2016 V1.6 thof: move class exception to separate header
*
* ============================================================================
*/
......@@ -43,79 +44,16 @@
#ifndef DATRW_ERROR_H_VERSION
#define DATRW_ERROR_H_VERSION \
"DATRW_ERROR_H V1.5"
"DATRW_ERROR_H V1.6"
#include <iostream>
#include <string>
#include <datrwxx/exception.h>
namespace datrw {
/*! \defgroup group_error Internal utility: Error handling module
*/
/*! \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 out the message during object
* initialization. If you don't like this, call dont_report_on_construct().
*
* \ingroup group_error
* \sa DATRW_Xassert
* \sa DATRW_assert
* \sa DATRW_abort
*/
class Exception
{
public:
//! Creates exception with no explaning comments
Exception();
//! Creates an exception with an explanation message
Exception(const std::string& message);
//! Creates an exception with message and failed assertion
Exception(const std::string& message,
const std::string& condition);
//! Create with message, failed assertion, and code position
Exception(const std::string& message,
const std::string& file,
const int& line,
const std::string& condition);
//! Create with message and code position
Exception(const std::string& message,
const std::string& 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();
//! set report on construct flag
static void report_on_construct_flag(const bool& flag)
{ Mreport_on_construct=flag; }
//! return report on construct flag
static bool report_on_construct_flag() { return(Mreport_on_construct); }
protected:
//! Screen report
void base_report() const;
private:
//! Shall we print to cerr at construction time?
static bool Mreport_on_construct;
//! pointer to message string
std::string Mmessage;
//! pointer to file name string
std::string Mfile;
//! pointer to line number in source file
const int& Mline;
//! pointer to assertion condition text string
std::string Mcondition;
}; // class Exception
/*! \brief report violation of condition
*
* \ingroup group_error
......
/*! \file exception.cc
* \brief libdatrwxx exception class (implementation)
*
* ----------------------------------------------------------------------------
*
* \author Thomas Forbriger
* \date 07/07/2016
*
* libdatrwxx exception class (implementation)
*
* Copyright (c) 2016 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, see <http://www.gnu.org/licenses/>.
* ----
*
* REVISIONS and CHANGES
* - 07/07/2016 V1.0 Thomas Forbriger
*
* ============================================================================
*/
#define DATRW_EXCEPTION_CC_VERSION \
"DATRW_EXCEPTION_CC V1.0"
#include <iostream>
#include <datrwxx/exception.h>
#include <datrwxx/aalibdatrwxx.h>
#include <datrwxx/util.h>
using std::cerr;
using std::endl;
namespace datrw {
//! initialize and instantiate
bool Exception::Mreport_on_construct=true;
//! construct from nothing
Exception::Exception():
Mmessage(0), Mfile(0), Mline(0), Mcondition(0)
{ if (Mreport_on_construct) { report(); } }
//! construct with message
Exception::Exception(const std::string& message):
Mmessage(message), Mfile(0), Mline(0), Mcondition(0)
{ if (Mreport_on_construct) { report(); } }
//! construct with message and file info
Exception::Exception(const std::string& message,
const std::string& condition):
Mmessage(message), Mfile(0), Mline(0), Mcondition(condition)
{ if (Mreport_on_construct) { report(); } }
//! construct with message and file info
Exception::Exception(const std::string& message,
const std::string& file,
const int& line):
Mmessage(message), Mfile(file), Mline(line), Mcondition(0)
{ if (Mreport_on_construct) { report(); } }
//! construct with message and file info and condition
Exception::Exception(const std::string& message,
const std::string& file,
const int& line,
const std::string& condition):
Mmessage(message), Mfile(file), Mline(line), Mcondition(condition)
{ if (Mreport_on_construct) { report(); } }
//! switch on
void Exception::report_on_construct()
{
Mreport_on_construct=true;
}
//! switch off
void Exception::dont_report_on_construct()
{
Mreport_on_construct=false;
}
//! report
void Exception::report() const
{
base_report();
}
//! report
void Exception::base_report() const
{
cerr << "ERROR message from " << datrw::libversion << ":" << endl;
cerr << " A problem occured while "
"reading or writing time series data!" << endl;
if (Mmessage.empty())
{
cerr << " No message" << endl;
}
else
{
cerr << " " << Mmessage << endl;
}
if (!Mcondition.empty())
{
cerr << " Violation of condition" << endl
<< " \"" << Mcondition << "\"" << endl;
}
if (!Mfile.empty())
{
cerr << " triggered in \"" << Mfile << "\" at line #" << Mline << endl;
}
}
} // namespace datrw
/* ----- END OF exception.cc ----- */
/*! \file exception.h
* \brief libdatrwxx exception class (prototypes)
*
* ----------------------------------------------------------------------------
*
* \author Thomas Forbriger
* \date 07/07/2016
*
* libdatrwxx exception class (prototypes)
*
* \ingroup group_error
*
* Copyright (c) 2016 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, see <http://www.gnu.org/licenses/>.
* ----
*
* REVISIONS and CHANGES
* - 07/07/2016 V1.0 Thomas Forbriger
*
* ============================================================================
*/
// include guard
#ifndef DATRW_EXCEPTION_H_VERSION
#define DATRW_EXCEPTION_H_VERSION \
"DATRW_EXCEPTION_H V1.0"
#include <string>
namespace datrw {
/*! \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 out the message during object
* initialization. If you don't like this, call dont_report_on_construct().
*
* \ingroup group_error
* \sa DATRW_Xassert
* \sa DATRW_assert
* \sa DATRW_abort
*/
class Exception
{
public:
//! Creates exception with no explaning comments
Exception();
//! Creates an exception with an explanation message
Exception(const std::string& message);
//! Creates an exception with message and failed assertion
Exception(const std::string& message,
const std::string& condition);
//! Create with message, failed assertion, and code position
Exception(const std::string& message,
const std::string& file,
const int& line,
const std::string& condition);
//! Create with message and code position
Exception(const std::string& message,
const std::string& 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();
//! set report on construct flag
static void report_on_construct_flag(const bool& flag)
{ Mreport_on_construct=flag; }
//! return report on construct flag
static bool report_on_construct_flag() { return(Mreport_on_construct); }
protected:
//! Screen report
void base_report() const;
private:
//! Shall we print to cerr at construction time?
static bool Mreport_on_construct;
//! pointer to message string
std::string Mmessage;
//! pointer to file name string
std::string Mfile;
//! pointer to line number in source file
const int& Mline;
//! pointer to assertion condition text string
std::string Mcondition;
}; // class Exception
} // namespace datrw
#endif // DATRW_EXCEPTION_H_VERSION (includeguard)
/* ----- END OF exception.h ----- */
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment