Bug 220214

Summary: [LFC Display] Introduce type-safe rect types for the display tree
Product: WebKit Reporter: Simon Fraser (smfr) <simon.fraser>
Component: Layout and RenderingAssignee: Simon Fraser (smfr) <simon.fraser>
Status: RESOLVED FIXED    
Severity: Normal CC: bfulgham, koivisto, sam, simon.fraser, webkit-bug-importer, zalan
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
none
Patch none

Description Simon Fraser (smfr) 2020-12-31 13:08:49 PST
[LFC Display] Introduce typesafe rect types for the display tree
Comment 1 Simon Fraser (smfr) 2020-12-31 13:10:58 PST
Created attachment 416870 [details]
Patch
Comment 2 Simon Fraser (smfr) 2020-12-31 13:11:23 PST
Feedback welcome on this approach.
Comment 3 Sam Weinig 2021-01-03 11:40:22 PST
Comment on attachment 416870 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=416870&action=review

> Source/WebCore/display/DisplayGeometryTypes.h:43
> +template<typename T> class TypedFloatRect : public FloatRect {
> +};
> +
> +enum AbsoluteCoordinates { };
> +using AbsoluteFloatRect = TypedFloatRect<AbsoluteCoordinates>;
> +
> +
> +enum ViewCoordinates { };
> +using ViewFloatRect = TypedFloatRect<ViewCoordinates>;

I'm not sure having the template adds much here. Maybe instead just have `class AbsoluteFloatRect : public FloatRect { };` etc. 

For the intersection() and unionRect() functions below, then you could just have:

template<typename T>
inline auto intersection(T& a, const T& b) -> typename std::enable_if_t<std::is_base_of_v<FloatRect, T>, T>
{
    T c = a;
    c.intersect(b);
    return c;
}

(you might just be able to replace the definition in FloatRect with this generic one actually, since std::is_base_of_v<FloatRect, FloatRect> == true).
Comment 4 Simon Fraser (smfr) 2021-01-03 11:41:56 PST
The templates were intended to prevent implicit conversion, but maybe I can achieve the same with explicit constructors etc?
Comment 5 Sam Weinig 2021-01-04 08:46:33 PST
(In reply to Simon Fraser (smfr) from comment #4)
> The templates were intended to prevent implicit conversion, but maybe I can
> achieve the same with explicit constructors etc?

Implicit conversion in which direction?

Just by having the subclass you will avoid implicit conversion of FloatRect to AbsoluteFloatRect. As an example

void foo(const AbsoluteFloatRect& rect)
{
}

FloatRect rect;
foo(rect);

This would fail to compile.

However, this would still work:

void bar(const FloatRect& rect)
{
}

AbsoluteFloatRect rect;
bar(rect);
Comment 6 Radar WebKit Bug Importer 2021-01-07 13:09:24 PST
<rdar://problem/72902364>
Comment 7 Simon Fraser (smfr) 2021-08-14 20:00:46 PDT
Created attachment 435555 [details]
Patch
Comment 8 EWS 2021-08-15 08:44:35 PDT
Committed r281061 (240523@main): <https://commits.webkit.org/240523@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 435555 [details].