- new file: implementation of SimGear general exception classes

This commit is contained in:
curt
2001-07-19 02:33:00 +00:00
parent 994a470fbf
commit a9cd67e0ce
2 changed files with 512 additions and 0 deletions

331
simgear/misc/exception.cxx Normal file
View File

@@ -0,0 +1,331 @@
// exception.cxx - implementation of SimGear base exceptions.
// Started Summer 2001 by David Megginson, david@megginson.com
// This code is released into the Public Domain.
//
// $Id$
#include "exception.hxx"
#include <stdio.h>
////////////////////////////////////////////////////////////////////////
// Implementation of sg_location class.
////////////////////////////////////////////////////////////////////////
sg_location::sg_location ()
: _path(""),
_line(-1),
_column(-1),
_byte(-1)
{
}
sg_location::sg_location (const string &path, int line, int column)
: _path(path),
_line(line),
_column(column),
_byte(-1)
{
}
sg_location::~sg_location ()
{
}
const string &
sg_location::getPath () const
{
return _path;
}
void
sg_location::setPath (const string &path)
{
_path = path;
}
int
sg_location::getLine () const
{
return _line;
}
void
sg_location::setLine (int line)
{
_line = line;
}
int
sg_location::getColumn () const
{
return _column;
}
void
sg_location::setColumn (int column)
{
_column = column;
}
int
sg_location::getByte () const
{
return _byte;
}
void
sg_location::setByte (int byte)
{
_byte = byte;
}
string
sg_location::asString () const
{
char buf[128];
string out = "";
if (_path != "") {
out += _path;
if (_line != -1 || _column != -1)
out += ",\n";
}
if (_line != -1) {
sprintf(buf, "line %d", _line);
out += buf;
if (_column != -1)
out += ",\n";
}
if (_column != -1) {
sprintf(buf, "column %d", _column);
out += buf;
}
return out;
}
////////////////////////////////////////////////////////////////////////
// Implementation of sg_throwable class.
////////////////////////////////////////////////////////////////////////
sg_throwable::sg_throwable ()
: _message(""),
_origin("")
{
}
sg_throwable::sg_throwable (const string &message, const string &origin)
: _message(message),
_origin(origin)
{
}
sg_throwable::~sg_throwable ()
{
}
const string &
sg_throwable::getMessage () const
{
return _message;
}
void
sg_throwable::setMessage (const string &message)
{
_message = message;
}
const string &
sg_throwable::getOrigin () const
{
return _origin;
}
void
sg_throwable::setOrigin (const string &origin)
{
_origin = origin;
}
sg_throwable *
sg_throwable::clone () const
{
return new sg_throwable(getMessage(), getOrigin());
}
////////////////////////////////////////////////////////////////////////
// Implementation of sg_error class.
////////////////////////////////////////////////////////////////////////
sg_error::sg_error ()
: sg_throwable ()
{
}
sg_error::sg_error (const string &message, const string &origin)
: sg_throwable(message, origin)
{
}
sg_error::~sg_error ()
{
}
sg_error *
sg_error::clone () const
{
return new sg_error(getMessage(), getOrigin());
}
////////////////////////////////////////////////////////////////////////
// Implementation of sg_exception class.
////////////////////////////////////////////////////////////////////////
sg_exception::sg_exception ()
: sg_throwable ()
{
}
sg_exception::sg_exception (const string &message, const string &origin)
: sg_throwable(message, origin)
{
}
sg_exception::~sg_exception ()
{
}
sg_exception *
sg_exception::clone () const
{
return new sg_exception(getMessage(), getOrigin());
}
////////////////////////////////////////////////////////////////////////
// Implementation of sg_io_exception.
////////////////////////////////////////////////////////////////////////
sg_io_exception::sg_io_exception ()
: sg_exception()
{
}
sg_io_exception::sg_io_exception (const string &message, const string &origin)
: sg_exception(message, origin)
{
}
sg_io_exception::sg_io_exception (const string &message,
const sg_location &location,
const string &origin)
: sg_exception(message, origin),
_location(location)
{
}
sg_io_exception::~sg_io_exception ()
{
}
const sg_location &
sg_io_exception::getLocation () const
{
return _location;
}
void
sg_io_exception::setLocation (const sg_location &location)
{
_location = location;
}
sg_io_exception *
sg_io_exception::clone () const
{
return new sg_io_exception(getMessage(), getLocation(), getOrigin());
}
////////////////////////////////////////////////////////////////////////
// Implementation of sg_format_exception.
////////////////////////////////////////////////////////////////////////
sg_format_exception::sg_format_exception ()
: sg_exception(),
_text("")
{
}
sg_format_exception::sg_format_exception (const string &message,
const string &text,
const string &origin)
: sg_exception(message, origin),
_text(text)
{
}
sg_format_exception::~sg_format_exception ()
{
}
const string &
sg_format_exception::getText () const
{
return _text;
}
void
sg_format_exception::setText (const string &text)
{
_text = text;
}
sg_format_exception *
sg_format_exception::clone () const
{
return new sg_format_exception(getMessage(), getText(), getOrigin());
}
////////////////////////////////////////////////////////////////////////
// Implementation of sg_range_exception.
////////////////////////////////////////////////////////////////////////
sg_range_exception::sg_range_exception ()
: sg_exception()
{
}
sg_range_exception::sg_range_exception (const string &message,
const string &origin)
: sg_exception(message, origin)
{
}
sg_range_exception::~sg_range_exception ()
{
}
sg_range_exception *
sg_range_exception::clone () const
{
return new sg_range_exception(getMessage(), getOrigin());
}
// end of exception.cxx

