Add C++ Value Array construction/extraction

This commit is contained in:
Simon Hausmann 2021-03-17 16:52:30 +01:00
parent e20dd384f5
commit 79612d7354
3 changed files with 61 additions and 7 deletions

View file

@ -77,7 +77,7 @@ public:
return {};
}
}
std::optional<sixtyfps::SharedVector<Value>> to_array() const;
inline std::optional<sixtyfps::SharedVector<Value>> to_array() const;
std::optional<std::shared_ptr<sixtyfps::Model<Value>>> to_model() const;
std::optional<sixtyfps::Brush> to_brush() const;
// std::optional<Struct> to_struct() const;
@ -89,7 +89,7 @@ public:
cbindgen_private::sixtyfps_interpreter_value_new_string(&str, &inner);
}
Value(bool b) { cbindgen_private::sixtyfps_interpreter_value_new_bool(b, &inner); }
Value(const SharedVector<Value> &);
inline Value(const SharedVector<Value> &);
Value(const std::shared_ptr<sixtyfps::Model<Value>> &);
Value(const sixtyfps::Brush &);
// Value(const Struct &);
@ -98,7 +98,23 @@ public:
Type type() const { return cbindgen_private::sixtyfps_interpreter_value_type(&inner); }
private:
sixtyfps::cbindgen_private::ValueOpaque inner;
using ValueOpaque = sixtyfps::cbindgen_private::ValueOpaque;
ValueOpaque inner;
};
inline Value::Value(const sixtyfps::SharedVector<Value> &array)
{
cbindgen_private::sixtyfps_interpreter_value_new_array(
&reinterpret_cast<const sixtyfps::SharedVector<ValueOpaque> &>(array), &inner);
}
inline std::optional<sixtyfps::SharedVector<Value>> Value::to_array() const
{
if (auto *array = cbindgen_private::sixtyfps_interpreter_value_to_array(&inner)) {
return *reinterpret_cast<const sixtyfps::SharedVector<Value> *>(array);
} else {
return {};
}
}
}

View file

@ -85,4 +85,20 @@ SCENARIO("Value API")
REQUIRE(bool_opt.has_value());
REQUIRE(bool_opt.value() == true);
}
SECTION("Construct an array")
{
REQUIRE(!value.to_array().has_value());
sixtyfps::SharedVector<Value> array { Value(42.0), Value(true) };
value = Value(array);
REQUIRE(value.type() == Value::Type::Array);
auto array_opt = value.to_array();
REQUIRE(array_opt.has_value());
auto extracted_array = array_opt.value();
REQUIRE(extracted_array.size() == 2);
REQUIRE(extracted_array[0].to_number().value() == 42);
REQUIRE(extracted_array[1].to_bool().value());
}
}