mirror of
https://github.com/allankoechke/SQLiteCpp.git
synced 2026-12-01 09:08:47 +00:00
Fix issue #7 : SQLITECPP_ENABLE_ASSERT_HANDLER
- SQLITECPP_ASSERT() can call a user defined SQLite::assertion_failed() handler.
This commit is contained in:
56
src/Assert.h
Normal file
56
src/Assert.h
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* @file Assert.h
|
||||
* @ingroup SQLiteCpp
|
||||
* @brief Definition of the SQLITECPP_ASSERT() macro.
|
||||
*
|
||||
* Copyright (c) 2012-2013 Sebastien Rombauts (sebastien.rombauts@gmail.com)
|
||||
*
|
||||
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
|
||||
* or copy at http://opensource.org/licenses/MIT)
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
|
||||
|
||||
/**
|
||||
* @def SQLITECPP_ASSERT SQLITECPP_ASSERT() is used in destructors, where exceptions shall not be thrown
|
||||
*
|
||||
* Define SQLITECPP_ENABLE_ASSERT_HANDLER at the project level
|
||||
* and define a SQLite::assertion_failed() assertion handler
|
||||
* to tell SQLiteC++ to use it instead of assert() when an assertion fail.
|
||||
*/
|
||||
#ifdef SQLITECPP_ENABLE_ASSERT_HANDLER
|
||||
|
||||
// if an assert handler is provided by user code, use it instead of assert()
|
||||
namespace SQLite
|
||||
{
|
||||
// declaration of the assert handler to define in user code
|
||||
void assertion_failed(const char* apFile, const long apLine, const char* apFunc, const char* apExpr, const char* apMsg);
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define __func__ __FUNCTION__
|
||||
#endif
|
||||
// call the assert handler provided by user code
|
||||
#define SQLITECPP_ASSERT(expression,message) \
|
||||
if (!(expression)) SQLite::assertion_failed(__FILE__, __LINE__, __func__, #expression, message)
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// if no assert handler provided by user code, use standard assert()
|
||||
|
||||
#ifndef NDEBUG
|
||||
// in debug mode, assert() :
|
||||
#define SQLITECPP_ASSERT(expression,message) assert(expression)
|
||||
#else
|
||||
// in release mode, nothing :
|
||||
#define SQLITECPP_ASSERT(expression,message) (expression)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
#pragma warning(disable:4290) // Disable warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
|
||||
#endif
|
||||
@@ -12,6 +12,7 @@
|
||||
# add sources of the wrapper as a "SQLiteCpp" static library
|
||||
add_library (SQLiteCpp
|
||||
SQLiteC++.h
|
||||
Assert.h
|
||||
Column.cpp
|
||||
Column.h
|
||||
Database.cpp
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "Database.h"
|
||||
|
||||
#include "Statement.h"
|
||||
#include "Assert.h"
|
||||
#include "Exception.h"
|
||||
|
||||
|
||||
@@ -37,8 +38,7 @@ Database::~Database(void) throw() // nothrow
|
||||
{
|
||||
int ret = sqlite3_close(mpSQLite);
|
||||
// Never throw an exception in a destructor
|
||||
//std::cout << sqlite3_errmsg(mpSQLite) << std::endl;
|
||||
SQLITE_CPP_ASSERT (SQLITE_OK == ret);
|
||||
SQLITECPP_ASSERT (SQLITE_OK == ret, sqlite3_errmsg(mpSQLite)); // See SQLITECPP_ENABLE_ASSERT_HANDLER
|
||||
}
|
||||
|
||||
// Shortcut to execute one or multiple SQL statements without results (UPDATE, INSERT, ALTER, COMMIT...).
|
||||
|
||||
@@ -11,23 +11,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdexcept>
|
||||
#include <cassert>
|
||||
|
||||
|
||||
// assert() is used in destructors, where exceptions are not allowed
|
||||
// here you can chose if you want to use them or not
|
||||
#ifndef NDEBUG
|
||||
// in debug mode :
|
||||
#define SQLITE_CPP_ASSERT(expression) assert(expression)
|
||||
#else
|
||||
// in release mode :
|
||||
#define SQLITE_CPP_ASSERT(expression) (expression)
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
#pragma warning(disable:4290) // Disable warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
|
||||
#endif
|
||||
|
||||
|
||||
namespace SQLite
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
|
||||
// Include useful headers of SQLiteC++
|
||||
#include "Assert.h"
|
||||
#include "Exception.h"
|
||||
#include "Database.h"
|
||||
#include "Statement.h"
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
#include "Database.h"
|
||||
#include "Column.h"
|
||||
#include "Assert.h"
|
||||
#include "Exception.h"
|
||||
|
||||
|
||||
@@ -316,8 +317,7 @@ Statement::Ptr::~Ptr(void) throw() // nothrow
|
||||
// as no Statement not Column objet use it anymore
|
||||
int ret = sqlite3_finalize(mpStmt);
|
||||
// Never throw an exception in a destructor
|
||||
//std::cout << sqlite3_errmsg(mpSQLite) << std::endl;
|
||||
SQLITE_CPP_ASSERT (SQLITE_OK == ret);
|
||||
SQLITECPP_ASSERT (SQLITE_OK == ret, sqlite3_errmsg(mpSQLite)); // See SQLITECPP_ENABLE_ASSERT_HANDLER
|
||||
|
||||
// and delete the reference counter
|
||||
delete mpRefCount;
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "Transaction.h"
|
||||
|
||||
#include "Database.h"
|
||||
#include "Assert.h"
|
||||
#include "Exception.h"
|
||||
|
||||
|
||||
@@ -35,11 +36,10 @@ Transaction::~Transaction(void) throw() // nothrow
|
||||
{
|
||||
mDatabase.exec("ROLLBACK");
|
||||
}
|
||||
catch (SQLite::Exception& /*e*/)
|
||||
catch (SQLite::Exception& e)
|
||||
{
|
||||
// Never throw an exception in a destructor
|
||||
//std::cout << e.what() << std::endl;
|
||||
SQLITE_CPP_ASSERT(false);
|
||||
SQLITECPP_ASSERT(false, e.what()); // See SQLITECPP_ENABLE_ASSERT_HANDLER
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user