From ac1d29fac0faa546c6a6464b586a62ebb935147d Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Thu, 31 Mar 2022 11:23:24 +0200 Subject: [PATCH] Add meta function `ConverterNeedsWriteableRef` --- extras/tests/JsonVariant/converters.cpp | 12 ++++++++++++ src/ArduinoJson/Variant/ConverterImpl.hpp | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/extras/tests/JsonVariant/converters.cpp b/extras/tests/JsonVariant/converters.cpp index 3c4ad6d3..785289e1 100644 --- a/extras/tests/JsonVariant/converters.cpp +++ b/extras/tests/JsonVariant/converters.cpp @@ -140,3 +140,15 @@ TEST_CASE("Custom converter with specialization") { REQUIRE(doc["value"]["imag"] == 3); } } + +TEST_CASE("ConverterNeedsWriteableRef") { + using namespace ARDUINOJSON_NAMESPACE; + CHECK(ConverterNeedsWriteableRef::value == false); + CHECK(ConverterNeedsWriteableRef::value == false); + CHECK(ConverterNeedsWriteableRef::value == true); + CHECK(ConverterNeedsWriteableRef::value == false); + CHECK(ConverterNeedsWriteableRef::value == true); + CHECK(ConverterNeedsWriteableRef::value == false); + CHECK(ConverterNeedsWriteableRef::value == true); + CHECK(ConverterNeedsWriteableRef::value == false); +} diff --git a/src/ArduinoJson/Variant/ConverterImpl.hpp b/src/ArduinoJson/Variant/ConverterImpl.hpp index baf1187a..e0222592 100644 --- a/src/ArduinoJson/Variant/ConverterImpl.hpp +++ b/src/ArduinoJson/Variant/ConverterImpl.hpp @@ -303,4 +303,18 @@ inline bool canConvertFromJson(VariantConstRef src, const std::string_view&) { #endif +template +struct ConverterNeedsWriteableRef { + protected: // <- to avoid GCC's "all member functions in class are private" + typedef char Yes[1]; + typedef char No[2]; + + static Yes& probe(T (*f)(VariantRef)); + static No& probe(T (*f)(VariantConstRef)); + + public: + static const bool value = + sizeof(probe(Converter::fromJson)) == sizeof(Yes); +}; + } // namespace ARDUINOJSON_NAMESPACE