Hello, I added <https://bugzilla.mozilla.org/show_bug.cgi?id=1455514> some static `FromEventTarget` helper methods like `FromNode`. This allows you to avoid to QI from `EventTarget` pointers.

For example, when you need to get `Element` pointer from `EventTarget` pointer, previous you needed to do:

namespace mozilla::dom {

void Foo(EventTarget* aEventTarget) {
  nsCOMPtr<Element> element = do_QueryInterface(aEventTarget);
  if (!element) {
    return;
  }
  ...
}

}  // namespace mozilla::dom

Now, you can write this as:

namespace mozilla::dom {

void Foo(EventTarget* aEventTarget) {
  Element* element = Element::FromEventTargetOrNull(aEventTarget);
  if (!element) {
    return;
  }
  ...
}

}  // namespace mozilla::dom

`nsINode::FromEventTarget()`, `nsPIDOMWindowInner::FromEventTarget()`, `nsPIDOMWindowOuter::FromEventTarget()` and `nsPIWindowRoot::FromEventTarget()` resolve this with *one**virtual call* so that they are never slower than `do_QueryInterface`.

Subclasses of `nsINode` helpers (e.g., `nsIContent::FromEventTarget()`, `Element::FromEventTarget()`, etc) resolve this with *two****virtual calls*. I guess that this is faster than `do_QueryInterface` in most cases since IID comparison is not cheap and most concrete classes of `nsINode` have a lot of interfaces.

`FromEventTarget` and `FromEventTargetOrNull` helpers for subclasses of `nsINode` are declared and defined by `NS_IMPL_FROMNODE_HELPER` macro <https://searchfox.org/mozilla-central/rev/364aa1d41eb9b5ea583fa6cedc56fb5343e3514f/dom/base/nsINode.h#2280,2290-2308,2310,2326-2338> automatically. So all classes which have `FromNode` and `FromNodeOrNull` have the new helper methods too.

Finally, like `dom::Element` and `dom::Text`, there are similar utility methods in `dom::EventTarget` <https://searchfox.org/mozilla-central/rev/364aa1d41eb9b5ea583fa6cedc56fb5343e3514f/dom/events/EventTarget.h#128-148> now.

--
Masayuki Nakano<[email protected]>
Working on DOM, Events, editor and IME handling for Gecko

--
You received this message because you are subscribed to the Google Groups 
"[email protected]" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/a/mozilla.org/d/msgid/dev-platform/d668fac5-f1dd-1f0e-d3bc-e6e42a99d0e7%40d-toybox.com.

Reply via email to