diff --git a/api/sixtyfps-cpp/include/sixtyfps.h b/api/sixtyfps-cpp/include/sixtyfps.h index 972af772f..2b3b74ff8 100644 --- a/api/sixtyfps-cpp/include/sixtyfps.h +++ b/api/sixtyfps-cpp/include/sixtyfps.h @@ -112,7 +112,7 @@ public: void init_items(Component *c, ItemTree items) const { cbindgen_private::sixtyfps_component_init_items( - vtable::VRef { &Component::component_type, c }, items, &inner); + vtable::VRef { &Component::static_vtable, c }, items, &inner); } template @@ -547,7 +547,7 @@ public: vtable::VRef item_at(int i) const { const auto &x = inner->data.at(i); - return { &C::component_type, const_cast(&(**x.ptr)) }; + return { &C::static_vtable, const_cast(&(**x.ptr)) }; } void compute_layout(cbindgen_private::Rect parent_rect) const @@ -555,7 +555,7 @@ public: if (!inner) return; for (auto &x : inner->data) { - (*x.ptr)->apply_layout({ &C::component_type, const_cast(&(**x.ptr)) }, + (*x.ptr)->apply_layout({ &C::static_vtable, const_cast(&(**x.ptr)) }, parent_rect); } } diff --git a/api/sixtyfps-cpp/include/vtable.h b/api/sixtyfps-cpp/include/vtable.h index f4d664167..9c7a82c48 100644 --- a/api/sixtyfps-cpp/include/vtable.h +++ b/api/sixtyfps-cpp/include/vtable.h @@ -66,7 +66,7 @@ struct VRcInner { template friend class VWeak; private: VRcInner() : layout {} {} - const VTable *vtable = &X::component_type; + const VTable *vtable = &X::static_vtable; int strong_ref = 1; int weak_ref = 1; std::uint16_t data_offset = offsetof(VRcInner, data); @@ -108,6 +108,9 @@ public: new(this) VRc(other); return *this; } + /// Construct a new VRc holding an X. + /// + /// The type X must have a static member `static_vtable` of type VTable template static VRc make(Args... args) { auto mem = ::operator new(sizeof(VRcInner), static_cast(alignof(VRcInner))); auto inner = new (mem) VRcInner; diff --git a/sixtyfps_compiler/generator/cpp.rs b/sixtyfps_compiler/generator/cpp.rs index 6b58e8bea..3b6ff2cfe 100644 --- a/sixtyfps_compiler/generator/cpp.rs +++ b/sixtyfps_compiler/generator/cpp.rs @@ -837,7 +837,7 @@ fn generate_component( .to_owned(), statements: Some(vec![ "float vp_w = viewport_width->get();".to_owned(), - format!("apply_layout({{&component_type, const_cast(static_cast(this))}}, sixtyfps::Rect{{ 0, *offset_y, vp_w, {h} }});", h = "0/*FIXME: should compute the heigth somehow*/"), + format!("apply_layout({{&static_vtable, const_cast(static_cast(this))}}, sixtyfps::Rect{{ 0, *offset_y, vp_w, {h} }});", h = "0/*FIXME: should compute the heigth somehow*/"), format!("{}.set(*offset_y);", p_y), // FIXME: shouldn't that be handled by apply layout? format!("*offset_y += {}.get();", p_height), format!("float w = {}.get();", p_width), @@ -855,7 +855,7 @@ fn generate_component( name: "box_layout_data".into(), signature: "() const -> sixtyfps::BoxLayoutCellData".to_owned(), statements: Some(vec![format!( - "return {{ layouting_info({{&component_type, const_cast(static_cast(this))}}), &{x}, &{y}, &{w}, &{h} }};", + "return {{ layouting_info({{&static_vtable, const_cast(static_cast(this))}}), &{x}, &{y}, &{w}, &{h} }};", x = p_x, y = p_y, w = p_width, @@ -1143,7 +1143,7 @@ fn generate_component( Access::Public, Declaration::Var(Var { ty: "static const sixtyfps::private_api::ComponentVTable".to_owned(), - name: "component_type".to_owned(), + name: "static_vtable".to_owned(), init: None, }), )); @@ -1186,7 +1186,7 @@ fn generate_component( if !component.is_global() { file.definitions.push(Declaration::Var(Var { ty: "const sixtyfps::private_api::ComponentVTable".to_owned(), - name: format!("{}::component_type", component_id), + name: format!("{}::static_vtable", component_id), init: Some(format!( "{{ visit_children, get_item_ref, layouting_info, apply_layout, sixtyfps::private_api::drop_in_place<{}>, sixtyfps::private_api::dealloc }}", component_id) diff --git a/tests/cases/children/children_placeholder.60 b/tests/cases/children/children_placeholder.60 index fc43d5949..07cfbfe7b 100644 --- a/tests/cases/children/children_placeholder.60 +++ b/tests/cases/children/children_placeholder.60 @@ -36,7 +36,7 @@ TestCase := Container { ```cpp auto handle = TestCase::create(); const TestCase &instance = *handle; -TestCase::apply_layout({&TestCase::component_type, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 200}); +TestCase::apply_layout({&TestCase::static_vtable, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 200}); assert(instance.get_rect1_pos_ok()); ``` diff --git a/tests/cases/children/children_placeholder_two_levels.60 b/tests/cases/children/children_placeholder_two_levels.60 index 74ffca0ba..7adc0536b 100644 --- a/tests/cases/children/children_placeholder_two_levels.60 +++ b/tests/cases/children/children_placeholder_two_levels.60 @@ -44,7 +44,7 @@ TestCase := MegaContainer { ```cpp auto handle = TestCase::create(); const TestCase &instance = *handle; -TestCase::apply_layout({&TestCase::component_type, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 200}); +TestCase::apply_layout({&TestCase::static_vtable, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 200}); assert(instance.get_rect1_pos_ok()); ``` diff --git a/tests/cases/layout/box_alignment.60 b/tests/cases/layout/box_alignment.60 index 843af6a67..f49299bec 100644 --- a/tests/cases/layout/box_alignment.60 +++ b/tests/cases/layout/box_alignment.60 @@ -75,7 +75,7 @@ TestCase := Rectangle { ```cpp auto handle = TestCase::create(); const TestCase &instance = *handle; -TestCase::apply_layout({&TestCase::component_type, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); +TestCase::apply_layout({&TestCase::static_vtable, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); assert(instance.get_v1()); assert(instance.get_v2()); assert(instance.get_v3()); diff --git a/tests/cases/layout/default_fill.60 b/tests/cases/layout/default_fill.60 index 5a63c880d..0f4380d36 100644 --- a/tests/cases/layout/default_fill.60 +++ b/tests/cases/layout/default_fill.60 @@ -28,7 +28,7 @@ TestCase := Rectangle { ```cpp auto handle = TestCase::create(); const TestCase &instance = *handle; -TestCase::apply_layout({&TestCase::component_type, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); +TestCase::apply_layout({&TestCase::static_vtable, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); assert(instance.get_elem1_ok()); assert(instance.get_elem2_ok()); assert(instance.get_elem3_ok()); diff --git a/tests/cases/layout/grid.60 b/tests/cases/layout/grid.60 index 74f7893a3..91fc36f57 100644 --- a/tests/cases/layout/grid.60 +++ b/tests/cases/layout/grid.60 @@ -42,7 +42,7 @@ TestCase := Rectangle { ```cpp auto handle = TestCase::create(); const TestCase &instance = *handle; -TestCase::apply_layout({&TestCase::component_type, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); +TestCase::apply_layout({&TestCase::static_vtable, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); assert(instance.get_rect1_pos_ok()); assert(instance.get_rect2_pos_ok()); assert(instance.get_rect3_pos_ok()); diff --git a/tests/cases/layout/grid_fixed_size.60 b/tests/cases/layout/grid_fixed_size.60 index c6661f38b..4ed44af67 100644 --- a/tests/cases/layout/grid_fixed_size.60 +++ b/tests/cases/layout/grid_fixed_size.60 @@ -60,7 +60,7 @@ TestCase := Rectangle { ```cpp auto handle = TestCase::create(); const TestCase &instance = *handle; -TestCase::apply_layout({&TestCase::component_type, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); +TestCase::apply_layout({&TestCase::static_vtable, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); assert(instance.get_rect1_pos_ok()); assert(instance.get_rect2_pos_ok()); assert(instance.get_rect3_pos_ok()); diff --git a/tests/cases/layout/grid_min_max.60 b/tests/cases/layout/grid_min_max.60 index 37bf43d75..c89da4254 100644 --- a/tests/cases/layout/grid_min_max.60 +++ b/tests/cases/layout/grid_min_max.60 @@ -54,7 +54,7 @@ TestCase := Rectangle { ```cpp auto handle = TestCase::create(); const TestCase &instance = *handle; -TestCase::apply_layout({&TestCase::component_type, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); +TestCase::apply_layout({&TestCase::static_vtable, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); assert(instance.get_rect1_pos_ok()); assert(instance.get_rect2_pos_ok()); assert(instance.get_rect3_pos_ok()); diff --git a/tests/cases/layout/grid_padding.60 b/tests/cases/layout/grid_padding.60 index 11b2da0fc..71bdb1d09 100644 --- a/tests/cases/layout/grid_padding.60 +++ b/tests/cases/layout/grid_padding.60 @@ -58,7 +58,7 @@ TestCase := Rectangle { ```cpp auto handle = TestCase::create(); const TestCase &instance = *handle; -TestCase::apply_layout({&TestCase::component_type, const_cast(&instance) }, sixtyfps::Rect{0, 0, 600, 300}); +TestCase::apply_layout({&TestCase::static_vtable, const_cast(&instance) }, sixtyfps::Rect{0, 0, 600, 300}); assert(instance.get_rect1_pos_ok()); assert(instance.get_rect2_pos_ok()); ``` diff --git a/tests/cases/layout/grid_within_for.60 b/tests/cases/layout/grid_within_for.60 index cd9bdb8b4..bc802941d 100644 --- a/tests/cases/layout/grid_within_for.60 +++ b/tests/cases/layout/grid_within_for.60 @@ -41,7 +41,7 @@ TestCase := Rectangle { auto handle = TestCase::create(); const TestCase &instance = *handle; sixtyfps::testing::send_mouse_click(&handle, -1., -1.); // FIXME: Force creation of repeater components before computing the layout -TestCase::apply_layout({&TestCase::component_type, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); +TestCase::apply_layout({&TestCase::static_vtable, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); sixtyfps::testing::send_mouse_click(&handle, 190., 190.); assert_eq(instance.get_value(), 1+1); diff --git a/tests/cases/layout/horizontal_sizes.60 b/tests/cases/layout/horizontal_sizes.60 index f227ae198..3c8600b84 100644 --- a/tests/cases/layout/horizontal_sizes.60 +++ b/tests/cases/layout/horizontal_sizes.60 @@ -69,7 +69,7 @@ TestCase := Rectangle { ```cpp auto handle = TestCase::create(); const TestCase &instance = *handle; -TestCase::apply_layout({&TestCase::component_type, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); +TestCase::apply_layout({&TestCase::static_vtable, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); assert(instance.get_blue_rect_ok()); assert(instance.get_red_rect_ok()); assert(instance.get_green_rect_ok()); diff --git a/tests/cases/layout/nested_boxes.60 b/tests/cases/layout/nested_boxes.60 index 6e2b06f8c..383a911b1 100644 --- a/tests/cases/layout/nested_boxes.60 +++ b/tests/cases/layout/nested_boxes.60 @@ -130,7 +130,7 @@ TestCase := Rectangle { ```cpp auto handle = TestCase::create(); const TestCase &instance = *handle; -TestCase::apply_layout({&TestCase::component_type, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); +TestCase::apply_layout({&TestCase::static_vtable, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); assert(instance.get_rect_blue_ok()); assert(instance.get_rect_orange_ok()); assert(instance.get_rect_red_ok()); diff --git a/tests/cases/layout/nested_grid.60 b/tests/cases/layout/nested_grid.60 index f45d2e803..6cad91a10 100644 --- a/tests/cases/layout/nested_grid.60 +++ b/tests/cases/layout/nested_grid.60 @@ -45,7 +45,7 @@ TestCase := Rectangle { ```cpp auto handle = TestCase::create(); const TestCase &instance = *handle; -TestCase::apply_layout({&TestCase::component_type, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); +TestCase::apply_layout({&TestCase::static_vtable, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); assert(instance.get_rect1_pos_ok()); assert(instance.get_rect2_pos_ok()); assert(instance.get_rect3_pos_ok()); diff --git a/tests/cases/layout/nested_grid_2.60 b/tests/cases/layout/nested_grid_2.60 index 683e06513..cbd194e9e 100644 --- a/tests/cases/layout/nested_grid_2.60 +++ b/tests/cases/layout/nested_grid_2.60 @@ -55,7 +55,7 @@ TestCase := Rectangle { ```cpp auto handle = TestCase::create(); const TestCase &instance = *handle; -TestCase::apply_layout({&TestCase::component_type, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); +TestCase::apply_layout({&TestCase::static_vtable, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); assert(instance.get_rect1_pos_ok()); assert(instance.get_rect2_pos_ok()); assert(instance.get_rect3_pos_ok()); diff --git a/tests/cases/layout/nested_grid_minmax.60 b/tests/cases/layout/nested_grid_minmax.60 index ac3a073c9..e59bb1243 100644 --- a/tests/cases/layout/nested_grid_minmax.60 +++ b/tests/cases/layout/nested_grid_minmax.60 @@ -44,7 +44,7 @@ TestCase := Rectangle { ```cpp auto handle = TestCase::create(); const TestCase &instance = *handle; -TestCase::apply_layout({&TestCase::component_type, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); +TestCase::apply_layout({&TestCase::static_vtable, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); assert(instance.get_rect1_pos_ok()); assert(instance.get_rect2_pos_ok()); ``` diff --git a/tests/cases/layout/path.60 b/tests/cases/layout/path.60 index 14e2f7bd9..94d732291 100644 --- a/tests/cases/layout/path.60 +++ b/tests/cases/layout/path.60 @@ -85,7 +85,7 @@ TestCase := Rectangle { ```cpp auto handle = TestCase::create(); const TestCase &instance = *handle; -TestCase::apply_layout({&TestCase::component_type, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); +TestCase::apply_layout({&TestCase::static_vtable, const_cast(&instance) }, sixtyfps::Rect{0, 0, 300, 300}); assert(instance.get_test1()); assert(instance.get_test2()); assert(instance.get_test3());