Skip to content

Commit f54f6b5

Browse files
committed
Add RAPIDJSON_NOEXCEPT_ASSERT
This is an alternative implementation to Tencent#1284 to handle asserts in noexcept contexts. Closes Tencent#1280.
1 parent 2bbd33b commit f54f6b5

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

include/rapidjson/document.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible lo
3737

3838
#ifdef __GNUC__
3939
RAPIDJSON_DIAG_OFF(effc++)
40-
#if __GNUC__ >= 6
41-
RAPIDJSON_DIAG_OFF(terminate) // ignore throwing RAPIDJSON_ASSERT in RAPIDJSON_NOEXCEPT functions
42-
#endif
4340
#endif // __GNUC__
4441

4542
#ifndef RAPIDJSON_NOMEMBERITERATORCLASS
@@ -625,11 +622,11 @@ class GenericValue {
625622
\note Default content for number is zero.
626623
*/
627624
explicit GenericValue(Type type) RAPIDJSON_NOEXCEPT : data_() {
628-
static const uint16_t defaultFlags[7] = {
625+
static const uint16_t defaultFlags[] = {
629626
kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kShortStringFlag,
630627
kNumberAnyFlag
631628
};
632-
RAPIDJSON_ASSERT(type >= kNullType && type <= kNumberType);
629+
RAPIDJSON_NOEXCEPT_ASSERT(type >= kNullType && type <= kNumberType);
633630
data_.f.flags = defaultFlags[type];
634631

635632
// Use ShortString to store empty string.
@@ -831,9 +828,10 @@ class GenericValue {
831828
/*! \param rhs Source of the assignment. It will become a null value after assignment.
832829
*/
833830
GenericValue& operator=(GenericValue& rhs) RAPIDJSON_NOEXCEPT {
834-
RAPIDJSON_ASSERT(this != &rhs);
835-
this->~GenericValue();
836-
RawAssign(rhs);
831+
if (RAPIDJSON_LIKELY(this != &rhs)) {
832+
this->~GenericValue();
833+
RawAssign(rhs);
834+
}
837835
return *this;
838836
}
839837

include/rapidjson/rapidjson.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,25 @@ RAPIDJSON_NAMESPACE_END
598598

599599
//!@endcond
600600

601+
//! Assertion (in non-throwing contexts).
602+
/*! \ingroup RAPIDJSON_CONFIG
603+
Some functions provide a \c noexcept guarantee, if the compiler supports it.
604+
In these cases, the \ref RAPIDJSON_ASSERT macro cannot be overridden to
605+
throw an exception. This macro adds a separate customization point for
606+
such cases.
607+
608+
Defaults to C \c assert() (as \ref RAPIDJSON_ASSERT), if \c noexcept is
609+
supported, and to \ref RAPIDJSON_ASSERT otherwise.
610+
*/
611+
#ifndef RAPIDJSON_NOEXCEPT_ASSERT
612+
#if RAPIDJSON_HAS_CXX11_NOEXCEPT
613+
#include <cassert>
614+
#define RAPIDJSON_NOEXCEPT_ASSERT(x) assert(x)
615+
#else
616+
#define RAPIDJSON_NOEXCEPT_ASSERT(x) RAPIDJSON_ASSERT(x)
617+
#endif // RAPIDJSON_HAS_CXX11_NOEXCEPT
618+
#endif // RAPIDJSON_NOEXCEPT_ASSERT
619+
601620
///////////////////////////////////////////////////////////////////////////////
602621
// new/delete
603622

0 commit comments

Comments
 (0)