forked from bblanchon/ArduinoJson
Add safe bool idiom in JsonString
This commit is contained in:
@ -4,6 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Misc/SafeBoolIdiom.hpp>
|
||||
#include <ArduinoJson/Namespace.hpp>
|
||||
#include <ArduinoJson/Polyfills/preprocessor.hpp>
|
||||
#include <ArduinoJson/Polyfills/static_array.hpp>
|
||||
@ -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<DeserializationError> {
|
||||
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<bool>(err) == value;
|
||||
|
26
src/ArduinoJson/Misc/SafeBoolIdiom.hpp
Normal file
26
src/ArduinoJson/Misc/SafeBoolIdiom.hpp
Normal file
@ -0,0 +1,26 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2021
|
||||
// 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
|
@ -4,9 +4,11 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Misc/SafeBoolIdiom.hpp>
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
class String {
|
||||
class String : public SafeBoolIdom<String> {
|
||||
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;
|
||||
|
Reference in New Issue
Block a user