Fixed enums serialized as booleans (fixes #1197)

This commit is contained in:
Benoit Blanchon
2020-02-26 16:16:20 +01:00
parent 0214c9bcad
commit 2996503b27
9 changed files with 149 additions and 1 deletions

View File

@ -9,7 +9,10 @@
#include "type_traits/integral_constant.hpp"
#include "type_traits/is_array.hpp"
#include "type_traits/is_base_of.hpp"
#include "type_traits/is_class.hpp"
#include "type_traits/is_const.hpp"
#include "type_traits/is_convertible.hpp"
#include "type_traits/is_enum.hpp"
#include "type_traits/is_floating_point.hpp"
#include "type_traits/is_integral.hpp"
#include "type_traits/is_same.hpp"

View File

@ -0,0 +1,14 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2020
// MIT License
#pragma once
#include <ArduinoJson/Namespace.hpp>
namespace ARDUINOJSON_NAMESPACE {
template <typename T>
T declval();
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -0,0 +1,26 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2020
// MIT License
#pragma once
#include "declval.hpp"
namespace ARDUINOJSON_NAMESPACE {
template <typename T>
struct is_class {
protected: // <- to avoid GCC's "all member functions in class are private"
typedef char Yes[1];
typedef char No[2];
template <typename U>
static Yes &probe(void (U::*)(void));
template <typename>
static No &probe(...);
public:
static const bool value = sizeof(probe<T>(0)) == sizeof(Yes);
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -0,0 +1,34 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2020
// MIT License
#pragma once
#include "declval.hpp"
#ifdef _MSC_VER
#pragma warning(push)
// conversion from 'T' to 'To', possible loss of data
#pragma warning(disable : 4244)
#endif
namespace ARDUINOJSON_NAMESPACE {
template <typename From, typename To>
struct is_convertible {
protected: // <- to avoid GCC's "all member functions in class are private"
typedef char Yes[1];
typedef char No[2];
static Yes &probe(To);
static No &probe(...);
public:
static const bool value = sizeof(probe(declval<From>())) == sizeof(Yes);
};
} // namespace ARDUINOJSON_NAMESPACE
#ifdef _MSC_VER
#pragma warning(pop)
#endif

View File

@ -0,0 +1,23 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2020
// MIT License
#pragma once
#include "is_class.hpp"
#include "is_convertible.hpp"
#include "is_floating_point.hpp"
#include "is_integral.hpp"
#include "is_same.hpp"
namespace ARDUINOJSON_NAMESPACE {
template <typename T>
struct is_enum {
static const bool value = is_convertible<T, int>::value &&
!is_class<T>::value && !is_integral<T>::value &&
!is_floating_point<T>::value &&
!is_same<T, bool>::value;
};
} // namespace ARDUINOJSON_NAMESPACE

View File

@ -153,7 +153,9 @@ class VariantRef : public VariantRefBase<VariantData>,
}
// set(bool value)
FORCE_INLINE bool set(bool value) const {
template <typename T>
FORCE_INLINE bool set(
T value, typename enable_if<is_same<T, bool>::value>::type * = 0) const {
return variantSetBoolean(_data, value);
}
@ -237,6 +239,13 @@ class VariantRef : public VariantRefBase<VariantData>,
typename enable_if<IsVisitable<TVariant>::value, bool>::type set(
const TVariant &value) const;
// set(enum value)
template <typename T>
FORCE_INLINE bool set(
T value, typename enable_if<is_enum<T>::value>::type * = 0) const {
return variantSetSignedInteger(_data, static_cast<Integer>(value));
}
// Get the variant as the specified type.
//
// std::string as<std::string>() const;