Remove safe bool idiom (#1820)

This commit is contained in:
Benoit Blanchon
2023-02-13 11:12:41 +01:00
parent 8f7211a50f
commit 34dd46110b
3 changed files with 8 additions and 37 deletions

View File

@ -4,7 +4,6 @@
#pragma once
#include <ArduinoJson/Misc/SafeBoolIdiom.hpp>
#include <ArduinoJson/Namespace.hpp>
#include <ArduinoJson/Polyfills/pgmspace_generic.hpp>
#include <ArduinoJson/Polyfills/preprocessor.hpp>
@ -15,7 +14,7 @@
namespace ARDUINOJSON_NAMESPACE {
class DeserializationError : public SafeBoolIdom<DeserializationError> {
class DeserializationError {
public:
enum Code {
Ok,
@ -53,9 +52,9 @@ class DeserializationError : public SafeBoolIdom<DeserializationError> {
return lhs != rhs._code;
}
// Behaves like a bool
operator bool_type() const {
return _code != Ok ? safe_true() : safe_false();
// Returns true if there is an error
explicit operator bool() const {
return _code != Ok;
}
// Returns internal enum, useful for switch statement

View File

@ -1,26 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2022, Benoit BLANCHON
// MIT License
#pragma once
#include <ArduinoJson/Polyfills/type_traits.hpp>
namespace ARDUINOJSON_NAMESPACE {
template <typename T>
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

View File

@ -4,8 +4,6 @@
#pragma once
#include <ArduinoJson/Misc/SafeBoolIdiom.hpp>
#if ARDUINOJSON_ENABLE_STD_STREAM
# include <ostream>
#endif
@ -14,7 +12,7 @@ namespace ARDUINOJSON_NAMESPACE {
// A string.
// https://arduinojson.org/v6/api/jsonstring/
class JsonString : public SafeBoolIdom<JsonString> {
class JsonString {
public:
enum Ownership { Copied, Linked };
@ -47,9 +45,9 @@ class JsonString : public SafeBoolIdom<JsonString> {
return _size;
}
// safe bool idiom
operator bool_type() const {
return _data ? safe_true() : safe_false();
// Returns true if the string is non-null
explicit operator bool() const {
return _data != 0;
}
// Returns true if strings are equal.