Skip to content

Commit f9e8c40

Browse files
authored
Merge pull request #86 from zauguin/uncaught_exceptions
Use std::uncaught_exceptions if possible
2 parents f9cf0fd + 9e37e6d commit f9e8c40

File tree

6 files changed

+88
-4
lines changed

6 files changed

+88
-4
lines changed

Makefile.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ tests/results:$(TEST_RESULT)
6565
@echo -------------- Test Results ---------------
6666
@cat tests/results
6767
@echo -------------------------------------------
68-
@ ! grep -qv OK tests/results
68+
@ ! grep -qv 'OK\|Skipped' tests/results
6969

7070

7171
#Build a test executable from a test program. On compile error,
@@ -87,6 +87,9 @@ tests/%.result_: tests/%.test
8787
if [ $$a -ge 128 and ] ; \
8888
then \
8989
echo Crash!! > $@ ; \
90+
elif [ $$a -eq 42 ] ;\
91+
then \
92+
echo Skipped > $@ ; \
9093
elif [ $$a -ne 126 ] ;\
9194
then \
9295
echo Failed > $@ ; \

hdr/sqlite_modern_cpp.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
#include <sqlite3.h>
2626

27-
#include <sqlite_modern_cpp/utility/function_traits.h>
27+
#include "sqlite_modern_cpp/utility/function_traits.h"
28+
#include "sqlite_modern_cpp/utility/uncaught_exceptions.h"
2829

2930
namespace sqlite {
3031

@@ -160,6 +161,7 @@ namespace sqlite {
160161
private:
161162
std::shared_ptr<sqlite3> _db;
162163
std::unique_ptr<sqlite3_stmt, decltype(&sqlite3_finalize)> _stmt;
164+
utility::UncaughtExceptionDetector _has_uncaught_exception;
163165

164166
int _inx;
165167

@@ -285,7 +287,7 @@ namespace sqlite {
285287
~database_binder() noexcept(false) {
286288
/* Will be executed if no >>op is found, but not if an exception
287289
is in mid flight */
288-
if(!execution_started && !std::uncaught_exception() && _stmt) {
290+
if(!execution_started && !_has_uncaught_exception && _stmt) {
289291
execute();
290292
}
291293
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include <cassert>
4+
#include <exception>
5+
#include <iostream>
6+
7+
namespace sqlite {
8+
namespace utility {
9+
#ifdef __cpp_lib_uncaught_exceptions
10+
class UncaughtExceptionDetector {
11+
public:
12+
operator bool() {
13+
return count != std::uncaught_exceptions();
14+
}
15+
private:
16+
int count = std::uncaught_exceptions();
17+
};
18+
#else
19+
class UncaughtExceptionDetector {
20+
public:
21+
operator bool() {
22+
return std::uncaught_exception();
23+
}
24+
};
25+
#endif
26+
}
27+
}

tests/exception_dont_execute.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <memory>
4+
#include <stdexcept>
5+
#include <sqlite_modern_cpp.h>
6+
using namespace sqlite;
7+
using namespace std;
8+
9+
10+
int main() {
11+
database db(":memory:");
12+
db << "CREATE TABLE person (id integer primary key not null, name TEXT not null);";
13+
14+
try {
15+
auto stmt = db << "INSERT INTO person (id,name) VALUES (?,?)";
16+
throw 1;
17+
} catch (int) {
18+
}
19+
exit(EXIT_SUCCESS);
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <memory>
4+
#include <stdexcept>
5+
#include <sqlite_modern_cpp.h>
6+
using namespace sqlite;
7+
using namespace std;
8+
9+
struct A {
10+
~A() {
11+
database db(":memory:");
12+
db << "CREATE TABLE person (id integer primary key not null, name TEXT not null);";
13+
14+
try {
15+
auto stmt = db << "INSERT INTO person (id,name) VALUES (?,?)";
16+
throw 1;
17+
} catch (int) {
18+
}
19+
}
20+
};
21+
int main() {
22+
#ifdef __cpp_lib_uncaught_exceptions
23+
try {
24+
A a;
25+
throw 1;
26+
} catch(int) {
27+
}
28+
exit(EXIT_SUCCESS);
29+
#else
30+
exit(42);
31+
#endif
32+
}

tests/std_optional.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ int main() {
7070
#else
7171
#pragma message "<optional> not found, test disabled."
7272
int main() {
73-
exit(EXIT_SUCCESS);
73+
exit(42);
7474
}
7575
#endif

0 commit comments

Comments
 (0)