From 511a30e40e79396aadcd0071af1e362d1bbec81c Mon Sep 17 00:00:00 2001 From: crashmatt Date: Tue, 18 Apr 2017 07:03:46 +0200 Subject: [PATCH 1/5] Added PICOJSON_NO_EXCEPTIONS define to optionally remove throw-catch from header and tests --- Makefile | 6 +++++- picojson.h | 43 ++++++++++++++++++++++++++++++++++++++++--- test.cc | 2 ++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index a03f4a0..a94bc3c 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ check: test test: test-core test-core-int64 ./test-core ./test-core-int64 + ./test-no-except test-core: picojson.h test.cc picotest/picotest.c picotest/picotest.h $(CXX) -Wall test.cc picotest/picotest.c -o $@ @@ -13,8 +14,11 @@ test-core: picojson.h test.cc picotest/picotest.c picotest/picotest.h test-core-int64: picojson.h test.cc picotest/picotest.c picotest/picotest.h $(CXX) -Wall -DPICOJSON_USE_INT64 test.cc picotest/picotest.c -o $@ +test-no-except: picojson.h test.cc picotest/picotest.c picotest/picotest.h + $(CXX) -Wall -DPICOJSON_NO_EXCEPTIONS test.cc picotest/picotest.c -o $@ + clean: - rm -f test-core test-core-int64 + rm -f test-core test-core-int64 test-no-except install: install -d $(DESTDIR)$(includedir) diff --git a/picojson.h b/picojson.h index f75c399..c2d64ad 100644 --- a/picojson.h +++ b/picojson.h @@ -37,7 +37,11 @@ #include #include #include +#if (!defined(PICOSJON_NO_EXCEPTIONS)) #include +#else +echo "picosjon no exceptions option\n" +#endif // PICOSJON_NO_EXCEPTIONS) #include #include #include @@ -46,7 +50,7 @@ #if __cplusplus >= 201103L #include #else -extern "C" { + extern "C" { #ifdef _MSC_VER #include #elif defined(__INTEL_COMPILER) @@ -83,12 +87,18 @@ extern "C" { #endif #ifndef PICOJSON_ASSERT +#if (!defined(PICOSJON_NO_EXCEPTIONS)) #define PICOJSON_ASSERT(e) \ do { \ if (!(e)) \ throw std::runtime_error(#e); \ } while (0) -#endif +#else +#define PICOJSON_ASSERT(e) \ + if (!e) \ + std::cerr << "picosjon runtime error\n"; +#endif // PICOSJON_NO_EXCEPTIONS +#endif // PICOJSON_ASSERT #ifdef _MSC_VER #define SNPRINTF _snprintf_s @@ -159,11 +169,20 @@ class value { ~value(); value(const value &x); value &operator=(const value &x); + #if PICOJSON_USE_RVALUE_REFERENCE +#if (!defined(PICOSJON_NO_EXCEPTIONS)) value(value &&x) throw(); value &operator=(value &&x) throw(); +#else + value(value &&x) value &operator=(value &&x) +#endif // PICOSJON_NO_EXCEPTIONS) #endif +#if (!defined(PICOSJON_NO_EXCEPTIONS)) void swap(value &x) throw(); +#else + void swap(value &x); +#endif // PICOSJON_NO_EXCEPTIONS) template bool is() const; template const T &get() const; template T &get(); @@ -237,7 +256,9 @@ inline value::value(double n) : type_(number_type), u_() { isnan(n) || isinf(n) #endif ) { +#if !defined(PICOSJON_NO_EXCEPTIONS) throw std::overflow_error(""); +#endif // PICOSJON_NO_EXCEPTIONS) } u_.number_ = n; } @@ -318,7 +339,7 @@ inline value &value::operator=(const value &x) { } return *this; } - +#if (!defined(PICOSJON_NO_EXCEPTIONS)) #if PICOJSON_USE_RVALUE_REFERENCE inline value::value(value &&x) throw() : type_(null_type), u_() { swap(x); @@ -332,6 +353,22 @@ inline void value::swap(value &x) throw() { std::swap(type_, x.type_); std::swap(u_, x.u_); } +#else // PICOSJON_NO_EXCEPTIONS +#if PICOJSON_USE_RVALUE_REFERENCE +inline value::value(value &&x) : type_(null_type), u_() { + swap(x); +} +inline value &value::operator=(value &&x) { + swap(x); + return *this; +} +#endif +inline void value::swap(value &x) { + std::swap(type_, x.type_); + std::swap(u_, x.u_); +} + +#endif // PICOSJON_NO_EXCEPTIONS #define IS(ctype, jtype) \ template <> inline bool value::is() const { \ diff --git a/test.cc b/test.cc index 0779c55..fdf4e6a 100644 --- a/test.cc +++ b/test.cc @@ -265,6 +265,7 @@ int main(void) _ok(v.serialize(true) == "{\n \"a\": 1,\n \"b\": [\n 2,\n {\n \"b1\": \"abc\"\n }\n ],\n \"c\": {},\n \"d\": []\n}\n", "prettifying output"); } +#if !defined(PICOSJON_NO_EXCEPTIONS) try { picojson::value v(std::numeric_limits::quiet_NaN()); _ok(false, "should not accept NaN"); @@ -287,6 +288,7 @@ int main(void) } catch (std::runtime_error e) { _ok(true, "get() should raise an error"); } +#endif #ifdef PICOJSON_USE_INT64 { From 4138b256340eabe26654dba638c3729038d26b8e Mon Sep 17 00:00:00 2001 From: crashmatt Date: Tue, 18 Apr 2017 07:35:48 +0200 Subject: [PATCH 2/5] Fixed typo --- picojson.h | 30 +++++++++++++++--------------- test.cc | 2 +- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/picojson.h b/picojson.h index c2d64ad..4e6fa23 100644 --- a/picojson.h +++ b/picojson.h @@ -37,11 +37,11 @@ #include #include #include -#if (!defined(PICOSJON_NO_EXCEPTIONS)) +#if (!defined(PICOJSON_NO_EXCEPTIONS)) #include #else -echo "picosjon no exceptions option\n" -#endif // PICOSJON_NO_EXCEPTIONS) +#pragma message("picojson no exceptions option") +#endif // PICOJSON_NO_EXCEPTIONS) #include #include #include @@ -50,7 +50,7 @@ echo "picosjon no exceptions option\n" #if __cplusplus >= 201103L #include #else - extern "C" { +extern "C" { #ifdef _MSC_VER #include #elif defined(__INTEL_COMPILER) @@ -87,7 +87,7 @@ extern "C" { #endif #ifndef PICOJSON_ASSERT -#if (!defined(PICOSJON_NO_EXCEPTIONS)) +#if (!defined(PICOJSON_NO_EXCEPTIONS)) #define PICOJSON_ASSERT(e) \ do { \ if (!(e)) \ @@ -97,7 +97,7 @@ extern "C" { #define PICOJSON_ASSERT(e) \ if (!e) \ std::cerr << "picosjon runtime error\n"; -#endif // PICOSJON_NO_EXCEPTIONS +#endif // PICOJSON_NO_EXCEPTIONS #endif // PICOJSON_ASSERT #ifdef _MSC_VER @@ -171,18 +171,18 @@ class value { value &operator=(const value &x); #if PICOJSON_USE_RVALUE_REFERENCE -#if (!defined(PICOSJON_NO_EXCEPTIONS)) +#if (!defined(PICOJSON_NO_EXCEPTIONS)) value(value &&x) throw(); value &operator=(value &&x) throw(); #else value(value &&x) value &operator=(value &&x) -#endif // PICOSJON_NO_EXCEPTIONS) +#endif // PICOJSON_NO_EXCEPTIONS) #endif -#if (!defined(PICOSJON_NO_EXCEPTIONS)) +#if (!defined(PICOJSON_NO_EXCEPTIONS)) void swap(value &x) throw(); #else void swap(value &x); -#endif // PICOSJON_NO_EXCEPTIONS) +#endif // PICOJSON_NO_EXCEPTIONS) template bool is() const; template const T &get() const; template T &get(); @@ -256,9 +256,9 @@ inline value::value(double n) : type_(number_type), u_() { isnan(n) || isinf(n) #endif ) { -#if !defined(PICOSJON_NO_EXCEPTIONS) +#if !defined(PICOJSON_NO_EXCEPTIONS) throw std::overflow_error(""); -#endif // PICOSJON_NO_EXCEPTIONS) +#endif // PICOJSON_NO_EXCEPTIONS) } u_.number_ = n; } @@ -339,7 +339,7 @@ inline value &value::operator=(const value &x) { } return *this; } -#if (!defined(PICOSJON_NO_EXCEPTIONS)) +#if (!defined(PICOJSON_NO_EXCEPTIONS)) #if PICOJSON_USE_RVALUE_REFERENCE inline value::value(value &&x) throw() : type_(null_type), u_() { swap(x); @@ -353,7 +353,7 @@ inline void value::swap(value &x) throw() { std::swap(type_, x.type_); std::swap(u_, x.u_); } -#else // PICOSJON_NO_EXCEPTIONS +#else // PICOJSON_NO_EXCEPTIONS #if PICOJSON_USE_RVALUE_REFERENCE inline value::value(value &&x) : type_(null_type), u_() { swap(x); @@ -368,7 +368,7 @@ inline void value::swap(value &x) { std::swap(u_, x.u_); } -#endif // PICOSJON_NO_EXCEPTIONS +#endif // PICOJSON_NO_EXCEPTIONS #define IS(ctype, jtype) \ template <> inline bool value::is() const { \ diff --git a/test.cc b/test.cc index fdf4e6a..bca2fa3 100644 --- a/test.cc +++ b/test.cc @@ -265,7 +265,7 @@ int main(void) _ok(v.serialize(true) == "{\n \"a\": 1,\n \"b\": [\n 2,\n {\n \"b1\": \"abc\"\n }\n ],\n \"c\": {},\n \"d\": []\n}\n", "prettifying output"); } -#if !defined(PICOSJON_NO_EXCEPTIONS) +#if !defined(PICOJSON_NO_EXCEPTIONS) try { picojson::value v(std::numeric_limits::quiet_NaN()); _ok(false, "should not accept NaN"); From d159859e2c5b53c3e99d81eab682d11ae4a6be1f Mon Sep 17 00:00:00 2001 From: crashmatt Date: Wed, 19 Apr 2017 06:36:58 +0200 Subject: [PATCH 3/5] Removed pragma that may be causing travis build problem --- picojson.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/picojson.h b/picojson.h index 4e6fa23..758d4f7 100644 --- a/picojson.h +++ b/picojson.h @@ -39,8 +39,6 @@ #include #if (!defined(PICOJSON_NO_EXCEPTIONS)) #include -#else -#pragma message("picojson no exceptions option") #endif // PICOJSON_NO_EXCEPTIONS) #include #include From c2693c8e9a1c5c83845006b90352fe28c51006bf Mon Sep 17 00:00:00 2001 From: crashmatt Date: Wed, 19 Apr 2017 06:49:27 +0200 Subject: [PATCH 4/5] Fixed missing build step for test --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 815b59f..135ffa0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ a.out test-core test-core-int64 +test-no-except From 38ccbccde3efbe586eff5ce53b74db87ec47e0a8 Mon Sep 17 00:00:00 2001 From: crashmatt Date: Wed, 19 Apr 2017 06:54:08 +0200 Subject: [PATCH 5/5] Really fix missing build step by committing the Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index a94bc3c..e787a3e 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ includedir=$(prefix)/include check: test -test: test-core test-core-int64 +test: test-core test-core-int64 test-no-except ./test-core ./test-core-int64 ./test-no-except