Re: [Alephmodular-devel] On errors
Status: Pre-Alpha
Brought to you by:
brefin
|
From: Br'fin <br...@ma...> - 2003-02-01 19:38:45
|
And still more thinking on errors, but more details too, I'm liking the
approach below:
///////////////////////////////////////////////////////////////////////
//
// $Id: CErrror.h $
///////////////////////////////////////////////////////////////////////
//
/*
* CErrror.h
* AlephModular
*
* CError defines an interface for classes that can error an go into a
* bad state. For instance, opening a file fails so you need to report
* this back to the calling function.
*
* The basic method this supports is the following:
* A class has a method this_may_fail that returns a boolean (If the
* method needs to return something meaningful like an opened file
* reference, then this is done with out arguments passed by
* reference) If the method is successful, then it returns true.
* Otherwise it returns false and puts the class into a failed state.
* You may get a class's current state to report why an operation
* failed.
*
* Created by Br'fin on Thu Jan 30 2003.
*
*/
#ifndef __CERROR_H
#define __CERROR_H
#include "cseries.h"
#include <string>
class CError;
/*
* IFailable
* Interface
*
* This is an interface to be supported by all classes that may fail.
* For instance, opening a file may fail.
*
*/
class IFailable
{
protected:
/*
* set_error
* arguments:
* an explanatory member of CError that explains the current error
*/
virtual void set_error(const CError &) = 0;
public:
/*
* is_good
* returns:
* true if and only if there is no error state on the object
*/
virtual bool is_good() const = 0;
/*
* get_error
* arguments:
* returns the objects's current error state.
*/
virtual void get_error(const CError &) const = 0;
/*
* clear_error
* This attempts to clear the error state of the object. This
* is not guarunteed to succeed.
* returns:
* true if the error can be cleared. False otherwise
*/
virtual bool clear_error() = 0;
virtual ~IFailable() {} // Required virtual destructor
};
/*
* -Jeremy
* We could go with all sort of subclasses and introspection and
details.
* However, that gets too convoluted for our needs. Like hitting a nail
* with a pile driver. And doesn't sit right with me.
*
* CError describes an error that occured in one part of the system.
*
* If the error occured in some part of the operating system, then the
* error's type should be systemError and the err_code can be an OS
* error code. But should conform to an agreed upon external code.
* For instance a file not found value.
*
* On the other hand, a gameError is tied to a state that AM must
* report to the user. And in many cases, one system turning up
* a systemError will have that error inspire a gameError to
* display to the user.
*
* Why this disjoint? Well the gui should be familiar with what
* gameErrors there are, and have access to appropriate resources
* and localization information to display them to the user.
*
* So, what is a CError exactly?
* It is a type of error with a given code. As explained above. And
* allows a string of details to be provided. There is no gauruntee
* that the details will be displayed.
*/
class CError
{
public:
typedef enum { /* types */
systemError,
gameError
} type;
typedef enum { /* Game Errors */
errNone= 0,
errMapFileNotSet,
errIndexOutOfRange,
errTooManyOpenFiles,
errUnknownWadVersion,
errWadIndexOutOfRange,
errServerDied,
errUnsyncOnLevelChange,
NUMBER_OF_GAME_ERRORS
} game_errors;
private:
type err_type;
int32 err_code;
std::string details;
public:
CError(type _type, int32 _code, std::string _details = "") :
err_type(_type), err_code(_code), details(_details)
{
#ifdef DEBUG
if(err_type==gameError) assert(err_code>=0 &&
err_code<NUMBER_OF_GAME_ERRORS);
#endif
}
CError(const CError& err) :
err_type(err.err_type), err_code(err.err_code), details(err.details)
{}
CError& operator=(const CError err)
{
err_type = err.err_type;
err_code = err.err_code;
details = err.details;
return *this;
}
type
get_type() { return err_type; }
int32
get_code() { return err_code; }
void
get_details(std::string& _string) { _string = details; }
bool
has_details() { return details.length() > 0; }
};
#endif
|