Fixed compilation error when index is not an int (issue #381)

This commit is contained in:
Benoit Blanchon
2016-11-13 20:19:36 +01:00
parent 1ce16ce449
commit f28157cab7
5 changed files with 17 additions and 10 deletions

View File

@ -5,6 +5,7 @@ HEAD
---- ----
* Added support for PROGMEM (issue #76) * Added support for PROGMEM (issue #76)
* Fixed compilation error when index is not an `int` (issue #381)
v5.7.0 v5.7.0
------ ------

View File

@ -44,6 +44,7 @@ struct CharPtrFuncs {
} }
static const bool has_append = false; static const bool has_append = false;
static const bool has_equals = true;
static const bool should_duplicate = false; static const bool should_duplicate = false;
}; };
@ -56,6 +57,9 @@ struct StringFuncs<char*> : CharPtrFuncs {};
template <size_t N> template <size_t N>
struct StringFuncs<char[N]> : CharPtrFuncs {}; struct StringFuncs<char[N]> : CharPtrFuncs {};
template <size_t N>
struct StringFuncs<const char[N]> : CharPtrFuncs {};
template <typename TString> template <typename TString>
struct StdStringFuncs { struct StdStringFuncs {
template <typename Buffer> template <typename Buffer>
@ -76,6 +80,7 @@ struct StdStringFuncs {
} }
static const bool has_append = true; static const bool has_append = true;
static const bool has_equals = true;
static const bool should_duplicate = true; static const bool should_duplicate = true;
}; };
@ -108,6 +113,7 @@ struct StringFuncs<const __FlashStringHelper*> {
} }
static const bool has_append = false; static const bool has_append = false;
static const bool has_equals = true;
static const bool should_duplicate = true; static const bool should_duplicate = true;
}; };
#endif #endif

View File

@ -84,13 +84,6 @@ template <typename TString>
inline JsonObjectSubscript<TString> JsonObject::operator[](const TString& key) { inline JsonObjectSubscript<TString> JsonObject::operator[](const TString& key) {
return JsonObjectSubscript<TString>(*this, key); return JsonObjectSubscript<TString>(*this, key);
} }
template <typename TImplem>
template <class TString>
inline const JsonObjectSubscript<TString> JsonVariantBase<TImplem>::operator[](
const TString& key) const {
return asObject()[key];
}
} // namespace ArduinoJson } // namespace ArduinoJson
#ifdef _MSC_VER #ifdef _MSC_VER

View File

@ -77,8 +77,12 @@ class JsonVariantBase : public Internals::JsonPrintable<TImpl> {
// an object. // an object.
// Return JsonVariant::invalid() if the variant is not an object. // Return JsonVariant::invalid() if the variant is not an object.
template <typename TString> template <typename TString>
FORCE_INLINE const JsonObjectSubscript<TString> operator[]( FORCE_INLINE
const TString &key) const; typename TypeTraits::EnableIf<Internals::StringFuncs<TString>::has_equals,
const JsonObjectSubscript<TString> >::type
operator[](const TString &key) const {
return asObject()[key];
}
private: private:
const TImpl *impl() const { const TImpl *impl() const {

View File

@ -5,8 +5,8 @@
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star! // If you like this project, please add a star!
#include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <gtest/gtest.h>
class JsonVariant_Subscript_Tests : public ::testing::Test { class JsonVariant_Subscript_Tests : public ::testing::Test {
protected: protected:
@ -24,6 +24,9 @@ TEST_F(JsonVariant_Subscript_Tests, Array) {
EXPECT_EQ(2, _variant.size()); EXPECT_EQ(2, _variant.size());
EXPECT_STREQ("element at index 0", _variant[0].asString()); EXPECT_STREQ("element at index 0", _variant[0].asString());
EXPECT_STREQ("element at index 1", _variant[1].asString()); EXPECT_STREQ("element at index 1", _variant[1].asString());
EXPECT_STREQ(
"element at index 0",
_variant[static_cast<unsigned char>(0)].asString()); // issue #381
EXPECT_FALSE(_variant[-1].success()); EXPECT_FALSE(_variant[-1].success());
EXPECT_FALSE(_variant[3].success()); EXPECT_FALSE(_variant[3].success());
EXPECT_FALSE(_variant["0"].success()); EXPECT_FALSE(_variant["0"].success());