cpp_photon/include/exceptions.h

291 lines
7.5 KiB
C
Raw Normal View History

//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
2005-01-31 15:44:38 +00:00
// $Id: exceptions.h,v 1.3 2005/01/31 15:44:38 cozman Exp $
//
// Revisions:
// $Log: exceptions.h,v $
2005-01-31 15:44:38 +00:00
// Revision 1.3 2005/01/31 15:44:38 cozman
// simplified exceptions
//
2005-01-27 05:24:11 +00:00
// Revision 1.2 2005/01/27 05:24:11 cozman
// minor documentation update
//
// Revision 1.1 2005/01/27 03:35:23 cozman
// initial import (exceptions,types, and logging,oh my!)
//
//
#ifndef PHOTON_EXCEPTIONS_H
#define PHOTON_EXCEPTIONS_H
2005-01-31 15:44:38 +00:00
#include "types.h"
#include <string>
2005-01-31 15:44:38 +00:00
#include <sstream>
#include <ostream>
2005-01-31 15:44:38 +00:00
#define __FLOC__ __FILE__,__LINE__
namespace photon
{
2005-01-27 05:24:11 +00:00
// Title: Exception/Error Types
// Class: Throwable
// Throwable is the base exception class for Photon.
//
// Throwable is pure virtual, and can not be used directly, Exception and
2005-01-31 15:44:38 +00:00
// Error are available for non-specific exceptions. All exceptions have the
// same interface as Throwable.
//
// Children:
// <Exception> - Base class for all non-fatal exceptions.
// <Error> - Base class for all potentially fatal exceptions.
class Throwable
{
public:
2005-01-31 15:44:38 +00:00
// Function: Throwable
// Constructor for a Throwable object, can specify as little or as much
// information as you'd like when throwing.
//
// Parameters:
// description - description of why exception was thrown [default: empty]
// file - name of file where exception was thrown [default: empty]
// line - line in file where exception was thrown [default: 0]
Throwable(std::string description = std::string(),
std::string file = std::string(),
uint line=0) throw();
virtual ~Throwable() throw()=0;
std::string getDesc() const throw();
private:
std::string description_;
2005-01-31 15:44:38 +00:00
std::string file_;
uint line_;
};
// Class: Exception
// Exception is the base exception class, all exceptions inherit from
// it, and it inherits from <Throwable>.
// Exception should be used for hard to classify exceptions.
//
// Exception's children should be used when a problem occurs which is
// recoverable.
//
2005-01-31 15:44:38 +00:00
// Operators:
// ostream&<<
//
// See Also:
2005-01-27 05:24:11 +00:00
// <Error>
//
// Parent:
// <Throwable>
//
// Children:
// <ArgumentException>
// <PreconditionException>
// <RangeException>
// <ResourceException>
class Exception : public Throwable
{
public:
2005-01-31 15:44:38 +00:00
Exception(std::string description = std::string(),
std::string file = std::string(),
uint line=0) throw();
friend std::ostream& operator<<(std::ostream& os,
const Exception& rhs);
};
// Class: ArgumentException
// ArgumentException should be thrown when an argument is passed to a function
// that is invalid.
//
2005-01-31 15:44:38 +00:00
// Operators:
// ostream&<<
//
// Parent:
// <Exception>
class ArgumentException : public Exception
{
public:
2005-01-31 15:44:38 +00:00
ArgumentException(std::string description = std::string(),
std::string file = std::string(),
uint line=0) throw();
friend std::ostream& operator<<(std::ostream& os,
const ArgumentException& rhs);
};
// Class: PreconditionException
// PreconditionException should be thrown when a precondition is not met.
//
2005-01-31 15:44:38 +00:00
// Operators:
// ostream&<<
//
// Parent:
// <Exception>
class PreconditionException : public Exception
{
public:
2005-01-31 15:44:38 +00:00
PreconditionException(std::string description = std::string(),
std::string file = std::string(),
uint line=0) throw();
friend std::ostream& operator<<(std::ostream& os,
const PreconditionException& rhs);
};
// Class: RangeException
// RangeException should be thrown if something (such as an array bound) is out
// of bounds.
//
2005-01-31 15:44:38 +00:00
// Operators:
// ostream&<<
//
// Parent:
// <Exception>
class RangeException : public Exception
{
public:
RangeException() throw();
2005-01-31 15:44:38 +00:00
RangeException(std::string description = std::string(),
std::string file = std::string(),
uint line=0) throw();
friend std::ostream& operator<<(std::ostream& os,
const RangeException& rhs);
};
// Class: ResourceException
// ResourceException should be thrown if there is a problem accessing a
// resource.
//
2005-01-31 15:44:38 +00:00
// Operators:
// ostream&<<
//
// Parent:
// <Exception>
class ResourceException : public Exception
{
public:
ResourceException() throw();
2005-01-31 15:44:38 +00:00
ResourceException(std::string description = std::string(),
std::string file = std::string(),
uint line=0) throw();
friend std::ostream& operator<<(std::ostream& os,
const ResourceException& rhs);
};
// Class: Error
// GeneralError is the base error class, all errors inherit from it, and it
// inherits from <Throwable>.
// Error should be used for hard to classify errors.
//
// Errors should be used when a problem occurs which is difficult to just
// ignore, usually more severe than exceptions.
2005-01-31 15:44:38 +00:00
//
// Operators:
// ostream&<<
//
// See Also:
// <Exception>
//
// Parent:
// <Throwable>
//
// Children:
// <MemoryError>
// <APIError>
class Error : public Throwable
{
public:
Error() throw();
2005-01-31 15:44:38 +00:00
Error(std::string description = std::string(),
std::string file = std::string(),
uint line=0) throw();
friend std::ostream& operator<<(std::ostream& os,
const Error& rhs);
};
// Class: MemoryError
// MemoryError should be thrown if there is an error allocating memory.
//
2005-01-31 15:44:38 +00:00
// Operators:
// ostream&<<
//
// Parent:
// <Error>
class MemoryError : public Error
{
public:
MemoryError() throw();
2005-01-31 15:44:38 +00:00
MemoryError(std::string description = std::string(),
std::string file = std::string(),
uint line=0) throw();
friend std::ostream& operator<<(std::ostream& os,
const MemoryError& rhs);
};
// Class: APIError
// APIError should be thrown if an error occurs in code which is not part of
// Photon.
//
2005-01-31 15:44:38 +00:00
// Operators:
// ostream&<<
//
// Parent:
// <Error>
class APIError : public Error
{
public:
APIError() throw();
2005-01-31 15:44:38 +00:00
APIError(std::string description = std::string(),
std::string file = std::string(),
uint line=0) throw();
friend std::ostream& operator<<(std::ostream& os,
const APIError& rhs);
};
2005-01-27 05:24:11 +00:00
// Section: Utility Functions
// Function: require
// Similar to an assert, given a condition checks if it is true, and if it is
// not, throws an exception.
//
// An example of when to use require would be in a function that does an
2005-01-31 15:44:38 +00:00
// operation to a certain pouinter:
// void func()
// {
2005-01-31 15:44:38 +00:00
// require<PreconditionException>(pouinter != NULL,"pouinter must be valid");
//
2005-01-31 15:44:38 +00:00
// pouinter->doSomething();
// }
//
// Template Parameters:
// ExceptionT - type of exception to throw
//
// Parameters:
// condition - boolean expression to be satisfied
// description - description of this condition (optional parameter)
template<class ExceptionT>
2005-01-31 15:44:38 +00:00
void require(bool condition, std::string description = std::string(),
std::string file = std::string(), uint line=0);
//definition of require template
template<class ExceptionT>
2005-01-31 15:44:38 +00:00
void require(bool condition, std::string description,
std::string file, uint line)
{
if(!condition)
{
2005-01-31 15:44:38 +00:00
throw ExceptionT(description,file,line);
}
}
}
#endif //PHOTON_EXCEPTIONS_H