Add type guards to the Struct range constructor

These provide much better error messages in case of a mismatch
This commit is contained in:
Simon Hausmann 2021-03-18 15:54:54 +01:00
parent 8916b7e13e
commit a734f7f63b

View file

@ -54,8 +54,21 @@ public:
inline Struct(std::initializer_list<std::pair<std::string_view, Value>> args); inline Struct(std::initializer_list<std::pair<std::string_view, Value>> args);
template<typename InputIterator> template<typename InputIterator,
inline Struct(InputIterator begin, InputIterator end); typename std::enable_if_t<
std::is_convertible<decltype(std::get<0>(*std::declval<InputIterator>())),
std::string_view>::value
&& std::is_convertible<decltype(std::get<1>(*std::declval<InputIterator>())),
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 // FIXME: this probably miss a lot of iterator api
struct iterator struct iterator
@ -123,16 +136,6 @@ private:
StructOpaque inner; StructOpaque inner;
friend class Value; friend class Value;
}; };
template<typename InputIterator>
inline Struct::Struct(InputIterator it, InputIterator end) : Struct()
{
for (; it != end; ++it) {
auto [key, value] = *it;
set_field(key, value);
}
}
class Value class Value
{ {
public: public:
@ -230,10 +233,12 @@ public:
Type type() const { return cbindgen_private::sixtyfps_interpreter_value_type(&inner); } 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); 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); return !cbindgen_private::sixtyfps_interpreter_value_eq(&a.inner, &b.inner);
} }