summaryrefslogtreecommitdiff
path: root/src/exceptionbase.h
blob: b6ad9ca8320a13489982ca7b24c73f3deb002880 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef BU_EXCEPTION_BASE_H
#define BU_EXCEPTION_BASE_H

#include <exception>
#include <stdarg.h>

// This shouldn't normally be defined here, I don't think it's all that portable
// and it also changes the class interface, we should find out how much of
// an issue that is, we could just put in an empty getBacktrace() function for
// when you don't have support for it...

namespace Bu
{
	/**
	 * A generalized Exception base class.  This is nice for making general and
	 * flexible child classes that can create new error code classes.
	 *
	 * In order to create your own exception class use these two lines.
	 * 
	 * in your header:  subExceptionDecl( NewClassName );
	 * 
	 * in your source:  subExcpetienDef( NewClassName );
	 */
	class ExceptionBase : public std::exception
	{
	public:
		/**
		 * Construct an exception with an error code of zero, but with a
		 * description.  The use of this is not reccomended most of the time,
		 * it's generally best to include an error code with the exception so
		 * your program can handle the exception in a better way.
		 * @param sFormat The format of the text.  See printf for more info.
		 */
		ExceptionBase( const char *sFormat, ... ) throw();
		
		/**
		 * 
		 * @param nCode 
		 * @param sFormat
		 */
		ExceptionBase( int nCode, const char *sFormat, ... ) throw();
		
		/**
		 * 
		 * @param nCode 
		 * @return 
		 */
		ExceptionBase( int nCode=0 ) throw();

		ExceptionBase( const ExceptionBase &e ) throw ();
		
		/**
		 * 
		 * @return 
		 */
		virtual ~ExceptionBase() throw();

		/**
		 * 
		 * @return 
		 */
		virtual const char *what() const throw();
		
		/**
		 * 
		 * @return 
		 */
		int getErrorCode();
		
		/**
		 * 
		 * @param lpFormat 
		 * @param vargs 
		 */
		void setWhat( const char *lpFormat, va_list &vargs );

		/**
		 *
		 * @param lpText
		 */
		void setWhat( const char *lpText );

	private:
		int nErrorCode;	/**< The code for the error that occured. */
		char *sWhat;	/**< The text string telling people what went wrong. */
	};
}

#define subExceptionDecl( name )											\
class name : public Bu::ExceptionBase											\
{																			\
	public:																	\
		name( const char *sFormat, ... ) throw ();							\
		name( int nCode, const char *sFormat, ... ) throw();				\
		name( int nCode=0 ) throw ();										\
		name( const name &e ) throw ();										\
};

#define subExceptionDeclChild( name, parent )								\
class name : public parent													\
{																			\
	public:																	\
		name( const char *sFormat, ... ) throw ();							\
		name( int nCode, const char *sFormat, ... ) throw();				\
		name( int nCode=0 ) throw ();										\
		name( const name &e ) throw ();										\
};

#define subExceptionDeclBegin( name )										\
class name : public Bu::ExceptionBase										\
{																			\
	public:																	\
		name( const char *sFormat, ... ) throw ();							\
		name( int nCode, const char *sFormat, ... ) throw();				\
		name( int nCode=0 ) throw ();										\
		name( const name &e ) throw ();

#define subExceptionDeclEnd()												\
};

#define subExceptionDef( name )												\
name::name( const char *lpFormat, ... ) throw() :							\
	ExceptionBase( 0 )														\
{																			\
	va_list ap;																\
	va_start( ap, lpFormat );												\
	setWhat( lpFormat, ap );												\
	va_end( ap );															\
}																			\
name::name( int nCode, const char *lpFormat, ... ) throw() :				\
	ExceptionBase( nCode )													\
{																			\
	va_list ap;																\
	va_start( ap, lpFormat );												\
	setWhat( lpFormat, ap );												\
	va_end( ap );															\
}																			\
name::name( int nCode ) throw() :											\
	ExceptionBase( nCode )													\
{																			\
}																			\
name::name( const name &e ) throw() :										\
	ExceptionBase( e )														\
{																			\
}

#define subExceptionDefChild( name, parent )								\
name::name( const char *lpFormat, ... ) throw() :							\
	parent( 0 )																\
{																			\
	va_list ap;																\
	va_start( ap, lpFormat );												\
	setWhat( lpFormat, ap );												\
	va_end( ap );															\
}																			\
name::name( int nCode, const char *lpFormat, ... ) throw() :				\
	parent( nCode )															\
{																			\
	va_list ap;																\
	va_start( ap, lpFormat );												\
	setWhat( lpFormat, ap );												\
	va_end( ap );															\
}																			\
name::name( int nCode ) throw() :											\
	parent( nCode )															\
{																			\
}																			\
name::name( const name &e ) throw() :										\
	ExceptionBase( e )														\
{																			\
}

namespace Bu
{
	// Exceptions that are so general they could be used anywhere go here.
	class UnsupportedException : public Bu::ExceptionBase
	{
	public:
		UnsupportedException() throw ();
	};
}

#endif