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 diff --git a/Makefile b/Makefile index a03f4a0..e787a3e 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,10 @@ 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 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..758d4f7 100644 --- a/picojson.h +++ b/picojson.h @@ -37,7 +37,9 @@ #include #include #include +#if (!defined(PICOJSON_NO_EXCEPTIONS)) #include +#endif // PICOJSON_NO_EXCEPTIONS) #include #include #include @@ -83,12 +85,18 @@ extern "C" { #endif #ifndef PICOJSON_ASSERT +#if (!defined(PICOJSON_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 // PICOJSON_NO_EXCEPTIONS +#endif // PICOJSON_ASSERT #ifdef _MSC_VER #define SNPRINTF _snprintf_s @@ -159,11 +167,20 @@ class value { ~value(); value(const value &x); value &operator=(const value &x); + #if PICOJSON_USE_RVALUE_REFERENCE +#if (!defined(PICOJSON_NO_EXCEPTIONS)) value(value &&x) throw(); value &operator=(value &&x) throw(); +#else + value(value &&x) value &operator=(value &&x) +#endif // PICOJSON_NO_EXCEPTIONS) #endif +#if (!defined(PICOJSON_NO_EXCEPTIONS)) void swap(value &x) throw(); +#else + void swap(value &x); +#endif // PICOJSON_NO_EXCEPTIONS) template bool is() const; template const T &get() const; template T &get(); @@ -237,7 +254,9 @@ inline value::value(double n) : type_(number_type), u_() { isnan(n) || isinf(n) #endif ) { +#if !defined(PICOJSON_NO_EXCEPTIONS) throw std::overflow_error(""); +#endif // PICOJSON_NO_EXCEPTIONS) } u_.number_ = n; } @@ -318,7 +337,7 @@ inline value &value::operator=(const value &x) { } return *this; } - +#if (!defined(PICOJSON_NO_EXCEPTIONS)) #if PICOJSON_USE_RVALUE_REFERENCE inline value::value(value &&x) throw() : type_(null_type), u_() { swap(x); @@ -332,6 +351,22 @@ inline void value::swap(value &x) throw() { std::swap(type_, x.type_); std::swap(u_, x.u_); } +#else // PICOJSON_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 // PICOJSON_NO_EXCEPTIONS #define IS(ctype, jtype) \ template <> inline bool value::is() const { \ diff --git a/test.cc b/test.cc index 0779c55..bca2fa3 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(PICOJSON_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 {