8000 Added `DeserializationError::code()` to be used in switch statements … · java64/ArduinoJson@0a97d4c · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a97d4c

Browse files
committed
Added DeserializationError::code() to be used in switch statements (closes bblanchon#846)
1 parent 5eee947 commit 0a97d4c

File tree

3 files changed

+131
-15
lines changed

3 files changed

+131
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ HEAD
99
* Replaced `T JsonArray::get<T>(i)` with `JsonVariant JsonArray::get(i)`
1010
* Replaced `T JsonObject::get<T>(k)` with `JsonVariant JsonObject::get(k)`
1111
* Added `JSON_STRING_SIZE()`
12-
* Replacing or removing a value now releases the memory.
12+
* Replacing or removing a value now releases the memory
13+
* Added `DeserializationError::code()` to be used in switch statements (issue #846)
1314

1415
v6.5.0-beta (2018-10-13)
1516
-----------

src/ArduinoJson/Deserialization/DeserializationError.hpp

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
namespace ARDUINOJSON_NAMESPACE {
1212

1313
class DeserializationError {
14+
// safe bool idiom
15+
typedef void (DeserializationError::*bool_type)() const;
16+
void safeBoolHelper() const {}
17+
1418
public:
1519
enum Code {
1620
Ok,
@@ -22,26 +26,52 @@ class DeserializationError {
2226
};
2327

2428
DeserializationError() {}
25-
DeserializationError(Code code) : _code(code) {}
29+
DeserializationError(Code c) : _code(c) {}
2630

27-
friend bool operator==(const DeserializationError& err, Code code) {
28-
return err._code == code;
31+
// Compare with DeserializationError
32+
friend bool operator==(const DeserializationError& lhs,
33+
const DeserializationError& rhs) {
34+
return lhs._code == rhs._code;
2935
}
30-
31-
friend bool operator==(Code code, const DeserializationError& err) {
32-
return err._code == code;
36+
friend bool operator!=(const DeserializationError& lhs,
37+
const DeserializationError& rhs) {
38+
return lhs._code != rhs._code;
3339
}
3440

35-
friend bool operator!=(const DeserializationError& err, Code code) {
36-
return err._code != code;
41+
// Compare with Code
42+
friend bool operator==(const DeserializationError& lhs, Code rhs) {
43+
return lhs._code == rhs;
44+
}
45+
friend bool operator==(Code lhs, const DeserializationError& rhs) {
46+
return lhs == rhs._code;
47+
}
48+
friend bool operator!=(const DeserializationError& lhs, Code rhs) {
49+
return lhs._code != rhs;
50+
}
51+
friend bool operator!=(Code lhs, const DeserializationError& rhs) {
52+
return lhs != rhs._code;
3753
}
3854

39-
friend bool operator!=(Code code, const DeserializationError& err) {
40-
return err._code != code;
55+
// Behaves like a bool
56+
operator bool_type() const {
57+
return _code != Ok ? &DeserializationError::safeBoolHelper : 0;
58+
}
59+
friend bool operator==(bool value, const DeserializationError& err) {
60+
return static_cast<bool>(err) == value;
61+
}
62+
friend bool operator==(const DeserializationError& err, bool value) {
63+
return static_cast<bool>(err) == value;
64+
}
65+
friend bool operator!=(bool value, const DeserializationError& err) {
66+
return static_cast<bool>(err) != value;
67+
}
68+
friend bool operator!=(const DeserializationError& err, bool value) {
69+
return static_cast<bool>(err) != value;
4170
}
4271

43-
operator bool() const {
44-
return _code != Ok;
72+
// Returns internal enum, useful for switch statement
73+
Code code() const {
74+
return _code;
4575
}
4676

4777
const char* c_str() const {

test/JsonDeserializer/DeserializationError.cpp

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ void testStringification(DeserializationError error, std::string expected) {
1010
}
1111

1212
57AE void testBoolification(DeserializationError error, bool expected) {
13+
// DeserializationError on left-hand side
1314
CHECK(error == expected);
15+
CHECK(error != !expected);
16+
CHECK(!error == !expected);
17+
18+
// DeserializationError on right-hand side
19+
CHECK(expected == error);
20+
CHECK(!expected != error);
21+
CHECK(!expected == !error);
1422
}
1523

1624
#define TEST_STRINGIFICATION(symbol) \
@@ -38,15 +46,92 @@ TEST_CASE("DeserializationError") {
3846
TEST_BOOLIFICATION(NotSupported, true);
3947
}
4048

41-
SECTION("ostream code") {
49+
SECTION("ostream DeserializationError") {
4250
std::stringstream s;
4351
s << DeserializationError(DeserializationError::InvalidInput);
4452
REQUIRE(s.str() == "InvalidInput");
4553
}
4654

47-
SECTION("ostream code") {
55+
SECTION("ostream DeserializationError::Code") {
4856
std::stringstream s;
4957
s << DeserializationError::InvalidInput;
5058
REQUIRE(s.str() == "InvalidInput");
5159
}
60+
61+
SECTION("out of range") {
62+
int code = 666;
63+
DeserializationError err(
64+
*reinterpret_cast<DeserializationError::Code*>(&code));
65+
REQUIRE(err.c_str() == std::string("???"));
66+
}
67+
68+
SECTION("switch") {
69+
DeserializationError err = DeserializationError::InvalidInput;
70+
switch (err.code()) {
71+
case DeserializationError::InvalidInput:
72+
SUCCEED();
73+
break;
74+
default:
75+
FAIL();
76+
break;
77+
}
78+
}
79+
80+
SECTION("Comparisons") {
81+
DeserializationError invalidInput(DeserializationError::InvalidInput);
82+
DeserializationError ok(DeserializationError::Ok);
83+
84+
SECTION("DeserializationError == bool") {
85+
REQUIRE(invalidInput == true);
86+
REQUIRE(ok == false);
87+
}
88+
89+
SECTION("bool == DeserializationError") {
90+
REQUIRE(true == invalidInput);
91+
REQUIRE(false == ok);
92+
}
93+
94+
SECTION("DeserializationError != bool") {
95+
REQUIRE(invalidInput != false);
96+
REQUIRE(ok != true);
97+
}
98+
99+
SECTION("bool != DeserializationError") {
100+
REQUIRE(false != invalidInput);
101+
REQUIRE(true != ok);
102+
}
103+
104+
SECTION("Negations") {
105+
REQUIRE(!invalidInput == false);
106+
REQUIRE(!ok == true);
107+
}
108+
109+
SECTION("DeserializationError == Code") {
110+
REQUIRE(invalidInput == DeserializationError::InvalidInput);
111+
REQUIRE(ok == DeserializationError::Ok);
112+
}
113+
114+
SECTION("Code == DeserializationError") {
115+
REQUIRE(DeserializationError::InvalidInput == invalidInput);
116+
REQUIRE(DeserializationError::Ok == ok);
117+
}
118+
119+
SECTION("DeserializationError != Code") {
120+
REQUIRE(invalidInput != DeserializationError::Ok);
121+
REQUIRE(ok != DeserializationError::InvalidInput);
122+
}
123+
124+
SECTION("Code != DeserializationError") {
125+
REQUIRE(DeserializationError::Ok != invalidInput);
126+
REQUIRE(DeserializationError::InvalidInput != ok);
127+
}
128+
129+
SECTION("DeserializationError == DeserializationError") {
130+
REQUIRE_FALSE(invalidInput == ok);
131+
}
132+
133+
SECTION("DeserializationError != DeserializationError") {
134+
REQUIRE(invalidInput != ok);
135+
}
136+
}
52137
}

0 commit comments

Comments
 (0)
0