11 #include "../daw_json_exception.h"
13 #include <daw/daw_hide.h>
14 #include <daw/daw_string_view.h>
23 #include <string_view>
25 #if defined( __GNUC__ ) or defined( __clang__ )
26 #define DAW_JSON_LIKELY( Bool ) ( __builtin_expect( !!( Bool ), 1 ) )
27 #define DAW_JSON_UNLIKELY( Bool ) ( __builtin_expect( !!( Bool ), 0 ) )
28 #define DAW_JSON_NOINLINE __attribute__( ( noinline ) )
29 #elif defined( _MSC_VER )
30 #define DAW_JSON_LIKELY( Bool ) !!( Bool )
31 #define DAW_JSON_UNLIKELY( Bool ) !!( Bool )
32 #define DAW_JSON_NOINLINE __declspec( noinline )
34 #define DAW_JSON_LIKELY( Bool ) !!( Bool )
35 #define DAW_JSON_UNLIKELY( Bool ) !!( Bool )
36 #define DAW_JSON_NOINLINE __attribute__( ( noinline ) )
39 #if not( defined( __cpp_exceptions ) or defined( __EXCEPTIONS ) or \
40 defined( _CPPUNWIND ) ) or \
41 defined( DAW_DONT_USE_JSON_EXCEPTIONS )
42 #ifdef DAW_USE_JSON_EXCEPTIONS
43 #undef DAW_USE_JSON_EXCEPTIONS
46 #ifndef DAW_USE_JSON_EXCEPTIONS
47 #define DAW_USE_JSON_EXCEPTIONS
51 #ifdef DAW_USE_JSON_EXCEPTIONS
57 template<
bool ShouldThrow = use_daw_json_exceptions_v>
60 #ifdef DAW_USE_JSON_EXCEPTIONS
61 if constexpr( ShouldThrow ) {
62 throw daw::json::json_exception( reason );
68 #ifdef DAW_USE_JSON_EXCEPTIONS
73 template<
bool ShouldThrow = use_daw_json_exceptions_v,
typename Range>
76 #ifdef DAW_USE_JSON_EXCEPTIONS
77 if constexpr( ShouldThrow ) {
78 if( location.first ) {
79 throw daw::json::json_exception( reason, location.first );
81 if( location.class_first ) {
82 throw daw::json::json_exception( reason, location.class_first );
84 throw daw::json::json_exception( reason );
91 #ifdef DAW_USE_JSON_EXCEPTIONS
96 template<
bool ShouldThrow = use_daw_json_exceptions_v>
99 #ifdef DAW_USE_JSON_EXCEPTIONS
100 if constexpr( ShouldThrow ) {
101 throw daw::json::json_exception( reason );
107 #ifdef DAW_USE_JSON_EXCEPTIONS
112 template<
bool ShouldThrow = use_daw_json_exceptions_v>
115 #ifdef DAW_USE_JSON_EXCEPTIONS
116 if constexpr( ShouldThrow ) {
117 throw daw::json::json_exception( reason );
123 #ifdef DAW_USE_JSON_EXCEPTIONS
128 template<
bool ShouldThrow = use_daw_json_exceptions_v,
typename Range>
131 Range
const &location ) {
132 #ifdef DAW_USE_JSON_EXCEPTIONS
133 if constexpr( ShouldThrow ) {
134 using namespace std::string_literals;
135 if( location.class_first and location.first ) {
136 static constexpr std::size_t max_len = 150;
137 std::size_t
const len = [&]( ) -> std::size_t {
138 if( location.first ==
nullptr or location.class_first ==
nullptr ) {
139 if( location.class_first ==
nullptr or
140 location.class_last ==
nullptr ) {
143 return std::min(
static_cast<std::size_t
>( std::distance(
144 location.class_first, location.class_last ) ),
147 return std::min(
static_cast<std::size_t
>( std::distance(
148 location.class_first, location.first + 1 ) ),
151 throw daw::json::json_exception(
152 reason, std::string_view( location.class_first, len ) );
154 throw daw::json::json_exception( reason );
162 #ifdef DAW_USE_JSON_EXCEPTIONS
167 template<
bool ShouldThrow = use_daw_json_exceptions_v,
typename Range>
170 Range
const &location ) {
171 #ifdef DAW_USE_JSON_EXCEPTIONS
172 if constexpr( ShouldThrow ) {
173 using namespace std::string_literals;
174 if( location.first ) {
175 throw daw::json::json_exception( reason, location.first );
177 if( location.class_first ) {
178 throw daw::json::json_exception( reason, location.class_first );
180 throw daw::json::json_exception( reason );
187 #ifdef DAW_USE_JSON_EXCEPTIONS
196 #define daw_json_assert( Bool, ... ) \
197 if( DAW_JSON_UNLIKELY( not( Bool ) ) ) { \
198 daw_json_error( __VA_ARGS__ ); \
206 #define daw_json_assert_weak( Bool, ... ) \
207 if constexpr( not Range::is_unchecked_input ) { \
208 if( DAW_JSON_UNLIKELY( not( Bool ) ) ) { \
209 daw_json_error( __VA_ARGS__ ); \