8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-31 15:23:03 +01:00
firebird-mirror/src/include/fb_exception.h

210 lines
5.8 KiB
C
Raw Normal View History

2001-12-24 03:51:06 +01:00
/*
* PROGRAM: Firebird exceptions classes
* MODULE: fb_exception.h
2008-12-05 01:56:15 +01:00
* DESCRIPTION: Firebird's exception classes
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Mike Nordell
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2001 Mike Nordell <tamlin at algonet.se>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
2001-12-24 03:51:06 +01:00
*
*
*/
2001-12-24 03:51:06 +01:00
#ifndef FB_EXCEPTION_H
#define FB_EXCEPTION_H
#include <stddef.h>
2004-05-03 06:25:06 +02:00
#include <string.h>
#include <new>
2001-12-24 03:51:06 +01:00
#include "fb_types.h"
#include "../common/StatusArg.h"
2001-12-24 03:51:06 +01:00
namespace Firebird {
class MemoryPool;
2008-12-05 01:56:15 +01:00
class StringsBuffer
{
2001-12-24 03:51:06 +01:00
public:
virtual const char* alloc(const char* string, size_t& length) = 0;
virtual ~StringsBuffer() {}
void makePermanentVector(ISC_STATUS* perm, const ISC_STATUS* temp);
static void makeEnginePermanentVector(ISC_STATUS* v);
2001-12-24 03:51:06 +01:00
};
template <size_t BUFFER_SIZE>
2008-12-05 01:56:15 +01:00
class CircularStringsBuffer : public StringsBuffer
{
2001-12-24 03:51:06 +01:00
public:
CircularStringsBuffer() throw() { init(); }
explicit CircularStringsBuffer(MemoryPool&) throw() { init(); }
virtual const char* alloc(const char* string, size_t& length)
2008-08-29 04:18:50 +02:00
{
// if string is already in our buffer - return it
// it was already saved in our buffer once
if (string >= buffer && string < &buffer[BUFFER_SIZE])
return string;
// if string too long, truncate it
if (length > BUFFER_SIZE / 4)
2008-12-05 01:56:15 +01:00
length = BUFFER_SIZE / 4;
// 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;
2008-12-05 01:56:15 +01:00
buffer_ptr += length + 1;
return new_string;
}
private:
void init() throw()
{
// This is to ensure we have zero at the end of buffer in case of buffer overflow
2008-12-05 01:56:15 +01:00
memset(buffer, 0, BUFFER_SIZE);
buffer_ptr = buffer;
}
char buffer[BUFFER_SIZE];
2008-01-23 20:03:16 +01:00
char* buffer_ptr;
2001-12-24 03:51:06 +01:00
};
class Exception
{
protected:
Exception() throw() { }
public:
virtual ~Exception() throw();
virtual ISC_STATUS stuff_exception(ISC_STATUS* const status_vector, StringsBuffer* sb = NULL) const throw() = 0;
virtual const char* what() const throw() = 0;
};
// Used as jmpbuf to unwind when needed
class LongJump : public Exception
{
public:
virtual ISC_STATUS stuff_exception(ISC_STATUS* const status_vector, StringsBuffer* sb = NULL) const throw();
virtual const char* what() const throw();
static void raise();
LongJump() throw() : Exception() { }
};
// Used in MemoryPool
class BadAlloc : public std::bad_alloc, public Exception
{
public:
virtual ISC_STATUS stuff_exception(ISC_STATUS* const status_vector, StringsBuffer* sb = NULL) const throw();
virtual const char* what() const throw();
static void raise();
BadAlloc() throw() : std::bad_alloc(), Exception() { }
};
// Main exception class in firebird
class status_exception : public Exception
2001-12-24 03:51:06 +01:00
{
public:
2008-12-05 01:56:15 +01:00
// This version of constructor receives status vector pointing to permanent or
// temp strings, depending upon second parameter.
status_exception(const ISC_STATUS *status_vector, bool permanent) throw();
virtual ~status_exception() throw();
virtual ISC_STATUS stuff_exception(ISC_STATUS* const status_vector, StringsBuffer* sb = NULL) const throw();
virtual const char* what() const throw();
const ISC_STATUS* value() const throw() { return m_status_vector; }
2008-12-05 01:56:15 +01:00
// Returns true if strings contained in status vector are located in magical
// permanent circular buffer. False means that exception object owns strings
// and is about to deallocate them in its destructor
bool strings_permanent() const throw() { return m_strings_permanent; }
// Takes permanent strings
static void raise(const ISC_STATUS *status_vector);
2008-12-05 01:56:15 +01:00
// Takes transient strings
static void raise(const Arg::StatusVector& statusVector);
2008-12-05 01:56:15 +01:00
protected:
2008-12-05 01:56:15 +01:00
// Create exception with undefined status vector, this constructor allows
// derived classes create empty exception ...
status_exception() throw();
// .. and adjust it later using somehow created status vector.
2006-01-08 02:11:06 +01:00
void set_status(const ISC_STATUS *new_vector, bool permanent) throw();
2008-12-05 01:56:15 +01:00
private:
ISC_STATUS_ARRAY m_status_vector;
bool m_strings_permanent;
void release_vector() throw();
2002-11-11 19:06:01 +01:00
};
2008-12-01 02:26:27 +01:00
// use this class if exception can be handled
class system_error : public status_exception
2002-11-11 19:06:01 +01:00
{
private:
int errorCode;
2002-11-11 19:06:01 +01:00
public:
system_error(const char* syscall, int error_code);
2002-11-11 19:06:01 +01:00
static void raise(const char* syscall, int error_code);
static void raise(const char* syscall);
2008-12-05 01:56:15 +01:00
int getErrorCode() const
{
return errorCode;
}
static int getSystemError();
};
2008-12-05 01:56:15 +01:00
// use this class if exception can't be handled
// it will call abort() in DEV_BUILD to create core dump
class system_call_failed : public system_error
{
public:
system_call_failed(const char* syscall, int error_code);
static void raise(const char* syscall, int error_code);
static void raise(const char* syscall);
2002-11-11 19:06:01 +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 raiseFmt(const char* format, ...);
const char* what() const throw();
2003-12-31 06:36:12 +01:00
static void raise(const char* message);
2001-12-24 03:51:06 +01:00
};
// Serialize exception into status_vector, put transient strings from exception into given StringsBuffer
ISC_STATUS stuff_exception(ISC_STATUS *status_vector, const Firebird::Exception& ex, StringsBuffer* sb = NULL) throw();
2001-12-24 03:51:06 +01:00
} // namespace Firebird
2001-12-24 03:51:06 +01:00
#endif // FB_EXCEPTION_H