8000 Add SIMDJSON_EXCEPTIONS=ON to turn on exception interface · JavaScriptExpert/simdjson@03c828c · GitHub
[go: up one dir, main page]

Skip to content

Commit 03c828c

Browse files
committed
Add SIMDJSON_EXCEPTIONS=ON to turn on exception interface
1 parent 758dc51 commit 03c828c

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

CMakeLists.txt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,35 @@ project(simdjson
1818
# set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
1919
#endif()
2020

21-
# usage: cmake -DSIMDJSON_DISABLE_AVX=on ..
22-
option(SIMDJSON_DISABLE_AVX "Forcefully disable AVX even if hardware supports it" OFF)
23-
2421
set(CMAKE_CXX_STANDARD 17)
2522
set(CMAKE_CXX_STANDARD_REQUIRED ON)
2623
set(CMAKE_MACOSX_RPATH OFF)
2724
set(CMAKE_THREAD_PREFER_PTHREAD ON)
2825
set(THREADS_PREFER_PTHREAD_FLAG ON)
2926

30-
31-
3227
set(SIMDJSON_LIB_NAME simdjson)
3328
set(PROJECT_VERSION_MAJOR 0)
3429
set(PROJECT_VERSION_MINOR 2)
3530
set(PROJECT_VERSION_PATCH 1)
3631
set(SIMDJSON_LIB_VERSION "0.2.1" CACHE STRING "simdjson library version")
3732
set(SIMDJSON_LIB_SOVERSION "0" CACHE STRING "simdjson library soversion")
3833

34+
option(SIMDJSON_DISABLE_AVX "Forcefully disable AVX even if hardware supports it" OFF)
3935
if(NOT MSVC)
4036
option(SIMDJSON_BUILD_STATIC "Build a static library" OFF) # turning it on disables the production of a dynamic library
4137
else()
4238
option(SIMDJSON_BUILD_STATIC "Build a static library" ON) # turning it on disables the production of a dynamic library
4339
endif()
4440
option(SIMDJSON_SANITIZE "Sanitize addresses" OFF)
41+
option(SIMDJSON_GOOGLE_BENCHMARKS "compile the Google Benchmark benchmarks" OFF)
42+
option(SIMDJSON_ENABLE_THREADS "enable threaded operation" ON)
43+
option(SIMDJSON_EXCEPTIONS "Enable simdjson's exception-throwing interface" ON)
4544

4645
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake")
4746

4847
find_package(CTargets)
4948
find_package(Options)
5049

51-
option(SIMDJSON_ENABLE_THREADS "enable threaded operation" ON)
5250
install(DIRECTORY include/${SIMDJSON_LIB_NAME} DESTINATION include)
5351
set (TEST_DATA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/jsonchecker/")
5452
set (BENCHMARK_DATA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/jsonexamples/")
@@ -61,7 +59,6 @@ add_subdirectory(tools)
6159
add_subdirectory(tests)
6260
add_subdirectory(benchmark)
6361

64-
option(SIMDJSON_GOOGLE_BENCHMARKS "compile the Google Benchmark benchmarks" OFF)
6562
if (SIMDJSON_GOOGLE_BENCHMARKS)
6663
if(NOT EXISTS dependencies/benchmark/CMakeLists.txt)
6764
# message(STATUS "Unable to find dependencies/benchmark/CMakeLists.txt")

include/simdjson/common_defs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66

77
namespace simdjson {
88

9+
#ifndef SIMDJSON_EXCEPTIONS
10+
#ifdef __cpp_exceptions
11+
#define SIMDJSON_EXCEPTIONS 1
12+
#else
13+
#define SIMDJSON_EXCEPTIONS 0
14+
#endif
15+
#endif
16+
917
/** The maximum document size supported by simdjson. */
1018
constexpr size_t SIMDJSON_MAXSIZE_BYTES = 0xFFFFFFFF;
1119

include/simdjson/document.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class document {
8484
* Get the root element of this document.
8585
*/
8686
operator element() const noexcept;
87+
88+
#if SIMDJSON_EXCEPTIONS
8789
/**
8890
* Read the root element of this document as a JSON array.
8991
*
@@ -98,6 +100,7 @@ class document {
98100
* @exception simdjson_error(UNEXPECTED_TYPE) if the JSON element is not an object
99101
*/
100102
operator object() const noexcept(false);
103+
#endif // SIMDJSON_EXCEPTIONS
101104

102105
/**
103106
* Get the value associated with the given key.
341A
@@ -253,13 +256,15 @@ class document::doc_result {
253256
*/
254257
error_code error;
255258

259+
#if SIMDJSON_EXCEPTIONS
256260
/**
257261
* Return the document, or throw an exception if it is invalid.
258262
*
259263
* @return the document.
260264
* @exception simdjson_error if the document is invalid or there was an error parsing it.
261265
*/
262266
operator document() noexcept(false);
267+
#endif // SIMDJSON_EXCEPTIONS
263268

264269
/**
265270
* Get the value associated with the given key.
@@ -339,13 +344,15 @@ class document::doc_ref_result {
339344
*/
340345
error_code error;
341346

347+
#if SIMDJSON_EXCEPTIONS
342348
/**
343349
* A reference to the document, or throw an exception if it is invalid.
344350
*
345351
* @return the document.
346352
* @exception simdjson_error if the document is invalid or there was an error parsing it.
347353
*/
348354
operator document&() noexcept(false);
355+
#endif // SIMDJSON_EXCEPTIONS
349356

350357
/**
351358
* Get the value associated with the given key.
@@ -521,6 +528,7 @@ class document::element : protected document::tape_ref {
521528
*/
522529
inline element_result<document::object> as_object() const noexcept;
523530

531+
#if SIMDJSON_EXCEPTIONS
524532
/**
525533
* Read this element as a boolean.
526534
*
@@ -589,6 +597,7 @@ class document::element : protected document::tape_ref {
589597
* @exception simdjson_error(UNEXPECTED_TYPE) if the JSON element is not an object
590598
*/
591599
inline operator document::object() const noexcept(false);
600+
#endif // SIMDJSON_EXCEPTIONS
592601

593602
/**
594603
* Get the value associated with the given key.
@@ -806,7 +815,9 @@ class document::element_result {
806815
/** The error code (or 0 if there is no error) */
807816
error_code error;
808817

818+
#if SIMDJSON_EXCEPTIONS
809819
inline operator T() const noexcept(false);
820+
#endif // SIMDJSON_EXCEPTIONS
810821

811822
private:
812823
really_inline element_result(T value) noexcept;
@@ -835,6 +846,7 @@ class document::element_result<document::element> {
835846
inline element_result<array> as_array() const noexcept;
836847
inline element_result<object> as_object() const noexcept;
837848

849+
#if SIMDJSON_EXCEPTIONS
838850
inline operator element() const noexcept(false);
839851
inline operator bool() const noexcept(false);
840852
inline explicit operator const char*() const noexcept(false);
@@ -844,6 +856,7 @@ class document::element_result<document::element> {
844856
inline operator double() const noexcept(false);
845857
inline operator array() const noexcept(false);
846858
inline operator object() const noexcept(false);
859+
#endif // SIMDJSON_EXCEPTIONS
847860

848861
inline element_result<element> operator[](const std::string_view &s) const noexcept;
849862
inline element_result<element> operator[](const char *s) const noexcept;
@@ -864,10 +877,12 @@ class document::element_result<document::array> {
864877
/** The error code (or 0 if there is no error) */
865878
error_code error;
866879

880+
#if SIMDJSON_EXCEPTIONS
867881
inline operator array() const noexcept(false);
868882

869883
inline array::iterator begin() const noexcept(false);
870884
inline array::iterator end() const noexcept(false);
885+
#endif // SIMDJSON_EXCEPTIONS
871886

872887
private:
873888
really_inline element_result(array value) noexcept;
@@ -885,10 +900,12 @@ class document::element_result<document::object> {
885900
/** The error code (or 0 if there is no error) */
886901
error_code error;
887902

903+
#if SIMDJSON_EXCEPTIONS
888904
inline operator object() const noexcept(false);
889905

890906
inline object::iterator begin() const noexcept(false);
891907
inline object::iterator end() const noexcept(false);
908+
#endif // SIMDJSON_EXCEPTIONS
892909

893910
inline element_result<element> operator[](const std::string_view &s) const noexcept;
894911
inline element_result<element> operator[](const char *s) const noexcept;
@@ -1593,8 +1610,10 @@ class document::parser {
15931610
// and auto-allocate if not.
15941611
inline error_code ensure_capacity(size_t desired_capacity) noexcept;
15951612

1613+
#if SIMDJSON_EXCEPTIONS
15961614
// Used internally to get the document
15971615
inline const document &get_document() const noexcept(false);
1616+
#endif // SIMDJSON_EXCEPTIONS
15981617

15991618
template<size_t max_depth> friend class document_iterator;
16001619
friend class document::stream;

include/simdjson/inline/document.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ template<typename T>
1919
inline document::element_result<T>::element_result(T _value) noexcept : value(_value), error{SUCCESS} {}
2020
template<typename T>
2121
inline document::element_result<T>::element_result(error_code _error) noexcept : value(), error{_error} {}
22+
23+
#if SIMDJSON_EXCEPTIONS
24+
2225
template<>
2326
inline document::element_result<std::string_view>::operator std::string_view() const noexcept(false) {
2427
if (error) { throw simdjson_error(error); }
@@ -50,11 +53,16 @@ inline document::element_result<double>::operator double() const noexcept(false)
5053
return value;
5154
}
5255

56+
#endif // SIMDJSON_EXCEPTIONS
57+
5358
//
5459
// document::element_result<document::array> inline implementation
5560
//
5661
inline document::element_result<document::array>::element_result(document::array _value) noexcept : value(_value), error{SUCCESS} {}
5762
inline document::element_result<document::array>::element_result(error_code _error) noexcept : value(), error{_error} {}
63+
64+
#if SIMDJSON_EXCEPTIONS
65+
5866
inline document::element_result<document::array>::operator document::array() const noexcept(false) {
5967
if (error) { throw simdjson_error(error); }
6068
return value;
@@ -68,11 +76,16 @@ inline document::array::iterator document::element_result<document::array>::end(
6876
return value.end();
6977
}
7078

79+
#endif // SIMDJSON_EXCEPTIONS
80+
7181
//
7282
// document::element_result<document::object> inline implementation
7383
//
7484
inline document::element_result<document::object>::element_result(document::object _value) noexcept : value(_value), error{SUCCESS} {}
7585
inline document::element_result<document::object>::element_result(error_code _error) noexcept : value(), error{_error} {}
86+
87+
#if SIMDJSON_EXCEPTIONS
88+
7689
inline document::element_result<document::object>::operator document::object() const noexcept(false) {
7790
if (error) { throw simdjson_error(error); }
7891
return value;
@@ -94,6 +107,8 @@ inline document::object::iterator document::element_result<document::object>::en
94107
return value.end();
95108
}
96109

110+
#endif // SIMDJSON_EXCEPTIONS
111+
97112
//
98113
// document::element_result<document::element> inline implementation
99114
//
@@ -341,6 +356,9 @@ inline bool document::dump_raw_tape(std::ostream &os) const noexcept {
341356
// document::doc_ref_result inline implementation
342357
//
343358
inline document::doc_ref_result::doc_ref_result(document &_doc, error_code _error) noexcept : doc(_doc), error(_error) { }
359+
360+
#if SIMDJSON_EXCEPTIONS
361+
344362
inline document::doc_ref_result::operator document&() noexcept(false) {
345363
if (error) { throw simdjson_error(error); }
346364
return doc;
@@ -354,12 +372,17 @@ inline document::element_result<document::element> document::doc_ref_result::ope
354372
return doc[key];
355373
}
356374

375+
#endif // SIMDJSON_EXCEPTIONS
376+
357377
//
358378
// document::doc_result inline implementation
359379
//
360380
inline document::doc_result::doc_result(document &&_doc, error_code _error) noexcept : doc(std::move(_doc)), error(_error) { }
361381
inline document::doc_result::doc_result(document &&_doc) noexcept : doc(std::move(_doc)), error(SUCCESS) { }
362382
inline document::doc_result::doc_result(error_code _error) noexcept : doc(), error(_error) { }
383+
E707 384+
#if SIMDJSON_EXCEPTIONS
385+
363386
inline document::doc_result::operator document() noexcept(false) {
364387
if (error) { throw simdjson_error(error); }
365388
return std::move(doc);
@@ -373,6 +396,8 @@ inline document::element_result<document::element> document::doc_result::operato
373396
return doc[key];
374397
}
375398

399+
#endif // SIMDJSON_EXCEPTIONS
400+
376401
//
377402
// document::parser inline implementation
378403
//
@@ -391,13 +416,18 @@ inline bool document::parser::print_json(std::ostream &os) const noexcept {
391416
inline bool document::parser::dump_raw_tape(std::ostream &os) const noexcept {
392417
return is_valid() ? doc.dump_raw_tape(os) : false;
393418
}
419+
420+
#if SIMDJSON_EXCEPTIONS
421+
394422
inline const document &document::parser::get_document() const noexcept(false) {
395423
if (!is_valid()) {
396424
throw simdjson_error(error);
397425
}
398426
return doc;
399427
}
400428

429+
#endif // SIMDJSON_EXCEPTIONS
430+
401431
inline document::doc_ref_result document::parser::load(const std::string &path) noexcept {
402432
auto [json, _error] = padded_string::load(path);
403433
if (_error) { return doc_ref_result(doc, _error); }

0 commit comments

Comments
 (0)
0