From 81f7e9399d96c9b4f155e8f1b47a5b677777bddd Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Fri, 20 Dec 2024 09:49:22 +0100 Subject: [PATCH] Move public facing SFINAEs to template declarations --- CHANGELOG.md | 1 + src/ArduinoJson/Array/JsonArray.hpp | 30 ++--- src/ArduinoJson/Array/JsonArrayConst.hpp | 12 +- src/ArduinoJson/Array/Utilities.hpp | 38 +++--- src/ArduinoJson/Document/JsonDocument.hpp | 116 +++++++++--------- src/ArduinoJson/Object/JsonObject.hpp | 62 +++++----- src/ArduinoJson/Object/JsonObjectConst.hpp | 34 ++--- src/ArduinoJson/Variant/JsonVariantConst.hpp | 65 +++++----- src/ArduinoJson/Variant/VariantOperators.hpp | 42 +++---- src/ArduinoJson/Variant/VariantRefBase.hpp | 70 +++++------ .../Variant/VariantRefBaseImpl.hpp | 47 +++---- 11 files changed, 257 insertions(+), 260 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bea7dd92..bccd0ef23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ HEAD * `JsonString` is now stored by copy, unless specified otherwise * Replace undocumented `JsonString::Ownership` with `bool` * Rename undocumented `JsonString::isLinked()` to `isStatic()` +* Move public facing SFINAEs to template declarations > ### BREAKING CHANGES > diff --git a/src/ArduinoJson/Array/JsonArray.hpp b/src/ArduinoJson/Array/JsonArray.hpp index 565cff26f..37c9ebfba 100644 --- a/src/ArduinoJson/Array/JsonArray.hpp +++ b/src/ArduinoJson/Array/JsonArray.hpp @@ -43,16 +43,18 @@ class JsonArray : public detail::VariantOperators { // Appends a new (empty) element to the array. // Returns a reference to the new element. // https://arduinojson.org/v7/api/jsonarray/add/ - template - detail::enable_if_t::value, T> add() const { + template ::value, int> = 0> + T add() const { return add().to(); } // Appends a new (null) element to the array. // Returns a reference to the new element. // https://arduinojson.org/v7/api/jsonarray/add/ - template - detail::enable_if_t::value, T> add() const { + template ::value, short> = 0> + JsonVariant add() const { return JsonVariant(detail::ArrayData::addElement(data_, resources_), resources_); } @@ -115,9 +117,9 @@ class JsonArray : public detail::VariantOperators { // Removes the element at the specified index. // https://arduinojson.org/v7/api/jsonarray/remove/ - template - detail::enable_if_t::value> remove( - const TVariant& variant) const { + template ::value, int> = 0> + void remove(const TVariant& variant) const { if (variant.template is()) remove(variant.template as()); } @@ -130,19 +132,17 @@ class JsonArray : public detail::VariantOperators { // Gets or sets the element at the specified index. // https://arduinojson.org/v7/api/jsonarray/subscript/ - template - detail::enable_if_t::value, - detail::ElementProxy> - operator[](T index) const { + template ::value, int> = 0> + detail::ElementProxy operator[](T index) const { return {*this, size_t(index)}; } // Gets or sets the element at the specified index. // https://arduinojson.org/v7/api/jsonarray/subscript/ - template - detail::enable_if_t::value, - detail::ElementProxy> - operator[](const TVariant& variant) const { + template ::value, short> = 0> + detail::ElementProxy operator[](const TVariant& variant) const { if (variant.template is()) return {*this, variant.template as()}; else diff --git a/src/ArduinoJson/Array/JsonArrayConst.hpp b/src/ArduinoJson/Array/JsonArrayConst.hpp index 6b9db1925..739464fbe 100644 --- a/src/ArduinoJson/Array/JsonArrayConst.hpp +++ b/src/ArduinoJson/Array/JsonArrayConst.hpp @@ -45,9 +45,9 @@ class JsonArrayConst : public detail::VariantOperators { // Returns the element at the specified index. // https://arduinojson.org/v7/api/jsonarrayconst/subscript/ - template - detail::enable_if_t::value, JsonVariantConst> - operator[](T index) const { + template ::value, int> = 0> + JsonVariantConst operator[](T index) const { return JsonVariantConst( detail::ArrayData::getElement(data_, size_t(index), resources_), resources_); @@ -55,9 +55,9 @@ class JsonArrayConst : public detail::VariantOperators { // Returns the element at the specified index. // https://arduinojson.org/v7/api/jsonarrayconst/subscript/ - template - detail::enable_if_t::value, JsonVariantConst> - operator[](const TVariant& variant) const { + template ::value, short> = 0> + JsonVariantConst operator[](const TVariant& variant) const { if (variant.template is()) return operator[](variant.template as()); else diff --git a/src/ArduinoJson/Array/Utilities.hpp b/src/ArduinoJson/Array/Utilities.hpp index 9073468c8..2f0638f0e 100644 --- a/src/ArduinoJson/Array/Utilities.hpp +++ b/src/ArduinoJson/Array/Utilities.hpp @@ -11,27 +11,27 @@ ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE // Copies a value to a JsonVariant. // This is a degenerated form of copyArray() to stop the recursion. -template -inline detail::enable_if_t::value, bool> copyArray( - const T& src, JsonVariant dst) { +template ::value>> +inline bool copyArray(const T& src, JsonVariant dst) { return dst.set(src); } // Copies values from an array to a JsonArray or a JsonVariant. // https://arduinojson.org/v7/api/misc/copyarray/ -template -inline detail::enable_if_t< - !detail::is_base_of::value, bool> -copyArray(T (&src)[N], const TDestination& dst) { +template ::value>> +inline bool copyArray(T (&src)[N], const TDestination& dst) { return copyArray(src, N, dst); } // Copies values from an array to a JsonArray or a JsonVariant. // https://arduinojson.org/v7/api/misc/copyarray/ -template -inline detail::enable_if_t< - !detail::is_base_of::value, bool> -copyArray(const T* src, size_t len, const TDestination& dst) { +template ::value>> +inline bool copyArray(const T* src, size_t len, const TDestination& dst) { bool ok = true; for (size_t i = 0; i < len; i++) { ok &= copyArray(src[i], dst.template add()); @@ -62,9 +62,9 @@ inline bool copyArray(const T* src, size_t len, JsonDocument& dst) { // Copies a value from a JsonVariant. // This is a degenerated form of copyArray() to stop the recursion. -template -inline detail::enable_if_t::value, size_t> copyArray( - JsonVariantConst src, T& dst) { +template ::value>> +inline size_t copyArray(JsonVariantConst src, T& dst) { dst = src.as(); return 1; } @@ -102,11 +102,11 @@ inline size_t copyArray(JsonVariantConst src, char (&dst)[N]) { // Copies values from a JsonDocument to an array. // https://arduinojson.org/v7/api/misc/copyarray/ -template -inline detail::enable_if_t::value && - detail::is_base_of::value, - size_t> -copyArray(const TSource& src, T& dst) { +template ::value && + detail::is_base_of::value>> +inline size_t copyArray(const TSource& src, T& dst) { return copyArray(src.template as(), dst); } diff --git a/src/ArduinoJson/Document/JsonDocument.hpp b/src/ArduinoJson/Document/JsonDocument.hpp index 403930809..53dc82e68 100644 --- a/src/ArduinoJson/Document/JsonDocument.hpp +++ b/src/ArduinoJson/Document/JsonDocument.hpp @@ -36,14 +36,14 @@ class JsonDocument : public detail::VariantOperators { } // Construct from variant, array, or object - template - JsonDocument( - const T& src, Allocator* alloc = detail::DefaultAllocator::instance(), - detail::enable_if_t::value || - detail::is_same::value || - detail::is_same::value || - detail::is_same::value || - detail::is_same::value>* = 0) + template ::value || + detail::is_same::value || + detail::is_same::value || + detail::is_same::value || + detail::is_same::value>> + JsonDocument(const T& src, + Allocator* alloc = detail::DefaultAllocator::instance()) : JsonDocument(alloc) { set(src); } @@ -136,9 +136,9 @@ class JsonDocument : public detail::VariantOperators { // Replaces the root with the specified value. // https://arduinojson.org/v7/api/jsondocument/set/ - template - detail::enable_if_t::value, bool> set( - const T& src) { + template ::value>> + bool set(const T& src) { return to().set(src); } @@ -168,68 +168,65 @@ class JsonDocument : public detail::VariantOperators { // DEPRECATED: use obj[key].is() instead // https://arduinojson.org/v7/api/jsondocument/containskey/ - template + template ::value, int> = 0> ARDUINOJSON_DEPRECATED("use doc[key].is() instead") - detail::enable_if_t::value, bool> containsKey( - const TString& key) const { + bool containsKey(const TString& key) const { return data_.getMember(detail::adaptString(key), &resources_) != 0; } // DEPRECATED: use obj[key].is() instead // https://arduinojson.org/v7/api/jsondocument/containskey/ - template + template ::value, short> = 0> ARDUINOJSON_DEPRECATED("use doc[key].is() instead") - detail::enable_if_t::value, bool> containsKey( - const TVariant& key) const { + bool containsKey(const TVariant& key) const { return containsKey(key.template as()); } // Gets or sets a root object's member. // https://arduinojson.org/v7/api/jsondocument/subscript/ - template - detail::enable_if_t< - detail::IsString::value, - detail::MemberProxy>> - operator[](const TString& key) { + template ::value>> + detail::MemberProxy> operator[]( + const TString& key) { return {*this, detail::adaptString(key)}; } // Gets or sets a root object's member. // https://arduinojson.org/v7/api/jsondocument/subscript/ - template - detail::enable_if_t< - detail::IsString::value && !detail::is_const::value, - detail::MemberProxy>> - operator[](TChar* key) { + template ::value && + !detail::is_const::value>> + detail::MemberProxy> operator[]( + TChar* key) { return {*this, detail::adaptString(key)}; } // Gets a root object's member. // https://arduinojson.org/v7/api/jsondocument/subscript/ - template - detail::enable_if_t::value, JsonVariantConst> - operator[](const TString& key) const { + template ::value>> + JsonVariantConst operator[](const TString& key) const { return JsonVariantConst( data_.getMember(detail::adaptString(key), &resources_), &resources_); } // Gets a root object's member. // https://arduinojson.org/v7/api/jsondocument/subscript/ - template - detail::enable_if_t::value && - !detail::is_const::value, - JsonVariantConst> - operator[](TChar* key) const { + template ::value && + !detail::is_const::value>> + JsonVariantConst operator[](TChar* key) const { return JsonVariantConst( data_.getMember(detail::adaptString(key), &resources_), &resources_); } // Gets or sets a root array's element. // https://arduinojson.org/v7/api/jsondocument/subscript/ - template - detail::enable_if_t::value, - detail::ElementProxy> - operator[](T index) { + template ::value, int> = 0> + detail::ElementProxy operator[](T index) { return {*this, size_t(index)}; } @@ -241,9 +238,9 @@ class JsonDocument : public detail::VariantOperators { // Gets or sets a root object's member. // https://arduinojson.org/v7/api/jsondocument/subscript/ - template - detail::enable_if_t::value, JsonVariantConst> - operator[](const TVariant& key) const { + template ::value, short> = 0> + JsonVariantConst operator[](const TVariant& key) const { if (key.template is()) return operator[](key.template as()); if (key.template is()) @@ -254,16 +251,18 @@ class JsonDocument : public detail::VariantOperators { // Appends a new (empty) element to the root array. // Returns a reference to the new element. // https://arduinojson.org/v7/api/jsondocument/add/ - template - detail::enable_if_t::value, T> add() { + template ::value, int> = 0> + T add() { return add().to(); } // Appends a new (null) element to the root array. // Returns a reference to the new element. // https://arduinojson.org/v7/api/jsondocument/add/ - template - detail::enable_if_t::value, T> add() { + template ::value, short> = 0> + JsonVariant add() { return JsonVariant(data_.addElement(&resources_), &resources_); } @@ -284,36 +283,37 @@ class JsonDocument : public detail::VariantOperators { // Removes an element of the root array. // https://arduinojson.org/v7/api/jsondocument/remove/ - template - detail::enable_if_t::value> remove(T index) { + template ::value>> + void remove(T index) { detail::VariantData::removeElement(getData(), size_t(index), getResourceManager()); } // Removes a member of the root object. // https://arduinojson.org/v7/api/jsondocument/remove/ - template - detail::enable_if_t::value && - !detail::is_const::value> - remove(TChar* key) { + template ::value && + !detail::is_const::value>> + void remove(TChar* key) { detail::VariantData::removeMember(getData(), detail::adaptString(key), getResourceManager()); } // Removes a member of the root object. // https://arduinojson.org/v7/api/jsondocument/remove/ - template - detail::enable_if_t::value> remove( - const TString& key) { + template ::value, int> = 0> + void remove(const TString& key) { detail::VariantData::removeMember(getData(), detail::adaptString(key), getResourceManager()); } // Removes a member of the root object or an element of the root array. // https://arduinojson.org/v7/api/jsondocument/remove/ - template - detail::enable_if_t::value> remove( - const TVariant& key) { + template ::value, short> = 0> + void remove(const TVariant& key) { if (key.template is()) remove(key.template as()); if (key.template is()) diff --git a/src/ArduinoJson/Object/JsonObject.hpp b/src/ArduinoJson/Object/JsonObject.hpp index c90b29553..f74a1ab0e 100644 --- a/src/ArduinoJson/Object/JsonObject.hpp +++ b/src/ArduinoJson/Object/JsonObject.hpp @@ -101,31 +101,29 @@ class JsonObject : public detail::VariantOperators { // Gets or sets the member with specified key. // https://arduinojson.org/v7/api/jsonobject/subscript/ - template - detail::enable_if_t< - detail::IsString::value, - detail::MemberProxy>> - operator[](const TString& key) const { + template ::value>> + detail::MemberProxy> operator[]( + const TString& key) const { return {*this, detail::adaptString(key)}; } // Gets or sets the member with specified key. // https://arduinojson.org/v7/api/jsonobject/subscript/ - template - detail::enable_if_t< - detail::IsString::value && !detail::is_const::value, - detail::MemberProxy>> - operator[](TChar* key) const { + template ::value && + !detail::is_const::value>> + detail::MemberProxy> operator[]( + TChar* key) const { return {*this, detail::adaptString(key)}; } // Gets or sets the member with specified key. // https://arduinojson.org/v7/api/jsonobject/subscript/ - template - detail::enable_if_t< - detail::IsVariant::value, - detail::MemberProxy>> - operator[](const TVariant& key) const { + template ::value>> + detail::MemberProxy> operator[]( + const TVariant& key) const { return {*this, detail::adaptString(key.template as())}; } @@ -137,18 +135,18 @@ class JsonObject : public detail::VariantOperators { // Removes the member with the specified key. // https://arduinojson.org/v7/api/jsonobject/remove/ - template - detail::enable_if_t::value> remove( - const TString& key) const { + template ::value, int> = 0> + void remove(const TString& key) const { detail::ObjectData::removeMember(data_, detail::adaptString(key), resources_); } // Removes the member with the specified key. // https://arduinojson.org/v7/api/jsonobject/remove/ - template - detail::enable_if_t::value> remove( - const TVariant& key) const { + template ::value, short> = 0> + void remove(const TVariant& key) const { if (key.template is()) remove(key.template as()); } @@ -163,31 +161,33 @@ class JsonObject : public detail::VariantOperators { // DEPRECATED: use obj[key].is() instead // https://arduinojson.org/v7/api/jsonobject/containskey/ - template + template ::value, int> = 0> ARDUINOJSON_DEPRECATED("use obj[key].is() instead") - detail::enable_if_t::value, bool> containsKey( - const TString& key) const { + bool containsKey(const TString& key) const { return detail::ObjectData::getMember(data_, detail::adaptString(key), resources_) != 0; } // DEPRECATED: use obj["key"].is() instead // https://arduinojson.org/v7/api/jsonobject/containskey/ - template + template ::value && + !detail::is_const::value, + short> = 0> ARDUINOJSON_DEPRECATED("use obj[\"key\"].is() instead") - detail::enable_if_t::value && - !detail::is_const::value, - bool> containsKey(TChar* key) const { + bool containsKey(TChar* key) const { return detail::ObjectData::getMember(data_, detail::adaptString(key), resources_) != 0; } // DEPRECATED: use obj[key].is() instead // https://arduinojson.org/v7/api/jsonobject/containskey/ - template + template ::value>, + int = 0> ARDUINOJSON_DEPRECATED("use obj[key].is() instead") - detail::enable_if_t::value, bool> containsKey( - const TVariant& key) const { + bool containsKey(const TVariant& key) const { return containsKey(key.template as()); } diff --git a/src/ArduinoJson/Object/JsonObjectConst.hpp b/src/ArduinoJson/Object/JsonObjectConst.hpp index 91d9dca5c..63c2a82ef 100644 --- a/src/ArduinoJson/Object/JsonObjectConst.hpp +++ b/src/ArduinoJson/Object/JsonObjectConst.hpp @@ -70,10 +70,10 @@ class JsonObjectConst : public detail::VariantOperators { // DEPRECATED: use obj[key].is() instead // https://arduinojson.org/v7/api/jsonobjectconst/containskey/ - template + template ::value, int> = 0> ARDUINOJSON_DEPRECATED("use obj[key].is() instead") - detail::enable_if_t::value, bool> containsKey( - const TString& key) const { + bool containsKey(const TString& key) const { return detail::ObjectData::getMember(data_, detail::adaptString(key), resources_) != 0; } @@ -89,18 +89,19 @@ class JsonObjectConst : public detail::VariantOperators { // DEPRECATED: use obj[key].is() instead // https://arduinojson.org/v7/api/jsonobjectconst/containskey/ - template + template ::value, short> = 0> ARDUINOJSON_DEPRECATED("use obj[key].is() instead") - detail::enable_if_t::value, bool> containsKey( - const TVariant& key) const { + bool containsKey(const TVariant& key) const { return containsKey(key.template as()); } // Gets the member with specified key. // https://arduinojson.org/v7/api/jsonobjectconst/subscript/ - template - detail::enable_if_t::value, JsonVariantConst> - operator[](const TString& key) const { + template ::value>, + short = 1> + JsonVariantConst operator[](const TString& key) const { return JsonVariantConst(detail::ObjectData::getMember( data_, detail::adaptString(key), resources_), resources_); @@ -108,11 +109,10 @@ class JsonObjectConst : public detail::VariantOperators { // Gets the member with specified key. // https://arduinojson.org/v7/api/jsonobjectconst/subscript/ - template - detail::enable_if_t::value && - !detail::is_const::value, - JsonVariantConst> - operator[](TChar* key) const { + template ::value && + !detail::is_const::value>> + JsonVariantConst operator[](TChar* key) const { return JsonVariantConst(detail::ObjectData::getMember( data_, detail::adaptString(key), resources_), resources_); @@ -120,9 +120,9 @@ class JsonObjectConst : public detail::VariantOperators { // Gets the member with specified key. // https://arduinojson.org/v7/api/jsonobjectconst/subscript/ - template - detail::enable_if_t::value, JsonVariantConst> - operator[](const TVariant& key) const { + template ::value>> + JsonVariantConst operator[](const TVariant& key) const { if (key.template is()) return operator[](key.template as()); else diff --git a/src/ArduinoJson/Variant/JsonVariantConst.hpp b/src/ArduinoJson/Variant/JsonVariantConst.hpp index f87b56ddb..5ab27df9c 100644 --- a/src/ArduinoJson/Variant/JsonVariantConst.hpp +++ b/src/ArduinoJson/Variant/JsonVariantConst.hpp @@ -68,27 +68,29 @@ class JsonVariantConst : public detail::VariantTag, // Casts the value to the specified type. // https://arduinojson.org/v7/api/jsonvariantconst/as/ template ::value, bool> = true> + typename = detail::enable_if_t::value>> T as() const { return Converter::fromJson(*this); } // Invalid conversion. Will not compile. - template ::value, bool> = true> + template ::value, bool>> detail::InvalidConversion as() const; // Returns true if the value is of the specified type. // https://arduinojson.org/v7/api/jsonvariantconst/is/ - template - detail::enable_if_t::value, bool> is() const { + template ::value, int> = 0> + bool is() const { return Converter::checkJson(*this); } // Always returns false for the unsupported types. // https://arduinojson.org/v7/api/jsonvariantconst/is/ - template - detail::enable_if_t::value, bool> is() const { + template ::value, short> = 0> + bool is() const { return false; } @@ -99,9 +101,9 @@ class JsonVariantConst : public detail::VariantTag, // Gets array's element at specified index. // https://arduinojson.org/v7/api/jsonvariantconst/subscript/ - template - detail::enable_if_t::value, JsonVariantConst> - operator[](T index) const { + template ::value>> + JsonVariantConst operator[](T index) const { return JsonVariantConst( detail::VariantData::getElement(data_, size_t(index), resources_), resources_); @@ -109,9 +111,9 @@ class JsonVariantConst : public detail::VariantTag, // Gets object's member with specified key. // https://arduinojson.org/v7/api/jsonvariantconst/subscript/ - template - detail::enable_if_t::value, JsonVariantConst> - operator[](const TString& key) const { + template ::value>> + JsonVariantConst operator[](const TString& key) const { return JsonVariantConst(detail::VariantData::getMember( data_, detail::adaptString(key), resources_), resources_); @@ -119,11 +121,11 @@ class JsonVariantConst : public detail::VariantTag, // Gets object's member with specified key. // https://arduinojson.org/v7/api/jsonvariantconst/subscript/ - template - detail::enable_if_t::value && - !detail::is_const::value, - JsonVariantConst> - operator[](TChar* key) const { + template ::value && + !detail::is_const::value, + int> = 0> + JsonVariantConst operator[](TChar* key) const { return JsonVariantConst(detail::VariantData::getMember( data_, detail::adaptString(key), resources_), resources_); @@ -132,9 +134,9 @@ class JsonVariantConst : public detail::VariantTag, // Gets object's member with specified key or the array's element at the // specified index. // https://arduinojson.org/v7/api/jsonvariantconst/subscript/ - template - detail::enable_if_t::value, JsonVariantConst> - operator[](const TVariant& key) const { + template ::value, short> = 0> + JsonVariantConst operator[](const TVariant& key) const { if (key.template is()) return operator[](key.template as()); else @@ -143,31 +145,32 @@ class JsonVariantConst : public detail::VariantTag, // DEPRECATED: use obj[key].is() instead // https://arduinojson.org/v7/api/jsonvariantconst/containskey/ - template + template ::value>> ARDUINOJSON_DEPRECATED("use var[key].is() instead") - detail::enable_if_t::value, bool> containsKey( - const TString& key) const { + bool containsKey(const TString& key) const { return detail::VariantData::getMember(getData(), detail::adaptString(key), resources_) != 0; } // DEPRECATED: use obj["key"].is() instead // https://arduinojson.org/v7/api/jsonvariantconst/containskey/ - template + template ::value && + !detail::is_const::value, + int> = 0> ARDUINOJSON_DEPRECATED("use obj[\"key\"].is() instead") - detail::enable_if_t::value && - !detail::is_const::value, - bool> containsKey(TChar* key) const { + bool containsKey(TChar* key) const { return detail::VariantData::getMember(getData(), detail::adaptString(key), resources_) != 0; } // DEPRECATED: use obj[key].is() instead // https://arduinojson.org/v7/api/jsonvariantconst/containskey/ - template + template ::value, short> = 0> ARDUINOJSON_DEPRECATED("use var[key].is() instead") - detail::enable_if_t::value, bool> containsKey( - const TVariant& key) const { + bool containsKey(const TVariant& key) const { return containsKey(key.template as()); } diff --git a/src/ArduinoJson/Variant/VariantOperators.hpp b/src/ArduinoJson/Variant/VariantOperators.hpp index 08e5bbd4f..25b9a5d0c 100644 --- a/src/ArduinoJson/Variant/VariantOperators.hpp +++ b/src/ArduinoJson/Variant/VariantOperators.hpp @@ -30,9 +30,9 @@ struct VariantOperators : VariantOperatorTag { // int operator|(JsonVariant, int) // float operator|(JsonVariant, float) // bool operator|(JsonVariant, bool) - template - friend enable_if_t::value && !is_array::value, T> operator|( - const TVariant& variant, const T& defaultValue) { + template ::value && !is_array::value>> + friend T operator|(const TVariant& variant, const T& defaultValue) { if (variant.template is()) return variant.template as(); else @@ -73,9 +73,9 @@ struct VariantOperators : VariantOperatorTag { friend bool operator==(const TVariant& lhs, T* rhs) { return compare(lhs, rhs) == COMPARE_RESULT_EQUAL; } - template - friend enable_if_t::value, bool> - operator==(const TVariant& lhs, const T& rhs) { + template ::value>> + friend bool operator==(const TVariant& lhs, const T& rhs) { return compare(lhs, rhs) == COMPARE_RESULT_EQUAL; } @@ -94,9 +94,9 @@ struct VariantOperators : VariantOperatorTag { friend bool operator!=(const TVariant& lhs, T* rhs) { return compare(lhs, rhs) != COMPARE_RESULT_EQUAL; } - template - friend enable_if_t::value, bool> - operator!=(TVariant lhs, const T& rhs) { + template ::value>> + friend bool operator!=(TVariant lhs, const T& rhs) { return compare(lhs, rhs) != COMPARE_RESULT_EQUAL; } @@ -115,9 +115,9 @@ struct VariantOperators : VariantOperatorTag { friend bool operator<(const TVariant& lhs, T* rhs) { return compare(lhs, rhs) == COMPARE_RESULT_LESS; } - template - friend enable_if_t::value, bool> operator<( - TVariant lhs, const T& rhs) { + template ::value>> + friend bool operator<(TVariant lhs, const T& rhs) { return compare(lhs, rhs) == COMPARE_RESULT_LESS; } @@ -136,9 +136,9 @@ struct VariantOperators : VariantOperatorTag { friend bool operator<=(const TVariant& lhs, T* rhs) { return (compare(lhs, rhs) & COMPARE_RESULT_LESS_OR_EQUAL) != 0; } - template - friend enable_if_t::value, bool> - operator<=(TVariant lhs, const T& rhs) { + template ::value>> + friend bool operator<=(TVariant lhs, const T& rhs) { return (compare(lhs, rhs) & COMPARE_RESULT_LESS_OR_EQUAL) != 0; } @@ -157,9 +157,9 @@ struct VariantOperators : VariantOperatorTag { friend bool operator>(const TVariant& lhs, T* rhs) { return compare(lhs, rhs) == COMPARE_RESULT_GREATER; } - template - friend enable_if_t::value, bool> operator>( - TVariant lhs, const T& rhs) { + template ::value>> + friend bool operator>(TVariant lhs, const T& rhs) { return compare(lhs, rhs) == COMPARE_RESULT_GREATER; } @@ -178,9 +178,9 @@ struct VariantOperators : VariantOperatorTag { friend bool operator>=(const TVariant& lhs, T* rhs) { return (compare(lhs, rhs) & COMPARE_RESULT_GREATER_OR_EQUAL) != 0; } - template - friend enable_if_t::value, bool> - operator>=(const TVariant& lhs, const T& rhs) { + template ::value>> + friend bool operator>=(const TVariant& lhs, const T& rhs) { return (compare(lhs, rhs) & COMPARE_RESULT_GREATER_OR_EQUAL) != 0; } }; diff --git a/src/ArduinoJson/Variant/VariantRefBase.hpp b/src/ArduinoJson/Variant/VariantRefBase.hpp index 30ee3e3e0..ea701ea45 100644 --- a/src/ArduinoJson/Variant/VariantRefBase.hpp +++ b/src/ArduinoJson/Variant/VariantRefBase.hpp @@ -55,18 +55,18 @@ class VariantRefBase : public VariantTag { // Sets the value to an empty array. // https://arduinojson.org/v7/api/jsonvariant/to/ - template - enable_if_t::value, JsonArray> to() const; + template ::value, long> = 0> + JsonArray to() const; // Sets the value to an empty object. // https://arduinojson.org/v7/api/jsonvariant/to/ - template - enable_if_t::value, JsonObject> to() const; + template ::value, short> = 0> + JsonObject to() const; // Sets the value to null. // https://arduinojson.org/v7/api/jsonvariant/to/ - template - enable_if_t::value, JsonVariant> to() const; + template ::value, int> = 0> + JsonVariant to() const; // Returns true if the value is of the specified type. // https://arduinojson.org/v7/api/jsonvariant/is/ @@ -105,16 +105,16 @@ class VariantRefBase : public VariantTag { // Appends a new (empty) element to the array. // Returns a reference to the new element. // https://arduinojson.org/v7/api/jsonvariant/add/ - template - enable_if_t::value, T> add() const { + template ::value, int> = 0> + T add() const { return add().template to(); } // Appends a new (null) element to the array. // Returns a reference to the new element. // https://arduinojson.org/v7/api/jsonvariant/add/ - template - enable_if_t::value, T> add() const; + template ::value, short> = 0> + T add() const; // Appends a value to the array. // https://arduinojson.org/v7/api/jsonvariant/add/ @@ -140,24 +140,25 @@ class VariantRefBase : public VariantTag { // Removes a member of the object. // https://arduinojson.org/v7/api/jsonvariant/remove/ - template - enable_if_t::value> remove(TChar* key) const { + template ::value>> + void remove(TChar* key) const { VariantData::removeMember(getData(), adaptString(key), getResourceManager()); } // Removes a member of the object. // https://arduinojson.org/v7/api/jsonvariant/remove/ - template - enable_if_t::value> remove(const TString& key) const { + template ::value, int> = 0> + void remove(const TString& key) const { VariantData::removeMember(getData(), adaptString(key), getResourceManager()); } // Removes a member of the object or an element of the array. // https://arduinojson.org/v7/api/jsonvariant/remove/ - template - enable_if_t::value> remove(const TVariant& key) const { + template ::value, short> = 0> + void remove(const TVariant& key) const { if (key.template is()) remove(key.template as()); else @@ -170,43 +171,42 @@ class VariantRefBase : public VariantTag { // DEPRECATED: use obj[key].is() instead // https://arduinojson.org/v7/api/jsonvariant/containskey/ - template + template ::value, int> = 0> ARDUINOJSON_DEPRECATED("use obj[key].is() instead") - enable_if_t::value, bool> containsKey( - const TString& key) const; + bool containsKey(const TString& key) const; // DEPRECATED: use obj["key"].is() instead // https://arduinojson.org/v7/api/jsonvariant/containskey/ - template + template ::value, short> = 0> ARDUINOJSON_DEPRECATED("use obj[\"key\"].is() instead") - enable_if_t::value, bool> containsKey(TChar* key) const; + bool containsKey(TChar* key) const; // DEPRECATED: use obj[key].is() instead // https://arduinojson.org/v7/api/jsonvariant/containskey/ - template + template ::value>, int = 0> ARDUINOJSON_DEPRECATED("use obj[key].is() instead") - enable_if_t::value, bool> containsKey( - const TVariant& key) const; + bool containsKey(const TVariant& key) const; // Gets or sets an object member. // https://arduinojson.org/v7/api/jsonvariant/subscript/ - template - FORCE_INLINE enable_if_t::value, - MemberProxy>> - operator[](const TString& key) const; + template ::value, int> = 0> + FORCE_INLINE MemberProxy> operator[]( + const TString& key) const; // Gets or sets an object member. // https://arduinojson.org/v7/api/jsonvariant/subscript/ - template - FORCE_INLINE enable_if_t::value && !is_const::value, - MemberProxy>> - operator[](TChar* key) const; + template ::value && !is_const::value, + short> = 0> + FORCE_INLINE MemberProxy> operator[]( + TChar* key) const; // Gets an object member or an array element. // https://arduinojson.org/v7/api/jsonvariant/subscript/ - template - enable_if_t::value, JsonVariantConst> operator[]( - const TVariant& key) const { + template ::value>> + JsonVariantConst operator[](const TVariant& key) const { if (key.template is()) return operator[](key.template as()); else diff --git a/src/ArduinoJson/Variant/VariantRefBaseImpl.hpp b/src/ArduinoJson/Variant/VariantRefBaseImpl.hpp index f69e01d3e..ec871ddeb 100644 --- a/src/ArduinoJson/Variant/VariantRefBaseImpl.hpp +++ b/src/ArduinoJson/Variant/VariantRefBaseImpl.hpp @@ -67,34 +67,30 @@ inline void convertToJson(const VariantRefBase& src, } template -template -inline enable_if_t::value, T> -VariantRefBase::add() const { +template ::value, short>> +inline T VariantRefBase::add() const { return JsonVariant( detail::VariantData::addElement(getOrCreateData(), getResourceManager()), getResourceManager()); } template -template -inline enable_if_t::value, bool> -VariantRefBase::containsKey(const TString& key) const { +template ::value, int>> +inline bool VariantRefBase::containsKey(const TString& key) const { return VariantData::getMember(getData(), adaptString(key), getResourceManager()) != 0; } template -template -inline enable_if_t::value, bool> -VariantRefBase::containsKey(TChar* key) const { +template ::value, short>> +inline bool VariantRefBase::containsKey(TChar* key) const { return VariantData::getMember(getData(), adaptString(key), getResourceManager()) != 0; } template -template -inline enable_if_t::value, bool> -VariantRefBase::containsKey(const TVariant& key) const { +template +inline bool VariantRefBase::containsKey(const TVariant& key) const { return containsKey(key.template as()); } @@ -123,17 +119,17 @@ inline ElementProxy VariantRefBase::operator[]( } template -template -inline enable_if_t::value && !is_const::value, - MemberProxy>> +template < + typename TChar, + enable_if_t::value && !is_const::value, short>> +inline MemberProxy> VariantRefBase::operator[](TChar* key) const { return {derived(), adaptString(key)}; } template -template -inline enable_if_t::value, - MemberProxy>> +template ::value, int>> +inline MemberProxy> VariantRefBase::operator[](const TString& key) const { return {derived(), adaptString(key)}; } @@ -153,27 +149,24 @@ inline bool VariantRefBase::doSet(const T& value, true_type) const { } template -template -inline enable_if_t::value, JsonArray> -VariantRefBase::to() const { +template ::value, long>> +inline JsonArray VariantRefBase::to() const { return JsonArray( VariantData::toArray(getOrCreateData(), getResourceManager()), getResourceManager()); } template -template -enable_if_t::value, JsonObject> -VariantRefBase::to() const { +template ::value, short>> +JsonObject VariantRefBase::to() const { return JsonObject( VariantData::toObject(getOrCreateData(), getResourceManager()), getResourceManager()); } template -template -enable_if_t::value, JsonVariant> -VariantRefBase::to() const { +template ::value, int>> +JsonVariant VariantRefBase::to() const { auto data = getOrCreateData(); auto resources = getResourceManager(); detail::VariantData::clear(data, resources);