From a734f7f63b95562b17a4673fbd143f8295a28dab Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Thu, 18 Mar 2021 15:54:54 +0100 Subject: [PATCH] Add type guards to the Struct range constructor These provide much better error messages in case of a mismatch --- .../include/sixtyfps_interpreter.h | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/api/sixtyfps-cpp/include/sixtyfps_interpreter.h b/api/sixtyfps-cpp/include/sixtyfps_interpreter.h index 59d001484..8e3adef3a 100644 --- a/api/sixtyfps-cpp/include/sixtyfps_interpreter.h +++ b/api/sixtyfps-cpp/include/sixtyfps_interpreter.h @@ -54,8 +54,21 @@ public: inline Struct(std::initializer_list> args); - template - inline Struct(InputIterator begin, InputIterator end); + template(*std::declval())), + std::string_view>::value + && std::is_convertible(*std::declval())), + Value>::value + + > * = nullptr> + Struct(InputIterator it, InputIterator end) : Struct() + { + for (; it != end; ++it) { + auto [key, value] = *it; + set_field(key, value); + } + } // FIXME: this probably miss a lot of iterator api struct iterator @@ -123,16 +136,6 @@ private: StructOpaque inner; friend class Value; }; - -template -inline Struct::Struct(InputIterator it, InputIterator end) : Struct() -{ - for (; it != end; ++it) { - auto [key, value] = *it; - set_field(key, value); - } -} - class Value { public: @@ -230,10 +233,12 @@ public: Type type() const { return cbindgen_private::sixtyfps_interpreter_value_type(&inner); } - friend bool operator==(const Value &a, const Value &b) { + friend bool operator==(const Value &a, const Value &b) + { return cbindgen_private::sixtyfps_interpreter_value_eq(&a.inner, &b.inner); } - friend bool operator!=(const Value &a, const Value &b) { + friend bool operator!=(const Value &a, const Value &b) + { return !cbindgen_private::sixtyfps_interpreter_value_eq(&a.inner, &b.inner); }