From d8b71b4eef48b61181ff374f6deb071ac7f0c89b Mon Sep 17 00:00:00 2001 From: reuk Date: Fri, 8 Sep 2023 14:48:29 +0100 Subject: [PATCH] VariantConverter: Use FromVar and ToVar as a fallback --- modules/juce_core/containers/juce_Variant.h | 22 --------- .../javascript/juce_JSONSerialisation.h | 49 +++++++++++++++++++ 2 files changed, 49 insertions(+), 22 deletions(-) diff --git a/modules/juce_core/containers/juce_Variant.h b/modules/juce_core/containers/juce_Variant.h index d83df094eb..d9a43bf3a1 100644 --- a/modules/juce_core/containers/juce_Variant.h +++ b/modules/juce_core/containers/juce_Variant.h @@ -340,26 +340,4 @@ JUCE_API bool operator== (const var&, const String&); JUCE_API bool operator!= (const var&, const String&); JUCE_API bool operator== (const var&, const char*); JUCE_API bool operator!= (const var&, const char*); - -//============================================================================== -/** This template-overloaded class can be used to convert between var and custom types. - - @tags{Core} -*/ -template -struct VariantConverter -{ - static Type fromVar (const var& v) { return static_cast (v); } - static var toVar (const Type& t) { return t; } -}; - -#ifndef DOXYGEN -template <> -struct VariantConverter -{ - static String fromVar (const var& v) { return v.toString(); } - static var toVar (const String& s) { return s; } -}; -#endif - } // namespace juce diff --git a/modules/juce_core/javascript/juce_JSONSerialisation.h b/modules/juce_core/javascript/juce_JSONSerialisation.h index 8726834c34..f51526e9ab 100644 --- a/modules/juce_core/javascript/juce_JSONSerialisation.h +++ b/modules/juce_core/javascript/juce_JSONSerialisation.h @@ -456,4 +456,53 @@ private: }; }; +//============================================================================== +/** + This template-overloaded class can be used to convert between var and custom types. + + If not specialised, the variant converter will attempt to use serialisation functions + if they are detected for the given type. + For details of what this entails, see the docs for SerialisationTraits. + + In short, the constant 'marshallingVersion', and either the single function 'serialise()', or + the function pair 'load()' and 'save()' must be defined for the type. These may be defined + as public members of the type T itself, or as public members of juce::SerialisationTraits, + which is a specialisation of the SerialisationTraits template struct for the type T. + + @see ToVar, FromVar + + @tags{Core} +*/ +template +struct VariantConverter +{ + static Type fromVar (const var& v) + { + if constexpr (detail::serialisationKind != detail::SerialisationKind::none) + { + auto converted = FromVar::convert (v); + jassert (converted.has_value()); + return std::move (converted).value_or (Type{}); + } + else + { + return static_cast (v); + } + } + + static var toVar (const Type& t) + { + if constexpr (detail::serialisationKind != detail::SerialisationKind::none) + { + auto converted = ToVar::convert<> (t); + jassert (converted.has_value()); + return std::move (converted).value_or (var{}); + } + else + { + return t; + } + } +}; + } // namespace juce