Skip to content

Commit 2570898

Browse files
authored
Merge pull request simdjson#958 from simdjson/jkeiser/is
Make simdjson_result<element>.is() return bool
2 parents c650ea9 + 2d84b6f commit 2570898

File tree

4 files changed

+40
-87
lines changed

4 files changed

+40
-87
lines changed

include/simdjson/dom/element.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ struct simdjson_result<dom::element> : public internal::simdjson_result_base<dom
455455

456456
really_inline simdjson_result<dom::element_type> type() const noexcept;
457457
template<typename T>
458-
really_inline simdjson_result<bool> is() const noexcept;
458+
really_inline bool is() const noexcept;
459459
template<typename T>
460460
really_inline simdjson_result<T> get() const noexcept;
461461
template<typename T>
@@ -470,14 +470,14 @@ struct simdjson_result<dom::element> : public internal::simdjson_result_base<dom
470470
really_inline simdjson_result<double> get_double() const noexcept;
471471
really_inline simdjson_result<bool> get_bool() const noexcept;
472472

473-
really_inline simdjson_result<bool> is_array() const noexcept;
474-
really_inline simdjson_result<bool> is_object() const noexcept;
475-
really_inline simdjson_result<bool> is_string() const noexcept;
476-
really_inline simdjson_result<bool> is_int64_t() const noexcept;
477-
really_inline simdjson_result<bool> is_uint64_t() const noexcept;
478-
really_inline simdjson_result<bool> is_double() const noexcept;
479-
really_inline simdjson_result<bool> is_bool() const noexcept;
480-
really_inline simdjson_result<bool> is_null() const noexcept;
473+
really_inline bool is_array() const noexcept;
474+
really_inline bool is_object() const noexcept;
475+
really_inline bool is_string() const noexcept;
476+
really_inline bool is_int64_t() const noexcept;
477+
really_inline bool is_uint64_t() const noexcept;
478+
really_inline bool is_double() const noexcept;
479+
really_inline bool is_bool() const noexcept;
480+
really_inline bool is_null() const noexcept;
481481

482482
really_inline simdjson_result<dom::element> operator[](const std::string_view &key) const noexcept;
483483
really_inline simdjson_result<dom::element> operator[](const char *key) const noexcept;

include/simdjson/inline/element.h

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ inline simdjson_result<dom::element_type> simdjson_result<dom::element>::type()
2424
}
2525

