2001-12-24 03:51:06 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2001 M. Nordell
|
|
|
|
//
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
//
|
|
|
|
// Special permission is given to the Firebird project to include,
|
|
|
|
// use and modify this file in the Firebird database engine.
|
|
|
|
//
|
|
|
|
#ifndef FB_EXCEPTION_H
|
|
|
|
#define FB_EXCEPTION_H
|
|
|
|
|
|
|
|
#include <exception>
|
2004-03-01 04:35:23 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdarg.h>
|
2001-12-24 03:51:06 +01:00
|
|
|
#include "fb_types.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
2004-03-01 04:35:23 +01:00
|
|
|
class StringsBuffer {
|
2001-12-24 03:51:06 +01:00
|
|
|
public:
|
2004-03-01 04:35:23 +01:00
|
|
|
virtual char* alloc(const char* string, size_t length) = 0;
|
2001-12-24 03:51:06 +01:00
|
|
|
};
|
|
|
|
|
2004-03-01 04:35:23 +01:00
|
|
|
template <size_t BUFFER_SIZE>
|
|
|
|
class CircularStringsBuffer : public StringsBuffer {
|
2001-12-24 03:51:06 +01:00
|
|
|
public:
|
2004-03-01 04:35:23 +01:00
|
|
|
CircularStringsBuffer() throw() {
|
|
|
|
// This is to ensure we have zero at the end of buffer in case of buffer overflow
|
|
|
|
memset(buffer, 0, BUFFER_SIZE);
|
|
|
|
buffer_ptr = buffer;
|
|
|
|
}
|
|
|
|
//virtual ~CircularStringsBuffer() {};
|
|
|
|
virtual char* alloc(const char* string, size_t length) {
|
|
|
|
// fb_assert(length+1 < BUFFER_SIZE);
|
|
|
|
// If there isn't any more room in the buffer, start at the beginning again
|
|
|
|
if (buffer_ptr + length + 1 > buffer + BUFFER_SIZE)
|
|
|
|
buffer_ptr = buffer;
|
|
|
|
char* new_string = buffer_ptr;
|
|
|
|
memcpy(new_string, string, length);
|
|
|
|
new_string[length] = 0;
|
|
|
|
buffer_ptr += length + 1;
|
|
|
|
return new_string;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
char buffer[BUFFER_SIZE];
|
|
|
|
char *buffer_ptr;
|
2001-12-24 03:51:06 +01:00
|
|
|
};
|
|
|
|
|
2004-03-01 04:35:23 +01:00
|
|
|
class status_exception : public std::exception
|
2001-12-24 03:51:06 +01:00
|
|
|
{
|
|
|
|
public:
|
2004-03-01 04:35:23 +01:00
|
|
|
// This version of constructor receives status vector pointing to permanent strings
|
|
|
|
// which may be returned to user in status vector directly without transfer of string ownership
|
|
|
|
explicit status_exception(const ISC_STATUS *status_vector) throw();
|
|
|
|
|
|
|
|
// These versions of constructor clone passed transient strings
|
|
|
|
status_exception(ISC_STATUS status, va_list status_args);
|
|
|
|
status_exception(ISC_STATUS status, ...);
|
|
|
|
|
|
|
|
// Create exception with undefined status vector, this constructor allows to use this
|
|
|
|
// class as jmpbuf replacement for transitional period
|
|
|
|
status_exception() throw();
|
|
|
|
|
|
|
|
virtual ~status_exception() throw();
|
2002-01-04 12:34:22 +01:00
|
|
|
virtual const char* what() const throw()
|
2004-03-01 04:35:23 +01:00
|
|
|
{ return "Firebird::status_exception"; }
|
|
|
|
const ISC_STATUS* value() const { return m_status_vector; }
|
|
|
|
bool strings_permanent() const { return m_strings_permanent; }
|
|
|
|
bool status_known() const { return m_status_known; }
|
2001-12-24 03:51:06 +01:00
|
|
|
|
2004-03-01 04:35:23 +01:00
|
|
|
// Takes permanent strings
|
|
|
|
static void raise(const ISC_STATUS *status_vector);
|
2002-11-11 19:06:01 +01:00
|
|
|
static void raise();
|
2004-03-01 04:35:23 +01:00
|
|
|
|
|
|
|
// Take transient strings
|
|
|
|
static void raise(ISC_STATUS status, ...);
|
|
|
|
private:
|
|
|
|
ISC_STATUS_ARRAY m_status_vector;
|
|
|
|
bool m_strings_permanent;
|
|
|
|
bool m_status_known;
|
|
|
|
void fill_status(ISC_STATUS status, va_list status_args);
|
2002-11-11 19:06:01 +01:00
|
|
|
};
|
|
|
|
|
2004-03-01 04:35:23 +01:00
|
|
|
class system_call_failed : public status_exception
|
2002-11-11 19:06:01 +01:00
|
|
|
{
|
|
|
|
public:
|
2004-03-01 04:35:23 +01:00
|
|
|
system_call_failed(const char* _syscall, int _error_code);
|
2002-11-11 19:06:01 +01:00
|
|
|
|
2004-03-01 04:35:23 +01:00
|
|
|
static void raise(const char* syscall, int error_code);
|
|
|
|
static void raise(const char* syscall);
|
2002-11-11 19:06:01 +01:00
|
|
|
};
|
|
|
|
|
2004-03-01 04:35:23 +01:00
|
|
|
class fatal_exception : public status_exception
|
2002-11-11 19:06:01 +01:00
|
|
|
{
|
|
|
|
public:
|
2003-12-31 06:36:12 +01:00
|
|
|
explicit fatal_exception(const char* message);
|
|
|
|
static void raise(const char* message);
|
2001-12-24 03:51:06 +01:00
|
|
|
};
|
|
|
|
|
2004-03-01 04:35:23 +01:00
|
|
|
|
|
|
|
// Serialize exception into status_vector, put transient strings from exception into given StringsBuffer
|
|
|
|
ISC_STATUS stuff_exception(ISC_STATUS *status_vector, const std::exception& ex, StringsBuffer* sb = NULL) throw();
|
|
|
|
|
|
|
|
// These routines put strings into process-level circular buffer
|
|
|
|
// They are obsolete, use transient version of status_exception::raise in combination with
|
|
|
|
// stuff_exception instead
|
|
|
|
const char* status_string(const char* string);
|
|
|
|
const char* status_nstring(const char* string, size_t length);
|
|
|
|
|
2001-12-24 03:51:06 +01:00
|
|
|
} // namespace Firebird
|
|
|
|
|
2004-03-01 04:35:23 +01:00
|
|
|
|
2001-12-24 03:51:06 +01:00
|
|
|
#endif // FB_EXCEPTION_H
|
2003-12-31 06:36:12 +01:00
|
|
|
|