Clarify model handling in C++ interpreter slightly

* Remove `Value::to_model()` as it is not implemented
* Document that models can be passed to .60 array/object properties
* Add a test for the extraction  / setting behavior of arrays.
This commit is contained in:
Simon Hausmann 2021-03-25 16:21:10 +01:00
parent b875a19310
commit 25a1e666d9
2 changed files with 42 additions and 4 deletions

View file

@ -220,6 +220,12 @@ private:
/// It is also possible to query the type the value holds by calling the Value::type() /// It is also possible to query the type the value holds by calling the Value::type()
/// function. /// function.
/// ///
/// Note that models are only represented in one direction: You can create a sixtyfps::Model<Value>
/// in C++, store it in a std::shared_ptr and construct Value from it. Then you can set it on a
/// property in your .60 code that was declared to be either an array (`property <[sometype]> foo;`)
/// or an object literal (`property <{foo: string, bar: int}> myprop;`). Such properties are dynamic
/// and accept models implemented in C++.
///
/// ``` /// ```
/// Value v(42.0); // Creates a value that holds a double with the value 42. /// Value v(42.0); // Creates a value that holds a double with the value 42.
/// ///
@ -306,10 +312,6 @@ public:
/// Type::Array, otherwise an empty optional is returned. /// Type::Array, otherwise an empty optional is returned.
inline std::optional<sixtyfps::SharedVector<Value>> to_array() const; inline std::optional<sixtyfps::SharedVector<Value>> to_array() const;
/// Returns a std::optional that contains a model of values if the type of this Value is
/// Type::Model, otherwise an empty optional is returned.
std::optional<std::shared_ptr<sixtyfps::Model<Value>>> to_model() const;
/// Returns a std::optional that contains a brush if the type of this Value is /// Returns a std::optional that contains a brush if the type of this Value is
/// Type::Brush, otherwise an empty optional is returned. /// Type::Brush, otherwise an empty optional is returned.
std::optional<sixtyfps::Brush> to_brush() const std::optional<sixtyfps::Brush> to_brush() const

View file

@ -337,3 +337,39 @@ SCENARIO("Invoke callback")
REQUIRE(!res.has_value()); REQUIRE(!res.has_value());
} }
} }
SCENARIO("Array between .60 and C++")
{
using namespace sixtyfps::interpreter;
using namespace sixtyfps;
ComponentCompiler compiler;
auto result = compiler.build_from_source(
"export Dummy := Rectangle { property <[int]> array: [1, 2, 3]; }", "");
REQUIRE(result.has_value());
auto instance = result->create();
SECTION(".60 to C++")
{
auto maybe_array = instance->get_property("array");
REQUIRE(maybe_array.has_value());
REQUIRE(maybe_array->type() == Value::Type::Array);
auto array = *maybe_array;
REQUIRE(array == sixtyfps::SharedVector<Value> { Value(1.), Value(2.), Value(3.) });
}
SECTION("C++ to .60")
{
sixtyfps::SharedVector<Value> cpp_array { Value(4.), Value(5.), Value(6.) };
instance->set_property("array", Value(cpp_array));
auto maybe_array = instance->get_property("array");
REQUIRE(maybe_array.has_value());
REQUIRE(maybe_array->type() == Value::Type::Array);
auto actual_array = *maybe_array;
REQUIRE(actual_array == cpp_array);
}
}