181
simgear/misc/exception.hxx Normal file
View File

@@ -0,0 +1,181 @@
/**
* \file exception.hxx
* Interface definition for SimGear base exceptions.
* Started Spring 2001 by David Megginson, david@megginson.com
* This code is released into the Public Domain.
*
* $Id$
*/
#include <simgear/compiler.h>
#include STL_STRING
SG_USING_STD(string);
/**
* Information encapsulating a single location in an external resource
*
* A position in the resource my optionally be provided, either by
* line number, line number and column number, or byte offset from the
* beginning of the resource.
*/
class sg_location
{
public:
sg_location ();
sg_location (const string &path, int line = -1, int column = -1);
virtual ~sg_location ();
virtual const string &getPath () const;
virtual void setPath (const string &path);
virtual int getLine () const;
virtual void setLine (int line);
virtual int getColumn () const;
virtual void setColumn (int column);
virtual int getByte () const;
virtual void setByte (int byte);
virtual string asString () const;
private:
string _path;
int _line;
int _column;
int _byte;
};
/**
* Abstract base class for all throwables.
*/
class sg_throwable
{
public:
sg_throwable ();
sg_throwable (const string &message, const string &origin = "");
virtual ~sg_throwable ();
virtual const string &getMessage () const;
virtual void setMessage (const string &message);
virtual const string &getOrigin () const;
virtual void setOrigin (const string &origin);
virtual sg_throwable * clone () const;
private:
string _message;
string _origin;
};
/**
* An unexpected fatal error.
*
* Methods and functions show throw this exception when something
* very bad has happened (such as memory corruption or
* a totally unexpected internal value). Applications should catch
* this exception only at the top level if at all, and should
* normally terminate execution soon afterwards.
*/
class sg_error : public sg_throwable
{
public:
sg_error ();
sg_error (const string &message, const string &origin = "");
virtual ~sg_error ();
virtual sg_error * clone () const;
};
/**
* Base class for all SimGear exceptions.
*
* SimGear-based code should throw this exception only when no
* more specific exception applies. It may not be caught until
* higher up in the application, where it is not possible to
* resume normal operations if desired.
*
* A caller can catch sg_exception by default to ensure that
* all exceptions are caught. Every SimGear exception can contain
* a human-readable error message and a human-readable string
* indicating the part of the application causing the exception
* (as an aid to debugging, only).
*/
class sg_exception : public sg_throwable
{
public:
sg_exception ();
sg_exception (const string &message, const string &origin = "");
virtual ~sg_exception ();
virtual sg_exception * clone () const;
};
/**
* An I/O-related SimGear exception.
*
* SimGear-based code should throw this exception when it fails
* to read from or write to an external resource, such as a file,
* socket, URL, or database.
*
* In addition to the functionality of sg_exception, an
* sg_io_exception may contain location information, such as the name
* of a file or URL, and possible also a location in that file or URL.
*/
class sg_io_exception : public sg_exception
{
public:
sg_io_exception ();
sg_io_exception (const string &message, const string &origin = "");
sg_io_exception (const string &message, const sg_location &location,
const string &origin = "");
virtual ~sg_io_exception ();
virtual const sg_location &getLocation () const;
virtual void setLocation (const sg_location &location);
virtual sg_io_exception * clone () const;
private:
sg_location _location;
};
/**
* A format-related SimGear exception.
*
* SimGear-based code should throw this exception when a string
* does not appear in the expected format (for example, a date
* string does not conform to ISO 8601).
*
* In addition to the functionality of sg_exception, an
* sg_format_exception can contain a copy of the original malformated
* text.
*/
class sg_format_exception : public sg_exception
{
public:
sg_format_exception ();
sg_format_exception (const string &message, const string &text,
const string &origin = "");
virtual ~sg_format_exception ();
virtual const string &getText () const;
virtual void setText (const string &text);
virtual sg_format_exception * clone () const;
private:
string _text;
};
/**
* A range-related SimGear exception.
*
* SimGear-based code should throw this exception when a value falls
* outside the range where it can reasonably be handled; examples
* include longitude outside the range -180:180, unrealistically high
* forces or velocities, an illegal airport code, etc. A range
* exception usually means that something has gone wrong internally.
*/
class sg_range_exception : public sg_exception
{
public:
sg_range_exception ();
sg_range_exception (const string &message, const string &origin = "");
virtual ~sg_range_exception ();
virtual sg_range_exception * clone () const;
};
// end of exception.hxx