~ubuntu-branches/ubuntu/karmic/firebird2.1/karmic

« back to all changes in this revision

Viewing changes to src/include/fb_exception.h

  • Committer: Bazaar Package Importer
  • Author(s): Damyan Ivanov
  • Date: 2008-05-26 23:59:25 UTC
  • Revision ID: james.westby@ubuntu.com-20080526235925-2pnqj6nxpppoeaer
Tags: upstream-2.1.0.17798-0.ds2
ImportĀ upstreamĀ versionĀ 2.1.0.17798-0.ds2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      PROGRAM:                Firebird exceptions classes
 
3
 *      MODULE:                 fb_exception.h
 
4
 *      DESCRIPTION:    Firebird's exception classes 
 
5
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
17
 *  The Original Code was created by Mike Nordell
 
18
 *  for the Firebird Open Source RDBMS project.
 
19
 *
 
20
 *  Copyright (c) 2001 Mike Nordell <tamlin at algonet.se>
 
21
 *  and all contributors signed below.
 
22
 *
 
23
 *  All Rights Reserved.
 
24
 *  Contributor(s): ______________________________________.
 
25
 *
 
26
 *
 
27
 */
 
28
 
 
29
 
 
30
#ifndef FB_EXCEPTION_H
 
31
#define FB_EXCEPTION_H
 
32
 
 
33
#include <stddef.h>
 
34
#include <string.h>
 
35
#include "fb_types.h"
 
36
 
 
37
namespace Firebird {
 
38
 
 
39
class StringsBuffer {
 
40
public:
 
41
        virtual char* alloc(const char* string, size_t length) = 0;
 
42
        virtual ~StringsBuffer() {}
 
43
 
 
44
        void makePermanentVector(ISC_STATUS* perm, const ISC_STATUS* temp);
 
45
};
 
46
 
 
47
template <size_t BUFFER_SIZE>
 
48
class CircularStringsBuffer : public StringsBuffer {
 
49
public:
 
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); 
 
53
                buffer_ptr = buffer;
 
54
        }
 
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)
 
59
                        buffer_ptr = buffer;
 
60
                char* new_string = buffer_ptr;
 
61
                memcpy(new_string, string, length);
 
62
                new_string[length] = 0;
 
63
                buffer_ptr += length + 1;       
 
64
                return new_string;
 
65
        }
 
66
private:
 
67
        char buffer[BUFFER_SIZE];
 
68
        char *buffer_ptr;
 
69
};
 
70
 
 
71
class Exception
 
72
{
 
73
protected:
 
74
        Exception() throw() { }
 
75
public:
 
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;
 
79
};
 
80
 
 
81
// Used as jmpbuf to unwind when needed
 
82
class LongJump : public Exception
 
83
{
 
84
public:
 
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"; }
 
87
        static void raise();
 
88
        LongJump() throw() : Exception() { }
 
89
};
 
90
 
 
91
// Used in MemoryPool
 
92
class BadAlloc : public Exception
 
93
{
 
94
public:
 
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"; }
 
97
        static void raise();
 
98
        BadAlloc() throw() : Exception() { }
 
99
};
 
100
 
 
101
// Main exception class in firebird
 
102
class status_exception : public Exception
 
103
{
 
104
public:
 
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();
 
109
 
 
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"; }
 
112
 
 
113
        const ISC_STATUS* value() const throw() { return m_status_vector; }
 
114
 
 
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; }
 
119
 
 
120
        // Takes permanent strings
 
121
        static void raise(const ISC_STATUS *status_vector);
 
122
        
 
123
        // Take transient strings
 
124
        static void raise(ISC_STATUS status, ...);
 
125
        
 
126
protected:
 
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();
 
132
        
 
133
private:
 
134
        ISC_STATUS_ARRAY m_status_vector;
 
135
        bool m_strings_permanent;
 
136
        void release_vector() throw();
 
137
};
 
138
 
 
139
class system_call_failed : public status_exception
 
140
{
 
141
private:
 
142
        int errorCode;
 
143
public:
 
144
        system_call_failed(const char* v_syscall, int v_error_code);
 
145
 
 
146
        static void raise(const char* syscall, int error_code);
 
147
        static void raise(const char* syscall);
 
148
        
 
149
        int getErrorCode() const
 
150
        {
 
151
                return errorCode;
 
152
        }
 
153
};
 
154
 
 
155
// Moved what() here due to gpre. Didn't want to use macros for gpre_static.
 
156
class fatal_exception : public status_exception
 
157
{
 
158
public:
 
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()
 
164
        {
 
165
                return reinterpret_cast<const char*>(value()[3]);
 
166
        }
 
167
        static void raise(const char* message);
 
168
};
 
169
 
 
170
 
 
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();
 
173
 
 
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);
 
179
 
 
180
}       // namespace Firebird
 
181
 
 
182
 
 
183
#endif  // FB_EXCEPTION_H
 
184