2626
template<typename T>
27-
really_inline simdjson_result<bool> simdjson_result<dom::element>::is() const noexcept {
28-
if (error()) { return error(); }
29-
return first.is<T>();
27+
really_inline bool simdjson_result<dom::element>::is() const noexcept {
28+
return !error() && first.is<T>();
3029
}
3130
template<typename T>
3231
really_inline simdjson_result<T> simdjson_result<dom::element>::get() const noexcept {
@@ -72,38 +71,30 @@ really_inline simdjson_result<bool> simdjson_result<dom::element>::get_bool() co
7271
return first.get_bool();
7372
}
7473

75-
really_inline simdjson_result<bool> simdjson_result<dom::element>::is_array() const noexcept {
76-
if (error()) { return error(); }
77-
return first.is_array();
74+
really_inline bool simdjson_result<dom::element>::is_array() const noexcept {
75+
return !error() && first.is_array();
7876
}
79-
really_inline simdjson_result<bool> simdjson_result<dom::element>::is_object() const noexcept {
80-
if (error()) { return error(); }
81-
return first.is_object();
77+
really_inline bool simdjson_result<dom::element>::is_object() const noexcept {
78+
return !error() && first.is_object();
8279
}
83-
really_inline simdjson_result<bool> simdjson_result<dom::element>::is_string() const noexcept {
84-
if (error()) { return error(); }
85-
return first.is_string();
80+
really_inline bool simdjson_result<dom::element>::is_string() const noexcept {
81+
return !error() && first.is_string();
8682
}
87-
really_inline simdjson_result<bool> simdjson_result<dom::element>::is_int64_t() const noexcept {
88-
if (error()) { return error(); }
89-
return first.is_int64_t();
83+
really_inline bool simdjson_result<dom::element>::is_int64_t() const noexcept {
84+
return !error() && first.is_int64_t();
9085
}
91-
really_inline simdjson_result<bool> simdjson_result<dom::element>::is_uint64_t() const noexcept {
92-
if (error()) { return error(); }
93-
return first.is_uint64_t();
86+
really_inline bool simdjson_result<dom::element>::is_uint64_t() const noexcept {
87+
return !error() && first.is_uint64_t();
9488
}
95-
really_inline simdjson_result<bool> simdjson_result<dom::element>::is_double() const noexcept {
96-
if (error()) { return error(); }
97-
return first.is_double();
89+
really_inline bool simdjson_result<dom::element>::is_double() const noexcept {
90+
return !error() && first.is_double();
9891
}
99-
really_inline simdjson_result<bool> simdjson_result<dom::element>::is_bool() const noexcept {
100-
if (error()) { return error(); }
101-
return first.is_bool();
92+
really_inline bool simdjson_result<dom::element>::is_bool() const noexcept {
93+
return !error() && first.is_bool();
10294
}
10395

104-
really_inline simdjson_result<bool> simdjson_result<dom::element>::is_null() const noexcept {
105-
if (error()) { return error(); }
106-
return first.is_null();
96+
really_inline bool simdjson_result<dom::element>::is_null() const noexcept {
97+
return !error() && first.is_null();
10798
}
10899

109100
really_inline simdjson_result<dom::element> simdjson_result<dom::element>::operator[](const std::string_view &key) const noexcept {

tests/basictests.cpp

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,27 +1441,9 @@ namespace type_tests {
14411441
std::cout << " test_is_null() expecting " << expected_is_null << std::endl;
14421442
// Grab the element out and check success
14431443
dom::element element = result.first;
1444-
bool actual_is_null;
1445-
auto error = result.is_null().get(actual_is_null);
1446-
ASSERT_SUCCESS(error);
1447-
ASSERT_EQUAL(actual_is_null, expected_is_null);
1448-
1449-
actual_is_null = element.is_null();
1450-
ASSERT_EQUAL(actual_is_null, expected_is_null);
1451-
1452-
#if SIMDJSON_EXCEPTIONS
1453-
1454-
try {
1455-
1456-
actual_is_null = result.is_null();
1457-
ASSERT_EQUAL(actual_is_null, expected_is_null);
1444+
ASSERT_EQUAL(result.is_null(), expected_is_null);
14581445

1459-
} catch(simdjson_error &e) {
1460-
std::cerr << e.error() << std::endl;
1461-
return false;
1462-
}
1463-
1464-
#endif // SIMDJSON_EXCEPTIONS
1446+
ASSERT_EQUAL(element.is_null(), expected_is_null);
14651447

14661448
return true;
14671449
}

tests/cast_tester.h

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class cast_tester {
4040

4141
bool test_is(element element, bool expected);
4242
bool test_is(simdjson_result<element> element, bool expected);
43-
bool test_is_error(simdjson_result<element> element, error_code expected_error);
4443

4544
bool test_named_get(element element, T expected = {});
4645
bool test_named_get(simdjson_result<element> element, T expected = {});
@@ -49,13 +48,12 @@ class cast_tester {
4948

5049
bool test_named_is(element element, bool expected);
5150
bool test_named_is(simdjson_result<element> element, bool expected);
52-
bool test_named_is_error(simdjson_result<element> element, error_code expected_error);
5351

5452
private:
5553
simdjson_result<T> named_get(element element);
5654
simdjson_result<T> named_get(simdjson_result<element> element);
5755
bool named_is(element element);
58-
simdjson_result<bool> named_is(simdjson_result<element> element);
56+
bool named_is(simdjson_result<element> element);
5957
bool assert_equal(const T& expected, const T& actual);
6058
};
6159

@@ -206,16 +204,7 @@ bool cast_tester<T>::test_is(element element, bool expected) {
206204

207205
template<typename T>
208206
bool cast_tester<T>::test_is(simdjson_result<element> element, bool expected) {
209-
bool actual;
210-
ASSERT_SUCCESS(element.is<T>().get(actual));
211-
ASSERT_EQUAL(actual, expected);
212-
return true;
213-
}
214-
215-
template<typename T>
216-
bool cast_tester<T>::test_is_error(simdjson_result<element> element, error_code expected_error) {
217-
UNUSED bool actual;
218-
ASSERT_EQUAL(element.is<T>().get(actual), expected_error);
207+
ASSERT_EQUAL(element.is<T>(), expected);
219208
return true;
220209
}
221210

@@ -227,16 +216,7 @@ bool cast_tester<T>::test_named_is(element element, bool expected) {
227216

228217
template<typename T>
229218
bool cast_tester<T>::test_named_is(simdjson_result<element> element, bool expected) {
230-
bool actual;
231-
ASSERT_SUCCESS(named_is(element).get(actual));
232-
ASSERT_EQUAL(actual, expected);
233-
return true;
234-
}
235-
236-
template<typename T>
237-
bool cast_tester<T>::test_named_is_error(simdjson_result<element> element, error_code expected_error) {
238-
bool actual;
239-
ASSERT_EQUAL(named_is(element).get(actual), expected_error);
219+
ASSERT_EQUAL(named_is(element), expected);
240220
return true;
241221
}
242222

@@ -267,14 +247,14 @@ template<> bool cast_tester<int64_t>::named_is(element element) { return element
267247
template<> bool cast_tester<double>::named_is(element element) { return element.is_double(); }
268248
template<> bool cast_tester<bool>::named_is(element element) { return element.is_bool(); }
269249

270-
template<> simdjson_result<bool> cast_tester<array>::named_is(simdjson_result<element> element) { return element.is_array(); }
271-
template<> simdjson_result<bool> cast_tester<object>::named_is(simdjson_result<element> element) { return element.is_object(); }
272-
template<> simdjson_result<bool> cast_tester<const char *>::named_is(simdjson_result<element> element) { return element.is_string(); }
273-
template<> simdjson_result<bool> cast_tester<std::string_view>::named_is(simdjson_result<element> element) { return element.is_string(); }
274-
template<> simdjson_result<bool> cast_tester<uint64_t>::named_is(simdjson_result<element> element) { return element.is_uint64_t(); }
275-
template<> simdjson_result<bool> cast_tester<int64_t>::named_is(simdjson_result<element> element) { return element.is_int64_t(); }
276-
template<> simdjson_result<bool> cast_tester<double>::named_is(simdjson_result<element> element) { return element.is_double(); }
277-
template<> simdjson_result<bool> cast_tester<bool>::named_is(simdjson_result<element> element) { return element.is_bool(); }
250+
template<> bool cast_tester<array>::named_is(simdjson_result<element> element) { return element.is_array(); }
251+
template<> bool cast_tester<object>::named_is(simdjson_result<element> element) { return element.is_object(); }
252+
template<> bool cast_tester<const char *>::named_is(simdjson_result<element> element) { return element.is_string(); }
253+
template<> bool cast_tester<std::string_view>::named_is(simdjson_result<element> element) { return element.is_string(); }
254+
template<> bool cast_tester<uint64_t>::named_is(simdjson_result<element> element) { return element.is_uint64_t(); }
255+
template<> bool cast_tester<int64_t>::named_is(simdjson_result<element> element) { return element.is_int64_t(); }
256+
template<> bool cast_tester<double>::named_is(simdjson_result<element> element) { return element.is_double(); }
257+
template<> bool cast_tester<bool>::named_is(simdjson_result<element> element) { return element.is_bool(); }
278258

279259
template<typename T> bool cast_tester<T>::assert_equal(const T& expected, const T& actual) {
280260
ASSERT_EQUAL(expected, actual);

0 commit comments

Comments
 (0)