From acfa174333471ea25fa1ef74e2d9c2542dff942a Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Fri, 22 Oct 2021 17:19:14 +0200 Subject: [PATCH] Add safe bool idiom in `JsonString` --- CHANGELOG.md | 1 + extras/tests/Misc/JsonString.cpp | 17 ++++++++++++ .../Deserialization/DeserializationError.hpp | 9 +++---- src/ArduinoJson/Misc/SafeBoolIdiom.hpp | 26 +++++++++++++++++++ src/ArduinoJson/Strings/String.hpp | 9 ++++++- 5 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 src/ArduinoJson/Misc/SafeBoolIdiom.hpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ad7200c..77dc0926 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ HEAD * Change the default of `ARDUINOJSON_USE_DOUBLE` to `1` * Change the default of `ARDUINOJSON_USE_LONG_LONG` to `1` on 32-bit platforms * Add `as()` and `is()` +* Add safe bool idiom in `JsonString` v6.18.5 (2021-09-28) ------- diff --git a/extras/tests/Misc/JsonString.cpp b/extras/tests/Misc/JsonString.cpp index eb251465..9ef8dc48 100644 --- a/extras/tests/Misc/JsonString.cpp +++ b/extras/tests/Misc/JsonString.cpp @@ -14,6 +14,23 @@ TEST_CASE("JsonString") { CHECK(s.isStatic() == true); } + SECTION("Compare null with boolean") { + JsonString s; + + CHECK(bool(s) == false); + CHECK(false == bool(s)); + CHECK(bool(s) != true); + CHECK(true != bool(s)); + } + + SECTION("Compare non-null with boolean") { + JsonString s("hello"); + CHECK(bool(s) == true); + CHECK(true == bool(s)); + CHECK(bool(s) != false); + CHECK(false != bool(s)); + } + SECTION("Compare null with null") { JsonString a, b; diff --git a/src/ArduinoJson/Deserialization/DeserializationError.hpp b/src/ArduinoJson/Deserialization/DeserializationError.hpp index 11ec7df6..5248a471 100644 --- a/src/ArduinoJson/Deserialization/DeserializationError.hpp +++ b/src/ArduinoJson/Deserialization/DeserializationError.hpp @@ -4,6 +4,7 @@ #pragma once +#include #include #include #include @@ -14,11 +15,7 @@ namespace ARDUINOJSON_NAMESPACE { -class DeserializationError { - // safe bool idiom - typedef void (DeserializationError::*bool_type)() const; - void safeBoolHelper() const {} - +class DeserializationError : public SafeBoolIdom { public: enum Code { Ok, @@ -58,7 +55,7 @@ class DeserializationError { // Behaves like a bool operator bool_type() const { - return _code != Ok ? &DeserializationError::safeBoolHelper : 0; + return _code != Ok ? safe_true() : safe_false(); } friend bool operator==(bool value, const DeserializationError& err) { return static_cast(err) == value; diff --git a/src/ArduinoJson/Misc/SafeBoolIdiom.hpp b/src/ArduinoJson/Misc/SafeBoolIdiom.hpp new file mode 100644 index 00000000..4b851d9c --- /dev/null +++ b/src/ArduinoJson/Misc/SafeBoolIdiom.hpp @@ -0,0 +1,26 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#pragma once + +#include + +namespace ARDUINOJSON_NAMESPACE { + +template +class SafeBoolIdom { + protected: + typedef void (T::*bool_type)() const; + void safeBoolHelper() const {} + + static bool_type safe_true() { + return &SafeBoolIdom::safeBoolHelper; + } + + static bool_type safe_false() { + return 0; + } +}; + +} // namespace ARDUINOJSON_NAMESPACE diff --git a/src/ArduinoJson/Strings/String.hpp b/src/ArduinoJson/Strings/String.hpp index fff4077d..4573e793 100644 --- a/src/ArduinoJson/Strings/String.hpp +++ b/src/ArduinoJson/Strings/String.hpp @@ -4,9 +4,11 @@ #pragma once +#include + namespace ARDUINOJSON_NAMESPACE { -class String { +class String : public SafeBoolIdom { public: String() : _data(0), _isStatic(true) {} String(const char* data, bool isStaticData = true) @@ -24,6 +26,11 @@ class String { return _isStatic; } + // safe bool idiom + operator bool_type() const { + return _data ? safe_true() : safe_false(); + } + friend bool operator==(String lhs, String rhs) { if (lhs._data == rhs._data) return true;