Commit f54f6b5a authored by Philipp A Hartmann's avatar Philipp A Hartmann Committed by Philipp A Hartmann

Add RAPIDJSON_NOEXCEPT_ASSERT

This is an alternative implementation to #1284 to handle
asserts in noexcept contexts.

Closes #1280.
parent 2bbd33b3
...@@ -37,9 +37,6 @@ RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible lo ...@@ -37,9 +37,6 @@ RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible lo
#ifdef __GNUC__ #ifdef __GNUC__
RAPIDJSON_DIAG_OFF(effc++) RAPIDJSON_DIAG_OFF(effc++)
#if __GNUC__ >= 6
RAPIDJSON_DIAG_OFF(terminate) // ignore throwing RAPIDJSON_ASSERT in RAPIDJSON_NOEXCEPT functions
#endif
#endif // __GNUC__ #endif // __GNUC__
#ifndef RAPIDJSON_NOMEMBERITERATORCLASS #ifndef RAPIDJSON_NOMEMBERITERATORCLASS
...@@ -625,11 +622,11 @@ public: ...@@ -625,11 +622,11 @@ public:
\note Default content for number is zero. \note Default content for number is zero.
*/ */
explicit GenericValue(Type type) RAPIDJSON_NOEXCEPT : data_() { explicit GenericValue(Type type) RAPIDJSON_NOEXCEPT : data_() {
static const uint16_t defaultFlags[7] = { static const uint16_t defaultFlags[] = {
kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kShortStringFlag, kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kShortStringFlag,
kNumberAnyFlag kNumberAnyFlag
}; };
RAPIDJSON_ASSERT(type >= kNullType && type <= kNumberType); RAPIDJSON_NOEXCEPT_ASSERT(type >= kNullType && type <= kNumberType);
data_.f.flags = defaultFlags[type]; data_.f.flags = defaultFlags[type];
// Use ShortString to store empty string. // Use ShortString to store empty string.
...@@ -831,9 +828,10 @@ public: ...@@ -831,9 +828,10 @@ public:
/*! \param rhs Source of the assignment. It will become a null value after assignment. /*! \param rhs Source of the assignment. It will become a null value after assignment.
*/ */
GenericValue& operator=(GenericValue& rhs) RAPIDJSON_NOEXCEPT { GenericValue& operator=(GenericValue& rhs) RAPIDJSON_NOEXCEPT {
RAPIDJSON_ASSERT(this != &rhs); if (RAPIDJSON_LIKELY(this != &rhs)) {
this->~GenericValue(); this->~GenericValue();
RawAssign(rhs); RawAssign(rhs);
}
return *this; return *this;
} }
......
...@@ -598,6 +598,25 @@ RAPIDJSON_NAMESPACE_END ...@@ -598,6 +598,25 @@ RAPIDJSON_NAMESPACE_END
//!@endcond //!@endcond
//! Assertion (in non-throwing contexts).
/*! \ingroup RAPIDJSON_CONFIG
Some functions provide a \c noexcept guarantee, if the compiler supports it.
In these cases, the \ref RAPIDJSON_ASSERT macro cannot be overridden to
throw an exception. This macro adds a separate customization point for
such cases.
Defaults to C \c assert() (as \ref RAPIDJSON_ASSERT), if \c noexcept is
supported, and to \ref RAPIDJSON_ASSERT otherwise.
*/
#ifndef RAPIDJSON_NOEXCEPT_ASSERT
#if RAPIDJSON_HAS_CXX11_NOEXCEPT
#include <cassert>
#define RAPIDJSON_NOEXCEPT_ASSERT(x) assert(x)
#else
#define RAPIDJSON_NOEXCEPT_ASSERT(x) RAPIDJSON_ASSERT(x)
#endif // RAPIDJSON_HAS_CXX11_NOEXCEPT
#endif // RAPIDJSON_NOEXCEPT_ASSERT
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// new/delete // new/delete
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment