Skip to content

Use std::uncaught_exceptions if possible #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ tests/results:$(TEST_RESULT)
@echo -------------- Test Results ---------------
@cat tests/results
@echo -------------------------------------------
@ ! grep -qv OK tests/results
@ ! grep -qv 'OK\|Skipped' tests/results


#Build a test executable from a test program. On compile error,
Expand All @@ -87,6 +87,9 @@ tests/%.result_: tests/%.test
if [ $$a -ge 128 and ] ; \
then \
echo Crash!! > $@ ; \
elif [ $$a -eq 42 ] ;\
then \
echo Skipped > $@ ; \
elif [ $$a -ne 126 ] ;\
then \
echo Failed > $@ ; \
Expand Down
6 changes: 4 additions & 2 deletions hdr/sqlite_modern_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

#include <sqlite3.h>

#include <sqlite_modern_cpp/utility/function_traits.h>
#include "sqlite_modern_cpp/utility/function_traits.h"
#include "sqlite_modern_cpp/utility/uncaught_exceptions.h"

namespace sqlite {

Expand Down Expand Up @@ -160,6 +161,7 @@ namespace sqlite {
private:
std::shared_ptr<sqlite3> _db;
std::unique_ptr<sqlite3_stmt, decltype(&sqlite3_finalize)> _stmt;
utility::UncaughtExceptionDetector _has_uncaught_exception;

int _inx;

Expand Down Expand Up @@ -285,7 +287,7 @@ namespace sqlite {
~database_binder() noexcept(false) {
/* Will be executed if no >>op is found, but not if an exception
is in mid flight */
if(!execution_started && !std::uncaught_exception() && _stmt) {
if(!execution_started && !_has_uncaught_exception && _stmt) {
execute();
}
}
Expand Down
27 changes: 27 additions & 0 deletions hdr/sqlite_modern_cpp/utility/uncaught_exceptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <cassert>
#include <exception>
#include <iostream>

namespace sqlite {
namespace utility {
#ifdef __cpp_lib_uncaught_exceptions
class UncaughtExceptionDetector {
public:
operator bool() {
return count != std::uncaught_exceptions();
}
private:
int count = std::uncaught_exceptions();
};
#else
class UncaughtExceptionDetector {
public:
operator bool() {
return std::uncaught_exception();
}
};
#endif
}
}
20 changes: 20 additions & 0 deletions tests/exception_dont_execute.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include <string>
#include <memory>
#include <stdexcept>
#include <sqlite_modern_cpp.h>
using namespace sqlite;
using namespace std;


int main() {
database db(":memory:");
db << "CREATE TABLE person (id integer primary key not null, name TEXT not null);";

try {
auto stmt = db << "INSERT INTO person (id,name) VALUES (?,?)";
throw 1;
} catch (int) {
}
exit(EXIT_SUCCESS);
}
32 changes: 32 additions & 0 deletions tests/exception_dont_execute_nested.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <string>
#include <memory>
#include <stdexcept>
#include <sqlite_modern_cpp.h>
using namespace sqlite;
using namespace std;

struct A {
~A() {
database db(":memory:");
db << "CREATE TABLE person (id integer primary key not null, name TEXT not null);";

try {
auto stmt = db << "INSERT INTO person (id,name) VALUES (?,?)";
throw 1;
} catch (int) {
}
}
};
int main() {
#ifdef __cpp_lib_uncaught_exceptions
try {
A a;
throw 1;
} catch(int) {
}
exit(EXIT_SUCCESS);
#else
exit(42);
#endif
}
2 changes: 1 addition & 1 deletion tests/std_optional.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ int main() {
#else
#pragma message "<optional> not found, test disabled."
int main() {
exit(EXIT_SUCCESS);
exit(42);
}
#endif