2
* PROGRAM: Firebird exceptions classes
3
* MODULE: fb_exception.h
4
* DESCRIPTION: Firebird's exception classes
6
* The contents of this file are subject to the Initial
7
* Developer's Public License Version 1.0 (the "License");
8
* you may not use this file except in compliance with the
9
* License. You may obtain a copy of the License at
10
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
12
* Software distributed under the License is distributed AS IS,
13
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
14
* See the License for the specific language governing rights
15
* and limitations under the License.
17
* The Original Code was created by Mike Nordell
18
* for the Firebird Open Source RDBMS project.
20
* Copyright (c) 2001 Mike Nordell <tamlin at algonet.se>
21
* and all contributors signed below.
23
* All Rights Reserved.
24
* Contributor(s): ______________________________________.
30
#ifndef FB_EXCEPTION_H
31
#define FB_EXCEPTION_H
41
virtual char* alloc(const char* string, size_t length) = 0;
42
virtual ~StringsBuffer() {}
44
void makePermanentVector(ISC_STATUS* perm, const ISC_STATUS* temp);
47
template <size_t BUFFER_SIZE>
48
class CircularStringsBuffer : public StringsBuffer {
50
CircularStringsBuffer() throw() {
51
// This is to ensure we have zero at the end of buffer in case of buffer overflow
52
memset(buffer, 0, BUFFER_SIZE);
55
virtual char* alloc(const char* string, size_t length) {
56
// fb_assert(length + 1 < BUFFER_SIZE);
57
// If there isn't any more room in the buffer, start at the beginning again
58
if (buffer_ptr + length + 1 > buffer + BUFFER_SIZE)
60
char* new_string = buffer_ptr;
61
memcpy(new_string, string, length);
62
new_string[length] = 0;
63
buffer_ptr += length + 1;
67
char buffer[BUFFER_SIZE];
74
Exception() throw() { }
76
virtual ~Exception() throw() { }
77
virtual ISC_STATUS stuff_exception(ISC_STATUS* const status_vector, StringsBuffer* sb = NULL) const throw() = 0;
78
virtual const char* what() const throw() = 0;
81
// Used as jmpbuf to unwind when needed
82
class LongJump : public Exception
85
virtual ISC_STATUS stuff_exception(ISC_STATUS* const status_vector, StringsBuffer* sb = NULL) const throw();
86
virtual const char* what() const throw() { return "Firebird::LongJump"; }
88
LongJump() throw() : Exception() { }
92
class BadAlloc : public Exception
95
virtual ISC_STATUS stuff_exception(ISC_STATUS* const status_vector, StringsBuffer* sb = NULL) const throw();
96
virtual const char* what() const throw() { return "Firebird::BadAlloc"; }
98
BadAlloc() throw() : Exception() { }
101
// Main exception class in firebird
102
class status_exception : public Exception
105
// This version of constructor receives status vector pointing to permanent or
106
// temp strings, depending upon second parameter.
107
status_exception(const ISC_STATUS *status_vector, bool permanent) throw();
108
virtual ~status_exception() throw();
110
virtual ISC_STATUS stuff_exception(ISC_STATUS* const status_vector, StringsBuffer* sb = NULL) const throw();
111
virtual const char* what() const throw() { return "Firebird::status_exception"; }
113
const ISC_STATUS* value() const throw() { return m_status_vector; }
115
// Returns true if strings contained in status vector are located in magical
116
// permanent circular buffer. False means that exception object owns strings
117
// and is about to deallocate them in its destructor
118
bool strings_permanent() const throw() { return m_strings_permanent; }
120
// Takes permanent strings
121
static void raise(const ISC_STATUS *status_vector);
123
// Take transient strings
124
static void raise(ISC_STATUS status, ...);
127
// Create exception with undefined status vector, this constructor allows
128
// derived classes create empty exception ...
129
status_exception() throw();
130
// .. and adjust it later using somehow created status vector.
131
void set_status(const ISC_STATUS *new_vector, bool permanent) throw();
134
ISC_STATUS_ARRAY m_status_vector;
135
bool m_strings_permanent;
136
void release_vector() throw();
139
class system_call_failed : public status_exception
144
system_call_failed(const char* v_syscall, int v_error_code);
146
static void raise(const char* syscall, int error_code);
147
static void raise(const char* syscall);
149
int getErrorCode() const
155
// Moved what() here due to gpre. Didn't want to use macros for gpre_static.
156
class fatal_exception : public status_exception
159
explicit fatal_exception(const char* message);
160
static void raiseFmt(const char* format, ...);
161
// Keep in sync with the constructor above, please; "message" becomes 4th element
162
// after initialization of status vector in constructor.
163
const char* what() const throw()
165
return reinterpret_cast<const char*>(value()[3]);
167
static void raise(const char* message);
171
// Serialize exception into status_vector, put transient strings from exception into given StringsBuffer
172
ISC_STATUS stuff_exception(ISC_STATUS *status_vector, const Firebird::Exception& ex, StringsBuffer* sb = NULL) throw();
174
// These routines put strings into process-level circular buffer
175
// They are obsolete, use transient version of status_exception::raise in combination with
176
// stuff_exception instead
177
const char* status_string(const char* string);
178
const char* status_nstring(const char* string, size_t length);
180
} // namespace Firebird
183
#endif // FB_EXCEPTION_H